A v8::HandleScope manages the lifetime of the backing store used by any handles/v8::Locals you create. It doesn't have anything to do with JavaScript variable scoping.
A v8::Context::Scope is a convenience tool for making it easy to ensure your code has matching pairs of Context::Enter() and Context::Exit() calls. Contexts map to frames/iframes on the web. I'm not sure how to solve your problem. If you want the scripts to act like modules, maybe one of the CompileModule() overloads would do the trick? Or, as a somewhat hacky solution, you could modify their sources to wrap them into {...} or (function() { ... })() ? Or maybe it'd be fine to simply run each script in its own v8::Context? More documentation: - v8.dev/docs/embed - comments in v8.h <https://chromium.googlesource.com/v8/v8/+/refs/heads/main/include/v8.h> - API tests in test/cctest/test-api* On Thu, Jul 15, 2021 at 9:08 AM Brian Long <new...@gmail.com> wrote: > Hi, new to v8 embedding here. > > I'm having a hard time understanding the difference between > v8::HandleScope and v8::Context::Scope. Where can I learn more? > > As a practical application, what I'm trying to do is to run two classic > scripts in "implicit" block scopes. (The way that modules are executed, I > suppose.) > > Let's suppose script1.js and script2.js both simply contain "let x = 0". > If I just run those scripts in the same isolate + context, the second one > will error with "Identifier 'x' has already been declared". > > I'm trying to understand how to "push" a "block scope" around the > execution of each script, so the moral equivalent of "{ let x = 0 }" is > actually happening when each script is run. > > I tried something like > > { v8::HandleScope scope(isolate); script1->Run(context).ToLocalChecked(); } > { v8::HandleScope scope(isolate); script2->Run(context).ToLocalChecked(); } > > But it doesn't work, the let x is occurring at the top-level, not in a > scope, and the second script errors about the duplicate declaration. > > How can I solve this immediate problem, and how can I learn more on my > own? The source code is not particularly illustrative, and no blog posts I > can find go deep enough. Is there a deeper reference manual or book > somewhere? > > Thanks. > > -- > -- > 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/ae973a53-0838-40a3-859d-c7afd7854d99n%40googlegroups.com > <https://groups.google.com/d/msgid/v8-users/ae973a53-0838-40a3-859d-c7afd7854d99n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- -- 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/CAKSzg3TsLhCA7Q%3D2kPhTUOm5xzAd%2BKPcCvyU8Wxdy38wBVAUfA%40mail.gmail.com.