JS_CompileFunction

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_CompileFunction
  • Revision title: JS_CompileFunction
  • Revision id: 110975
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment style

Revision Content

{{ Jsapi_ref_header("JS_CompileFunction") }}

Creates a JavaScript function from a text string.

Syntax

JSFunction * JS_CompileFunction(JSContext *cx, JSObject *obj,
    const char *name, uintN nargs, const char **argnames,
    const char *body, size_t length, const char *filename,
    uintN lineno);

JSFunction * JS_CompileUCFunction(JSContext *cx, JSObject *obj,
    const char *name, uintN nargs, const char **argnames,
    const jschar *body, size_t length, const char *filename,
    uintN lineno);
Name Type Description
cx JSContext * The context in which to compile the function. {{ Jsapi-requires-request() }}
obj JSObject * Object with which the function is associated.
name const char * Name to assign the newly compiled function.
nargs uintN Number of arguments to pass to the function.
argnames const char ** Names to assign to the arguments passed to the function.
body const char * or const jschar * String containing the body of the function to compile.
length size_t The length, in characters, of body.
filename const char * Name of file or URL containing the function. 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.

Description

JS_CompileFunction compiles a function from a text string, bytes, and optionally associates it with a JS object, obj. JS_CompileUCFunction is the Unicode version of the function.

name is the name to assign to the newly created function. nargs is the number of arguments the function takes, and argnames is a pointer to an array of names to assign each argument. The number of argument names should match the number of arguments specified in nargs.

body is a string containing the source code of the function. length is the length of the source code in characters.

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

If both obj and name are non-null, the new function becomes a method of obj. (A new property is defined on obj with the given name and the new Function object as its value.)

On success, JS_CompileFunction and JS_CompileUCFunction return a pointer to the newly compiled function. On error or exception, they return NULL.

See Also

{{ LXRSearch("ident", "i", "JS_CompileFunction") }}
{{ LXRSearch("ident", "i", "JS_CompileUCFunction") }}

JSFUN_BOUND_METHOD, JSFUN_GLOBAL_PARENT, JS_CallFunction, JS_CallFunctionName, JS_CallFunctionValue, JS_DecompileFunction, JS_DecompileFunctionBody, JS_DefineFunction, JS_DefineFunctions, JS_GetFunctionObject, JS_NewFunction, JS_SetBranchCallback, JS_ValueToFunction

Revision Source

<p>{{ Jsapi_ref_header("JS_CompileFunction") }}
</p><p>Creates a JavaScript function from a text string.
</p>
<h2 name="Syntax"> Syntax </h2>
<pre class="eval"><a href="en/JSFunction">JSFunction</a> * <b>JS_CompileFunction</b>(<a href="en/JSContext">JSContext</a> *cx, <a href="en/JSObject">JSObject</a> *obj,
    const char *name, <a href="en/UintN">uintN</a> nargs, const char **argnames,
    const char *body, size_t length, const char *filename,
    <a href="en/UintN">uintN</a> lineno);

<a href="en/JSFunction">JSFunction</a> * <b>JS_CompileUCFunction</b>(<a href="en/JSContext">JSContext</a> *cx, <a href="en/JSObject">JSObject</a> *obj,
    const char *name, <a href="en/UintN">uintN</a> nargs, const char **argnames,
    const <a href="en/Jschar">jschar</a> *body, size_t length, const char *filename,
    <a href="en/UintN">uintN</a> lineno);
</pre>
<table class="fullwidth-table">
<tbody><tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td><code>cx</code></td>
<td><code><a href="en/JSContext">JSContext</a> *</code></td>
<td>The context in which to compile the function.
{{ Jsapi-requires-request() }}</td>
</tr>
<tr>
<td><code>obj</code></td>
<td><code><a href="en/JSObject">JSObject</a> *</code></td>
<td>Object with which the function is associated.</td>
</tr>
<tr>
<td><code>name</code></td>
<td><code>const char *</code></td>
<td>Name to assign the newly compiled function.</td>
</tr>
<tr>
<td><code>nargs</code></td>
<td><code><a href="en/UintN">uintN</a></code></td>
<td>Number of arguments to pass to the function.</td>
</tr>
<tr>
<td><code>argnames</code></td>
<td><code>const char **</code></td>
<td>Names to assign to the arguments passed to the function.</td>
</tr>
<tr>
<td><code>body</code></td>
<td><code>const char *</code> <i>or</i> <code>const <a href="en/Jschar">jschar</a> *</code></td>
<td>String containing the body of the function to compile.</td>
</tr>
<tr>
<td><code>length</code></td>
<td><code>size_t</code></td>
<td>The length, in characters, of <code>body</code>.</td>
</tr>
<tr>
<td><code>filename</code></td>
<td><code>const char *</code></td>
<td>Name of file or URL containing the function. Used to report filename or URL in error messages.</td>
</tr>
<tr>
<td><code>lineno</code></td>
<td><code><a href="en/UintN">uintN</a></code></td>
<td>Line number. Used to report the offending line in the file or URL if an error occurs.</td>
</tr>
</tbody></table>
<h2 name="Description"> Description </h2>
<p><code>JS_CompileFunction</code> compiles a function from a text string, <code>bytes</code>, and optionally associates it with a JS object, <code>obj</code>. <code>JS_CompileUCFunction</code> is the Unicode version of the function.
</p><p><code>name</code> is the name to assign to the newly created function. <code>nargs</code> is the number of arguments the function takes, and <code>argnames</code> is a pointer to an array of names to assign each argument. The number of argument names should match the number of arguments specified in <code>nargs</code>.
</p><p><code>body</code> is a string containing the source code of the function. <code>length</code> is the length of the source code in characters.
</p><p><code>filename</code> is the name of the file (or URL) containing the function. This information is used in error messages if an error occurs during compilation. Similarly, <code>lineno</code> is used to report the line number where an error occurred during compilation.
</p><p>If both <code>obj</code> and <code>name</code> are non-null, the new function becomes a method of <code>obj</code>. (A new property is defined on <code>obj</code> with the given <code>name</code> and the new <code>Function</code> object as its value.)
</p><p>On success, <code>JS_CompileFunction</code> and <code>JS_CompileUCFunction</code> return a pointer to the newly compiled function. On error or exception, they return <code>NULL</code>.
</p>
<h2 name="See_Also"> See Also </h2>
<p>{{ LXRSearch("ident", "i", "JS_CompileFunction") }}<br>
{{ LXRSearch("ident", "i", "JS_CompileUCFunction") }}
</p><p><a href="en/JSFUN_BOUND_METHOD">JSFUN_BOUND_METHOD</a>,
<a href="en/JSFUN_GLOBAL_PARENT">JSFUN_GLOBAL_PARENT</a>,
<a href="en/JS_CallFunction">JS_CallFunction</a>,
<a href="en/JS_CallFunctionName">JS_CallFunctionName</a>,
<a href="en/JS_CallFunctionValue">JS_CallFunctionValue</a>,
<a href="en/JS_DecompileFunction">JS_DecompileFunction</a>,
<a href="en/JS_DecompileFunctionBody">JS_DecompileFunctionBody</a>,
<a href="en/JS_DefineFunction">JS_DefineFunction</a>,
<a href="en/JS_DefineFunctions">JS_DefineFunctions</a>,
<a href="en/JS_GetFunctionObject">JS_GetFunctionObject</a>,
<a href="en/JS_NewFunction">JS_NewFunction</a>,
<a href="en/JS_SetBranchCallback">JS_SetBranchCallback</a>,
<a href="en/JS_ValueToFunction">JS_ValueToFunction</a>
</p>
Revert to this revision