TS Callback Functions from Native Code #1688
Replies: 1 comment
-
|
The easiest way to invoke a JS callback from native is by using JSI. You just need a way to initially pass the JS closure to native somehow. Storing it as a property on an object is an easy way to accomplish that. We have a very straight-forward example with JS callbacks here: hermes/benchmarks/lib/libgui/shgui.cpp Lines 59 to 65 in 7f44b7d Note that it would be more efficient to extract the closure once instead of retrieving it from the object every time, but that's really a detail. My WIP code for efficient callbacks is not really in a publishable form, and it is very complex, but the basic idea there is that we expose a JS API that allocates an opaque context pointer and emits (at compile time) a native callback wrapper that accepts this context pointer as one of its parameters (configurable which one). When called, the wrapper uses the information stored in the context to invoke the actual JS closure. Conceptually, the allocated context contains references to the JS runtime and the JS closure. That is sufficient for the wrapper to pass the execution to JS. There are other details around how to handle JS exceptions and what parameter types to expect, etc. It is all quite complex, since it involves emitting code at compile time, making the context available to the GC, etc. I wish I had more time to finish it. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I saw in another github discussion thread that there is some unpublished code for supporting calling ts callbacks from native code and I want know if there is a timeline on when callback support would be added and if you would be willing to put out some of the unpublished code even if it is unfinished?
I've been having a lot of fun with shermes (its very cool and very impressive) and I've been making a toy web server in typescript with it. For this web server I needed to be able to call ts callbacks from native code so I implemented my own way of doing that by having constructing a JSObject and attaching a NativeFunction that takes a ts callback and uses libffi's closure creation functions to wrap the function and return a c compatible function pointer.
I want to make a compile time solution for calling ts callbacks from native so I am very interested in whatever solution y'all came up with to implement this feature even if it is not fully working or in a less than ideal state. If the code cannot be released I would be very interested in any notes, information, plans, or internal documentation that y'all have (that could be publicly released) about how you could implement ts callbacks from native. I am basically looking for a good direction to go in for my compile time solution for calling these callbacks.
Beta Was this translation helpful? Give feedback.
All reactions