> However, it clears microtasks for _all_ contexts. Presumably that's
> not what you want.

Yes, but I now guess it won't happen that microtasks of different
contexts exist at the same time. We will keep executing js code
of context-1 until async API is called, and then we switch to
context-2, before which all the microtasks of context-1 should be
executed by v8 since we use the auto policy.

The assumption is based on my superficial understanding of v8 and
it's microtask policy so please correct me if anything is wrong.

> You can run microtasks manually and call CancelTerminateExecution
> beforehand but then microtasks from the terminated context still run.

That's not what I want either, but unfortunately, I didn't find any
v8 utility to help clean up the microtask queue manually.

On Thursday, October 27, 2022 at 3:13:52 PM UTC+8 Ben Noordhuis wrote:

> On Wed, Oct 26, 2022 at 5:29 AM Jianyong Chen <balu...@gmail.com> wrote:
> >
> > Thanks, Ben.
> >
> > > Assuming your execution contexts don't interleave (context A can't
> > > call functions from context B), then all you have to do is place
> > > CancelTerminateExecution calls in the right spots.
> >
> > Yes, we do not have contexts interleaving(but context switch due to
> > async calls).
> >
> > As to the "right spots" could you please give me more details or
> > advice about it? Now I'm planning to `CancelTerminateExecution`
> > when the `v8::TryCatch` is `HasTerminated`, which is created before
>
> Yes, that should work. "Right spots" means C++ -> JS calls (ex.
> v8::Function::Call) and JS->C++ calls (ex. v8::FunctionTemplate
> callbacks.)
>
> > entering js code, And mark the current execution environment as
> > terminated so that we can skip Resolve/Reject when async operations
> > are completed and thus ensure that no new microtask is generated
> > or ran(now we use the auto policy for microtask).
> >
> > Besides the right spots, I'm also concerned that how the
> > per-isolate(shared by all contexts) data are processed when I call
> > 'TerminateExecution'. Such as the microtask queue, will the queue be
> > cleared up by v8 automatically or I should do it manually?
>
> The microtask queue is a good point. V8 clears it out when microtasks
> are about to run but execution is terminating.
>
> However, it clears microtasks for _all_ contexts. Presumably that's
> not what you want.
>
> You can run microtasks manually and call CancelTerminateExecution
> beforehand but then microtasks from the terminated context still run.
>
> I don't think you can really avoid that unless you control all places
> where promises are created, e.g., because you use
> `v8::Promise::Resolver` for everything.
>

-- 
-- 
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/ff3b8624-88dd-41ec-ae5e-1fcb9e204986n%40googlegroups.com.

Reply via email to