JS_ExecuteScript 编辑

Execute a compiled script.

Syntax

bool
JS_ExecuteScript(JSContext *cx, JS::HandleScript script,
                 JS::MutableHandleValue rval); // Added in SpiderMonkey 45

bool
JS_ExecuteScript(JSContext *cx, JS::HandleScript script); // Added in SpiderMonkey 45

bool
JS_ExecuteScript(JSContext *cx, JS::AutoObjectVector &scopeChain,
                 JS::HandleScript script, JS::MutableHandleValue rval); // Added in SpiderMonkey 36

bool
JS_ExecuteScript(JSContext *cx, JS::AutoObjectVector &scopeChain,
                 JS::HandleScript script); // Added in SpiderMonkey 36

bool
JS_ExecuteScript(JSContext *cx, JS::HandleObject obj, JS::HandleScript script,
                 JS::MutableHandleValue rval); // Obsolete since JSAPI 39

bool
JS_ExecuteScript(JSContext *cx, JS::HandleObject obj, JS::HandleScript script); // Obsolete since JSAPI 39

bool
JS::CloneAndExecuteScript(JSContext *cx, JS::Handle<JSScript*> script); // Added in SpiderMonkey 45

bool
JS::CloneAndExecuteScript(JSContext *cx, JS::Handle<JSObject*> obj,
                          JS::Handle<JSScript*> script); // Added in SpiderMonkey 31, obsoleted since JSAPI 39
NameTypeDescription
cxJSContext *The context in which to execute the script. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
objJS::HandleObject

The scope in which to execute the script. In the simplest cases, this should just be the embedding's global object. Obsolete since JSAPI 39

obj must not be an array, an E4X XML object, a With object, or a proxy.

In ECMAScript terms, the script is executed in a new execution context, but that context is not initialized quite as described in any of the three cases in ECMA 262-3 §10.2. Instead:

  • The scope chain is initialized to contain obj, followed by its parent, then its parent's parent, etc. until NULL is reached.
  • Variable initialization is performed. If the JSOPTION_VAROBJFIX option is in effect (recommended), then the last object in the scope chain is used as the variable object. Otherwise obj is used.
  • The this value is obj.
scopeChainJS::AutoObjectVector &amp;An explicit scope chain. scopeChain must not include the global object on it; that's implicit. It needs to contain the other objects that should end up on the scripts's scope chain. Objects in the vector should be ordered from inner to the outer scope.
scriptJS::HandleScriptThe compiled script to execute.
rvalJS::MutableHandleValueOut parameter. On success, rval receives the value from the last executed expression statement processed in the script.

Description

JS_ExecuteScript executes a previously-compiled script, script.

JS::CloneAndExecuteScript handles a cross-compartment script. If the script is cross-compartment, it is cloned into the current compartment before executing.

If the script executes successfully, rval receives the value from the last executed expression statement processed in the script, and JS_ExecuteScript returns true. Otherwise it returns false, and the value left in rval is unspecified.

The JSAPI User Guide contains example code using compiled scripts.

To execute a script that has not been compiled, use JS::Evaluate instead.

See Also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:102 次

字数:6422

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文