JS_EvaluateScript

本文详细介绍了SpiderMonkey中JS_EvaluateScript及JS_EvaluateUCScript函数的使用方法。这两个函数用于在指定的作用域中编译并执行JavaScript脚本。文章解释了函数参数的意义,包括上下文、作用域、源代码字符串等,并说明了如何处理错误。

TABLE OF CONTENTS

  1. Syntax
  2. Description
  3. See Also

Compile and execute a script.

Syntax

JSBool JS_EvaluateScript(JSContext *cx, JSObject *obj,
    const char *src, uintN length, const char *filename,
    uintN lineno, jsval *rval);

JSBool JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
    const jschar *src, uintN length, const char *filename,
    uintN lineno, jsval *rval);
Name Type Description
cx JSContext * The context in which to run the script. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JSObject * The scope in which to execute the script. This parameter is documented in detail at JS_ExecuteScript.
src const char * orconst jschar * String containing the script to compile and execute.
length uintN The length of src, in characters.
filename const char * Name of file or URL containing the script. Used to report filename or URL in error messages.
lineno uintN Line number. Used to report the offending line in the file or URL if an error occurs.
rval jsval * Out parameter. On success, if rval is not NULL*rval receives the result value.

Description

JS_EvaluateScript compiles and executes a script in the specified scope, objJS_EvaluateUCScript is the Unicode version of the function.

src is the string containing the text of the script. length indicates the size of the text version of the script in characters.

filename is the name of the file (or URL) containing the script. This information is used in messages if an error occurs during compilation. Similarly, lineno is used to report the line number of the script or file where an error occurred during compilation.

rval is an optional out parameter. If it is non-null, then on success, the result value is stored in *rval. This value is determined the same way as for the standard evalfunction. In the common case where the script is just an expression, the result value is simply the value of that expression. More generally, the result value is the value of the last-executed expression statement in the script that isn't in a function.

If a script compiles and executes successfully, JS_EvaluateScript or JS_EvaluateUCScript stores the result in *rval, if non-null, and returns JS_TRUE. Otherwise it returns JS_FALSE and the value left in *rval is undefined.

See Also

MXR ID Search for JS_EvaluateScript
MXR ID Search for JS_EvaluateUCScript

JS_CompileFileJS_CompileScriptJS_DecompileScriptJS_DestroyScriptJS_EvaluateScriptForPrincipals

TAGS (2)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值