[英]V8: create new variable scope
In JavaScript, when I enter a new function, I get a new local variable scope. 在JavaScript中,当我输入一个新函数时,会得到一个新的局部变量作用域。 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. 我现在想在V8中做同样的事情。 I have
other code
as a String
and compile it via Script::Compile
and run it via Script::Run
. 我还有
other code
作为String
,并通过Script::Compile
,并通过Script::Run
。
Right now, I create a new Context
but I think this is total overkill. 现在,我创建了一个新的
Context
但是我认为这完全是矫kill过正。 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. 如果我理解正确,那么我想假设您希望使C ++辅助脚本在新的隔离上下文中运行,因为您不希望它意外地别名或修改全局环境。 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. 这样可以确保sYourCode中的代码与您可能进行的其他调用保持隔离。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.