[quickjs-devel] Worker threads accessing global objects

  • From: Brandon Saunders <brandon.scot.saunders@xxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Tue, 11 May 2021 13:52:51 -0700

I'm attempting to write code that accesses global functions defined via

JSValue global_obj = JS_GetGlobalObject(js_ctx);
JS_SetPropertyStr(js_ctx, global_obj, "myFunction",
JS_NewCFunction(js_ctx, myFunction, "myFunction", 1));

The module that spins up the worker thread is defined via

sprintf(buf, "import {myModule} from 'localPath.js';
myModule(\"%s\",%i);", arg1, arg2);
int buf_len = strlen(buf);
JSValue val = JS_Eval(js_ctx, buf, buf_len, "<myFile>", JS_EVAL_TYPE_MODULE);

and calls the worker via the JS code

myWorker = new os.Worker("./myWorker.js");
myWorker.onmessage = function (e) {
   // non-relevant code here
}

Inside of myWorker.js I'm trying to access myFunction but cannot.  I
CAN access myFunction inside of localPath.js (i.e. in the code that
starts up myWorker).  It seems like the contexts are not shared.  Is
there a method of being able to access defined c functions inside of
worker threads?

Thanks!

Other related posts:

  • » [quickjs-devel] Worker threads accessing global objects - Brandon Saunders