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.