ComputerCraft icon indicating copy to clipboard operation
ComputerCraft copied to clipboard

RFC: Replace ILuaContext with a non-blocking alternative

Open SquidDev opened this issue 5 years ago • 0 comments

This is an initial prototype for ways we could better integrate Lua's asynchronous functionality (coroutines) with peripherals and other Lua APIs.

The existing system assumes that coroutines each have a backing thread, and so yields will suspect the current thread. This takes inspiration from various "promise" libraries - asynchronous operations (such as executing on the main thread or waiting for an event) return a promise - the result of which can be consumed with .then.

This is very much WIP - there's some things I'd like to do before considering this "ready":

  • Test: Most importantly, see whether this actually integrates well with other runtimes. I'd quite like to integrate this with my work on single threaded Cobalt.
  • Get lots of feedback: this is still very much in flux, and there's definitely elements I'm not entirely sure about.
  • Profile this: There is some overhead due to additional object allocation, so I'd expect a slight slowdown.

Usage

Just a couple of diffs to show how the old and new APIs compare:

- return context.executeMainThreadTask( () ->
+ return MethodResult.onMainThread( () ->
      if( WorldUtil.isBlockInWorld( world, position ) )
      {
-         return new Object[]{ getBlockInfo( world, position ) };
+         return MethodResult.of( getBlockInfo( world, position ) );
      }

As you can see, there's not really a lot different - it's just a rather tedious refactor. That said, executing things in loops does end up being much uglier, compare this to this. I don't think this is a major issue, as most people won't need to do this, but is worth bearing in mind.

SquidDev avatar Sep 09 '18 16:09 SquidDev