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

RPC-FFI-like interop when embedding into a shared library #178

Open
brandonros opened this issue Oct 9, 2021 · 3 comments
Open

RPC-FFI-like interop when embedding into a shared library #178

brandonros opened this issue Oct 9, 2021 · 3 comments

Comments

@brandonros
Copy link

brandonros commented Oct 9, 2021

mildly related to https://github.com/saghul/txiki.js/pull/179/files

image

It's pretty clear that TJS_Run is most likely a blocking call. That's fine. You can get around that by putting TJS on its own thread.

My question to you is, how could the native calling code (for example a Win32 DLL in DllMain) interact with the TJS runtime? How could messages be passed to and from? Obviously you can do things like named pipes, shared memory, a queue, etc. but I'm looking if it's possible to inject events.

struct TJSRuntime {
    TJSRunOptions options;
    JSRuntime *rt;
    JSContext *ctx;

Is there any easy way to pass messages from the QuickJS runtime/context into native code, and back from native code into QuickJS runtime?

@brandonros brandonros changed the title RPC FFI interop when embedding into a shared library RPC-FFI-like interop when embedding into a shared library Oct 9, 2021
@brandonros
Copy link
Author

I see with tjs__bootstrap_globals I have some options like EventTarget. I'm also wondering if I can do something like window.addEventListener.

$ ./build/tjs 
Welcome to txiki.js — The tiny JavaScript runtime
Type "\h" for help
> window.addEventListener('foo', (event) => console.log(event))
undefined
> window.dispatchEvent(new Event('foo'))
{ isTrusted: false }
true

@brandonros
Copy link
Author

brandonros commented Oct 9, 2021

Looks like from C I can pass an event like this:

/* Emit window 'load' event. */
    if (!JS_IsException(ret) && is_main) {
        static char emit_window_load[] = "window.dispatchEvent(new Event('load'));";
        JSValue ret1 = JS_Eval(ctx, emit_window_load, strlen(emit_window_load), "<global>", JS_EVAL_TYPE_GLOBAL);
        if (JS_IsException(ret1)) {
            tjs_dump_error(ctx);
        }
    }

Now to find how to call C from JS.

Most likely going to need to compile tjs executable as a shared library instead too...

@saghul
Copy link
Owner

saghul commented Oct 11, 2021

TBH I never considered this use case so far.

The whole thing is not thread safe, so we'd need to add some new API to make is possible to call stuff from outside the thread running.

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

No branches or pull requests

2 participants