1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsapi_h___
41 #define jsapi_h___
42 /*
43 * JavaScript API.
44 */
45 #include <stddef.h>
46 #include <stdio.h>
47 #include "jspubtd.h"
49 JS_BEGIN_EXTERN_C
51 /*
52 * Type tags stored in the low bits of a jsval.
53 */
54 #define JSVAL_OBJECT 0x0 /* untagged reference to object */
55 #define JSVAL_INT 0x1 /* tagged 31-bit integer value */
56 #define JSVAL_DOUBLE 0x2 /* tagged reference to double */
57 #define JSVAL_STRING 0x4 /* tagged reference to string */
58 #define JSVAL_BOOLEAN 0x6 /* tagged boolean value */
60 /* Type tag bitfield length and derived macros. */
61 #define JSVAL_TAGBITS 3
62 #define JSVAL_TAGMASK JS_BITMASK(JSVAL_TAGBITS)
63 #define JSVAL_TAG(v) ((v) & JSVAL_TAGMASK)
64 #define JSVAL_SETTAG(v,t) ((v) | (t))
65 #define JSVAL_CLRTAG(v) ((v) & ~(jsval)JSVAL_TAGMASK)
66 #define JSVAL_ALIGN JS_BIT(JSVAL_TAGBITS)
68 /* Predicates for type testing. */
69 #define JSVAL_IS_OBJECT(v) (JSVAL_TAG(v) == JSVAL_OBJECT)
70 #define JSVAL_IS_NUMBER(v) (JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v))
71 #define JSVAL_IS_INT(v) (((v) & JSVAL_INT) && (v) != JSVAL_VOID)
72 #define JSVAL_IS_DOUBLE(v) (JSVAL_TAG(v) == JSVAL_DOUBLE)
73 #define JSVAL_IS_STRING(v) (JSVAL_TAG(v) == JSVAL_STRING)
74 #define JSVAL_IS_BOOLEAN(v) (JSVAL_TAG(v) == JSVAL_BOOLEAN)
75 #define JSVAL_IS_NULL(v) ((v) == JSVAL_NULL)
76 #define JSVAL_IS_VOID(v) ((v) == JSVAL_VOID)
77 #define JSVAL_IS_PRIMITIVE(v) (!JSVAL_IS_OBJECT(v) || JSVAL_IS_NULL(v))
79 /* Objects, strings, and doubles are GC'ed. */
80 #define JSVAL_IS_GCTHING(v) (!((v) & JSVAL_INT) && !JSVAL_IS_BOOLEAN(v))
81 #define JSVAL_TO_GCTHING(v) ((void *)JSVAL_CLRTAG(v))
82 #define JSVAL_TO_OBJECT(v) ((JSObject *)JSVAL_TO_GCTHING(v))
83 #define JSVAL_TO_DOUBLE(v) ((jsdouble *)JSVAL_TO_GCTHING(v))
84 #define JSVAL_TO_STRING(v) ((JSString *)JSVAL_TO_GCTHING(v))
85 #define OBJECT_TO_JSVAL(obj) ((jsval)(obj))
86 #define DOUBLE_TO_JSVAL(dp) JSVAL_SETTAG((jsval)(dp), JSVAL_DOUBLE)
87 #define STRING_TO_JSVAL(str) JSVAL_SETTAG((jsval)(str), JSVAL_STRING)
89 /* Lock and unlock the GC thing held by a jsval. */
90 #define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
91 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
92 : JS_TRUE)
93 #define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
94 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
95 : JS_TRUE)
97 /* Domain limits for the jsval int type. */
98 #define JSVAL_INT_BITS 31
99 #define JSVAL_INT_POW2(n) ((jsval)1 << (n))
100 #define JSVAL_INT_MIN ((jsval)1 - JSVAL_INT_POW2(30))
101 #define JSVAL_INT_MAX (JSVAL_INT_POW2(30) - 1)
102 #define INT_FITS_IN_JSVAL(i) ((jsuint)((i)+JSVAL_INT_MAX) <= 2*JSVAL_INT_MAX)
103 #define JSVAL_TO_INT(v) ((jsint)(v) >> 1)
104 #define INT_TO_JSVAL(i) (((jsval)(i) << 1) | JSVAL_INT)
106 /* Convert between boolean and jsval. */
107 #define JSVAL_TO_BOOLEAN(v) ((JSBool)((v) >> JSVAL_TAGBITS))
108 #define BOOLEAN_TO_JSVAL(b) JSVAL_SETTAG((jsval)(b) << JSVAL_TAGBITS, \
109 JSVAL_BOOLEAN)
111 /* A private data pointer (2-byte-aligned) can be stored as an int jsval. */
112 #define JSVAL_TO_PRIVATE(v) ((void *)((v) & ~JSVAL_INT))
113 #define PRIVATE_TO_JSVAL(p) ((jsval)(p) | JSVAL_INT)
115 /* Property attributes, set in JSPropertySpec and passed to API functions. */
116 #define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
117 #define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
118 #define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
119 #define JSPROP_EXPORTED 0x08 /* property is exported from object */
120 #define JSPROP_GETTER 0x10 /* property holds getter function */
121 #define JSPROP_SETTER 0x20 /* property holds setter function */
122 #define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
123 property; don't copy the property on
124 set of the same-named property in an
125 object that delegates to a prototype
126 containing this property */
127 #define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
129 /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
130 #define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
131 #define JSFUN_GETTER JSPROP_GETTER
132 #define JSFUN_SETTER JSPROP_SETTER
133 #define JSFUN_BOUND_METHOD 0x40 /* bind this to fun->object's parent */
134 #define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
135 #define JSFUN_FLAGS_MASK 0xf8 /* overlay JSFUN_* attributes */
137 /*
138 * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
139 * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
140 * methods of a class prototype that are exposed as static methods taking an
141 * extra leading argument: the generic |this| parameter.
142 *
143 * If you set this flag in a JSFunctionSpec struct's flags initializer, then
144 * that struct must live at least as long as the native static method object
145 * created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
146 * JSFunctionSpec structs are allocated in static arrays.
147 */
148 #define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
150 /*
151 * Well-known JS values. The extern'd variables are initialized when the
152 * first JSContext is created by JS_NewContext (see below).
153 */
154 #define JSVAL_VOID INT_TO_JSVAL(0 - JSVAL_INT_POW2(30))
155 #define JSVAL_NULL OBJECT_TO_JSVAL(0)
156 #define JSVAL_ZERO INT_TO_JSVAL(0)
157 #define JSVAL_ONE INT_TO_JSVAL(1)
158 #define JSVAL_FALSE BOOLEAN_TO_JSVAL(JS_FALSE)
159 #define JSVAL_TRUE BOOLEAN_TO_JSVAL(JS_TRUE)
161 /*
162 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
163 * comment in jstypes.h regarding safe int64 usage.
164 */
165 extern JS_PUBLIC_API(int64)
166 JS_Now();
168 /* Don't want to export data, so provide accessors for non-inline jsvals. */
169 extern JS_PUBLIC_API(jsval)
170 JS_GetNaNValue(JSContext *cx);
172 extern JS_PUBLIC_API(jsval)
173 JS_GetNegativeInfinityValue(JSContext *cx);
175 extern JS_PUBLIC_API(jsval)
176 JS_GetPositiveInfinityValue(JSContext *cx);
178 extern JS_PUBLIC_API(jsval)
179 JS_GetEmptyStringValue(JSContext *cx);
181 /*
182 * Format is a string of the following characters (spaces are insignificant),
183 * specifying the tabulated type conversions:
184 *
185 * b JSBool Boolean
186 * c uint16/jschar ECMA uint16, Unicode char
187 * i int32 ECMA int32
188 * u uint32 ECMA uint32
189 * j int32 Rounded int32 (coordinate)
190 * d jsdouble IEEE double
191 * I jsdouble Integral IEEE double
192 * s char * C string
193 * S JSString * Unicode string, accessed by a JSString pointer
194 * W jschar * Unicode character vector, 0-terminated (W for wide)
195 * o JSObject * Object reference
196 * f JSFunction * Function private
197 * v jsval Argument value (no conversion)
198 * * N/A Skip this argument (no vararg)
199 * / N/A End of required arguments
200 *
201 * The variable argument list after format must consist of &b, &c, &s, e.g.,
202 * where those variables have the types given above. For the pointer types
203 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
204 * to the JS runtime, not to the calling native code. The runtime promises
205 * to keep this memory valid so long as argv refers to allocated stack space
206 * (so long as the native function is active).
207 *
208 * Fewer arguments than format specifies may be passed only if there is a /
209 * in format after the last required argument specifier and argc is at least
210 * the number of required arguments. More arguments than format specifies
211 * may be passed without error; it is up to the caller to deal with trailing
212 * unconverted arguments.
213 */
214 extern JS_PUBLIC_API(JSBool)
215 JS_ConvertArguments(JSContext *cx, uintN argc, jsval *argv, const char *format,
216 ...);
218 #ifdef va_start
219 extern JS_PUBLIC_API(JSBool)
220 JS_ConvertArgumentsVA(JSContext *cx, uintN argc, jsval *argv,
221 const char *format, va_list ap);
222 #endif
224 /*
225 * Inverse of JS_ConvertArguments: scan format and convert trailing arguments
226 * into jsvals, GC-rooted if necessary by the JS stack. Return null on error,
227 * and a pointer to the new argument vector on success. Also return a stack
228 * mark on success via *markp, in which case the caller must eventually clean
229 * up by calling JS_PopArguments.
230 *
231 * Note that the number of actual arguments supplied is specified exclusively
232 * by format, so there is no argc parameter.
233 */
234 extern JS_PUBLIC_API(jsval *)
235 JS_PushArguments(JSContext *cx, void **markp, const char *format, ...);
237 #ifdef va_start
238 extern JS_PUBLIC_API(jsval *)
239 JS_PushArgumentsVA(JSContext *cx, void **markp, const char *format, va_list ap);
240 #endif
242 extern JS_PUBLIC_API(void)
243 JS_PopArguments(JSContext *cx, void *mark);
245 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
247 /*
248 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
249 * The handler function has this signature (see jspubtd.h):
250 *
251 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
252 * JSBool fromJS, jsval **vpp, va_list *app);
253 *
254 * It should return true on success, and return false after reporting an error
255 * or detecting an already-reported error.
256 *
257 * For a given format string, for example "AA", the formatter is called from
258 * JS_ConvertArgumentsVA like so:
259 *
260 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
261 *
262 * sp points into the arguments array on the JS stack, while ap points into
263 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
264 * the formatter to convert zero or more jsvals at sp to zero or more C values
265 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
266 * (via *app) to point past the converted arguments and their result pointers
267 * on the C stack.
268 *
269 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
270 *
271 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
272 *
273 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
274 * format specifier and store them at sp, updating ap and sp appropriately.
275 *
276 * The "..." after "AA" is the rest of the format string that was passed into
277 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
278 * in each Convert or PushArguments call is passed to the formatter, so that
279 * one such function may implement several formats, in order to share code.
280 *
281 * Remove just forgets about any handler associated with format. Add does not
282 * copy format, it points at the string storage allocated by the caller, which
283 * is typically a string constant. If format is in dynamic storage, it is up
284 * to the caller to keep the string alive until Remove is called.
285 */
286 extern JS_PUBLIC_API(JSBool)
287 JS_AddArgumentFormatter(JSContext *cx, const char *format,
288 JSArgumentFormatter formatter);
290 extern JS_PUBLIC_API(void)
291 JS_RemoveArgumentFormatter(JSContext *cx, const char *format);
293 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
295 extern JS_PUBLIC_API(JSBool)
296 JS_ConvertValue(JSContext *cx, jsval v, JSType type, jsval *vp);
298 extern JS_PUBLIC_API(JSBool)
299 JS_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
301 extern JS_PUBLIC_API(JSFunction *)
302 JS_ValueToFunction(JSContext *cx, jsval v);
304 extern JS_PUBLIC_API(JSFunction *)
305 JS_ValueToConstructor(JSContext *cx, jsval v);
307 extern JS_PUBLIC_API(JSString *)
308 JS_ValueToString(JSContext *cx, jsval v);
310 extern JS_PUBLIC_API(JSBool)
311 JS_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
313 /*
314 * Convert a value to a number, then to an int32, according to the ECMA rules
315 * for ToInt32.
316 */
317 extern JS_PUBLIC_API(JSBool)
318 JS_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip);
320 /*
321 * Convert a value to a number, then to a uint32, according to the ECMA rules
322 * for ToUint32.
323 */
324 extern JS_PUBLIC_API(JSBool)
325 JS_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip);
327 /*
328 * Convert a value to a number, then to an int32 if it fits by rounding to
329 * nearest; but failing with an error report if the double is out of range
330 * or unordered.
331 */
332 extern JS_PUBLIC_API(JSBool)
333 JS_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
335 /*
336 * ECMA ToUint16, for mapping a jsval to a Unicode point.
337 */
338 extern JS_PUBLIC_API(JSBool)
339 JS_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
341 extern JS_PUBLIC_API(JSBool)
342 JS_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp);
344 extern JS_PUBLIC_API(JSType)
345 JS_TypeOfValue(JSContext *cx, jsval v);
347 extern JS_PUBLIC_API(const char *)
348 JS_GetTypeName(JSContext *cx, JSType type);
350 /************************************************************************/
352 /*
353 * Initialization, locking, contexts, and memory allocation.
354 */
355 #define JS_NewRuntime JS_Init
356 #define JS_DestroyRuntime JS_Finish
357 #define JS_LockRuntime JS_Lock
358 #define JS_UnlockRuntime JS_Unlock
360 extern JS_PUBLIC_API(JSRuntime *)
361 JS_NewRuntime(uint32 maxbytes);
363 extern JS_PUBLIC_API(void)
364 JS_DestroyRuntime(JSRuntime *rt);
366 extern JS_PUBLIC_API(void)
367 JS_ShutDown(void);
369 JS_PUBLIC_API(void *)
370 JS_GetRuntimePrivate(JSRuntime *rt);
372 JS_PUBLIC_API(void)
373 JS_SetRuntimePrivate(JSRuntime *rt, void *data);
375 #ifdef JS_THREADSAFE
377 extern JS_PUBLIC_API(void)
378 JS_BeginRequest(JSContext *cx);
380 extern JS_PUBLIC_API(void)
381 JS_EndRequest(JSContext *cx);
383 /* Yield to pending GC operations, regardless of request depth */
384 extern JS_PUBLIC_API(void)
385 JS_YieldRequest(JSContext *cx);
387 extern JS_PUBLIC_API(jsrefcount)
388 JS_SuspendRequest(JSContext *cx);
390 extern JS_PUBLIC_API(void)
391 JS_ResumeRequest(JSContext *cx, jsrefcount saveDepth);
393 #endif /* JS_THREADSAFE */
395 extern JS_PUBLIC_API(void)
396 JS_Lock(JSRuntime *rt);
398 extern JS_PUBLIC_API(void)
399 JS_Unlock(JSRuntime *rt);
401 extern JS_PUBLIC_API(JSContext *)
402 JS_NewContext(JSRuntime *rt, size_t stackChunkSize);
404 extern JS_PUBLIC_API(void)
405 JS_DestroyContext(JSContext *cx);
407 extern JS_PUBLIC_API(void)
408 JS_DestroyContextNoGC(JSContext *cx);
410 extern JS_PUBLIC_API(void)
411 JS_DestroyContextMaybeGC(JSContext *cx);
413 extern JS_PUBLIC_API(void *)
414 JS_GetContextPrivate(JSContext *cx);
416 extern JS_PUBLIC_API(void)
417 JS_SetContextPrivate(JSContext *cx, void *data);
419 extern JS_PUBLIC_API(JSRuntime *)
420 JS_GetRuntime(JSContext *cx);
422 extern JS_PUBLIC_API(JSContext *)
423 JS_ContextIterator(JSRuntime *rt, JSContext **iterp);
425 extern JS_PUBLIC_API(JSVersion)
426 JS_GetVersion(JSContext *cx);
428 extern JS_PUBLIC_API(JSVersion)
429 JS_SetVersion(JSContext *cx, JSVersion version);
431 extern JS_PUBLIC_API(const char *)
432 JS_VersionToString(JSVersion version);
434 extern JS_PUBLIC_API(JSVersion)
435 JS_StringToVersion(const char *string);
437 /*
438 * JS options are orthogonal to version, and may be freely composed with one
439 * another as well as with version.
440 *
441 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
442 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
443 */
444 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
445 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
446 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
447 the last object on its 'obj'
448 param's scope chain as the
449 ECMA 'variables object' */
450 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
451 JS_BIT(3) /* context private data points
452 to an nsISupports subclass */
453 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
454 promises to execute compiled
455 script once only; enables
456 compile-time scope chain
457 resolution of consts. */
458 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
459 option supported for the
460 XUL preprocessor and kindred
461 beasts. */
462 #define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
463 parse <!-- --> as a token,
464 not backward compatible with
465 the comment-hiding hack used
466 in HTML script tags. */
467 #define JSOPTION_NATIVE_BRANCH_CALLBACK \
468 JS_BIT(7) /* the branch callback set by
469 JS_SetBranchCallback may be
470 called with a null script
471 parameter, by native code
472 that loops intensively */
474 extern JS_PUBLIC_API(uint32)
475 JS_GetOptions(JSContext *cx);
477 extern JS_PUBLIC_API(uint32)
478 JS_SetOptions(JSContext *cx, uint32 options);
480 extern JS_PUBLIC_API(uint32)
481 JS_ToggleOptions(JSContext *cx, uint32 options);
483 extern JS_PUBLIC_API(const char *)
484 JS_GetImplementationVersion(void);
486 extern JS_PUBLIC_API(JSObject *)
487 JS_GetGlobalObject(JSContext *cx);
489 extern JS_PUBLIC_API(void)
490 JS_SetGlobalObject(JSContext *cx, JSObject *obj);
492 /*
493 * Initialize standard JS class constructors, prototypes, and any top-level
494 * functions and constants associated with the standard classes (e.g. isNaN
495 * for Number).
496 *
497 * NB: This sets cx's global object to obj if it was null.
498 */
499 extern JS_PUBLIC_API(JSBool)
500 JS_InitStandardClasses(JSContext *cx, JSObject *obj);
502 /*
503 * Resolve id, which must contain either a string or an int, to a standard
504 * class name in obj if possible, defining the class's constructor and/or
505 * prototype and storing true in *resolved. If id does not name a standard
506 * class or a top-level property induced by initializing a standard class,
507 * store false in *resolved and just return true. Return false on error,
508 * as usual for JSBool result-typed API entry points.
509 *
510 * This API can be called directly from a global object class's resolve op,
511 * to define standard classes lazily. The class's enumerate op should call
512 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
513 * loops any classes not yet resolved lazily.
514 */
515 extern JS_PUBLIC_API(JSBool)
516 JS_ResolveStandardClass(JSContext *cx, JSObject *obj, jsval id,
517 JSBool *resolved);
519 extern JS_PUBLIC_API(JSBool)
520 JS_EnumerateStandardClasses(JSContext *cx, JSObject *obj);
522 /*
523 * Enumerate any already-resolved standard class ids into ida, or into a new
524 * JSIdArray if ida is null. Return the augmented array on success, null on
525 * failure with ida (if it was non-null on entry) destroyed.
526 */
527 extern JS_PUBLIC_API(JSIdArray *)
528 JS_EnumerateResolvedStandardClasses(JSContext *cx, JSObject *obj,
529 JSIdArray *ida);
531 extern JS_PUBLIC_API(JSObject *)
532 JS_GetScopeChain(JSContext *cx);
534 extern JS_PUBLIC_API(void *)
535 JS_malloc(JSContext *cx, size_t nbytes);
537 extern JS_PUBLIC_API(void *)
538 JS_realloc(JSContext *cx, void *p, size_t nbytes);
540 extern JS_PUBLIC_API(void)
541 JS_free(JSContext *cx, void *p);
543 extern JS_PUBLIC_API(char *)
544 JS_strdup(JSContext *cx, const char *s);
546 extern JS_PUBLIC_API(jsdouble *)
547 JS_NewDouble(JSContext *cx, jsdouble d);
549 extern JS_PUBLIC_API(JSBool)
550 JS_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
552 extern JS_PUBLIC_API(JSBool)
553 JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
555 /*
556 * A JS GC root is a pointer to a JSObject *, JSString *, or jsdouble * that
557 * itself points into the GC heap (more recently, we support this extension:
558 * a root may be a pointer to a jsval v for which JSVAL_IS_GCTHING(v) is true).
559 *
560 * Therefore, you never pass JSObject *obj to JS_AddRoot(cx, obj). You always
561 * call JS_AddRoot(cx, &obj), passing obj by reference. And later, before obj
562 * or the structure it is embedded within goes out of scope or is freed, you
563 * must call JS_RemoveRoot(cx, &obj).
564 *
565 * Also, use JS_AddNamedRoot(cx, &structPtr->memberObj, "structPtr->memberObj")
566 * in preference to JS_AddRoot(cx, &structPtr->memberObj), in order to identify
567 * roots by their source callsites. This way, you can find the callsite while
568 * debugging if you should fail to do JS_RemoveRoot(cx, &structPtr->memberObj)
569 * before freeing structPtr's memory.
570 */
571 extern JS_PUBLIC_API(JSBool)
572 JS_AddRoot(JSContext *cx, void *rp);
574 #ifdef NAME_ALL_GC_ROOTS
575 #define JS_DEFINE_TO_TOKEN(def) #def
576 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
577 #define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
578 #endif
580 extern JS_PUBLIC_API(JSBool)
581 JS_AddNamedRoot(JSContext *cx, void *rp, const char *name);
583 extern JS_PUBLIC_API(JSBool)
584 JS_AddNamedRootRT(JSRuntime *rt, void *rp, const char *name);
586 extern JS_PUBLIC_API(JSBool)
587 JS_RemoveRoot(JSContext *cx, void *rp);
589 extern JS_PUBLIC_API(JSBool)
590 JS_RemoveRootRT(JSRuntime *rt, void *rp);
592 /*
593 * The last GC thing of each type (object, string, double, external string
594 * types) created on a given context is kept alive until another thing of the
595 * same type is created, using a newborn root in the context. These newborn
596 * roots help native code protect newly-created GC-things from GC invocations
597 * activated before those things can be rooted using local or global roots.
598 *
599 * However, the newborn roots can also entrain great gobs of garbage, so the
600 * JS_GC entry point clears them for the context on which GC is being forced.
601 * Embeddings may need to do likewise for all contexts.
602 *
603 * See the scoped local root API immediately below for a better way to manage
604 * newborns in cases where native hooks (functions, getters, setters, etc.)
605 * create many GC-things, potentially without connecting them to predefined
606 * local roots such as *rval or argv[i] in an active native function. Using
607 * JS_EnterLocalRootScope disables updating of the context's per-gc-thing-type
608 * newborn roots, until control flow unwinds and leaves the outermost nesting
609 * local root scope.
610 */
611 extern JS_PUBLIC_API(void)
612 JS_ClearNewbornRoots(JSContext *cx);
614 /*
615 * Scoped local root management allows native functions, getter/setters, etc.
616 * to avoid worrying about the newborn root pigeon-holes, overloading local
617 * roots allocated in argv and *rval, or ending up having to call JS_Add*Root
618 * and JS_RemoveRoot to manage global roots temporarily.
619 *
620 * Instead, calling JS_EnterLocalRootScope and JS_LeaveLocalRootScope around
621 * the body of the native hook causes the engine to allocate a local root for
622 * each newborn created in between the two API calls, using a local root stack
623 * associated with cx. For example:
624 *
625 * JSBool
626 * my_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
627 * {
628 * JSBool ok;
629 *
630 * if (!JS_EnterLocalRootScope(cx))
631 * return JS_FALSE;
632 * ok = my_GetPropertyBody(cx, obj, id, vp);
633 * JS_LeaveLocalRootScope(cx);
634 * return ok;
635 * }
636 *
637 * NB: JS_LeaveLocalRootScope must be called once for every prior successful
638 * call to JS_EnterLocalRootScope. If JS_EnterLocalRootScope fails, you must
639 * not make the matching JS_LeaveLocalRootScope call.
640 *
641 * In case a native hook allocates many objects or other GC-things, but the
642 * native protects some of those GC-things by storing them as property values
643 * in an object that is itself protected, the hook can call JS_ForgetLocalRoot
644 * to free the local root automatically pushed for the now-protected GC-thing.
645 *
646 * JS_ForgetLocalRoot works on any GC-thing allocated in the current local
647 * root scope, but it's more time-efficient when called on references to more
648 * recently created GC-things. Calling it successively on other than the most
649 * recently allocated GC-thing will tend to average the time inefficiency, and
650 * may risk O(n^2) growth rate, but in any event, you shouldn't allocate too
651 * many local roots if you can root as you go (build a tree of objects from
652 * the top down, forgetting each latest-allocated GC-thing immediately upon
653 * linking it to its parent).
654 */
655 extern JS_PUBLIC_API(JSBool)
656 JS_EnterLocalRootScope(JSContext *cx);
658 extern JS_PUBLIC_API(void)
659 JS_LeaveLocalRootScope(JSContext *cx);
661 extern JS_PUBLIC_API(void)
662 JS_ForgetLocalRoot(JSContext *cx, void *thing);
664 #ifdef DEBUG
665 extern JS_PUBLIC_API(void)
666 JS_DumpNamedRoots(JSRuntime *rt,
667 void (*dump)(const char *name, void *rp, void *data),
668 void *data);
669 #endif
671 /*
672 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
673 * The root is pointed at by rp; if the root is unnamed, name is null; data is
674 * supplied from the third parameter to JS_MapGCRoots.
675 *
676 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
677 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
678 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
679 * constants are flags; you can OR them together.
680 *
681 * This function acquires and releases rt's GC lock around the mapping of the
682 * roots table, so the map function should run to completion in as few cycles
683 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
684 * or any JS API entry point that acquires locks, without double-tripping or
685 * deadlocking on the GC lock.
686 *
687 * JS_MapGCRoots returns the count of roots that were successfully mapped.
688 */
689 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
690 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
691 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
693 typedef intN
694 (* JS_DLL_CALLBACK JSGCRootMapFun)(void *rp, const char *name, void *data);
696 extern JS_PUBLIC_API(uint32)
697 JS_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data);
699 extern JS_PUBLIC_API(JSBool)
700 JS_LockGCThing(JSContext *cx, void *thing);
702 extern JS_PUBLIC_API(JSBool)
703 JS_LockGCThingRT(JSRuntime *rt, void *thing);
705 extern JS_PUBLIC_API(JSBool)
706 JS_UnlockGCThing(JSContext *cx, void *thing);
708 extern JS_PUBLIC_API(JSBool)
709 JS_UnlockGCThingRT(JSRuntime *rt, void *thing);
711 /*
712 * For implementors of JSObjectOps.mark, to mark a GC-thing reachable via a
713 * property or other strong ref identified for debugging purposes by name.
714 * The name argument's storage needs to live only as long as the call to
715 * this routine.
716 *
717 * The final arg is used by GC_MARK_DEBUG code to build a ref path through
718 * the GC's live thing graph. Implementors of JSObjectOps.mark should pass
719 * its final arg through to this function when marking all GC-things that are
720 * directly reachable from the object being marked.
721 *
722 * See the JSMarkOp typedef in jspubtd.h, and the JSObjectOps struct below.
723 */
724 extern JS_PUBLIC_API(void)
725 JS_MarkGCThing(JSContext *cx, void *thing, const char *name, void *arg);
727 extern JS_PUBLIC_API(void)
728 JS_GC(JSContext *cx);
730 extern JS_PUBLIC_API(void)
731 JS_MaybeGC(JSContext *cx);
733 extern JS_PUBLIC_API(JSGCCallback)
734 JS_SetGCCallback(JSContext *cx, JSGCCallback cb);
736 extern JS_PUBLIC_API(JSGCCallback)
737 JS_SetGCCallbackRT(JSRuntime *rt, JSGCCallback cb);
739 extern JS_PUBLIC_API(JSBool)
740 JS_IsAboutToBeFinalized(JSContext *cx, void *thing);
742 typedef enum JSGCParamKey {
743 JSGC_MAX_BYTES = 0, /* maximum nominal heap before last ditch GC */
744 JSGC_MAX_MALLOC_BYTES = 1 /* # of JS_malloc bytes before last ditch GC */
745 } JSGCParamKey;
747 extern JS_PUBLIC_API(void)
748 JS_SetGCParameter(JSRuntime *rt, JSGCParamKey key, uint32 value);
750 /*
751 * Add a finalizer for external strings created by JS_NewExternalString (see
752 * below) using a type-code returned from this function, and that understands
753 * how to free or release the memory pointed at by JS_GetStringChars(str).
754 *
755 * Return a nonnegative type index if there is room for finalizer in the
756 * global GC finalizers table, else return -1. If the engine is compiled
757 * JS_THREADSAFE and used in a multi-threaded environment, this function must
758 * be invoked on the primordial thread only, at startup -- or else the entire
759 * program must single-thread itself while loading a module that calls this
760 * function.
761 */
762 extern JS_PUBLIC_API(intN)
763 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer);
765 /*
766 * Remove finalizer from the global GC finalizers table, returning its type
767 * code if found, -1 if not found.
768 *
769 * As with JS_AddExternalStringFinalizer, there is a threading restriction
770 * if you compile the engine JS_THREADSAFE: this function may be called for a
771 * given finalizer pointer on only one thread; different threads may call to
772 * remove distinct finalizers safely.
773 *
774 * You must ensure that all strings with finalizer's type have been collected
775 * before calling this function. Otherwise, string data will be leaked by the
776 * GC, for want of a finalizer to call.
777 */
778 extern JS_PUBLIC_API(intN)
779 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer);
781 /*
782 * Create a new JSString whose chars member refers to external memory, i.e.,
783 * memory requiring special, type-specific finalization. The type code must
784 * be a nonnegative return value from JS_AddExternalStringFinalizer.
785 */
786 extern JS_PUBLIC_API(JSString *)
787 JS_NewExternalString(JSContext *cx, jschar *chars, size_t length, intN type);
789 /*
790 * Returns the external-string finalizer index for this string, or -1 if it is
791 * an "internal" (native to JS engine) string.
792 */
793 extern JS_PUBLIC_API(intN)
794 JS_GetExternalStringGCType(JSRuntime *rt, JSString *str);
796 /*
797 * Sets maximum (if stack grows upward) or minimum (downward) legal stack byte
798 * address in limitAddr for the thread or process stack used by cx. To disable
799 * stack size checking, pass 0 for limitAddr.
800 */
801 extern JS_PUBLIC_API(void)
802 JS_SetThreadStackLimit(JSContext *cx, jsuword limitAddr);
804 /************************************************************************/
806 /*
807 * Classes, objects, and properties.
808 */
810 /* For detailed comments on the function pointer types, see jspubtd.h. */
811 struct JSClass {
812 const char *name;
813 uint32 flags;
815 /* Mandatory non-null function pointer members. */
816 JSPropertyOp addProperty;
817 JSPropertyOp delProperty;
818 JSPropertyOp getProperty;
819 JSPropertyOp setProperty;
820 JSEnumerateOp enumerate;
821 JSResolveOp resolve;
822 JSConvertOp convert;
823 JSFinalizeOp finalize;
825 /* Optionally non-null members start here. */
826 JSGetObjectOps getObjectOps;
827 JSCheckAccessOp checkAccess;
828 JSNative call;
829 JSNative construct;
830 JSXDRObjectOp xdrObject;
831 JSHasInstanceOp hasInstance;
832 JSMarkOp mark;
833 JSReserveSlotsOp reserveSlots;
834 };
836 struct JSExtendedClass {
837 JSClass base;
838 JSEqualityOp equality;
839 JSObjectOp outerObject;
840 JSObjectOp innerObject;
841 jsword reserved0;
842 jsword reserved1;
843 jsword reserved2;
844 jsword reserved3;
845 jsword reserved4;
846 };
848 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
849 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
850 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
851 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
852 #define JSCLASS_SHARE_ALL_PROPERTIES (1<<4) /* all properties are SHARED */
853 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
854 object in prototype chain
855 passed in via *objp in/out
856 parameter */
857 #define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
858 prototype */
859 #define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
861 /*
862 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
863 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
864 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
865 */
866 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
867 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
868 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
869 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
870 << JSCLASS_RESERVED_SLOTS_SHIFT)
871 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
872 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
873 & JSCLASS_RESERVED_SLOTS_MASK)
875 #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
876 JSCLASS_RESERVED_SLOTS_WIDTH)
878 /* True if JSClass is really a JSExtendedClass. */
879 #define JSCLASS_IS_EXTENDED (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
881 /* Initializer for unused members of statically initialized JSClass structs. */
882 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,0
883 #define JSCLASS_NO_RESERVED_MEMBERS 0,0,0,0,0
885 /* For detailed comments on these function pointer types, see jspubtd.h. */
886 struct JSObjectOps {
887 /* Mandatory non-null function pointer members. */
888 JSNewObjectMapOp newObjectMap;
889 JSObjectMapOp destroyObjectMap;
890 JSLookupPropOp lookupProperty;
891 JSDefinePropOp defineProperty;
892 JSPropertyIdOp getProperty;
893 JSPropertyIdOp setProperty;
894 JSAttributesOp getAttributes;
895 JSAttributesOp setAttributes;
896 JSPropertyIdOp deleteProperty;
897 JSConvertOp defaultValue;
898 JSNewEnumerateOp enumerate;
899 JSCheckAccessIdOp checkAccess;
901 /* Optionally non-null members start here. */
902 JSObjectOp thisObject;
903 JSPropertyRefOp dropProperty;
904 JSNative call;
905 JSNative construct;
906 JSXDRObjectOp xdrObject;
907 JSHasInstanceOp hasInstance;
908 JSSetObjectSlotOp setProto;
909 JSSetObjectSlotOp setParent;
910 JSMarkOp mark;
911 JSFinalizeOp clear;
912 JSGetRequiredSlotOp getRequiredSlot;
913 JSSetRequiredSlotOp setRequiredSlot;
914 };
916 struct JSXMLObjectOps {
917 JSObjectOps base;
918 JSGetMethodOp getMethod;
919 JSSetMethodOp setMethod;
920 JSEnumerateValuesOp enumerateValues;
921 JSEqualityOp equality;
922 JSConcatenateOp concatenate;
923 };
925 /*
926 * Classes that expose JSObjectOps via a non-null getObjectOps class hook may
927 * derive a property structure from this struct, return a pointer to it from
928 * lookupProperty and defineProperty, and use the pointer to avoid rehashing
929 * in getAttributes and setAttributes.
930 *
931 * The jsid type contains either an int jsval (see JSVAL_IS_INT above), or an
932 * internal pointer that is opaque to users of this API, but which users may
933 * convert from and to a jsval using JS_ValueToId and JS_IdToValue.
934 */
935 struct JSProperty {
936 jsid id;
937 };
939 struct JSIdArray {
940 jsint length;
941 jsid vector[1]; /* actually, length jsid words */
942 };
944 extern JS_PUBLIC_API(void)
945 JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
947 extern JS_PUBLIC_API(JSBool)
948 JS_ValueToId(JSContext *cx, jsval v, jsid *idp);
950 extern JS_PUBLIC_API(JSBool)
951 JS_IdToValue(JSContext *cx, jsid id, jsval *vp);
953 /*
954 * The magic XML namespace id is int-tagged, but not a valid integer jsval.
955 * Global object classes in embeddings that enable JS_HAS_XML_SUPPORT (E4X)
956 * should handle this id specially before converting id via JSVAL_TO_INT.
957 */
958 #define JS_DEFAULT_XML_NAMESPACE_ID ((jsid) JSVAL_VOID)
960 /*
961 * JSNewResolveOp flag bits.
962 */
963 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
964 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
965 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
966 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
967 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
969 extern JS_PUBLIC_API(JSBool)
970 JS_PropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
972 extern JS_PUBLIC_API(JSBool)
973 JS_EnumerateStub(JSContext *cx, JSObject *obj);
975 extern JS_PUBLIC_API(JSBool)
976 JS_ResolveStub(JSContext *cx, JSObject *obj, jsval id);
978 extern JS_PUBLIC_API(JSBool)
979 JS_ConvertStub(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
981 extern JS_PUBLIC_API(void)
982 JS_FinalizeStub(JSContext *cx, JSObject *obj);
984 struct JSConstDoubleSpec {
985 jsdouble dval;
986 const char *name;
987 uint8 flags;
988 uint8 spare[3];
989 };
991 /*
992 * To define an array element rather than a named property member, cast the
993 * element's index to (const char *) and initialize name with it, and set the
994 * JSPROP_INDEX bit in flags.
995 */
996 struct JSPropertySpec {
997 const char *name;
998 int8 tinyid;
999 uint8 flags;
1000 JSPropertyOp getter;
1001 JSPropertyOp setter;
1002 };
1004 struct JSFunctionSpec {
1005 const char *name;
1006 JSNative call;
1007 uint8 nargs;
1008 uint8 flags;
1009 uint16 extra; /* number of arg slots for local GC roots */
1010 };
1012 extern JS_PUBLIC_API(JSObject *)
1013 JS_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
1014 JSClass *clasp, JSNative constructor, uintN nargs,
1015 JSPropertySpec *ps, JSFunctionSpec *fs,
1016 JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
1018 #ifdef JS_THREADSAFE
1019 extern JS_PUBLIC_API(JSClass *)
1020 JS_GetClass(JSContext *cx, JSObject *obj);
1022 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
1023 #else
1024 extern JS_PUBLIC_API(JSClass *)
1025 JS_GetClass(JSObject *obj);
1027 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
1028 #endif
1030 extern JS_PUBLIC_API(JSBool)
1031 JS_InstanceOf(JSContext *cx, JSObject *obj, JSClass *clasp, jsval *argv);
1033 extern JS_PUBLIC_API(JSBool)
1034 JS_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
1036 extern JS_PUBLIC_API(void *)
1037 JS_GetPrivate(JSContext *cx, JSObject *obj);
1039 extern JS_PUBLIC_API(JSBool)
1040 JS_SetPrivate(JSContext *cx, JSObject *obj, void *data);
1042 extern JS_PUBLIC_API(void *)
1043 JS_GetInstancePrivate(JSContext *cx, JSObject *obj, JSClass *clasp,
1044 jsval *argv);
1046 extern JS_PUBLIC_API(JSObject *)
1047 JS_GetPrototype(JSContext *cx, JSObject *obj);
1049 extern JS_PUBLIC_API(JSBool)
1050 JS_SetPrototype(JSContext *cx, JSObject *obj, JSObject *proto);
1052 extern JS_PUBLIC_API(JSObject *)
1053 JS_GetParent(JSContext *cx, JSObject *obj);
1055 extern JS_PUBLIC_API(JSBool)
1056 JS_SetParent(JSContext *cx, JSObject *obj, JSObject *parent);
1058 extern JS_PUBLIC_API(JSObject *)
1059 JS_GetConstructor(JSContext *cx, JSObject *proto);
1061 /*
1062 * Get a unique identifier for obj, good for the lifetime of obj (even if it
1063 * is moved by a copying GC). Return false on failure (likely out of memory),
1064 * and true with *idp containing the unique id on success.
1065 */
1066 extern JS_PUBLIC_API(JSBool)
1067 JS_GetObjectId(JSContext *cx, JSObject *obj, jsid *idp);
1069 extern JS_PUBLIC_API(JSObject *)
1070 JS_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent);
1072 extern JS_PUBLIC_API(JSBool)
1073 JS_SealObject(JSContext *cx, JSObject *obj, JSBool deep);
1075 extern JS_PUBLIC_API(JSObject *)
1076 JS_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
1077 JSObject *parent);
1079 extern JS_PUBLIC_API(JSObject *)
1080 JS_ConstructObjectWithArguments(JSContext *cx, JSClass *clasp, JSObject *proto,
1081 JSObject *parent, uintN argc, jsval *argv);
1083 extern JS_PUBLIC_API(JSObject *)
1084 JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, JSClass *clasp,
1085 JSObject *proto, uintN attrs);
1087 extern JS_PUBLIC_API(JSBool)
1088 JS_DefineConstDoubles(JSContext *cx, JSObject *obj, JSConstDoubleSpec *cds);
1090 extern JS_PUBLIC_API(JSBool)
1091 JS_DefineProperties(JSContext *cx, JSObject *obj, JSPropertySpec *ps);
1093 extern JS_PUBLIC_API(JSBool)
1094 JS_DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
1095 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
1097 /*
1098 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1099 *
1100 * If the object does not have a property by that name, *foundp will be
1101 * JS_FALSE and the value of *attrsp is undefined.
1102 */
1103 extern JS_PUBLIC_API(JSBool)
1104 JS_GetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
1105 uintN *attrsp, JSBool *foundp);
1107 /*
1108 * The same, but if the property is native, return its getter and setter via
1109 * *getterp and *setterp, respectively (and only if the out parameter pointer
1110 * is not null).
1111 */
1112 extern JS_PUBLIC_API(JSBool)
1113 JS_GetPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
1114 const char *name,
1115 uintN *attrsp, JSBool *foundp,
1116 JSPropertyOp *getterp,
1117 JSPropertyOp *setterp);
1119 /*
1120 * Set the attributes of a property on a given object.
1121 *
1122 * If the object does not have a property by that name, *foundp will be
1123 * JS_FALSE and nothing will be altered.
1124 */
1125 extern JS_PUBLIC_API(JSBool)
1126 JS_SetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
1127 uintN attrs, JSBool *foundp);
1129 extern JS_PUBLIC_API(JSBool)
1130 JS_DefinePropertyWithTinyId(JSContext *cx, JSObject *obj, const char *name,
1131 int8 tinyid, jsval value,
1132 JSPropertyOp getter, JSPropertyOp setter,
1133 uintN attrs);
1135 extern JS_PUBLIC_API(JSBool)
1136 JS_AliasProperty(JSContext *cx, JSObject *obj, const char *name,
1137 const char *alias);
1139 extern JS_PUBLIC_API(JSBool)
1140 JS_HasProperty(JSContext *cx, JSObject *obj, const char *name, JSBool *foundp);
1142 extern JS_PUBLIC_API(JSBool)
1143 JS_LookupProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1145 extern JS_PUBLIC_API(JSBool)
1146 JS_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, const char *name,
1147 uintN flags, jsval *vp);
1149 extern JS_PUBLIC_API(JSBool)
1150 JS_GetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1152 extern JS_PUBLIC_API(JSBool)
1153 JS_GetMethod(JSContext *cx, JSObject *obj, const char *name, JSObject **objp,
1154 jsval *vp);
1156 extern JS_PUBLIC_API(JSBool)
1157 JS_SetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1159 extern JS_PUBLIC_API(JSBool)
1160 JS_DeleteProperty(JSContext *cx, JSObject *obj, const char *name);
1162 extern JS_PUBLIC_API(JSBool)
1163 JS_DeleteProperty2(JSContext *cx, JSObject *obj, const char *name,
1164 jsval *rval);
1166 extern JS_PUBLIC_API(JSBool)
1167 JS_DefineUCProperty(JSContext *cx, JSObject *obj,
1168 const jschar *name, size_t namelen, jsval value,
1169 JSPropertyOp getter, JSPropertyOp setter,
1170 uintN attrs);
1172 /*
1173 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1174 *
1175 * If the object does not have a property by that name, *foundp will be
1176 * JS_FALSE and the value of *attrsp is undefined.
1177 */
1178 extern JS_PUBLIC_API(JSBool)
1179 JS_GetUCPropertyAttributes(JSContext *cx, JSObject *obj,
1180 const jschar *name, size_t namelen,
1181 uintN *attrsp, JSBool *foundp);
1183 /*
1184 * The same, but if the property is native, return its getter and setter via
1185 * *getterp and *setterp, respectively (and only if the out parameter pointer
1186 * is not null).
1187 */
1188 extern JS_PUBLIC_API(JSBool)
1189 JS_GetUCPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
1190 const jschar *name, size_t namelen,
1191 uintN *attrsp, JSBool *foundp,
1192 JSPropertyOp *getterp,
1193 JSPropertyOp *setterp);
1195 /*
1196 * Set the attributes of a property on a given object.
1197 *
1198 * If the object does not have a property by that name, *foundp will be
1199 * JS_FALSE and nothing will be altered.
1200 */
1201 extern JS_PUBLIC_API(JSBool)
1202 JS_SetUCPropertyAttributes(JSContext *cx, JSObject *obj,
1203 const jschar *name, size_t namelen,
1204 uintN attrs, JSBool *foundp);
1207 extern JS_PUBLIC_API(JSBool)
1208 JS_DefineUCPropertyWithTinyId(JSContext *cx, JSObject *obj,
1209 const jschar *name, size_t namelen,
1210 int8 tinyid, jsval value,
1211 JSPropertyOp getter, JSPropertyOp setter,
1212 uintN attrs);
1214 extern JS_PUBLIC_API(JSBool)
1215 JS_HasUCProperty(JSContext *cx, JSObject *obj,
1216 const jschar *name, size_t namelen,
1217 JSBool *vp);
1219 extern JS_PUBLIC_API(JSBool)
1220 JS_LookupUCProperty(JSContext *cx, JSObject *obj,
1221 const jschar *name, size_t namelen,
1222 jsval *vp);
1224 extern JS_PUBLIC_API(JSBool)
1225 JS_GetUCProperty(JSContext *cx, JSObject *obj,
1226 const jschar *name, size_t namelen,
1227 jsval *vp);
1229 extern JS_PUBLIC_API(JSBool)
1230 JS_SetUCProperty(JSContext *cx, JSObject *obj,
1231 const jschar *name, size_t namelen,
1232 jsval *vp);
1234 extern JS_PUBLIC_API(JSBool)
1235 JS_DeleteUCProperty2(JSContext *cx, JSObject *obj,
1236 const jschar *name, size_t namelen,
1237 jsval *rval);
1239 extern JS_PUBLIC_API(JSObject *)
1240 JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector);
1242 extern JS_PUBLIC_API(JSBool)
1243 JS_IsArrayObject(JSContext *cx, JSObject *obj);
1245 extern JS_PUBLIC_API(JSBool)
1246 JS_GetArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1248 extern JS_PUBLIC_API(JSBool)
1249 JS_SetArrayLength(JSContext *cx, JSObject *obj, jsuint length);
1251 extern JS_PUBLIC_API(JSBool)
1252 JS_HasArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1254 extern JS_PUBLIC_API(JSBool)
1255 JS_DefineElement(JSContext *cx, JSObject *obj, jsint index, jsval value,
1256 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
1258 extern JS_PUBLIC_API(JSBool)
1259 JS_AliasElement(JSContext *cx, JSObject *obj, const char *name, jsint alias);
1261 extern JS_PUBLIC_API(JSBool)
1262 JS_HasElement(JSContext *cx, JSObject *obj, jsint index, JSBool *foundp);
1264 extern JS_PUBLIC_API(JSBool)
1265 JS_LookupElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1267 extern JS_PUBLIC_API(JSBool)
1268 JS_GetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1270 extern JS_PUBLIC_API(JSBool)
1271 JS_SetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1273 extern JS_PUBLIC_API(JSBool)
1274 JS_DeleteElement(JSContext *cx, JSObject *obj, jsint index);
1276 extern JS_PUBLIC_API(JSBool)
1277 JS_DeleteElement2(JSContext *cx, JSObject *obj, jsint index, jsval *rval);
1279 extern JS_PUBLIC_API(void)
1280 JS_ClearScope(JSContext *cx, JSObject *obj);
1282 extern JS_PUBLIC_API(JSIdArray *)
1283 JS_Enumerate(JSContext *cx, JSObject *obj);
1285 /*
1286 * Create an object to iterate over enumerable properties of obj, in arbitrary
1287 * property definition order. NB: This differs from longstanding for..in loop
1288 * order, which uses order of property definition in obj.
1289 */
1290 extern JS_PUBLIC_API(JSObject *)
1291 JS_NewPropertyIterator(JSContext *cx, JSObject *obj);
1293 /*
1294 * Return true on success with *idp containing the id of the next enumerable
1295 * property to visit using iterobj, or JSVAL_VOID if there is no such property
1296 * left to visit. Return false on error.
1297 */
1298 extern JS_PUBLIC_API(JSBool)
1299 JS_NextProperty(JSContext *cx, JSObject *iterobj, jsid *idp);
1301 extern JS_PUBLIC_API(JSBool)
1302 JS_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
1303 jsval *vp, uintN *attrsp);
1305 extern JS_PUBLIC_API(JSCheckAccessOp)
1306 JS_SetCheckObjectAccessCallback(JSRuntime *rt, JSCheckAccessOp acb);
1308 extern JS_PUBLIC_API(JSBool)
1309 JS_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
1311 extern JS_PUBLIC_API(JSBool)
1312 JS_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
1314 /************************************************************************/
1316 /*
1317 * Security protocol.
1318 */
1319 struct JSPrincipals {
1320 char *codebase;
1322 /* XXX unspecified and unused by Mozilla code -- can we remove these? */
1323 void * (* JS_DLL_CALLBACK getPrincipalArray)(JSContext *cx, JSPrincipals *);
1324 JSBool (* JS_DLL_CALLBACK globalPrivilegesEnabled)(JSContext *cx, JSPrincipals *);
1326 /* Don't call "destroy"; use reference counting macros below. */
1327 jsrefcount refcount;
1329 void (* JS_DLL_CALLBACK destroy)(JSContext *cx, JSPrincipals *);
1330 JSBool (* JS_DLL_CALLBACK subsume)(JSPrincipals *, JSPrincipals *);
1331 };
1333 #ifdef JS_THREADSAFE
1334 #define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
1335 #define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
1337 extern JS_PUBLIC_API(jsrefcount)
1338 JS_HoldPrincipals(JSContext *cx, JSPrincipals *principals);
1340 extern JS_PUBLIC_API(jsrefcount)
1341 JS_DropPrincipals(JSContext *cx, JSPrincipals *principals);
1343 #else
1344 #define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
1345 #define JSPRINCIPALS_DROP(cx, principals) \
1346 ((--(principals)->refcount == 0) \
1347 ? ((*(principals)->destroy)((cx), (principals)), 0) \
1348 : (principals)->refcount)
1349 #endif
1351 extern JS_PUBLIC_API(JSPrincipalsTranscoder)
1352 JS_SetPrincipalsTranscoder(JSRuntime *rt, JSPrincipalsTranscoder px);
1354 extern JS_PUBLIC_API(JSObjectPrincipalsFinder)
1355 JS_SetObjectPrincipalsFinder(JSRuntime *rt, JSObjectPrincipalsFinder fop);
1357 /************************************************************************/
1359 /*
1360 * Functions and scripts.
1361 */
1362 extern JS_PUBLIC_API(JSFunction *)
1363 JS_NewFunction(JSContext *cx, JSNative call, uintN nargs, uintN flags,
1364 JSObject *parent, const char *name);
1366 extern JS_PUBLIC_API(JSObject *)
1367 JS_GetFunctionObject(JSFunction *fun);
1369 /*
1370 * Deprecated, useful only for diagnostics. Use JS_GetFunctionId instead for
1371 * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
1372 */
1373 extern JS_PUBLIC_API(const char *)
1374 JS_GetFunctionName(JSFunction *fun);
1376 /*
1377 * Return the function's identifier as a JSString, or null if fun is unnamed.
1378 * The returned string lives as long as fun, so you don't need to root a saved
1379 * reference to it if fun is well-connected or rooted, and provided you bound
1380 * the use of the saved reference by fun's lifetime.
1381 *
1382 * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
1383 * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
1384 * from UTF-16-ish jschars.
1385 */
1386 extern JS_PUBLIC_API(JSString *)
1387 JS_GetFunctionId(JSFunction *fun);
1389 /*
1390 * Return JSFUN_* flags for fun.
1391 */
1392 extern JS_PUBLIC_API(uintN)
1393 JS_GetFunctionFlags(JSFunction *fun);
1395 /*
1396 * Return the arity (length) of fun.
1397 */
1398 extern JS_PUBLIC_API(uint16)
1399 JS_GetFunctionArity(JSFunction *fun);
1401 /*
1402 * Infallible predicate to test whether obj is a function object (faster than
1403 * comparing obj's class name to "Function", but equivalent unless someone has
1404 * overwritten the "Function" identifier with a different constructor and then
1405 * created instances using that constructor that might be passed in as obj).
1406 */
1407 extern JS_PUBLIC_API(JSBool)
1408 JS_ObjectIsFunction(JSContext *cx, JSObject *obj);
1410 extern JS_PUBLIC_API(JSBool)
1411 JS_DefineFunctions(JSContext *cx, JSObject *obj, JSFunctionSpec *fs);
1413 extern JS_PUBLIC_API(JSFunction *)
1414 JS_DefineFunction(JSContext *cx, JSObject *obj, const char *name, JSNative call,
1415 uintN nargs, uintN attrs);
1417 extern JS_PUBLIC_API(JSFunction *)
1418 JS_DefineUCFunction(JSContext *cx, JSObject *obj,
1419 const jschar *name, size_t namelen, JSNative call,
1420 uintN nargs, uintN attrs);
1422 extern JS_PUBLIC_API(JSObject *)
1423 JS_CloneFunctionObject(JSContext *cx, JSObject *funobj, JSObject *parent);
1425 /*
1426 * Given a buffer, return JS_FALSE if the buffer might become a valid
1427 * javascript statement with the addition of more lines. Otherwise return
1428 * JS_TRUE. The intent is to support interactive compilation - accumulate
1429 * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
1430 * the compiler.
1431 */
1432 extern JS_PUBLIC_API(JSBool)
1433 JS_BufferIsCompilableUnit(JSContext *cx, JSObject *obj,
1434 const char *bytes, size_t length);
1436 /*
1437 * The JSScript objects returned by the following functions refer to string and
1438 * other kinds of literals, including doubles and RegExp objects. These
1439 * literals are vulnerable to garbage collection; to root script objects and
1440 * prevent literals from being collected, create a rootable object using
1441 * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
1442 */
1443 extern JS_PUBLIC_API(JSScript *)
1444 JS_CompileScript(JSContext *cx, JSObject *obj,
1445 const char *bytes, size_t length,
1446 const char *filename, uintN lineno);
1448 extern JS_PUBLIC_API(JSScript *)
1449 JS_CompileScriptForPrincipals(JSContext *cx, JSObject *obj,
1450 JSPrincipals *principals,
1451 const char *bytes, size_t length,
1452 const char *filename, uintN lineno);
1454 extern JS_PUBLIC_API(JSScript *)
1455 JS_CompileUCScript(JSContext *cx, JSObject *obj,
1456 const jschar *chars, size_t length,
1457 const char *filename, uintN lineno);
1459 extern JS_PUBLIC_API(JSScript *)
1460 JS_CompileUCScriptForPrincipals(JSContext *cx, JSObject *obj,
1461 JSPrincipals *principals,
1462 const jschar *chars, size_t length,
1463 const char *filename, uintN lineno);
1465 extern JS_PUBLIC_API(JSScript *)
1466 JS_CompileFile(JSContext *cx, JSObject *obj, const char *filename);
1468 extern JS_PUBLIC_API(JSScript *)
1469 JS_CompileFileHandle(JSContext *cx, JSObject *obj, const char *filename,
1470 FILE *fh);
1472 extern JS_PUBLIC_API(JSScript *)
1473 JS_CompileFileHandleForPrincipals(JSContext *cx, JSObject *obj,
1474 const char *filename, FILE *fh,
1475 JSPrincipals *principals);
1477 /*
1478 * NB: you must use JS_NewScriptObject and root a pointer to its return value
1479 * in order to keep a JSScript and its atoms safe from garbage collection after
1480 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
1481 * E.g., and without error checks:
1482 *
1483 * JSScript *script = JS_CompileFile(cx, global, filename);
1484 * JSObject *scrobj = JS_NewScriptObject(cx, script);
1485 * JS_AddNamedRoot(cx, &scrobj, "scrobj");
1486 * do {
1487 * jsval result;
1488 * JS_ExecuteScript(cx, global, script, &result);
1489 * JS_GC();
1490 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
1491 * JS_RemoveRoot(cx, &scrobj);
1492 */
1493 extern JS_PUBLIC_API(JSObject *)
1494 JS_NewScriptObject(JSContext *cx, JSScript *script);
1496 /*
1497 * Infallible getter for a script's object. If JS_NewScriptObject has not been
1498 * called on script yet, the return value will be null.
1499 */
1500 extern JS_PUBLIC_API(JSObject *)
1501 JS_GetScriptObject(JSScript *script);
1503 extern JS_PUBLIC_API(void)
1504 JS_DestroyScript(JSContext *cx, JSScript *script);
1506 extern JS_PUBLIC_API(JSFunction *)
1507 JS_CompileFunction(JSContext *cx, JSObject *obj, const char *name,
1508 uintN nargs, const char **argnames,
1509 const char *bytes, size_t length,
1510 const char *filename, uintN lineno);
1512 extern JS_PUBLIC_API(JSFunction *)
1513 JS_CompileFunctionForPrincipals(JSContext *cx, JSObject *obj,
1514 JSPrincipals *principals, const char *name,
1515 uintN nargs, const char **argnames,
1516 const char *bytes, size_t length,
1517 const char *filename, uintN lineno);
1519 extern JS_PUBLIC_API(JSFunction *)
1520 JS_CompileUCFunction(JSContext *cx, JSObject *obj, const char *name,
1521 uintN nargs, const char **argnames,
1522 const jschar *chars, size_t length,
1523 const char *filename, uintN lineno);
1525 extern JS_PUBLIC_API(JSFunction *)
1526 JS_CompileUCFunctionForPrincipals(JSContext *cx, JSObject *obj,
1527 JSPrincipals *principals, const char *name,
1528 uintN nargs, const char **argnames,
1529 const jschar *chars, size_t length,
1530 const char *filename, uintN lineno);
1532 extern JS_PUBLIC_API(JSString *)
1533 JS_DecompileScript(JSContext *cx, JSScript *script, const char *name,
1534 uintN indent);
1536 /*
1537 * API extension: OR this into indent to avoid pretty-printing the decompiled
1538 * source resulting from JS_DecompileFunction{,Body}.
1539 */
1540 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
1542 extern JS_PUBLIC_API(JSString *)
1543 JS_DecompileFunction(JSContext *cx, JSFunction *fun, uintN indent);
1545 extern JS_PUBLIC_API(JSString *)
1546 JS_DecompileFunctionBody(JSContext *cx, JSFunction *fun, uintN indent);
1548 /*
1549 * NB: JS_ExecuteScript, JS_ExecuteScriptPart, and the JS_Evaluate*Script*
1550 * quadruplets all use the obj parameter as the initial scope chain header,
1551 * the 'this' keyword value, and the variables object (ECMA parlance for where
1552 * 'var' and 'function' bind names) of the execution context for script.
1553 *
1554 * Using obj as the variables object is problematic if obj's parent (which is
1555 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
1556 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
1557 * created by assignment to an unbound id, 'x = 0', go in the last object on
1558 * the scope chain linked by parent.
1559 *
1560 * ECMA calls that last scoping object the "global object", but note that many
1561 * embeddings have several such objects. ECMA requires that "global code" be
1562 * executed with the variables object equal to this global object. But these
1563 * JS API entry points provide freedom to execute code against a "sub-global",
1564 * i.e., a parented or scoped object, in which case the variables object will
1565 * differ from the last object on the scope chain, resulting in confusing and
1566 * non-ECMA explicit vs. implicit variable creation.
1567 *
1568 * Caveat embedders: unless you already depend on this buggy variables object
1569 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
1570 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
1571 * someone may have set other options on cx already -- for each context in the
1572 * application, if you pass parented objects as the obj parameter, or may ever
1573 * pass such objects in the future.
1574 *
1575 * Why a runtime option? The alternative is to add six or so new API entry
1576 * points with signatures matching the following six, and that doesn't seem
1577 * worth the code bloat cost. Such new entry points would probably have less
1578 * obvious names, too, so would not tend to be used. The JS_SetOption call,
1579 * OTOH, can be more easily hacked into existing code that does not depend on
1580 * the bug; such code can continue to use the familiar JS_EvaluateScript,
1581 * etc., entry points.
1582 */
1583 extern JS_PUBLIC_API(JSBool)
1584 JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval);
1586 /*
1587 * Execute either the function-defining prolog of a script, or the script's
1588 * main body, but not both.
1589 */
1590 typedef enum JSExecPart { JSEXEC_PROLOG, JSEXEC_MAIN } JSExecPart;
1592 extern JS_PUBLIC_API(JSBool)
1593 JS_ExecuteScriptPart(JSContext *cx, JSObject *obj, JSScript *script,
1594 JSExecPart part, jsval *rval);
1596 extern JS_PUBLIC_API(JSBool)
1597 JS_EvaluateScript(JSContext *cx, JSObject *obj,
1598 const char *bytes, uintN length,
1599 const char *filename, uintN lineno,
1600 jsval *rval);
1602 extern JS_PUBLIC_API(JSBool)
1603 JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj,
1604 JSPrincipals *principals,
1605 const char *bytes, uintN length,
1606 const char *filename, uintN lineno,
1607 jsval *rval);
1609 extern JS_PUBLIC_API(JSBool)
1610 JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
1611 const jschar *chars, uintN length,
1612 const char *filename, uintN lineno,
1613 jsval *rval);
1615 extern JS_PUBLIC_API(JSBool)
1616 JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj,
1617 JSPrincipals *principals,
1618 const jschar *chars, uintN length,
1619 const char *filename, uintN lineno,
1620 jsval *rval);
1622 extern JS_PUBLIC_API(JSBool)
1623 JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc,
1624 jsval *argv, jsval *rval);
1626 extern JS_PUBLIC_API(JSBool)
1627 JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, uintN argc,
1628 jsval *argv, jsval *rval);
1630 extern JS_PUBLIC_API(JSBool)
1631 JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc,
1632 jsval *argv, jsval *rval);
1634 extern JS_PUBLIC_API(JSBranchCallback)
1635 JS_SetBranchCallback(JSContext *cx, JSBranchCallback cb);
1637 extern JS_PUBLIC_API(JSBool)
1638 JS_IsRunning(JSContext *cx);
1640 extern JS_PUBLIC_API(JSBool)
1641 JS_IsConstructing(JSContext *cx);
1643 /*
1644 * Returns true if a script is executing and its current bytecode is a set
1645 * (assignment) operation, even if there are native (no script) stack frames
1646 * between the script and the caller to JS_IsAssigning.
1647 */
1648 extern JS_FRIEND_API(JSBool)
1649 JS_IsAssigning(JSContext *cx);
1651 /*
1652 * Set the second return value, which should be a string or int jsval that
1653 * identifies a property in the returned object, to form an ECMA reference
1654 * type value (obj, id). Only native methods can return reference types,
1655 * and if the returned value is used on the left-hand side of an assignment
1656 * op, the identified property will be set. If the return value is in an
1657 * r-value, the interpreter just gets obj[id]'s value.
1658 */
1659 extern JS_PUBLIC_API(void)
1660 JS_SetCallReturnValue2(JSContext *cx, jsval v);
1662 /************************************************************************/
1664 /*
1665 * Strings.
1666 *
1667 * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
1668 * on error (signified by null return), it leaves bytes owned by the caller.
1669 * So the caller must free bytes in the error case, if it has no use for them.
1670 * In contrast, all the JS_New*StringCopy* functions do not take ownership of
1671 * the character memory passed to them -- they copy it.
1672 */
1673 extern JS_PUBLIC_API(JSString *)
1674 JS_NewString(JSContext *cx, char *bytes, size_t length);
1676 extern JS_PUBLIC_API(JSString *)
1677 JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);
1679 extern JS_PUBLIC_API(JSString *)
1680 JS_NewStringCopyZ(JSContext *cx, const char *s);
1682 extern JS_PUBLIC_API(JSString *)
1683 JS_InternString(JSContext *cx, const char *s);
1685 extern JS_PUBLIC_API(JSString *)
1686 JS_NewUCString(JSContext *cx, jschar *chars, size_t length);
1688 extern JS_PUBLIC_API(JSString *)
1689 JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n);
1691 extern JS_PUBLIC_API(JSString *)
1692 JS_NewUCStringCopyZ(JSContext *cx, const jschar *s);
1694 extern JS_PUBLIC_API(JSString *)
1695 JS_InternUCStringN(JSContext *cx, const jschar *s, size_t length);
1697 extern JS_PUBLIC_API(JSString *)
1698 JS_InternUCString(JSContext *cx, const jschar *s);
1700 extern JS_PUBLIC_API(char *)
1701 JS_GetStringBytes(JSString *str);
1703 extern JS_PUBLIC_API(jschar *)
1704 JS_GetStringChars(JSString *str);
1706 extern JS_PUBLIC_API(size_t)
1707 JS_GetStringLength(JSString *str);
1709 extern JS_PUBLIC_API(intN)
1710 JS_CompareStrings(JSString *str1, JSString *str2);
1712 /*
1713 * Mutable string support. A string's characters are never mutable in this JS
1714 * implementation, but a growable string has a buffer that can be reallocated,
1715 * and a dependent string is a substring of another (growable, dependent, or
1716 * immutable) string. The direct data members of the (opaque to API clients)
1717 * JSString struct may be changed in a single-threaded way for growable and
1718 * dependent strings.
1719 *
1720 * Therefore mutable strings cannot be used by more than one thread at a time.
1721 * You may call JS_MakeStringImmutable to convert the string from a mutable
1722 * (growable or dependent) string to an immutable (and therefore thread-safe)
1723 * string. The engine takes care of converting growable and dependent strings
1724 * to immutable for you if you store strings in multi-threaded objects using
1725 * JS_SetProperty or kindred API entry points.
1726 *
1727 * If you store a JSString pointer in a native data structure that is (safely)
1728 * accessible to multiple threads, you must call JS_MakeStringImmutable before
1729 * retiring the store.
1730 */
1731 extern JS_PUBLIC_API(JSString *)
1732 JS_NewGrowableString(JSContext *cx, jschar *chars, size_t length);
1734 /*
1735 * Create a dependent string, i.e., a string that owns no character storage,
1736 * but that refers to a slice of another string's chars. Dependent strings
1737 * are mutable by definition, so the thread safety comments above apply.
1738 */
1739 extern JS_PUBLIC_API(JSString *)
1740 JS_NewDependentString(JSContext *cx, JSString *str, size_t start,
1741 size_t length);
1743 /*
1744 * Concatenate two strings, resulting in a new growable string. If you create
1745 * the left string and pass it to JS_ConcatStrings on a single thread, try to
1746 * use JS_NewGrowableString to create the left string -- doing so helps Concat
1747 * avoid allocating a new buffer for the result and copying left's chars into
1748 * the new buffer. See above for thread safety comments.
1749 */
1750 extern JS_PUBLIC_API(JSString *)
1751 JS_ConcatStrings(JSContext *cx, JSString *left, JSString *right);
1753 /*
1754 * Convert a dependent string into an independent one. This function does not
1755 * change the string's mutability, so the thread safety comments above apply.
1756 */
1757 extern JS_PUBLIC_API(const jschar *)
1758 JS_UndependString(JSContext *cx, JSString *str);
1760 /*
1761 * Convert a mutable string (either growable or dependent) into an immutable,
1762 * thread-safe one.
1763 */
1764 extern JS_PUBLIC_API(JSBool)
1765 JS_MakeStringImmutable(JSContext *cx, JSString *str);
1767 /*
1768 * Return JS_TRUE if C (char []) strings passed via the API and internally
1769 * are UTF-8. The source must be compiled with JS_C_STRINGS_ARE_UTF8 defined
1770 * to get UTF-8 support.
1771 */
1772 JS_PUBLIC_API(JSBool)
1773 JS_StringsAreUTF8();
1775 /*
1776 * Character encoding support.
1777 *
1778 * For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
1779 * of the destination buffer before the call; on return, *dstlenp contains the
1780 * number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
1781 * stored. To determine the necessary destination buffer size, make a sizing
1782 * call that passes NULL for dst.
1783 *
1784 * On errors, the functions report the error. In that case, *dstlenp contains
1785 * the number of characters or bytes transferred so far. If cx is NULL, no
1786 * error is reported on failure, and the functions simply return JS_FALSE.
1787 *
1788 * NB: Neither function stores an additional zero byte or jschar after the
1789 * transcoded string.
1790 *
1791 * If the source has been compiled with the #define JS_C_STRINGS_ARE_UTF8 to
1792 * enable UTF-8 interpretation of C char[] strings, then JS_EncodeCharacters
1793 * encodes to UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create
1794 * addititional errors if the character sequence is malformed. If UTF-8
1795 * support is disabled, the functions deflate and inflate, respectively.
1796 */
1797 JS_PUBLIC_API(JSBool)
1798 JS_EncodeCharacters(JSContext *cx, const jschar *src, size_t srclen, char *dst,
1799 size_t *dstlenp);
1801 JS_PUBLIC_API(JSBool)
1802 JS_DecodeBytes(JSContext *cx, const char *src, size_t srclen, jschar *dst,
1803 size_t *dstlenp);
1805 /************************************************************************/
1807 /*
1808 * Locale specific string conversion callback.
1809 */
1810 struct JSLocaleCallbacks {
1811 JSLocaleToUpperCase localeToUpperCase;
1812 JSLocaleToLowerCase localeToLowerCase;
1813 JSLocaleCompare localeCompare;
1814 JSLocaleToUnicode localeToUnicode;
1815 };
1817 /*
1818 * Establish locale callbacks. The pointer must persist as long as the
1819 * JSContext. Passing NULL restores the default behaviour.
1820 */
1821 extern JS_PUBLIC_API(void)
1822 JS_SetLocaleCallbacks(JSContext *cx, JSLocaleCallbacks *callbacks);
1824 /*
1825 * Return the address of the current locale callbacks struct, which may
1826 * be NULL.
1827 */
1828 extern JS_PUBLIC_API(JSLocaleCallbacks *)
1829 JS_GetLocaleCallbacks(JSContext *cx);
1831 /************************************************************************/
1833 /*
1834 * Error reporting.
1835 */
1837 /*
1838 * Report an exception represented by the sprintf-like conversion of format
1839 * and its arguments. This exception message string is passed to a pre-set
1840 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
1841 * the JSErrorReporter typedef).
1842 */
1843 extern JS_PUBLIC_API(void)
1844 JS_ReportError(JSContext *cx, const char *format, ...);
1846 /*
1847 * Use an errorNumber to retrieve the format string, args are char *
1848 */
1849 extern JS_PUBLIC_API(void)
1850 JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
1851 void *userRef, const uintN errorNumber, ...);
1853 /*
1854 * Use an errorNumber to retrieve the format string, args are jschar *
1855 */
1856 extern JS_PUBLIC_API(void)
1857 JS_ReportErrorNumberUC(JSContext *cx, JSErrorCallback errorCallback,
1858 void *userRef, const uintN errorNumber, ...);
1860 /*
1861 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
1862 * Return true if there was no error trying to issue the warning, and if the
1863 * warning was not converted into an error due to the JSOPTION_WERROR option
1864 * being set, false otherwise.
1865 */
1866 extern JS_PUBLIC_API(JSBool)
1867 JS_ReportWarning(JSContext *cx, const char *format, ...);
1869 extern JS_PUBLIC_API(JSBool)
1870 JS_ReportErrorFlagsAndNumber(JSContext *cx, uintN flags,
1871 JSErrorCallback errorCallback, void *userRef,
1872 const uintN errorNumber, ...);
1874 extern JS_PUBLIC_API(JSBool)
1875 JS_ReportErrorFlagsAndNumberUC(JSContext *cx, uintN flags,
1876 JSErrorCallback errorCallback, void *userRef,
1877 const uintN errorNumber, ...);
1879 /*
1880 * Complain when out of memory.
1881 */
1882 extern JS_PUBLIC_API(void)
1883 JS_ReportOutOfMemory(JSContext *cx);
1885 struct JSErrorReport {
1886 const char *filename; /* source file name, URL, etc., or null */
1887 uintN lineno; /* source line number */
1888 const char *linebuf; /* offending source line without final \n */
1889 const char *tokenptr; /* pointer to error token in linebuf */
1890 const jschar *uclinebuf; /* unicode (original) line buffer */
1891 const jschar *uctokenptr; /* unicode (original) token pointer */
1892 uintN flags; /* error/warning, etc. */
1893 uintN errorNumber; /* the error number, e.g. see js.msg */
1894 const jschar *ucmessage; /* the (default) error message */
1895 const jschar **messageArgs; /* arguments for the error message */
1896 };
1898 /*
1899 * JSErrorReport flag values. These may be freely composed.
1900 */
1901 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
1902 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
1903 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
1904 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
1906 /*
1907 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
1908 * has been thrown for this runtime error, and the host should ignore it.
1909 * Exception-aware hosts should also check for JS_IsExceptionPending if
1910 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
1911 * appropriate.
1912 */
1913 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
1914 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
1915 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
1917 extern JS_PUBLIC_API(JSErrorReporter)
1918 JS_SetErrorReporter(JSContext *cx, JSErrorReporter er);
1920 /************************************************************************/
1922 /*
1923 * Regular Expressions.
1924 */
1925 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
1926 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
1927 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
1929 extern JS_PUBLIC_API(JSObject *)
1930 JS_NewRegExpObject(JSContext *cx, char *bytes, size_t length, uintN flags);
1932 extern JS_PUBLIC_API(JSObject *)
1933 JS_NewUCRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags);
1935 extern JS_PUBLIC_API(void)
1936 JS_SetRegExpInput(JSContext *cx, JSString *input, JSBool multiline);
1938 extern JS_PUBLIC_API(void)
1939 JS_ClearRegExpStatics(JSContext *cx);
1941 extern JS_PUBLIC_API(void)
1942 JS_ClearRegExpRoots(JSContext *cx);
1944 /* TODO: compile, exec, get/set other statics... */
1946 /************************************************************************/
1948 extern JS_PUBLIC_API(JSBool)
1949 JS_IsExceptionPending(JSContext *cx);
1951 extern JS_PUBLIC_API(JSBool)
1952 JS_GetPendingException(JSContext *cx, jsval *vp);
1954 extern JS_PUBLIC_API(void)
1955 JS_SetPendingException(JSContext *cx, jsval v);
1957 extern JS_PUBLIC_API(void)
1958 JS_ClearPendingException(JSContext *cx);
1960 extern JS_PUBLIC_API(JSBool)
1961 JS_ReportPendingException(JSContext *cx);
1963 /*
1964 * Save the current exception state. This takes a snapshot of cx's current
1965 * exception state without making any change to that state.
1966 *
1967 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
1968 * (to restore that saved state, overriding any more recent state) or else to
1969 * JS_DropExceptionState (to free the state struct in case it is not correct
1970 * or desirable to restore it). Both Restore and Drop free the state struct,
1971 * so callers must stop using the pointer returned from Save after calling the
1972 * Release or Drop API.
1973 */
1974 extern JS_PUBLIC_API(JSExceptionState *)
1975 JS_SaveExceptionState(JSContext *cx);
1977 extern JS_PUBLIC_API(void)
1978 JS_RestoreExceptionState(JSContext *cx, JSExceptionState *state);
1980 extern JS_PUBLIC_API(void)
1981 JS_DropExceptionState(JSContext *cx, JSExceptionState *state);
1983 /*
1984 * If the given value is an exception object that originated from an error,
1985 * the exception will contain an error report struct, and this API will return
1986 * the address of that struct. Otherwise, it returns NULL. The lifetime of
1987 * the error report struct that might be returned is the same as the lifetime
1988 * of the exception object.
1989 */
1990 extern JS_PUBLIC_API(JSErrorReport *)
1991 JS_ErrorFromException(JSContext *cx, jsval v);
1993 /*
1994 * Given a reported error's message and JSErrorReport struct pointer, throw
1995 * the corresponding exception on cx.
1996 */
1997 extern JS_PUBLIC_API(JSBool)
1998 JS_ThrowReportedError(JSContext *cx, const char *message,
1999 JSErrorReport *reportp);
2001 #ifdef JS_THREADSAFE
2003 /*
2004 * Associate the current thread with the given context. This is done
2005 * implicitly by JS_NewContext.
2006 *
2007 * Returns the old thread id for this context, which should be treated as
2008 * an opaque value. This value is provided for comparison to 0, which
2009 * indicates that ClearContextThread has been called on this context
2010 * since the last SetContextThread, or non-0, which indicates the opposite.
2011 */
2012 extern JS_PUBLIC_API(jsword)
2013 JS_GetContextThread(JSContext *cx);
2015 extern JS_PUBLIC_API(jsword)
2016 JS_SetContextThread(JSContext *cx);
2018 extern JS_PUBLIC_API(jsword)
2019 JS_ClearContextThread(JSContext *cx);
2021 #endif /* JS_THREADSAFE */
2023 /************************************************************************/
2025 JS_END_EXTERN_C
2027 #endif /* jsapi_h___ */