JS_CompileFunction

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_CompileFunction
  • Revision title: JS_CompileFunction
  • Revision id: 110973
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment /* Description */ tweaks

Revision Content

{{template.Jsapiref()}}

Summary

Function

Creates a JS function from a text string.

Syntax

JSFunction * JS_CompileFunction(JSContext *cx, JSObject *obj,
    const char *name, uintN nargs, const char **argnames,
    const char *bytes, size_t length, const char *filename,
    uintN lineno);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. {{wiki.template('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.
bytes const char * String containing the function to compile.
length size_t Size, in bytes, of the function to compile.
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 associates it with a JS object, obj.

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.

bytes is the string containing the text of the function. <code>length indicates the size of the text version of the function in bytes.

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 of the function or file where an error occurred during compilation.

On success, JS_CompileFunction returns a pointer to the newly compiled function. Otherwise, it returns NULL.

See Also

Groups Functions
Documents {{template.LXRSearch("ident", "i", "JS_CompileFunction", "LXR ID Search")}}
Entries

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> 
{{template.Jsapiref()}}
</p>
<h2 name="Summary"> Summary </h2>
<p><b>Function</b>
</p><p>Creates a JS function from a text string.
</p>
<h2 name="Syntax"> Syntax </h2>
<pre>JSFunction * JS_CompileFunction(JSContext *cx, JSObject *obj,
    const char *name, uintN nargs, const char **argnames,
    const char *bytes, size_t length, const char *filename,
    uintN 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>JSContext *</code></td>
<td>Pointer to a JS context from which to derive runtime information.
{{wiki.template('Jsapi-requires-request')}}</td>
</tr>
<tr>
<td><code>obj</code></td>
<td><code>JSObject *</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>uintN</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>bytes</code></td>
<td><code>const char *</code></td>
<td>String containing the function to compile.</td>
</tr>
<tr>
<td><code>length</code></td>
<td><code>size_t</code></td>
<td>Size, in bytes, of the function to compile.</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>uintN</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 associates it with a JS object, <code>obj</code>.
</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>bytes is the string containing the text of the function. &lt;code&gt;length</code> indicates the size of the text version of the function in bytes.
</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 of the function or file where an error occurred during compilation.
</p><p>On success, <code>JS_CompileFunction</code> returns a pointer to the newly compiled function. Otherwise, it returns <code>NULL</code>.
</p>
<h2 name="See_Also"> See Also </h2>
<table class="fullwidth-table">
<tbody><tr>
<td>Groups</td>
<td><a href="en/JSAPI_Reference#Functions">Functions</a></td>
</tr>
<tr>
<td>Documents</td>
<td>{{template.LXRSearch("ident", "i", "JS_CompileFunction", "LXR ID Search")}}</td>
</tr>
<tr>
<td>Entries</td>
<td>
<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>
</td>
</tr>
</tbody></table>
Revert to this revision