It seems v8 functions which return other v8 objects must be called on the main thread (correct me if I'm wrong). In order to work around this, I've registered an extension which invokes a callback function which takes one parameter: the pointer of the actual callback function. When I want to run some v8 functions, I call execute_java_script with a javascript string, containing the address of the code block I want to execute converted to a string. This callback function seems to always be called on the main thread, so from there I can get the address of the code block address and invoke it.
The extension looks something like this:
- Code: Select all
var mainThread;
if (!mainThread)
mainThread = {};
(function() {
mainThread.callback = function(funcptr) {
native function callback(a);
return callback(funcptr);
};
})();
I execute it using a javascript string that looks something like this:
- Code: Select all
mainThread.callback(348273987);
Where that number is a string-serialized pointer to the real callback. It's unfortunate that this works because it's so horrible. Is there a better way?