I have an application that extends JavaScript via JavaScriptCore, in a webkit-gtk browser. Right now I have several classes that I add to the global context like so:
void create_js(gpointer context, char* className, JSClassDefinition clasDefinition) { JSClassRef classDef = JSClassCreate(&clasDefinition); JSObjectRef classObj = JSObjectMake(context, classDef, context); JSObjectRef globalObj = JSContextGetGlobalObject(context); JSStringRef str = JSStringCreateWithUTF8CString(className); JSObjectSetProperty(context, globalObj, str, classObj, kJSPropertyAttributeNone, NULL); JSStringRelease(str); }
Now, I'd like to also add those classes to the WebWorker's context, so I can call them from workers instantiated in JS.
I've tried getting the Worker
object like so:
JSStringRef workerStr = JSStringCreateWithUTF8CString("Worker"); JSObjectRef worker = JSObjectGetProperty(context, globalObj, workerStr, NULL); JSObjectSetProperty(context, worker, str, classObj, kJSPropertyAttributeNone, NULL); JSStringRelease(workerStr);
But that adds it to the WorkerConstructor
object, and when a new Worker()
is called, the classes are not available.
Web Workers are primarily used for CPU-intensive tasks to be run in the background without any network connectivity required to work on the tasks.
A web worker is a piece of browser functionality. It is the real OS threads that can be spawned in the background of your current page so that it can perform complex and resource-intensive tasks. Imagine that you have some large data to fetch from the server, or some complex rendering needs to be done on the UI.
When a runtime error occurs in the worker, its onerror event handler is called. It receives an event named error which implements the ErrorEvent interface.
Web Workers are a simple means of running scripts in background threads for web content. Without interfering with the user interface, the worker thread may perform tasks.
There is no way to modify the WorkerGlobalScope
or comparable scopes/contexts before a web worker is started in most common browser implementations. These scopes become available only to the web workers context as soon as this specific web worker is launched.
The only way to use shared methods is to define them in a separate shared file/resource and include them using importScripts()
self.importScripts('foo.js'); self.importScripts('foo.js', 'bar.js', ...); importScripts('foo.js'); importScripts('foo.js', 'bar.js', ...);
Note:
importScripts()
andself.importScripts()
are effectively equivalent — both representimportScripts()
being called from inside the worker's inner scope.
Sources
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With