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 * Well-known JS values. The extern'd variables are initialized when the
139 * first JSContext is created by JS_NewContext (see below).
140 */
141 #define JSVAL_VOID INT_TO_JSVAL(0 - JSVAL_INT_POW2(30))
142 #define JSVAL_NULL OBJECT_TO_JSVAL(0)
143 #define JSVAL_ZERO INT_TO_JSVAL(0)
144 #define JSVAL_ONE INT_TO_JSVAL(1)
145 #define JSVAL_FALSE BOOLEAN_TO_JSVAL(JS_FALSE)
146 #define JSVAL_TRUE BOOLEAN_TO_JSVAL(JS_TRUE)
148 /*
149 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
150 * comment in jstypes.h regarding safe int64 usage.
151 */
152 extern JS_PUBLIC_API(int64)
153 JS_Now();
155 /* Don't want to export data, so provide accessors for non-inline jsvals. */
156 extern JS_PUBLIC_API(jsval)
157 JS_GetNaNValue(JSContext *cx);
159 extern JS_PUBLIC_API(jsval)
160 JS_GetNegativeInfinityValue(JSContext *cx);
162 extern JS_PUBLIC_API(jsval)
163 JS_GetPositiveInfinityValue(JSContext *cx);
165 extern JS_PUBLIC_API(jsval)
166 JS_GetEmptyStringValue(JSContext *cx);
168 /*
169 * Format is a string of the following characters (spaces are insignificant),
170 * specifying the tabulated type conversions:
171 *
172 * b JSBool Boolean
173 * c uint16/jschar ECMA uint16, Unicode char
174 * i int32 ECMA int32
175 * u uint32 ECMA uint32
176 * j int32 Rounded int32 (coordinate)
177 * d jsdouble IEEE double
178 * I jsdouble Integral IEEE double
179 * s char * C string
180 * S JSString * Unicode string, accessed by a JSString pointer
181 * W jschar * Unicode character vector, 0-terminated (W for wide)
182 * o JSObject * Object reference
183 * f JSFunction * Function private
184 * v jsval Argument value (no conversion)
185 * * N/A Skip this argument (no vararg)
186 * / N/A End of required arguments
187 *
188 * The variable argument list after format must consist of &b, &c, &s, e.g.,
189 * where those variables have the types given above. For the pointer types
190 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
191 * to the JS runtime, not to the calling native code. The runtime promises
192 * to keep this memory valid so long as argv refers to allocated stack space
193 * (so long as the native function is active).
194 *
195 * Fewer arguments than format specifies may be passed only if there is a /
196 * in format after the last required argument specifier and argc is at least
197 * the number of required arguments. More arguments than format specifies
198 * may be passed without error; it is up to the caller to deal with trailing
199 * unconverted arguments.
200 */
201 extern JS_PUBLIC_API(JSBool)
202 JS_ConvertArguments(JSContext *cx, uintN argc, jsval *argv, const char *format,
203 ...);
205 #ifdef va_start
206 extern JS_PUBLIC_API(JSBool)
207 JS_ConvertArgumentsVA(JSContext *cx, uintN argc, jsval *argv,
208 const char *format, va_list ap);
209 #endif
211 /*
212 * Inverse of JS_ConvertArguments: scan format and convert trailing arguments
213 * into jsvals, GC-rooted if necessary by the JS stack. Return null on error,
214 * and a pointer to the new argument vector on success. Also return a stack
215 * mark on success via *markp, in which case the caller must eventually clean
216 * up by calling JS_PopArguments.
217 *
218 * Note that the number of actual arguments supplied is specified exclusively
219 * by format, so there is no argc parameter.
220 */
221 extern JS_PUBLIC_API(jsval *)
222 JS_PushArguments(JSContext *cx, void **markp, const char *format, ...);
224 #ifdef va_start
225 extern JS_PUBLIC_API(jsval *)
226 JS_PushArgumentsVA(JSContext *cx, void **markp, const char *format, va_list ap);
227 #endif
229 extern JS_PUBLIC_API(void)
230 JS_PopArguments(JSContext *cx, void *mark);
232 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
234 /*
235 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
236 * The handler function has this signature (see jspubtd.h):
237 *
238 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
239 * JSBool fromJS, jsval **vpp, va_list *app);
240 *
241 * It should return true on success, and return false after reporting an error
242 * or detecting an already-reported error.
243 *
244 * For a given format string, for example "AA", the formatter is called from
245 * JS_ConvertArgumentsVA like so:
246 *
247 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
248 *
249 * sp points into the arguments array on the JS stack, while ap points into
250 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
251 * the formatter to convert zero or more jsvals at sp to zero or more C values
252 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
253 * (via *app) to point past the converted arguments and their result pointers
254 * on the C stack.
255 *
256 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
257 *
258 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
259 *
260 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
261 * format specifier and store them at sp, updating ap and sp appropriately.
262 *
263 * The "..." after "AA" is the rest of the format string that was passed into
264 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
265 * in each Convert or PushArguments call is passed to the formatter, so that
266 * one such function may implement several formats, in order to share code.
267 *
268 * Remove just forgets about any handler associated with format. Add does not
269 * copy format, it points at the string storage allocated by the caller, which
270 * is typically a string constant. If format is in dynamic storage, it is up
271 * to the caller to keep the string alive until Remove is called.
272 */
273 extern JS_PUBLIC_API(JSBool)
274 JS_AddArgumentFormatter(JSContext *cx, const char *format,
275 JSArgumentFormatter formatter);
277 extern JS_PUBLIC_API(void)
278 JS_RemoveArgumentFormatter(JSContext *cx, const char *format);
280 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
282 extern JS_PUBLIC_API(JSBool)
283 JS_ConvertValue(JSContext *cx, jsval v, JSType type, jsval *vp);
285 extern JS_PUBLIC_API(JSBool)
286 JS_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
288 extern JS_PUBLIC_API(JSFunction *)
289 JS_ValueToFunction(JSContext *cx, jsval v);
291 extern JS_PUBLIC_API(JSFunction *)
292 JS_ValueToConstructor(JSContext *cx, jsval v);
294 extern JS_PUBLIC_API(JSString *)
295 JS_ValueToString(JSContext *cx, jsval v);
297 extern JS_PUBLIC_API(JSBool)
298 JS_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
300 /*
301 * Convert a value to a number, then to an int32, according to the ECMA rules
302 * for ToInt32.
303 */
304 extern JS_PUBLIC_API(JSBool)
305 JS_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip);
307 /*
308 * Convert a value to a number, then to a uint32, according to the ECMA rules
309 * for ToUint32.
310 */
311 extern JS_PUBLIC_API(JSBool)
312 JS_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip);
314 /*
315 * Convert a value to a number, then to an int32 if it fits by rounding to
316 * nearest; but failing with an error report if the double is out of range
317 * or unordered.
318 */
319 extern JS_PUBLIC_API(JSBool)
320 JS_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
322 /*
323 * ECMA ToUint16, for mapping a jsval to a Unicode point.
324 */
325 extern JS_PUBLIC_API(JSBool)
326 JS_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
328 extern JS_PUBLIC_API(JSBool)
329 JS_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp);
331 extern JS_PUBLIC_API(JSType)
332 JS_TypeOfValue(JSContext *cx, jsval v);
334 extern JS_PUBLIC_API(const char *)
335 JS_GetTypeName(JSContext *cx, JSType type);
337 /************************************************************************/
339 /*
340 * Initialization, locking, contexts, and memory allocation.
341 */
342 #define JS_NewRuntime JS_Init
343 #define JS_DestroyRuntime JS_Finish
344 #define JS_LockRuntime JS_Lock
345 #define JS_UnlockRuntime JS_Unlock
347 extern JS_PUBLIC_API(JSRuntime *)
348 JS_NewRuntime(uint32 maxbytes);
350 extern JS_PUBLIC_API(void)
351 JS_DestroyRuntime(JSRuntime *rt);
353 extern JS_PUBLIC_API(void)
354 JS_ShutDown(void);
356 JS_PUBLIC_API(void *)
357 JS_GetRuntimePrivate(JSRuntime *rt);
359 JS_PUBLIC_API(void)
360 JS_SetRuntimePrivate(JSRuntime *rt, void *data);
362 #ifdef JS_THREADSAFE
364 extern JS_PUBLIC_API(void)
365 JS_BeginRequest(JSContext *cx);
367 extern JS_PUBLIC_API(void)
368 JS_EndRequest(JSContext *cx);
370 /* Yield to pending GC operations, regardless of request depth */
371 extern JS_PUBLIC_API(void)
372 JS_YieldRequest(JSContext *cx);
374 extern JS_PUBLIC_API(jsrefcount)
375 JS_SuspendRequest(JSContext *cx);
377 extern JS_PUBLIC_API(void)
378 JS_ResumeRequest(JSContext *cx, jsrefcount saveDepth);
380 #endif /* JS_THREADSAFE */
382 extern JS_PUBLIC_API(void)
383 JS_Lock(JSRuntime *rt);
385 extern JS_PUBLIC_API(void)
386 JS_Unlock(JSRuntime *rt);
388 extern JS_PUBLIC_API(JSContext *)
389 JS_NewContext(JSRuntime *rt, size_t stackChunkSize);
391 extern JS_PUBLIC_API(void)
392 JS_DestroyContext(JSContext *cx);
394 extern JS_PUBLIC_API(void)
395 JS_DestroyContextNoGC(JSContext *cx);
397 extern JS_PUBLIC_API(void)
398 JS_DestroyContextMaybeGC(JSContext *cx);
400 extern JS_PUBLIC_API(void *)
401 JS_GetContextPrivate(JSContext *cx);
403 extern JS_PUBLIC_API(void)
404 JS_SetContextPrivate(JSContext *cx, void *data);
406 extern JS_PUBLIC_API(JSRuntime *)
407 JS_GetRuntime(JSContext *cx);
409 extern JS_PUBLIC_API(JSContext *)
410 JS_ContextIterator(JSRuntime *rt, JSContext **iterp);
412 extern JS_PUBLIC_API(JSVersion)
413 JS_GetVersion(JSContext *cx);
415 extern JS_PUBLIC_API(JSVersion)
416 JS_SetVersion(JSContext *cx, JSVersion version);
418 extern JS_PUBLIC_API(const char *)
419 JS_VersionToString(JSVersion version);
421 extern JS_PUBLIC_API(JSVersion)
422 JS_StringToVersion(const char *string);
424 /*
425 * JS options are orthogonal to version, and may be freely composed with one
426 * another as well as with version.
427 *
428 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
429 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
430 */
431 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
432 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
433 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
434 the last object on its 'obj'
435 param's scope chain as the
436 ECMA 'variables object' */
437 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
438 JS_BIT(3) /* context private data points
439 to an nsISupports subclass */
440 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
441 promises to execute compiled
442 script once only; enables
443 compile-time scope chain
444 resolution of consts. */
445 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
446 option supported for the
447 XUL preprocessor and kindred
448 beasts. */
450 extern JS_PUBLIC_API(uint32)
451 JS_GetOptions(JSContext *cx);
453 extern JS_PUBLIC_API(uint32)
454 JS_SetOptions(JSContext *cx, uint32 options);
456 extern JS_PUBLIC_API(uint32)
457 JS_ToggleOptions(JSContext *cx, uint32 options);
459 extern JS_PUBLIC_API(const char *)
460 JS_GetImplementationVersion(void);
462 extern JS_PUBLIC_API(JSObject *)
463 JS_GetGlobalObject(JSContext *cx);
465 extern JS_PUBLIC_API(void)
466 JS_SetGlobalObject(JSContext *cx, JSObject *obj);
468 /*
469 * Initialize standard JS class constructors, prototypes, and any top-level
470 * functions and constants associated with the standard classes (e.g. isNaN
471 * for Number).
472 *
473 * NB: This sets cx's global object to obj if it was null.
474 */
475 extern JS_PUBLIC_API(JSBool)
476 JS_InitStandardClasses(JSContext *cx, JSObject *obj);
478 /*
479 * Resolve id, which must contain either a string or an int, to a standard
480 * class name in obj if possible, defining the class's constructor and/or
481 * prototype and storing true in *resolved. If id does not name a standard
482 * class or a top-level property induced by initializing a standard class,
483 * store false in *resolved and just return true. Return false on error,
484 * as usual for JSBool result-typed API entry points.
485 *
486 * This API can be called directly from a global object class's resolve op,
487 * to define standard classes lazily. The class's enumerate op should call
488 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
489 * loops any classes not yet resolved lazily.
490 */
491 extern JS_PUBLIC_API(JSBool)
492 JS_ResolveStandardClass(JSContext *cx, JSObject *obj, jsval id,
493 JSBool *resolved);
495 extern JS_PUBLIC_API(JSBool)
496 JS_EnumerateStandardClasses(JSContext *cx, JSObject *obj);
498 extern JS_PUBLIC_API(JSObject *)
499 JS_GetScopeChain(JSContext *cx);
501 extern JS_PUBLIC_API(void *)
502 JS_malloc(JSContext *cx, size_t nbytes);
504 extern JS_PUBLIC_API(void *)
505 JS_realloc(JSContext *cx, void *p, size_t nbytes);
507 extern JS_PUBLIC_API(void)
508 JS_free(JSContext *cx, void *p);
510 extern JS_PUBLIC_API(char *)
511 JS_strdup(JSContext *cx, const char *s);
513 extern JS_PUBLIC_API(jsdouble *)
514 JS_NewDouble(JSContext *cx, jsdouble d);
516 extern JS_PUBLIC_API(JSBool)
517 JS_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
519 extern JS_PUBLIC_API(JSBool)
520 JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
522 /*
523 * A JS GC root is a pointer to a JSObject *, JSString *, or jsdouble * that
524 * itself points into the GC heap (more recently, we support this extension:
525 * a root may be a pointer to a jsval v for which JSVAL_IS_GCTHING(v) is true).
526 *
527 * Therefore, you never pass JSObject *obj to JS_AddRoot(cx, obj). You always
528 * call JS_AddRoot(cx, &obj), passing obj by reference. And later, before obj
529 * or the structure it is embedded within goes out of scope or is freed, you
530 * must call JS_RemoveRoot(cx, &obj).
531 *
532 * Also, use JS_AddNamedRoot(cx, &structPtr->memberObj, "structPtr->memberObj")
533 * in preference to JS_AddRoot(cx, &structPtr->memberObj), in order to identify
534 * roots by their source callsites. This way, you can find the callsite while
535 * debugging if you should fail to do JS_RemoveRoot(cx, &structPtr->memberObj)
536 * before freeing structPtr's memory.
537 */
538 extern JS_PUBLIC_API(JSBool)
539 JS_AddRoot(JSContext *cx, void *rp);
541 #ifdef NAME_ALL_GC_ROOTS
542 #define JS_DEFINE_TO_TOKEN(def) #def
543 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
544 #define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
545 #endif
547 extern JS_PUBLIC_API(JSBool)
548 JS_AddNamedRoot(JSContext *cx, void *rp, const char *name);
550 extern JS_PUBLIC_API(JSBool)
551 JS_AddNamedRootRT(JSRuntime *rt, void *rp, const char *name);
553 extern JS_PUBLIC_API(JSBool)
554 JS_RemoveRoot(JSContext *cx, void *rp);
556 extern JS_PUBLIC_API(JSBool)
557 JS_RemoveRootRT(JSRuntime *rt, void *rp);
559 /*
560 * The last GC thing of each type (object, string, double, external string
561 * types) created on a given context is kept alive until another thing of the
562 * same type is created, using a newborn root in the context. These newborn
563 * roots help native code protect newly-created GC-things from GC invocations
564 * activated before those things can be rooted using local or global roots.
565 *
566 * However, the newborn roots can also entrain great gobs of garbage, so the
567 * JS_GC entry point clears them for the context on which GC is being forced.
568 * Embeddings may need to do likewise for all contexts.
569 *
570 * See the scoped local root API immediately below for a better way to manage
571 * newborns in cases where native hooks (functions, getters, setters, etc.)
572 * create many GC-things, potentially without connecting them to predefined
573 * local roots such as *rval or argv[i] in an active native function. Using
574 * JS_EnterLocalRootScope disables updating of the context's per-gc-thing-type
575 * newborn roots, until control flow unwinds and leaves the outermost nesting
576 * local root scope.
577 */
578 extern JS_PUBLIC_API(void)
579 JS_ClearNewbornRoots(JSContext *cx);
581 /*
582 * Scoped local root management allows native functions, getter/setters, etc.
583 * to avoid worrying about the newborn root pigeon-holes, overloading local
584 * roots allocated in argv and *rval, or ending up having to call JS_Add*Root
585 * and JS_RemoveRoot to manage global roots temporarily.
586 *
587 * Instead, calling JS_EnterLocalRootScope and JS_LeaveLocalRootScope around
588 * the body of the native hook causes the engine to allocate a local root for
589 * each newborn created in between the two API calls, using a local root stack
590 * associated with cx. For example:
591 *
592 * JSBool
593 * my_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
594 * {
595 * JSBool ok;
596 *
597 * if (!JS_EnterLocalRootScope(cx))
598 * return JS_FALSE;
599 * ok = my_GetPropertyBody(cx, obj, id, vp);
600 * JS_LeaveLocalRootScope(cx);
601 * return ok;
602 * }
603 *
604 * NB: JS_LeaveLocalRootScope must be called once for every prior successful
605 * call to JS_EnterLocalRootScope. If JS_EnterLocalRootScope fails, you must
606 * not make the matching JS_LeaveLocalRootScope call.
607 *
608 * In case a native hook allocates many objects or other GC-things, but the
609 * native protects some of those GC-things by storing them as property values
610 * in an object that is itself protected, the hook can call JS_ForgetLocalRoot
611 * to free the local root automatically pushed for the now-protected GC-thing.
612 *
613 * JS_ForgetLocalRoot works on any GC-thing allocated in the current local
614 * root scope, but it's more time-efficient when called on references to more
615 * recently created GC-things. Calling it successively on other than the most
616 * recently allocated GC-thing will tend to average the time inefficiency, and
617 * may risk O(n^2) growth rate, but in any event, you shouldn't allocate too
618 * many local roots if you can root as you go (build a tree of objects from
619 * the top down, forgetting each latest-allocated GC-thing immediately upon
620 * linking it to its parent).
621 */
622 extern JS_PUBLIC_API(JSBool)
623 JS_EnterLocalRootScope(JSContext *cx);
625 extern JS_PUBLIC_API(void)
626 JS_LeaveLocalRootScope(JSContext *cx);
628 extern JS_PUBLIC_API(void)
629 JS_ForgetLocalRoot(JSContext *cx, void *thing);
631 #ifdef DEBUG
632 extern JS_PUBLIC_API(void)
633 JS_DumpNamedRoots(JSRuntime *rt,
634 void (*dump)(const char *name, void *rp, void *data),
635 void *data);
636 #endif
638 /*
639 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
640 * The root is pointed at by rp; if the root is unnamed, name is null; data is
641 * supplied from the third parameter to JS_MapGCRoots.
642 *
643 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
644 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
645 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
646 * constants are flags; you can OR them together.
647 *
648 * This function acquires and releases rt's GC lock around the mapping of the
649 * roots table, so the map function should run to completion in as few cycles
650 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
651 * or any JS API entry point that acquires locks, without double-tripping or
652 * deadlocking on the GC lock.
653 *
654 * JS_MapGCRoots returns the count of roots that were successfully mapped.
655 */
656 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
657 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
658 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
660 typedef intN
661 (* JS_DLL_CALLBACK JSGCRootMapFun)(void *rp, const char *name, void *data);
663 extern JS_PUBLIC_API(uint32)
664 JS_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data);
666 extern JS_PUBLIC_API(JSBool)
667 JS_LockGCThing(JSContext *cx, void *thing);
669 extern JS_PUBLIC_API(JSBool)
670 JS_LockGCThingRT(JSRuntime *rt, void *thing);
672 extern JS_PUBLIC_API(JSBool)
673 JS_UnlockGCThing(JSContext *cx, void *thing);
675 extern JS_PUBLIC_API(JSBool)
676 JS_UnlockGCThingRT(JSRuntime *rt, void *thing);
678 /*
679 * For implementors of JSObjectOps.mark, to mark a GC-thing reachable via a
680 * property or other strong ref identified for debugging purposes by name.
681 * The name argument's storage needs to live only as long as the call to
682 * this routine.
683 *
684 * The final arg is used by GC_MARK_DEBUG code to build a ref path through
685 * the GC's live thing graph. Implementors of JSObjectOps.mark should pass
686 * its final arg through to this function when marking all GC-things that are
687 * directly reachable from the object being marked.
688 *
689 * See the JSMarkOp typedef in jspubtd.h, and the JSObjectOps struct below.
690 */
691 extern JS_PUBLIC_API(void)
692 JS_MarkGCThing(JSContext *cx, void *thing, const char *name, void *arg);
694 extern JS_PUBLIC_API(void)
695 JS_GC(JSContext *cx);
697 extern JS_PUBLIC_API(void)
698 JS_MaybeGC(JSContext *cx);
700 extern JS_PUBLIC_API(JSGCCallback)
701 JS_SetGCCallback(JSContext *cx, JSGCCallback cb);
703 extern JS_PUBLIC_API(JSGCCallback)
704 JS_SetGCCallbackRT(JSRuntime *rt, JSGCCallback cb);
706 extern JS_PUBLIC_API(JSBool)
707 JS_IsAboutToBeFinalized(JSContext *cx, void *thing);
709 /*
710 * Add an external string finalizer, one created by JS_NewExternalString (see
711 * below) using a type-code returned from this function, and that understands
712 * how to free or release the memory pointed at by JS_GetStringChars(str).
713 *
714 * Return a nonnegative type index if there is room for finalizer in the
715 * global GC finalizers table, else return -1. If the engine is compiled
716 * JS_THREADSAFE and used in a multi-threaded environment, this function must
717 * be invoked on the primordial thread only, at startup -- or else the entire
718 * program must single-thread itself while loading a module that calls this
719 * function.
720 */
721 extern JS_PUBLIC_API(intN)
722 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer);
724 /*
725 * Remove finalizer from the global GC finalizers table, returning its type
726 * code if found, -1 if not found.
727 *
728 * As with JS_AddExternalStringFinalizer, there is a threading restriction
729 * if you compile the engine JS_THREADSAFE: this function may be called for a
730 * given finalizer pointer on only one thread; different threads may call to
731 * remove distinct finalizers safely.
732 *
733 * You must ensure that all strings with finalizer's type have been collected
734 * before calling this function. Otherwise, string data will be leaked by the
735 * GC, for want of a finalizer to call.
736 */
737 extern JS_PUBLIC_API(intN)
738 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer);
740 /*
741 * Create a new JSString whose chars member refers to external memory, i.e.,
742 * memory requiring special, type-specific finalization. The type code must
743 * be a nonnegative return value from JS_AddExternalStringFinalizer.
744 */
745 extern JS_PUBLIC_API(JSString *)
746 JS_NewExternalString(JSContext *cx, jschar *chars, size_t length, intN type);
748 /*
749 * Returns the external-string finalizer index for this string, or -1 if it is
750 * an "internal" (native to JS engine) string.
751 */
752 extern JS_PUBLIC_API(intN)
753 JS_GetExternalStringGCType(JSRuntime *rt, JSString *str);
755 /*
756 * Sets maximum (if stack grows upward) or minimum (downward) legal stack byte
757 * address in limitAddr for the thread or process stack used by cx. To disable
758 * stack size checking, pass 0 for limitAddr.
759 */
760 extern JS_PUBLIC_API(void)
761 JS_SetThreadStackLimit(JSContext *cx, jsuword limitAddr);
763 /************************************************************************/
765 /*
766 * Classes, objects, and properties.
767 */
769 /* For detailed comments on the function pointer types, see jspubtd.h. */
770 struct JSClass {
771 const char *name;
772 uint32 flags;
774 /* Mandatory non-null function pointer members. */
775 JSPropertyOp addProperty;
776 JSPropertyOp delProperty;
777 JSPropertyOp getProperty;
778 JSPropertyOp setProperty;
779 JSEnumerateOp enumerate;
780 JSResolveOp resolve;
781 JSConvertOp convert;
782 JSFinalizeOp finalize;
784 /* Optionally non-null members start here. */
785 JSGetObjectOps getObjectOps;
786 JSCheckAccessOp checkAccess;
787 JSNative call;
788 JSNative construct;
789 JSXDRObjectOp xdrObject;
790 JSHasInstanceOp hasInstance;
791 JSMarkOp mark;
792 JSReserveSlotsOp reserveSlots;
793 };
795 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
796 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
797 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
798 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
799 #define JSCLASS_SHARE_ALL_PROPERTIES (1<<4) /* all properties are SHARED */
800 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
801 object in prototype chain
802 passed in via *objp in/out
803 parameter */
805 /*
806 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
807 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
808 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
809 */
810 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
811 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
812 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
813 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
814 << JSCLASS_RESERVED_SLOTS_SHIFT)
815 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
816 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
817 & JSCLASS_RESERVED_SLOTS_MASK)
819 /* Initializer for unused members of statically initialized JSClass structs. */
820 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,0
822 /* For detailed comments on these function pointer types, see jspubtd.h. */
823 struct JSObjectOps {
824 /* Mandatory non-null function pointer members. */
825 JSNewObjectMapOp newObjectMap;
826 JSObjectMapOp destroyObjectMap;
827 JSLookupPropOp lookupProperty;
828 JSDefinePropOp defineProperty;
829 JSPropertyIdOp getProperty;
830 JSPropertyIdOp setProperty;
831 JSAttributesOp getAttributes;
832 JSAttributesOp setAttributes;
833 JSPropertyIdOp deleteProperty;
834 JSConvertOp defaultValue;
835 JSNewEnumerateOp enumerate;
836 JSCheckAccessIdOp checkAccess;
838 /* Optionally non-null members start here. */
839 JSObjectOp thisObject;
840 JSPropertyRefOp dropProperty;
841 JSNative call;
842 JSNative construct;
843 JSXDRObjectOp xdrObject;
844 JSHasInstanceOp hasInstance;
845 JSSetObjectSlotOp setProto;
846 JSSetObjectSlotOp setParent;
847 JSMarkOp mark;
848 JSFinalizeOp clear;
849 JSGetRequiredSlotOp getRequiredSlot;
850 JSSetRequiredSlotOp setRequiredSlot;
851 };
853 /*
854 * Classes that expose JSObjectOps via a non-null getObjectOps class hook may
855 * derive a property structure from this struct, return a pointer to it from
856 * lookupProperty and defineProperty, and use the pointer to avoid rehashing
857 * in getAttributes and setAttributes.
858 *
859 * The jsid type contains either an int jsval (see JSVAL_IS_INT above), or an
860 * internal pointer that is opaque to users of this API, but which users may
861 * convert from and to a jsval using JS_ValueToId and JS_IdToValue.
862 */
863 struct JSProperty {
864 jsid id;
865 };
867 struct JSIdArray {
868 jsint length;
869 jsid vector[1]; /* actually, length jsid words */
870 };
872 extern JS_PUBLIC_API(void)
873 JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
875 extern JS_PUBLIC_API(JSBool)
876 JS_ValueToId(JSContext *cx, jsval v, jsid *idp);
878 extern JS_PUBLIC_API(JSBool)
879 JS_IdToValue(JSContext *cx, jsid id, jsval *vp);
881 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
882 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
883 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
884 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
885 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
887 extern JS_PUBLIC_API(JSBool)
888 JS_PropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
890 extern JS_PUBLIC_API(JSBool)
891 JS_EnumerateStub(JSContext *cx, JSObject *obj);
893 extern JS_PUBLIC_API(JSBool)
894 JS_ResolveStub(JSContext *cx, JSObject *obj, jsval id);
896 extern JS_PUBLIC_API(JSBool)
897 JS_ConvertStub(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
899 extern JS_PUBLIC_API(void)
900 JS_FinalizeStub(JSContext *cx, JSObject *obj);
902 struct JSConstDoubleSpec {
903 jsdouble dval;
904 const char *name;
905 uint8 flags;
906 uint8 spare[3];
907 };
909 /*
910 * To define an array element rather than a named property member, cast the
911 * element's index to (const char *) and initialize name with it, and set the
912 * JSPROP_INDEX bit in flags.
913 */
914 struct JSPropertySpec {
915 const char *name;
916 int8 tinyid;
917 uint8 flags;
918 JSPropertyOp getter;
919 JSPropertyOp setter;
920 };
922 struct JSFunctionSpec {
923 const char *name;
924 JSNative call;
925 uint8 nargs;
926 uint8 flags;
927 uint16 extra; /* number of arg slots for local GC roots */
928 };
930 extern JS_PUBLIC_API(JSObject *)
931 JS_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
932 JSClass *clasp, JSNative constructor, uintN nargs,
933 JSPropertySpec *ps, JSFunctionSpec *fs,
934 JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
936 #ifdef JS_THREADSAFE
937 extern JS_PUBLIC_API(JSClass *)
938 JS_GetClass(JSContext *cx, JSObject *obj);
940 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
941 #else
942 extern JS_PUBLIC_API(JSClass *)
943 JS_GetClass(JSObject *obj);
945 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
946 #endif
948 extern JS_PUBLIC_API(JSBool)
949 JS_InstanceOf(JSContext *cx, JSObject *obj, JSClass *clasp, jsval *argv);
951 extern JS_PUBLIC_API(void *)
952 JS_GetPrivate(JSContext *cx, JSObject *obj);
954 extern JS_PUBLIC_API(JSBool)
955 JS_SetPrivate(JSContext *cx, JSObject *obj, void *data);
957 extern JS_PUBLIC_API(void *)
958 JS_GetInstancePrivate(JSContext *cx, JSObject *obj, JSClass *clasp,
959 jsval *argv);
961 extern JS_PUBLIC_API(JSObject *)
962 JS_GetPrototype(JSContext *cx, JSObject *obj);
964 extern JS_PUBLIC_API(JSBool)
965 JS_SetPrototype(JSContext *cx, JSObject *obj, JSObject *proto);
967 extern JS_PUBLIC_API(JSObject *)
968 JS_GetParent(JSContext *cx, JSObject *obj);
970 extern JS_PUBLIC_API(JSBool)
971 JS_SetParent(JSContext *cx, JSObject *obj, JSObject *parent);
973 extern JS_PUBLIC_API(JSObject *)
974 JS_GetConstructor(JSContext *cx, JSObject *proto);
976 /*
977 * Get a unique identifier for obj, good for the lifetime of obj (even if it
978 * is moved by a copying GC). Return false on failure (likely out of memory),
979 * and true with *idp containing the unique id on success.
980 */
981 extern JS_PUBLIC_API(JSBool)
982 JS_GetObjectId(JSContext *cx, JSObject *obj, jsid *idp);
984 extern JS_PUBLIC_API(JSObject *)
985 JS_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent);
987 extern JS_PUBLIC_API(JSBool)
988 JS_SealObject(JSContext *cx, JSObject *obj, JSBool deep);
990 extern JS_PUBLIC_API(JSObject *)
991 JS_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
992 JSObject *parent);
994 extern JS_PUBLIC_API(JSObject *)
995 JS_ConstructObjectWithArguments(JSContext *cx, JSClass *clasp, JSObject *proto,
996 JSObject *parent, uintN argc, jsval *argv);
998 extern JS_PUBLIC_API(JSObject *)
999 JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, JSClass *clasp,
1000 JSObject *proto, uintN attrs);
1002 extern JS_PUBLIC_API(JSBool)
1003 JS_DefineConstDoubles(JSContext *cx, JSObject *obj, JSConstDoubleSpec *cds);
1005 extern JS_PUBLIC_API(JSBool)
1006 JS_DefineProperties(JSContext *cx, JSObject *obj, JSPropertySpec *ps);
1008 extern JS_PUBLIC_API(JSBool)
1009 JS_DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
1010 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
1012 /*
1013 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1014 *
1015 * If the object does not have a property by that name, *foundp will be
1016 * JS_FALSE and the value of *attrsp is undefined.
1017 */
1018 extern JS_PUBLIC_API(JSBool)
1019 JS_GetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
1020 uintN *attrsp, JSBool *foundp);
1022 /*
1023 * Set the attributes of a property on a given object.
1024 *
1025 * If the object does not have a property by that name, *foundp will be
1026 * JS_FALSE and nothing will be altered.
1027 */
1028 extern JS_PUBLIC_API(JSBool)
1029 JS_SetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
1030 uintN attrs, JSBool *foundp);
1032 extern JS_PUBLIC_API(JSBool)
1033 JS_DefinePropertyWithTinyId(JSContext *cx, JSObject *obj, const char *name,
1034 int8 tinyid, jsval value,
1035 JSPropertyOp getter, JSPropertyOp setter,
1036 uintN attrs);
1038 extern JS_PUBLIC_API(JSBool)
1039 JS_AliasProperty(JSContext *cx, JSObject *obj, const char *name,
1040 const char *alias);
1042 extern JS_PUBLIC_API(JSBool)
1043 JS_HasProperty(JSContext *cx, JSObject *obj, const char *name, JSBool *foundp);
1045 extern JS_PUBLIC_API(JSBool)
1046 JS_LookupProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1048 extern JS_PUBLIC_API(JSBool)
1049 JS_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, const char *name,
1050 uintN flags, jsval *vp);
1052 extern JS_PUBLIC_API(JSBool)
1053 JS_GetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1055 extern JS_PUBLIC_API(JSBool)
1056 JS_SetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1058 extern JS_PUBLIC_API(JSBool)
1059 JS_DeleteProperty(JSContext *cx, JSObject *obj, const char *name);
1061 extern JS_PUBLIC_API(JSBool)
1062 JS_DeleteProperty2(JSContext *cx, JSObject *obj, const char *name,
1063 jsval *rval);
1065 extern JS_PUBLIC_API(JSBool)
1066 JS_DefineUCProperty(JSContext *cx, JSObject *obj,
1067 const jschar *name, size_t namelen, jsval value,
1068 JSPropertyOp getter, JSPropertyOp setter,
1069 uintN attrs);
1071 /*
1072 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1073 *
1074 * If the object does not have a property by that name, *foundp will be
1075 * JS_FALSE and the value of *attrsp is undefined.
1076 */
1077 extern JS_PUBLIC_API(JSBool)
1078 JS_GetUCPropertyAttributes(JSContext *cx, JSObject *obj,
1079 const jschar *name, size_t namelen,
1080 uintN *attrsp, JSBool *foundp);
1082 /*
1083 * Set the attributes of a property on a given object.
1084 *
1085 * If the object does not have a property by that name, *foundp will be
1086 * JS_FALSE and nothing will be altered.
1087 */
1088 extern JS_PUBLIC_API(JSBool)
1089 JS_SetUCPropertyAttributes(JSContext *cx, JSObject *obj,
1090 const jschar *name, size_t namelen,
1091 uintN attrs, JSBool *foundp);
1094 extern JS_PUBLIC_API(JSBool)
1095 JS_DefineUCPropertyWithTinyId(JSContext *cx, JSObject *obj,
1096 const jschar *name, size_t namelen,
1097 int8 tinyid, jsval value,
1098 JSPropertyOp getter, JSPropertyOp setter,
1099 uintN attrs);
1101 extern JS_PUBLIC_API(JSBool)
1102 JS_HasUCProperty(JSContext *cx, JSObject *obj,
1103 const jschar *name, size_t namelen,
1104 JSBool *vp);
1106 extern JS_PUBLIC_API(JSBool)
1107 JS_LookupUCProperty(JSContext *cx, JSObject *obj,
1108 const jschar *name, size_t namelen,
1109 jsval *vp);
1111 extern JS_PUBLIC_API(JSBool)
1112 JS_GetUCProperty(JSContext *cx, JSObject *obj,
1113 const jschar *name, size_t namelen,
1114 jsval *vp);
1116 extern JS_PUBLIC_API(JSBool)
1117 JS_SetUCProperty(JSContext *cx, JSObject *obj,
1118 const jschar *name, size_t namelen,
1119 jsval *vp);
1121 extern JS_PUBLIC_API(JSBool)
1122 JS_DeleteUCProperty2(JSContext *cx, JSObject *obj,
1123 const jschar *name, size_t namelen,
1124 jsval *rval);
1126 extern JS_PUBLIC_API(JSObject *)
1127 JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector);
1129 extern JS_PUBLIC_API(JSBool)
1130 JS_IsArrayObject(JSContext *cx, JSObject *obj);
1132 extern JS_PUBLIC_API(JSBool)
1133 JS_GetArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1135 extern JS_PUBLIC_API(JSBool)
1136 JS_SetArrayLength(JSContext *cx, JSObject *obj, jsuint length);
1138 extern JS_PUBLIC_API(JSBool)
1139 JS_HasArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1141 extern JS_PUBLIC_API(JSBool)
1142 JS_DefineElement(JSContext *cx, JSObject *obj, jsint index, jsval value,
1143 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
1145 extern JS_PUBLIC_API(JSBool)
1146 JS_AliasElement(JSContext *cx, JSObject *obj, const char *name, jsint alias);
1148 extern JS_PUBLIC_API(JSBool)
1149 JS_HasElement(JSContext *cx, JSObject *obj, jsint index, JSBool *foundp);
1151 extern JS_PUBLIC_API(JSBool)
1152 JS_LookupElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1154 extern JS_PUBLIC_API(JSBool)
1155 JS_GetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1157 extern JS_PUBLIC_API(JSBool)
1158 JS_SetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1160 extern JS_PUBLIC_API(JSBool)
1161 JS_DeleteElement(JSContext *cx, JSObject *obj, jsint index);
1163 extern JS_PUBLIC_API(JSBool)
1164 JS_DeleteElement2(JSContext *cx, JSObject *obj, jsint index, jsval *rval);
1166 extern JS_PUBLIC_API(void)
1167 JS_ClearScope(JSContext *cx, JSObject *obj);
1169 extern JS_PUBLIC_API(JSIdArray *)
1170 JS_Enumerate(JSContext *cx, JSObject *obj);
1172 extern JS_PUBLIC_API(JSBool)
1173 JS_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
1174 jsval *vp, uintN *attrsp);
1176 extern JS_PUBLIC_API(JSCheckAccessOp)
1177 JS_SetCheckObjectAccessCallback(JSRuntime *rt, JSCheckAccessOp acb);
1179 extern JS_PUBLIC_API(JSBool)
1180 JS_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
1182 extern JS_PUBLIC_API(JSBool)
1183 JS_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
1185 /************************************************************************/
1187 /*
1188 * Security protocol.
1189 */
1190 struct JSPrincipals {
1191 char *codebase;
1192 void * (* JS_DLL_CALLBACK getPrincipalArray)(JSContext *cx, JSPrincipals *);
1193 JSBool (* JS_DLL_CALLBACK globalPrivilegesEnabled)(JSContext *cx, JSPrincipals *);
1195 /* Don't call "destroy"; use reference counting macros below. */
1196 jsrefcount refcount;
1197 void (* JS_DLL_CALLBACK destroy)(JSContext *cx, struct JSPrincipals *);
1198 };
1200 #ifdef JS_THREADSAFE
1201 #define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
1202 #define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
1204 extern JS_PUBLIC_API(jsrefcount)
1205 JS_HoldPrincipals(JSContext *cx, JSPrincipals *principals);
1207 extern JS_PUBLIC_API(jsrefcount)
1208 JS_DropPrincipals(JSContext *cx, JSPrincipals *principals);
1210 #else
1211 #define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
1212 #define JSPRINCIPALS_DROP(cx, principals) \
1213 ((--(principals)->refcount == 0) \
1214 ? ((*(principals)->destroy)((cx), (principals)), 0) \
1215 : (principals)->refcount)
1216 #endif
1218 extern JS_PUBLIC_API(JSPrincipalsTranscoder)
1219 JS_SetPrincipalsTranscoder(JSRuntime *rt, JSPrincipalsTranscoder px);
1221 extern JS_PUBLIC_API(JSObjectPrincipalsFinder)
1222 JS_SetObjectPrincipalsFinder(JSContext *cx, JSObjectPrincipalsFinder fop);
1224 /************************************************************************/
1226 /*
1227 * Functions and scripts.
1228 */
1229 extern JS_PUBLIC_API(JSFunction *)
1230 JS_NewFunction(JSContext *cx, JSNative call, uintN nargs, uintN flags,
1231 JSObject *parent, const char *name);
1233 extern JS_PUBLIC_API(JSObject *)
1234 JS_GetFunctionObject(JSFunction *fun);
1236 /*
1237 * Deprecated, useful only for diagnostics. Use JS_GetFunctionId instead for
1238 * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
1239 */
1240 extern JS_PUBLIC_API(const char *)
1241 JS_GetFunctionName(JSFunction *fun);
1243 /*
1244 * Return the function's identifier as a JSString, or null if fun is unnamed.
1245 * The returned string lives as long as fun, so you don't need to root a saved
1246 * reference to it if fun is well-connected or rooted, and provided you bound
1247 * the use of the saved reference by fun's lifetime.
1248 *
1249 * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
1250 * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
1251 * from UTF-16-ish jschars.
1252 */
1253 extern JS_PUBLIC_API(JSString *)
1254 JS_GetFunctionId(JSFunction *fun);
1256 /*
1257 * Return JSFUN_* flags for fun.
1258 */
1259 extern JS_PUBLIC_API(uintN)
1260 JS_GetFunctionFlags(JSFunction *fun);
1262 /*
1263 * Infallible predicate to test whether obj is a function object (faster than
1264 * comparing obj's class name to "Function", but equivalent unless someone has
1265 * overwritten the "Function" identifier with a different constructor and then
1266 * created instances using that constructor that might be passed in as obj).
1267 */
1268 extern JS_PUBLIC_API(JSBool)
1269 JS_ObjectIsFunction(JSContext *cx, JSObject *obj);
1271 extern JS_PUBLIC_API(JSBool)
1272 JS_DefineFunctions(JSContext *cx, JSObject *obj, JSFunctionSpec *fs);
1274 extern JS_PUBLIC_API(JSFunction *)
1275 JS_DefineFunction(JSContext *cx, JSObject *obj, const char *name, JSNative call,
1276 uintN nargs, uintN attrs);
1278 extern JS_PUBLIC_API(JSFunction *)
1279 JS_DefineUCFunction(JSContext *cx, JSObject *obj,
1280 const jschar *name, size_t namelen, JSNative call,
1281 uintN nargs, uintN attrs);
1283 extern JS_PUBLIC_API(JSObject *)
1284 JS_CloneFunctionObject(JSContext *cx, JSObject *funobj, JSObject *parent);
1286 /*
1287 * Given a buffer, return JS_FALSE if the buffer might become a valid
1288 * javascript statement with the addition of more lines. Otherwise return
1289 * JS_TRUE. The intent is to support interactive compilation - accumulate
1290 * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
1291 * the compiler.
1292 */
1293 extern JS_PUBLIC_API(JSBool)
1294 JS_BufferIsCompilableUnit(JSContext *cx, JSObject *obj,
1295 const char *bytes, size_t length);
1297 /*
1298 * The JSScript objects returned by the following functions refer to string and
1299 * other kinds of literals, including doubles and RegExp objects. These
1300 * literals are vulnerable to garbage collection; to root script objects and
1301 * prevent literals from being collected, create a rootable object using
1302 * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
1303 */
1304 extern JS_PUBLIC_API(JSScript *)
1305 JS_CompileScript(JSContext *cx, JSObject *obj,
1306 const char *bytes, size_t length,
1307 const char *filename, uintN lineno);
1309 extern JS_PUBLIC_API(JSScript *)
1310 JS_CompileScriptForPrincipals(JSContext *cx, JSObject *obj,
1311 JSPrincipals *principals,
1312 const char *bytes, size_t length,
1313 const char *filename, uintN lineno);
1315 extern JS_PUBLIC_API(JSScript *)
1316 JS_CompileUCScript(JSContext *cx, JSObject *obj,
1317 const jschar *chars, size_t length,
1318 const char *filename, uintN lineno);
1320 extern JS_PUBLIC_API(JSScript *)
1321 JS_CompileUCScriptForPrincipals(JSContext *cx, JSObject *obj,
1322 JSPrincipals *principals,
1323 const jschar *chars, size_t length,
1324 const char *filename, uintN lineno);
1326 extern JS_PUBLIC_API(JSScript *)
1327 JS_CompileFile(JSContext *cx, JSObject *obj, const char *filename);
1329 extern JS_PUBLIC_API(JSScript *)
1330 JS_CompileFileHandle(JSContext *cx, JSObject *obj, const char *filename,
1331 FILE *fh);
1333 extern JS_PUBLIC_API(JSScript *)
1334 JS_CompileFileHandleForPrincipals(JSContext *cx, JSObject *obj,
1335 const char *filename, FILE *fh,
1336 JSPrincipals *principals);
1338 /*
1339 * NB: you must use JS_NewScriptObject and root a pointer to its return value
1340 * in order to keep a JSScript and its atoms safe from garbage collection after
1341 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
1342 * E.g., and without error checks:
1343 *
1344 * JSScript *script = JS_CompileFile(cx, global, filename);
1345 * JSObject *scrobj = JS_NewScriptObject(cx, script);
1346 * JS_AddNamedRoot(cx, &scrobj, "scrobj");
1347 * do {
1348 * jsval result;
1349 * JS_ExecuteScript(cx, global, script, &result);
1350 * JS_GC();
1351 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
1352 * JS_RemoveRoot(cx, &scrobj);
1353 */
1354 extern JS_PUBLIC_API(JSObject *)
1355 JS_NewScriptObject(JSContext *cx, JSScript *script);
1357 /*
1358 * Infallible getter for a script's object. If JS_NewScriptObject has not been
1359 * called on script yet, the return value will be null.
1360 */
1361 extern JS_PUBLIC_API(JSObject *)
1362 JS_GetScriptObject(JSScript *script);
1364 extern JS_PUBLIC_API(void)
1365 JS_DestroyScript(JSContext *cx, JSScript *script);
1367 extern JS_PUBLIC_API(JSFunction *)
1368 JS_CompileFunction(JSContext *cx, JSObject *obj, const char *name,
1369 uintN nargs, const char **argnames,
1370 const char *bytes, size_t length,
1371 const char *filename, uintN lineno);
1373 extern JS_PUBLIC_API(JSFunction *)
1374 JS_CompileFunctionForPrincipals(JSContext *cx, JSObject *obj,
1375 JSPrincipals *principals, const char *name,
1376 uintN nargs, const char **argnames,
1377 const char *bytes, size_t length,
1378 const char *filename, uintN lineno);
1380 extern JS_PUBLIC_API(JSFunction *)
1381 JS_CompileUCFunction(JSContext *cx, JSObject *obj, const char *name,
1382 uintN nargs, const char **argnames,
1383 const jschar *chars, size_t length,
1384 const char *filename, uintN lineno);
1386 extern JS_PUBLIC_API(JSFunction *)
1387 JS_CompileUCFunctionForPrincipals(JSContext *cx, JSObject *obj,
1388 JSPrincipals *principals, const char *name,
1389 uintN nargs, const char **argnames,
1390 const jschar *chars, size_t length,
1391 const char *filename, uintN lineno);
1393 extern JS_PUBLIC_API(JSString *)
1394 JS_DecompileScript(JSContext *cx, JSScript *script, const char *name,
1395 uintN indent);
1397 /*
1398 * API extension: OR this into indent to avoid pretty-printing the decompiled
1399 * source resulting from JS_DecompileFunction{,Body}.
1400 */
1401 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
1403 extern JS_PUBLIC_API(JSString *)
1404 JS_DecompileFunction(JSContext *cx, JSFunction *fun, uintN indent);
1406 extern JS_PUBLIC_API(JSString *)
1407 JS_DecompileFunctionBody(JSContext *cx, JSFunction *fun, uintN indent);
1409 /*
1410 * NB: JS_ExecuteScript, JS_ExecuteScriptPart, and the JS_Evaluate*Script*
1411 * quadruplets all use the obj parameter as the initial scope chain header,
1412 * the 'this' keyword value, and the variables object (ECMA parlance for where
1413 * 'var' and 'function' bind names) of the execution context for script.
1414 *
1415 * Using obj as the variables object is problematic if obj's parent (which is
1416 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
1417 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
1418 * created by assignment to an unbound id, 'x = 0', go in the last object on
1419 * the scope chain linked by parent.
1420 *
1421 * ECMA calls that last scoping object the "global object", but note that many
1422 * embeddings have several such objects. ECMA requires that "global code" be
1423 * executed with the variables object equal to this global object. But these
1424 * JS API entry points provide freedom to execute code against a "sub-global",
1425 * i.e., a parented or scoped object, in which case the variables object will
1426 * differ from the last object on the scope chain, resulting in confusing and
1427 * non-ECMA explicit vs. implicit variable creation.
1428 *
1429 * Caveat embedders: unless you already depend on this buggy variables object
1430 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
1431 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
1432 * someone may have set other options on cx already -- for each context in the
1433 * application, if you pass parented objects as the obj parameter, or may ever
1434 * pass such objects in the future.
1435 *
1436 * Why a runtime option? The alternative is to add six or so new API entry
1437 * points with signatures matching the following six, and that doesn't seem
1438 * worth the code bloat cost. Such new entry points would probably have less
1439 * obvious names, too, so would not tend to be used. The JS_SetOption call,
1440 * OTOH, can be more easily hacked into existing code that does not depend on
1441 * the bug; such code can continue to use the familiar JS_EvaluateScript,
1442 * etc., entry points.
1443 */
1444 extern JS_PUBLIC_API(JSBool)
1445 JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval);
1447 /*
1448 * Execute either the function-defining prolog of a script, or the script's
1449 * main body, but not both.
1450 */
1451 typedef enum JSExecPart { JSEXEC_PROLOG, JSEXEC_MAIN } JSExecPart;
1453 extern JS_PUBLIC_API(JSBool)
1454 JS_ExecuteScriptPart(JSContext *cx, JSObject *obj, JSScript *script,
1455 JSExecPart part, jsval *rval);
1457 extern JS_PUBLIC_API(JSBool)
1458 JS_EvaluateScript(JSContext *cx, JSObject *obj,
1459 const char *bytes, uintN length,
1460 const char *filename, uintN lineno,
1461 jsval *rval);
1463 extern JS_PUBLIC_API(JSBool)
1464 JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj,
1465 JSPrincipals *principals,
1466 const char *bytes, uintN length,
1467 const char *filename, uintN lineno,
1468 jsval *rval);
1470 extern JS_PUBLIC_API(JSBool)
1471 JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
1472 const jschar *chars, uintN length,
1473 const char *filename, uintN lineno,
1474 jsval *rval);
1476 extern JS_PUBLIC_API(JSBool)
1477 JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj,
1478 JSPrincipals *principals,
1479 const jschar *chars, uintN length,
1480 const char *filename, uintN lineno,
1481 jsval *rval);
1483 extern JS_PUBLIC_API(JSBool)
1484 JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc,
1485 jsval *argv, jsval *rval);
1487 extern JS_PUBLIC_API(JSBool)
1488 JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, uintN argc,
1489 jsval *argv, jsval *rval);
1491 extern JS_PUBLIC_API(JSBool)
1492 JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc,
1493 jsval *argv, jsval *rval);
1495 extern JS_PUBLIC_API(JSBranchCallback)
1496 JS_SetBranchCallback(JSContext *cx, JSBranchCallback cb);
1498 extern JS_PUBLIC_API(JSBool)
1499 JS_IsRunning(JSContext *cx);
1501 extern JS_PUBLIC_API(JSBool)
1502 JS_IsConstructing(JSContext *cx);
1504 /*
1505 * Returns true if a script is executing and its current bytecode is a set
1506 * (assignment) operation, even if there are native (no script) stack frames
1507 * between the script and the caller to JS_IsAssigning.
1508 */
1509 extern JS_FRIEND_API(JSBool)
1510 JS_IsAssigning(JSContext *cx);
1512 /*
1513 * Set the second return value, which should be a string or int jsval that
1514 * identifies a property in the returned object, to form an ECMA reference
1515 * type value (obj, id). Only native methods can return reference types,
1516 * and if the returned value is used on the left-hand side of an assignment
1517 * op, the identified property will be set. If the return value is in an
1518 * r-value, the interpreter just gets obj[id]'s value.
1519 */
1520 extern JS_PUBLIC_API(void)
1521 JS_SetCallReturnValue2(JSContext *cx, jsval v);
1523 /************************************************************************/
1525 /*
1526 * Strings.
1527 *
1528 * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
1529 * on error (signified by null return), it leaves bytes owned by the caller.
1530 * So the caller must free bytes in the error case, if it has no use for them.
1531 * In contrast, all the JS_New*StringCopy* functions do not take ownership of
1532 * the character memory passed to them -- they copy it.
1533 */
1534 extern JS_PUBLIC_API(JSString *)
1535 JS_NewString(JSContext *cx, char *bytes, size_t length);
1537 extern JS_PUBLIC_API(JSString *)
1538 JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);
1540 extern JS_PUBLIC_API(JSString *)
1541 JS_NewStringCopyZ(JSContext *cx, const char *s);
1543 extern JS_PUBLIC_API(JSString *)
1544 JS_InternString(JSContext *cx, const char *s);
1546 extern JS_PUBLIC_API(JSString *)
1547 JS_NewUCString(JSContext *cx, jschar *chars, size_t length);
1549 extern JS_PUBLIC_API(JSString *)
1550 JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n);
1552 extern JS_PUBLIC_API(JSString *)
1553 JS_NewUCStringCopyZ(JSContext *cx, const jschar *s);
1555 extern JS_PUBLIC_API(JSString *)
1556 JS_InternUCStringN(JSContext *cx, const jschar *s, size_t length);
1558 extern JS_PUBLIC_API(JSString *)
1559 JS_InternUCString(JSContext *cx, const jschar *s);
1561 extern JS_PUBLIC_API(char *)
1562 JS_GetStringBytes(JSString *str);
1564 extern JS_PUBLIC_API(jschar *)
1565 JS_GetStringChars(JSString *str);
1567 extern JS_PUBLIC_API(size_t)
1568 JS_GetStringLength(JSString *str);
1570 extern JS_PUBLIC_API(intN)
1571 JS_CompareStrings(JSString *str1, JSString *str2);
1573 /*
1574 * Mutable string support. A string's characters are never mutable in this JS
1575 * implementation, but a growable string has a buffer that can be reallocated,
1576 * and a dependent string is a substring of another (growable, dependent, or
1577 * immutable) string. The direct data members of the (opaque to API clients)
1578 * JSString struct may be changed in a single-threaded way for growable and
1579 * dependent strings.
1580 *
1581 * Therefore mutable strings cannot be used by more than one thread at a time.
1582 * You may call JS_MakeStringImmutable to convert the string from a mutable
1583 * (growable or dependent) string to an immutable (and therefore thread-safe)
1584 * string. The engine takes care of converting growable and dependent strings
1585 * to immutable for you if you store strings in multi-threaded objects using
1586 * JS_SetProperty or kindred API entry points.
1587 *
1588 * If you store a JSString pointer in a native data structure that is (safely)
1589 * accessible to multiple threads, you must call JS_MakeStringImmutable before
1590 * retiring the store.
1591 */
1592 extern JS_PUBLIC_API(JSString *)
1593 JS_NewGrowableString(JSContext *cx, jschar *chars, size_t length);
1595 /*
1596 * Create a dependent string, i.e., a string that owns no character storage,
1597 * but that refers to a slice of another string's chars. Dependent strings
1598 * are mutable by definition, so the thread safety comments above apply.
1599 */
1600 extern JS_PUBLIC_API(JSString *)
1601 JS_NewDependentString(JSContext *cx, JSString *str, size_t start,
1602 size_t length);
1604 /*
1605 * Concatenate two strings, resulting in a new growable string. If you create
1606 * the left string and pass it to JS_ConcatStrings on a single thread, try to
1607 * use JS_NewGrowableString to create the left string -- doing so helps Concat
1608 * avoid allocating a new buffer for the result and copying left's chars into
1609 * the new buffer. See above for thread safety comments.
1610 */
1611 extern JS_PUBLIC_API(JSString *)
1612 JS_ConcatStrings(JSContext *cx, JSString *left, JSString *right);
1614 /*
1615 * Convert a dependent string into an independent one. This function does not
1616 * change the string's mutability, so the thread safety comments above apply.
1617 */
1618 extern JS_PUBLIC_API(const jschar *)
1619 JS_UndependString(JSContext *cx, JSString *str);
1621 /*
1622 * Convert a mutable string (either growable or dependent) into an immutable,
1623 * thread-safe one.
1624 */
1625 extern JS_PUBLIC_API(JSBool)
1626 JS_MakeStringImmutable(JSContext *cx, JSString *str);
1628 /************************************************************************/
1630 /*
1631 * Locale specific string conversion callback.
1632 */
1633 struct JSLocaleCallbacks {
1634 JSLocaleToUpperCase localeToUpperCase;
1635 JSLocaleToLowerCase localeToLowerCase;
1636 JSLocaleCompare localeCompare;
1637 JSLocaleToUnicode localeToUnicode;
1638 };
1640 /*
1641 * Establish locale callbacks. The pointer must persist as long as the
1642 * JSContext. Passing NULL restores the default behaviour.
1643 */
1644 extern JS_PUBLIC_API(void)
1645 JS_SetLocaleCallbacks(JSContext *cx, JSLocaleCallbacks *callbacks);
1647 /*
1648 * Return the address of the current locale callbacks struct, which may
1649 * be NULL.
1650 */
1651 extern JS_PUBLIC_API(JSLocaleCallbacks *)
1652 JS_GetLocaleCallbacks(JSContext *cx);
1654 /************************************************************************/
1656 /*
1657 * Error reporting.
1658 */
1660 /*
1661 * Report an exception represented by the sprintf-like conversion of format
1662 * and its arguments. This exception message string is passed to a pre-set
1663 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
1664 * the JSErrorReporter typedef).
1665 */
1666 extern JS_PUBLIC_API(void)
1667 JS_ReportError(JSContext *cx, const char *format, ...);
1669 /*
1670 * Use an errorNumber to retrieve the format string, args are char *
1671 */
1672 extern JS_PUBLIC_API(void)
1673 JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
1674 void *userRef, const uintN errorNumber, ...);
1676 /*
1677 * Use an errorNumber to retrieve the format string, args are jschar *
1678 */
1679 extern JS_PUBLIC_API(void)
1680 JS_ReportErrorNumberUC(JSContext *cx, JSErrorCallback errorCallback,
1681 void *userRef, const uintN errorNumber, ...);
1683 /*
1684 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
1685 * Return true if there was no error trying to issue the warning, and if the
1686 * warning was not converted into an error due to the JSOPTION_WERROR option
1687 * being set, false otherwise.
1688 */
1689 extern JS_PUBLIC_API(JSBool)
1690 JS_ReportWarning(JSContext *cx, const char *format, ...);
1692 extern JS_PUBLIC_API(JSBool)
1693 JS_ReportErrorFlagsAndNumber(JSContext *cx, uintN flags,
1694 JSErrorCallback errorCallback, void *userRef,
1695 const uintN errorNumber, ...);
1697 extern JS_PUBLIC_API(JSBool)
1698 JS_ReportErrorFlagsAndNumberUC(JSContext *cx, uintN flags,
1699 JSErrorCallback errorCallback, void *userRef,
1700 const uintN errorNumber, ...);
1702 /*
1703 * Complain when out of memory.
1704 */
1705 extern JS_PUBLIC_API(void)
1706 JS_ReportOutOfMemory(JSContext *cx);
1708 struct JSErrorReport {
1709 const char *filename; /* source file name, URL, etc., or null */
1710 uintN lineno; /* source line number */
1711 const char *linebuf; /* offending source line without final \n */
1712 const char *tokenptr; /* pointer to error token in linebuf */
1713 const jschar *uclinebuf; /* unicode (original) line buffer */
1714 const jschar *uctokenptr; /* unicode (original) token pointer */
1715 uintN flags; /* error/warning, etc. */
1716 uintN errorNumber; /* the error number, e.g. see js.msg */
1717 const jschar *ucmessage; /* the (default) error message */
1718 const jschar **messageArgs; /* arguments for the error message */
1719 };
1721 /*
1722 * JSErrorReport flag values. These may be freely composed.
1723 */
1724 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
1725 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
1726 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
1727 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
1729 /*
1730 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
1731 * has been thrown for this runtime error, and the host should ignore it.
1732 * Exception-aware hosts should also check for JS_IsExceptionPending if
1733 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
1734 * appropriate.
1735 */
1736 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
1737 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
1738 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
1740 extern JS_PUBLIC_API(JSErrorReporter)
1741 JS_SetErrorReporter(JSContext *cx, JSErrorReporter er);
1743 /************************************************************************/
1745 /*
1746 * Regular Expressions.
1747 */
1748 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
1749 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
1750 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
1752 extern JS_PUBLIC_API(JSObject *)
1753 JS_NewRegExpObject(JSContext *cx, char *bytes, size_t length, uintN flags);
1755 extern JS_PUBLIC_API(JSObject *)
1756 JS_NewUCRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags);
1758 extern JS_PUBLIC_API(void)
1759 JS_SetRegExpInput(JSContext *cx, JSString *input, JSBool multiline);
1761 extern JS_PUBLIC_API(void)
1762 JS_ClearRegExpStatics(JSContext *cx);
1764 extern JS_PUBLIC_API(void)
1765 JS_ClearRegExpRoots(JSContext *cx);
1767 /* TODO: compile, exec, get/set other statics... */
1769 /************************************************************************/
1771 extern JS_PUBLIC_API(JSBool)
1772 JS_IsExceptionPending(JSContext *cx);
1774 extern JS_PUBLIC_API(JSBool)
1775 JS_GetPendingException(JSContext *cx, jsval *vp);
1777 extern JS_PUBLIC_API(void)
1778 JS_SetPendingException(JSContext *cx, jsval v);
1780 extern JS_PUBLIC_API(void)
1781 JS_ClearPendingException(JSContext *cx);
1783 extern JS_PUBLIC_API(JSBool)
1784 JS_ReportPendingException(JSContext *cx);
1786 /*
1787 * Save the current exception state. This takes a snapshot of cx's current
1788 * exception state without making any change to that state.
1789 *
1790 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
1791 * (to restore that saved state, overriding any more recent state) or else to
1792 * JS_DropExceptionState (to free the state struct in case it is not correct
1793 * or desirable to restore it). Both Restore and Drop free the state struct,
1794 * so callers must stop using the pointer returned from Save after calling the
1795 * Release or Drop API.
1796 */
1797 extern JS_PUBLIC_API(JSExceptionState *)
1798 JS_SaveExceptionState(JSContext *cx);
1800 extern JS_PUBLIC_API(void)
1801 JS_RestoreExceptionState(JSContext *cx, JSExceptionState *state);
1803 extern JS_PUBLIC_API(void)
1804 JS_DropExceptionState(JSContext *cx, JSExceptionState *state);
1806 /*
1807 * If the given value is an exception object that originated from an error,
1808 * the exception will contain an error report struct, and this API will return
1809 * the address of that struct. Otherwise, it returns NULL. The lifetime of
1810 * the error report struct that might be returned is the same as the lifetime
1811 * of the exception object.
1812 */
1813 extern JS_PUBLIC_API(JSErrorReport *)
1814 JS_ErrorFromException(JSContext *cx, jsval v);
1816 #ifdef JS_THREADSAFE
1818 /*
1819 * Associate the current thread with the given context. This is done
1820 * implicitly by JS_NewContext.
1821 *
1822 * Returns the old thread id for this context, which should be treated as
1823 * an opaque value. This value is provided for comparison to 0, which
1824 * indicates that ClearContextThread has been called on this context
1825 * since the last SetContextThread, or non-0, which indicates the opposite.
1826 */
1827 extern JS_PUBLIC_API(jsword)
1828 JS_GetContextThread(JSContext *cx);
1830 extern JS_PUBLIC_API(jsword)
1831 JS_SetContextThread(JSContext *cx);
1833 extern JS_PUBLIC_API(jsword)
1834 JS_ClearContextThread(JSContext *cx);
1836 #endif /* JS_THREADSAFE */
1838 /************************************************************************/
1840 JS_END_EXTERN_C
1842 #endif /* jsapi_h___ */