Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Replace ILuaContext with a non-blocking alternative #574

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

SquidDev
Copy link
Contributor

@SquidDev SquidDev commented Sep 9, 2018

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.

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`.

While I am not aware of plans to change the Lua implementation, this
does give us greater flexibility in the future, leading the way for
Rembulan, single-threaded Cobalt, and even possibly OC support.
This is definitely a little ugly in places, mostly due to how we've
handled backwards compatibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant