I managed to get this working by replacing the native Promise object by 
executing the following script:

    global.Promise = new Proxy(global.Promise, {
        construct: function(target, args) {
            const origFunc = args[0];
            return new target(function(resolve, reject) {
                origFunc(
                    value => executeOnMainThread(resolve.bind(this, value)),
                    reason => executeOnMainThread(reject.bind(this, reason))
                );
            });
        }
    });

In this example, I have registered the "executeOnMainThread" function on 
the global object which will call the specified js function argument on the 
main thread.

Of course if this can be achieved without replacing the global Promise 
object, that would be even better.

On Tuesday, February 11, 2020 at 10:59:40 PM UTC+2, Darin Dimitrov wrote:
>
> Hi Ben,
>
> Thanks for the reply. My program is actually a MacOS application and it 
> uses its SDK to create threads. I am using v8::Locker to ensure that only 
> one thread is accessing the isolate. And this works quite well. I can also 
> use the MacOS SDK to schedule some work on any thread. The problem I am 
> having is how to hook into V8 promises so that I can control over which 
> thread the resolve callback i executed.
>
> For example JavaScriptCore has the possibility to specify a callback when 
> initializing the global object which allows to control and customize how 
> are the promise callbacks executed: 
> https://github.com/WebKit/webkit/blob/master/Source/JavaScriptCore/runtime/JSGlobalObject.cpp#L341
>
> I am looking for a similar API in V8 if it exists.
>
> On Tuesday, February 11, 2020 at 10:31:18 PM UTC+2, Ben Noordhuis wrote:
>>
>> On Tue, Feb 11, 2020 at 8:42 PM Darin Dimitrov <darin....@gmail.com> 
>> wrote: 
>> > 
>> > I am embedding V8 in my C++ application and I have registered a custom 
>> "test" function on the global object taking a callback as parameter: 
>> > 
>> > test(function() { 
>> >     console.log("callback"); 
>> > }); 
>> > 
>> > The "test" function starts a new thread and executes the callback on 
>> this thread. 
>> > 
>> > Now I can wrap this function in a Promise: 
>> > 
>> > new Promise(function(resolve, reject) { 
>> >     test(resolve); 
>> > }).then(function() { 
>> >     console.log("this callback is executed on the background thread 
>> created by the 'test' function"); 
>> > }); 
>> > 
>> > I am looking for a way to somehow hook into V8 promises so that they 
>> are always resolved on the main thread of my application. 
>> > 
>> > I thought that using a custom platform might help but couldn't find any 
>> useful method that I can override. It does provide the "CallOnWorkerThread" 
>> method but can I relate this to promises? Does V8 provide some API to 
>> intercept and replace the promise implementation? 
>>
>> Some additional info is needed because it's not wholly clear to me how 
>> you envision this would work. How exactly is your program using 
>> threads? 
>>
>> A V8 isolate is not reentrant. You can migrate it between threads but 
>> only one thread can enter it (and should hold a v8::Locker to ensure 
>> that it's the only one.) 
>>
>> You have some control over when microtasks (promises) are executed 
>> with isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit) and 
>> isolate->RunMicrotasks(). 
>>
>

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/dabcd8ba-26e4-4095-ac15-8f1ee446c26e%40googlegroups.com.

Reply via email to