Visit Mozilla.org

JSNative

From MDC

JSNative is the type of many JSAPI callbacks. In particular, APIs such as JS_InitClass and JS_DefineFunctions create custom methods on JavaScript objects that are implemented as JSNative callbacks provided by the application, written in C/C++ code.

The term "native" here refers to C/C++ code as opposed to JavaScript code.

[edit] Syntax

typedef JSBool (*JSNative)(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
Name Type Description
cx JSContext * Pointer to the JS context in which execution is taking place.

Provides request. (In JS_THREADSAFE builds, the JS engine calls this callback only from within an active request on cx. The callback does not need to call JS_BeginRequest.)

obj JSObject * Pointer to the this object supplied to the function at execution time.
argc uintN The actual number of arguments supplied to the function, as opposed to possibly the number of arguments the function is specified to take in its JSFunctionSpec.
argv jsval * The arguments provided to the function, along with a few other values noted below.
rval jsval * Out parameter. On success, the callback stores a return value in *rval. Defaults to point to JSVAL_VOID, in case this function does not return a value.

[edit] Description

JSNative defines the type of native implementations of JS functions.

When a JSNative is invoked by SpiderMonkey, the following locations are available as GC roots:

  • JS_ARGV_CALLEE(argv), which initially roots the JS Function object for the function being executed
  • argv[0] through argv[argc - 1], which hold the arguments provided to the function
  • argv[argc] through argv[minargs - 1], where minargs is JSFunctionSpec.nargs, if the JSFunctionSpec for the function being invoked specified more arguments than were provided to the function
  • argv[minargs] through argv[minargs + extra - 1], where extra is (JSFunctionSpec.extra & 0xFFFF) and minargs is as above