In JavaScript, when I enter a new function, I get a new local variable scope. Eg like this:
function f() {
var x = 42; // this is in our local variable scope
// other code
}
I want to do the same now in V8. I have other code
as a String
and compile it via Script::Compile
and run it via Script::Run
.
Right now, I create a new Context
but I think this is total overkill. It also means that I have to reinit my globals in the new context.
If I understand correctly, you want to get your C++ side script to run in a new isolated context, I assume because you don't want it accidentally aliasing or modifying the global environment. In that case, before you execute your code, do this:
std::string sScopedCode = "(function(){" + sYourCode + "})();"
That will ensure the code in sYourCode is kept isolated from other invocations you might make.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.