Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add a new native class to WebWorker's context in JavaScriptCore?

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.

like image 320
Pedro Vanzella Avatar asked Aug 12 '15 17:08

Pedro Vanzella


People also ask

Why use webworkers?

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.

How webworker works?

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.

Which event is used to handle error in web workers?

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.

Is web worker a thread?

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.


1 Answers

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() and self.importScripts() are effectively equivalent — both represent importScripts() being called from inside the worker's inner scope.


Sources

  • WorkerGlobalScope
  • DedicatedWorkerGlobalScope
  • SharedWorkerGlobalScope
like image 111
janniks Avatar answered Oct 06 '22 07:10

janniks