Code

moving trunk for module inkscape
[inkscape.git] / src / extension / script / js / jsapi.h
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 */
441 extern JS_PUBLIC_API(uint32)
442 JS_GetOptions(JSContext *cx);
444 extern JS_PUBLIC_API(uint32)
445 JS_SetOptions(JSContext *cx, uint32 options);
447 extern JS_PUBLIC_API(uint32)
448 JS_ToggleOptions(JSContext *cx, uint32 options);
450 extern JS_PUBLIC_API(const char *)
451 JS_GetImplementationVersion(void);
453 extern JS_PUBLIC_API(JSObject *)
454 JS_GetGlobalObject(JSContext *cx);
456 extern JS_PUBLIC_API(void)
457 JS_SetGlobalObject(JSContext *cx, JSObject *obj);
459 /*
460  * Initialize standard JS class constructors, prototypes, and any top-level
461  * functions and constants associated with the standard classes (e.g. isNaN
462  * for Number).
463  *
464  * NB: This sets cx's global object to obj if it was null.
465  */
466 extern JS_PUBLIC_API(JSBool)
467 JS_InitStandardClasses(JSContext *cx, JSObject *obj);
469 /*
470  * Resolve id, which must contain either a string or an int, to a standard
471  * class name in obj if possible, defining the class's constructor and/or
472  * prototype and storing true in *resolved.  If id does not name a standard
473  * class or a top-level property induced by initializing a standard class,
474  * store false in *resolved and just return true.  Return false on error,
475  * as usual for JSBool result-typed API entry points.
476  *
477  * This API can be called directly from a global object class's resolve op,
478  * to define standard classes lazily.  The class's enumerate op should call
479  * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
480  * loops any classes not yet resolved lazily.
481  */
482 extern JS_PUBLIC_API(JSBool)
483 JS_ResolveStandardClass(JSContext *cx, JSObject *obj, jsval id,
484                         JSBool *resolved);
486 extern JS_PUBLIC_API(JSBool)
487 JS_EnumerateStandardClasses(JSContext *cx, JSObject *obj);
489 extern JS_PUBLIC_API(JSObject *)
490 JS_GetScopeChain(JSContext *cx);
492 extern JS_PUBLIC_API(void *)
493 JS_malloc(JSContext *cx, size_t nbytes);
495 extern JS_PUBLIC_API(void *)
496 JS_realloc(JSContext *cx, void *p, size_t nbytes);
498 extern JS_PUBLIC_API(void)
499 JS_free(JSContext *cx, void *p);
501 extern JS_PUBLIC_API(char *)
502 JS_strdup(JSContext *cx, const char *s);
504 extern JS_PUBLIC_API(jsdouble *)
505 JS_NewDouble(JSContext *cx, jsdouble d);
507 extern JS_PUBLIC_API(JSBool)
508 JS_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
510 extern JS_PUBLIC_API(JSBool)
511 JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
513 /*
514  * A JS GC root is a pointer to a JSObject *, JSString *, or jsdouble * that
515  * itself points into the GC heap (more recently, we support this extension:
516  * a root may be a pointer to a jsval v for which JSVAL_IS_GCTHING(v) is true).
517  *
518  * Therefore, you never pass JSObject *obj to JS_AddRoot(cx, obj).  You always
519  * call JS_AddRoot(cx, &obj), passing obj by reference.  And later, before obj
520  * or the structure it is embedded within goes out of scope or is freed, you
521  * must call JS_RemoveRoot(cx, &obj).
522  *
523  * Also, use JS_AddNamedRoot(cx, &structPtr->memberObj, "structPtr->memberObj")
524  * in preference to JS_AddRoot(cx, &structPtr->memberObj), in order to identify
525  * roots by their source callsites.  This way, you can find the callsite while
526  * debugging if you should fail to do JS_RemoveRoot(cx, &structPtr->memberObj)
527  * before freeing structPtr's memory.
528  */
529 extern JS_PUBLIC_API(JSBool)
530 JS_AddRoot(JSContext *cx, void *rp);
532 #ifdef NAME_ALL_GC_ROOTS
533 #define JS_DEFINE_TO_TOKEN(def) #def
534 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
535 #define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
536 #endif
538 extern JS_PUBLIC_API(JSBool)
539 JS_AddNamedRoot(JSContext *cx, void *rp, const char *name);
541 extern JS_PUBLIC_API(JSBool)
542 JS_AddNamedRootRT(JSRuntime *rt, void *rp, const char *name);
544 extern JS_PUBLIC_API(JSBool)
545 JS_RemoveRoot(JSContext *cx, void *rp);
547 extern JS_PUBLIC_API(JSBool)
548 JS_RemoveRootRT(JSRuntime *rt, void *rp);
550 /*
551  * The last GC thing of each type (object, string, double, external string
552  * types) created on a given context is kept alive until another thing of the
553  * same type is created, using a newborn root in the context.  These newborn
554  * roots help native code protect newly-created GC-things from GC invocations
555  * activated before those things can be rooted using local or global roots.
556  *
557  * However, the newborn roots can also entrain great gobs of garbage, so the
558  * JS_GC entry point clears them for the context on which GC is being forced.
559  * Embeddings may need to do likewise for all contexts.
560  *
561  * XXXbe See bug 40757 (http://bugzilla.mozilla.org/show_bug.cgi?id=40757),
562  * which proposes switching (with an #ifdef, alas, if we want to maintain API
563  * compatibility) to a JNI-like extensible local root frame stack model.
564  */
565 extern JS_PUBLIC_API(void)
566 JS_ClearNewbornRoots(JSContext *cx);
568 #ifdef DEBUG
569 extern JS_PUBLIC_API(void)
570 JS_DumpNamedRoots(JSRuntime *rt,
571                   void (*dump)(const char *name, void *rp, void *data),
572                   void *data);
573 #endif
575 /*
576  * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
577  * The root is pointed at by rp; if the root is unnamed, name is null; data is
578  * supplied from the third parameter to JS_MapGCRoots.
579  *
580  * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
581  * enumerated root to be removed.  To stop enumeration, set JS_MAP_GCROOT_STOP
582  * in the return value.  To keep on mapping, return JS_MAP_GCROOT_NEXT.  These
583  * constants are flags; you can OR them together.
584  *
585  * This function acquires and releases rt's GC lock around the mapping of the
586  * roots table, so the map function should run to completion in as few cycles
587  * as possible.  Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
588  * or any JS API entry point that acquires locks, without double-tripping or
589  * deadlocking on the GC lock.
590  *
591  * JS_MapGCRoots returns the count of roots that were successfully mapped.
592  */
593 #define JS_MAP_GCROOT_NEXT      0       /* continue mapping entries */
594 #define JS_MAP_GCROOT_STOP      1       /* stop mapping entries */
595 #define JS_MAP_GCROOT_REMOVE    2       /* remove and free the current entry */
597 typedef intN
598 (* JS_DLL_CALLBACK JSGCRootMapFun)(void *rp, const char *name, void *data);
600 extern JS_PUBLIC_API(uint32)
601 JS_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data);
603 extern JS_PUBLIC_API(JSBool)
604 JS_LockGCThing(JSContext *cx, void *thing);
606 extern JS_PUBLIC_API(JSBool)
607 JS_LockGCThingRT(JSRuntime *rt, void *thing);
609 extern JS_PUBLIC_API(JSBool)
610 JS_UnlockGCThing(JSContext *cx, void *thing);
612 extern JS_PUBLIC_API(JSBool)
613 JS_UnlockGCThingRT(JSRuntime *rt, void *thing);
615 /*
616  * For implementors of JSObjectOps.mark, to mark a GC-thing reachable via a
617  * property or other strong ref identified for debugging purposes by name.
618  * The name argument's storage needs to live only as long as the call to
619  * this routine.
620  *
621  * The final arg is used by GC_MARK_DEBUG code to build a ref path through
622  * the GC's live thing graph.  Implementors of JSObjectOps.mark should pass
623  * its final arg through to this function when marking all GC-things that are
624  * directly reachable from the object being marked.
625  *
626  * See the JSMarkOp typedef in jspubtd.h, and the JSObjectOps struct below.
627  */
628 extern JS_PUBLIC_API(void)
629 JS_MarkGCThing(JSContext *cx, void *thing, const char *name, void *arg);
631 extern JS_PUBLIC_API(void)
632 JS_GC(JSContext *cx);
634 extern JS_PUBLIC_API(void)
635 JS_MaybeGC(JSContext *cx);
637 extern JS_PUBLIC_API(JSGCCallback)
638 JS_SetGCCallback(JSContext *cx, JSGCCallback cb);
640 extern JS_PUBLIC_API(JSGCCallback)
641 JS_SetGCCallbackRT(JSRuntime *rt, JSGCCallback cb);
643 extern JS_PUBLIC_API(JSBool)
644 JS_IsAboutToBeFinalized(JSContext *cx, void *thing);
646 /*
647  * Add an external string finalizer, one created by JS_NewExternalString (see
648  * below) using a type-code returned from this function, and that understands
649  * how to free or release the memory pointed at by JS_GetStringChars(str).
650  *
651  * Return a nonnegative type index if there is room for finalizer in the
652  * global GC finalizers table, else return -1.  If the engine is compiled
653  * JS_THREADSAFE and used in a multi-threaded environment, this function must
654  * be invoked on the primordial thread only, at startup -- or else the entire
655  * program must single-thread itself while loading a module that calls this
656  * function.
657  */
658 extern JS_PUBLIC_API(intN)
659 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer);
661 /*
662  * Remove finalizer from the global GC finalizers table, returning its type
663  * code if found, -1 if not found.
664  *
665  * As with JS_AddExternalStringFinalizer, there is a threading restriction
666  * if you compile the engine JS_THREADSAFE: this function may be called for a
667  * given finalizer pointer on only one thread; different threads may call to
668  * remove distinct finalizers safely.
669  *
670  * You must ensure that all strings with finalizer's type have been collected
671  * before calling this function.  Otherwise, string data will be leaked by the
672  * GC, for want of a finalizer to call.
673  */
674 extern JS_PUBLIC_API(intN)
675 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer);
677 /*
678  * Create a new JSString whose chars member refers to external memory, i.e.,
679  * memory requiring special, type-specific finalization.  The type code must
680  * be a nonnegative return value from JS_AddExternalStringFinalizer.
681  */
682 extern JS_PUBLIC_API(JSString *)
683 JS_NewExternalString(JSContext *cx, jschar *chars, size_t length, intN type);
685 /*
686  * Returns the external-string finalizer index for this string, or -1 if it is
687  * an "internal" (native to JS engine) string.
688  */
689 extern JS_PUBLIC_API(intN)
690 JS_GetExternalStringGCType(JSRuntime *rt, JSString *str);
692 /*
693  * Sets maximum (if stack grows upward) or minimum (downward) legal stack byte
694  * address in limitAddr for the thread or process stack used by cx.  To disable
695  * stack size checking, pass 0 for limitAddr.
696  */
697 extern JS_PUBLIC_API(void)
698 JS_SetThreadStackLimit(JSContext *cx, jsuword limitAddr);
700 /************************************************************************/
702 /*
703  * Classes, objects, and properties.
704  */
706 /* For detailed comments on the function pointer types, see jspubtd.h. */
707 struct JSClass {
708     const char          *name;
709     uint32              flags;
711     /* Mandatory non-null function pointer members. */
712     JSPropertyOp        addProperty;
713     JSPropertyOp        delProperty;
714     JSPropertyOp        getProperty;
715     JSPropertyOp        setProperty;
716     JSEnumerateOp       enumerate;
717     JSResolveOp         resolve;
718     JSConvertOp         convert;
719     JSFinalizeOp        finalize;
721     /* Optionally non-null members start here. */
722     JSGetObjectOps      getObjectOps;
723     JSCheckAccessOp     checkAccess;
724     JSNative            call;
725     JSNative            construct;
726     JSXDRObjectOp       xdrObject;
727     JSHasInstanceOp     hasInstance;
728     JSMarkOp            mark;
729     jsword              spare;
730 };
732 #define JSCLASS_HAS_PRIVATE             (1<<0)  /* objects have private slot */
733 #define JSCLASS_NEW_ENUMERATE           (1<<1)  /* has JSNewEnumerateOp hook */
734 #define JSCLASS_NEW_RESOLVE             (1<<2)  /* has JSNewResolveOp hook */
735 #define JSCLASS_PRIVATE_IS_NSISUPPORTS  (1<<3)  /* private is (nsISupports *) */
736 #define JSCLASS_SHARE_ALL_PROPERTIES    (1<<4)  /* all properties are SHARED */
737 #define JSCLASS_NEW_RESOLVE_GETS_START  (1<<5)  /* JSNewResolveOp gets starting
738                                                    object in prototype chain
739                                                    passed in via *objp in/out
740                                                    parameter */
742 /*
743  * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
744  * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
745  * n is a constant in [1, 255].  Reserved slots are indexed from 0 to n-1.
746  */
747 #define JSCLASS_RESERVED_SLOTS_SHIFT    8       /* room for 8 flags below */
748 #define JSCLASS_RESERVED_SLOTS_WIDTH    8       /* and 16 above this field */
749 #define JSCLASS_RESERVED_SLOTS_MASK     JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
750 #define JSCLASS_HAS_RESERVED_SLOTS(n)   (((n) & JSCLASS_RESERVED_SLOTS_MASK)   \
751                                          << JSCLASS_RESERVED_SLOTS_SHIFT)
752 #define JSCLASS_RESERVED_SLOTS(clasp)   (((clasp)->flags                      \
753                                           >> JSCLASS_RESERVED_SLOTS_SHIFT)     \
754                                          & JSCLASS_RESERVED_SLOTS_MASK)
756 /* Initializer for unused members of statically initialized JSClass structs. */
757 #define JSCLASS_NO_OPTIONAL_MEMBERS     0,0,0,0,0,0,0,0
759 /* For detailed comments on these function pointer types, see jspubtd.h. */
760 struct JSObjectOps {
761     /* Mandatory non-null function pointer members. */
762     JSNewObjectMapOp    newObjectMap;
763     JSObjectMapOp       destroyObjectMap;
764     JSLookupPropOp      lookupProperty;
765     JSDefinePropOp      defineProperty;
766     JSPropertyIdOp      getProperty;
767     JSPropertyIdOp      setProperty;
768     JSAttributesOp      getAttributes;
769     JSAttributesOp      setAttributes;
770     JSPropertyIdOp      deleteProperty;
771     JSConvertOp         defaultValue;
772     JSNewEnumerateOp    enumerate;
773     JSCheckAccessIdOp   checkAccess;
775     /* Optionally non-null members start here. */
776     JSObjectOp          thisObject;
777     JSPropertyRefOp     dropProperty;
778     JSNative            call;
779     JSNative            construct;
780     JSXDRObjectOp       xdrObject;
781     JSHasInstanceOp     hasInstance;
782     JSSetObjectSlotOp   setProto;
783     JSSetObjectSlotOp   setParent;
784     JSMarkOp            mark;
785     JSFinalizeOp        clear;
786     JSGetRequiredSlotOp getRequiredSlot;
787     JSSetRequiredSlotOp setRequiredSlot;
788 };
790 /*
791  * Classes that expose JSObjectOps via a non-null getObjectOps class hook may
792  * derive a property structure from this struct, return a pointer to it from
793  * lookupProperty and defineProperty, and use the pointer to avoid rehashing
794  * in getAttributes and setAttributes.
795  *
796  * The jsid type contains either an int jsval (see JSVAL_IS_INT above), or an
797  * internal pointer that is opaque to users of this API, but which users may
798  * convert from and to a jsval using JS_ValueToId and JS_IdToValue.
799  */
800 struct JSProperty {
801     jsid id;
802 };
804 struct JSIdArray {
805     jsint length;
806     jsid  vector[1];    /* actually, length jsid words */
807 };
809 extern JS_PUBLIC_API(void)
810 JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
812 extern JS_PUBLIC_API(JSBool)
813 JS_ValueToId(JSContext *cx, jsval v, jsid *idp);
815 extern JS_PUBLIC_API(JSBool)
816 JS_IdToValue(JSContext *cx, jsid id, jsval *vp);
818 #define JSRESOLVE_QUALIFIED     0x01    /* resolve a qualified property id */
819 #define JSRESOLVE_ASSIGNING     0x02    /* resolve on the left of assignment */
821 extern JS_PUBLIC_API(JSBool)
822 JS_PropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
824 extern JS_PUBLIC_API(JSBool)
825 JS_EnumerateStub(JSContext *cx, JSObject *obj);
827 extern JS_PUBLIC_API(JSBool)
828 JS_ResolveStub(JSContext *cx, JSObject *obj, jsval id);
830 extern JS_PUBLIC_API(JSBool)
831 JS_ConvertStub(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
833 extern JS_PUBLIC_API(void)
834 JS_FinalizeStub(JSContext *cx, JSObject *obj);
836 struct JSConstDoubleSpec {
837     jsdouble        dval;
838     const char      *name;
839     uint8           flags;
840     uint8           spare[3];
841 };
843 /*
844  * To define an array element rather than a named property member, cast the
845  * element's index to (const char *) and initialize name with it, and set the
846  * JSPROP_INDEX bit in flags.
847  */
848 struct JSPropertySpec {
849     const char      *name;
850     int8            tinyid;
851     uint8           flags;
852     JSPropertyOp    getter;
853     JSPropertyOp    setter;
854 };
856 struct JSFunctionSpec {
857     const char      *name;
858     JSNative        call;
859     uint8           nargs;
860     uint8           flags;
861     uint16          extra;      /* number of arg slots for local GC roots */
862 };
864 extern JS_PUBLIC_API(JSObject *)
865 JS_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
866              JSClass *clasp, JSNative constructor, uintN nargs,
867              JSPropertySpec *ps, JSFunctionSpec *fs,
868              JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
870 #ifdef JS_THREADSAFE
871 extern JS_PUBLIC_API(JSClass *)
872 JS_GetClass(JSContext *cx, JSObject *obj);
874 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
875 #else
876 extern JS_PUBLIC_API(JSClass *)
877 JS_GetClass(JSObject *obj);
879 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
880 #endif
882 extern JS_PUBLIC_API(JSBool)
883 JS_InstanceOf(JSContext *cx, JSObject *obj, JSClass *clasp, jsval *argv);
885 extern JS_PUBLIC_API(void *)
886 JS_GetPrivate(JSContext *cx, JSObject *obj);
888 extern JS_PUBLIC_API(JSBool)
889 JS_SetPrivate(JSContext *cx, JSObject *obj, void *data);
891 extern JS_PUBLIC_API(void *)
892 JS_GetInstancePrivate(JSContext *cx, JSObject *obj, JSClass *clasp,
893                       jsval *argv);
895 extern JS_PUBLIC_API(JSObject *)
896 JS_GetPrototype(JSContext *cx, JSObject *obj);
898 extern JS_PUBLIC_API(JSBool)
899 JS_SetPrototype(JSContext *cx, JSObject *obj, JSObject *proto);
901 extern JS_PUBLIC_API(JSObject *)
902 JS_GetParent(JSContext *cx, JSObject *obj);
904 extern JS_PUBLIC_API(JSBool)
905 JS_SetParent(JSContext *cx, JSObject *obj, JSObject *parent);
907 extern JS_PUBLIC_API(JSObject *)
908 JS_GetConstructor(JSContext *cx, JSObject *proto);
910 /*
911  * Get a unique identifier for obj, good for the lifetime of obj (even if it
912  * is moved by a copying GC).  Return false on failure (likely out of memory),
913  * and true with *idp containing the unique id on success.
914  */
915 extern JS_PUBLIC_API(JSBool)
916 JS_GetObjectId(JSContext *cx, JSObject *obj, jsid *idp);
918 extern JS_PUBLIC_API(JSObject *)
919 JS_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent);
921 extern JS_PUBLIC_API(JSBool)
922 JS_SealObject(JSContext *cx, JSObject *obj, JSBool deep);
924 extern JS_PUBLIC_API(JSObject *)
925 JS_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
926                    JSObject *parent);
928 extern JS_PUBLIC_API(JSObject *)
929 JS_ConstructObjectWithArguments(JSContext *cx, JSClass *clasp, JSObject *proto,
930                                 JSObject *parent, uintN argc, jsval *argv);
932 extern JS_PUBLIC_API(JSObject *)
933 JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, JSClass *clasp,
934                 JSObject *proto, uintN attrs);
936 extern JS_PUBLIC_API(JSBool)
937 JS_DefineConstDoubles(JSContext *cx, JSObject *obj, JSConstDoubleSpec *cds);
939 extern JS_PUBLIC_API(JSBool)
940 JS_DefineProperties(JSContext *cx, JSObject *obj, JSPropertySpec *ps);
942 extern JS_PUBLIC_API(JSBool)
943 JS_DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
944                   JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
946 /*
947  * Determine the attributes (JSPROP_* flags) of a property on a given object.
948  *
949  * If the object does not have a property by that name, *foundp will be
950  * JS_FALSE and the value of *attrsp is undefined.
951  */
952 extern JS_PUBLIC_API(JSBool)
953 JS_GetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
954                          uintN *attrsp, JSBool *foundp);
956 /*
957  * Set the attributes of a property on a given object.
958  *
959  * If the object does not have a property by that name, *foundp will be
960  * JS_FALSE and nothing will be altered.
961  */
962 extern JS_PUBLIC_API(JSBool)
963 JS_SetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
964                          uintN attrs, JSBool *foundp);
966 extern JS_PUBLIC_API(JSBool)
967 JS_DefinePropertyWithTinyId(JSContext *cx, JSObject *obj, const char *name,
968                             int8 tinyid, jsval value,
969                             JSPropertyOp getter, JSPropertyOp setter,
970                             uintN attrs);
972 extern JS_PUBLIC_API(JSBool)
973 JS_AliasProperty(JSContext *cx, JSObject *obj, const char *name,
974                  const char *alias);
976 extern JS_PUBLIC_API(JSBool)
977 JS_LookupProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
979 extern JS_PUBLIC_API(JSBool)
980 JS_GetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
982 extern JS_PUBLIC_API(JSBool)
983 JS_SetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
985 extern JS_PUBLIC_API(JSBool)
986 JS_DeleteProperty(JSContext *cx, JSObject *obj, const char *name);
988 extern JS_PUBLIC_API(JSBool)
989 JS_DeleteProperty2(JSContext *cx, JSObject *obj, const char *name,
990                    jsval *rval);
992 extern JS_PUBLIC_API(JSBool)
993 JS_DefineUCProperty(JSContext *cx, JSObject *obj,
994                     const jschar *name, size_t namelen, jsval value,
995                     JSPropertyOp getter, JSPropertyOp setter,
996                     uintN attrs);
998 /*
999  * Determine the attributes (JSPROP_* flags) of a property on a given object.
1000  *
1001  * If the object does not have a property by that name, *foundp will be
1002  * JS_FALSE and the value of *attrsp is undefined.
1003  */
1004 extern JS_PUBLIC_API(JSBool)
1005 JS_GetUCPropertyAttributes(JSContext *cx, JSObject *obj,
1006                            const jschar *name, size_t namelen,
1007                            uintN *attrsp, JSBool *foundp);
1009 /*
1010  * Set the attributes of a property on a given object.
1011  *
1012  * If the object does not have a property by that name, *foundp will be
1013  * JS_FALSE and nothing will be altered.
1014  */
1015 extern JS_PUBLIC_API(JSBool)
1016 JS_SetUCPropertyAttributes(JSContext *cx, JSObject *obj,
1017                            const jschar *name, size_t namelen,
1018                            uintN attrs, JSBool *foundp);
1021 extern JS_PUBLIC_API(JSBool)
1022 JS_DefineUCPropertyWithTinyId(JSContext *cx, JSObject *obj,
1023                               const jschar *name, size_t namelen,
1024                               int8 tinyid, jsval value,
1025                               JSPropertyOp getter, JSPropertyOp setter,
1026                               uintN attrs);
1028 extern JS_PUBLIC_API(JSBool)
1029 JS_LookupUCProperty(JSContext *cx, JSObject *obj,
1030                     const jschar *name, size_t namelen,
1031                     jsval *vp);
1033 extern JS_PUBLIC_API(JSBool)
1034 JS_GetUCProperty(JSContext *cx, JSObject *obj,
1035                  const jschar *name, size_t namelen,
1036                  jsval *vp);
1038 extern JS_PUBLIC_API(JSBool)
1039 JS_SetUCProperty(JSContext *cx, JSObject *obj,
1040                  const jschar *name, size_t namelen,
1041                  jsval *vp);
1043 extern JS_PUBLIC_API(JSBool)
1044 JS_DeleteUCProperty2(JSContext *cx, JSObject *obj,
1045                      const jschar *name, size_t namelen,
1046                      jsval *rval);
1048 extern JS_PUBLIC_API(JSObject *)
1049 JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector);
1051 extern JS_PUBLIC_API(JSBool)
1052 JS_IsArrayObject(JSContext *cx, JSObject *obj);
1054 extern JS_PUBLIC_API(JSBool)
1055 JS_GetArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1057 extern JS_PUBLIC_API(JSBool)
1058 JS_SetArrayLength(JSContext *cx, JSObject *obj, jsuint length);
1060 extern JS_PUBLIC_API(JSBool)
1061 JS_HasArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1063 extern JS_PUBLIC_API(JSBool)
1064 JS_DefineElement(JSContext *cx, JSObject *obj, jsint index, jsval value,
1065                  JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
1067 extern JS_PUBLIC_API(JSBool)
1068 JS_AliasElement(JSContext *cx, JSObject *obj, const char *name, jsint alias);
1070 extern JS_PUBLIC_API(JSBool)
1071 JS_LookupElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1073 extern JS_PUBLIC_API(JSBool)
1074 JS_GetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1076 extern JS_PUBLIC_API(JSBool)
1077 JS_SetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1079 extern JS_PUBLIC_API(JSBool)
1080 JS_DeleteElement(JSContext *cx, JSObject *obj, jsint index);
1082 extern JS_PUBLIC_API(JSBool)
1083 JS_DeleteElement2(JSContext *cx, JSObject *obj, jsint index, jsval *rval);
1085 extern JS_PUBLIC_API(void)
1086 JS_ClearScope(JSContext *cx, JSObject *obj);
1088 extern JS_PUBLIC_API(JSIdArray *)
1089 JS_Enumerate(JSContext *cx, JSObject *obj);
1091 extern JS_PUBLIC_API(JSBool)
1092 JS_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
1093                jsval *vp, uintN *attrsp);
1095 extern JS_PUBLIC_API(JSCheckAccessOp)
1096 JS_SetCheckObjectAccessCallback(JSRuntime *rt, JSCheckAccessOp acb);
1098 extern JS_PUBLIC_API(JSBool)
1099 JS_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
1101 extern JS_PUBLIC_API(JSBool)
1102 JS_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
1104 /************************************************************************/
1106 /*
1107  * Security protocol.
1108  */
1109 struct JSPrincipals {
1110     char *codebase;
1111     void * (* JS_DLL_CALLBACK getPrincipalArray)(JSContext *cx, JSPrincipals *);
1112     JSBool (* JS_DLL_CALLBACK globalPrivilegesEnabled)(JSContext *cx, JSPrincipals *);
1114     /* Don't call "destroy"; use reference counting macros below. */
1115     jsrefcount refcount;
1116     void (* JS_DLL_CALLBACK destroy)(JSContext *cx, struct JSPrincipals *);
1117 };
1119 #ifdef JS_THREADSAFE
1120 #define JSPRINCIPALS_HOLD(cx, principals)   JS_HoldPrincipals(cx,principals)
1121 #define JSPRINCIPALS_DROP(cx, principals)   JS_DropPrincipals(cx,principals)
1123 extern JS_PUBLIC_API(jsrefcount)
1124 JS_HoldPrincipals(JSContext *cx, JSPrincipals *principals);
1126 extern JS_PUBLIC_API(jsrefcount)
1127 JS_DropPrincipals(JSContext *cx, JSPrincipals *principals);
1129 #else
1130 #define JSPRINCIPALS_HOLD(cx, principals)   (++(principals)->refcount)
1131 #define JSPRINCIPALS_DROP(cx, principals)                                     \
1132     ((--(principals)->refcount == 0)                                          \
1133      ? ((*(principals)->destroy)((cx), (principals)), 0)                      \
1134      : (principals)->refcount)
1135 #endif
1137 extern JS_PUBLIC_API(JSPrincipalsTranscoder)
1138 JS_SetPrincipalsTranscoder(JSRuntime *rt, JSPrincipalsTranscoder px);
1140 extern JS_PUBLIC_API(JSObjectPrincipalsFinder)
1141 JS_SetObjectPrincipalsFinder(JSContext *cx, JSObjectPrincipalsFinder fop);
1143 /************************************************************************/
1145 /*
1146  * Functions and scripts.
1147  */
1148 extern JS_PUBLIC_API(JSFunction *)
1149 JS_NewFunction(JSContext *cx, JSNative call, uintN nargs, uintN flags,
1150                JSObject *parent, const char *name);
1152 extern JS_PUBLIC_API(JSObject *)
1153 JS_GetFunctionObject(JSFunction *fun);
1155 /*
1156  * Deprecated, useful only for diagnostics.  Use JS_GetFunctionId instead for
1157  * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
1158  */
1159 extern JS_PUBLIC_API(const char *)
1160 JS_GetFunctionName(JSFunction *fun);
1162 /*
1163  * Return the function's identifier as a JSString, or null if fun is unnamed.
1164  * The returned string lives as long as fun, so you don't need to root a saved
1165  * reference to it if fun is well-connected or rooted, and provided you bound
1166  * the use of the saved reference by fun's lifetime.
1167  *
1168  * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
1169  * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
1170  * from UTF-16-ish jschars.
1171  */
1172 extern JS_PUBLIC_API(JSString *)
1173 JS_GetFunctionId(JSFunction *fun);
1175 /*
1176  * Return JSFUN_* flags for fun.
1177  */
1178 extern JS_PUBLIC_API(uintN)
1179 JS_GetFunctionFlags(JSFunction *fun);
1181 /*
1182  * Infallible predicate to test whether obj is a function object (faster than
1183  * comparing obj's class name to "Function", but equivalent unless someone has
1184  * overwritten the "Function" identifier with a different constructor and then
1185  * created instances using that constructor that might be passed in as obj).
1186  */
1187 extern JS_PUBLIC_API(JSBool)
1188 JS_ObjectIsFunction(JSContext *cx, JSObject *obj);
1190 extern JS_PUBLIC_API(JSBool)
1191 JS_DefineFunctions(JSContext *cx, JSObject *obj, JSFunctionSpec *fs);
1193 extern JS_PUBLIC_API(JSFunction *)
1194 JS_DefineFunction(JSContext *cx, JSObject *obj, const char *name, JSNative call,
1195                   uintN nargs, uintN attrs);
1197 extern JS_PUBLIC_API(JSObject *)
1198 JS_CloneFunctionObject(JSContext *cx, JSObject *funobj, JSObject *parent);
1200 /*
1201  * Given a buffer, return JS_FALSE if the buffer might become a valid
1202  * javascript statement with the addition of more lines.  Otherwise return
1203  * JS_TRUE.  The intent is to support interactive compilation - accumulate
1204  * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
1205  * the compiler.
1206  */
1207 extern JS_PUBLIC_API(JSBool)
1208 JS_BufferIsCompilableUnit(JSContext *cx, JSObject *obj,
1209                           const char *bytes, size_t length);
1211 /*
1212  * The JSScript objects returned by the following functions refer to string and
1213  * other kinds of literals, including doubles and RegExp objects.  These
1214  * literals are vulnerable to garbage collection; to root script objects and
1215  * prevent literals from being collected, create a rootable object using
1216  * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
1217  */
1218 extern JS_PUBLIC_API(JSScript *)
1219 JS_CompileScript(JSContext *cx, JSObject *obj,
1220                  const char *bytes, size_t length,
1221                  const char *filename, uintN lineno);
1223 extern JS_PUBLIC_API(JSScript *)
1224 JS_CompileScriptForPrincipals(JSContext *cx, JSObject *obj,
1225                               JSPrincipals *principals,
1226                               const char *bytes, size_t length,
1227                               const char *filename, uintN lineno);
1229 extern JS_PUBLIC_API(JSScript *)
1230 JS_CompileUCScript(JSContext *cx, JSObject *obj,
1231                    const jschar *chars, size_t length,
1232                    const char *filename, uintN lineno);
1234 extern JS_PUBLIC_API(JSScript *)
1235 JS_CompileUCScriptForPrincipals(JSContext *cx, JSObject *obj,
1236                                 JSPrincipals *principals,
1237                                 const jschar *chars, size_t length,
1238                                 const char *filename, uintN lineno);
1240 extern JS_PUBLIC_API(JSScript *)
1241 JS_CompileFile(JSContext *cx, JSObject *obj, const char *filename);
1243 extern JS_PUBLIC_API(JSScript *)
1244 JS_CompileFileHandle(JSContext *cx, JSObject *obj, const char *filename,
1245                      FILE *fh);
1247 extern JS_PUBLIC_API(JSScript *)
1248 JS_CompileFileHandleForPrincipals(JSContext *cx, JSObject *obj,
1249                                   const char *filename, FILE *fh,
1250                                   JSPrincipals *principals);
1252 /*
1253  * NB: you must use JS_NewScriptObject and root a pointer to its return value
1254  * in order to keep a JSScript and its atoms safe from garbage collection after
1255  * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
1256  * E.g., and without error checks:
1257  *
1258  *    JSScript *script = JS_CompileFile(cx, global, filename);
1259  *    JSObject *scrobj = JS_NewScriptObject(cx, script);
1260  *    JS_AddNamedRoot(cx, &scrobj, "scrobj");
1261  *    do {
1262  *        jsval result;
1263  *        JS_ExecuteScript(cx, global, script, &result);
1264  *        JS_GC();
1265  *    } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
1266  *    JS_RemoveRoot(cx, &scrobj);
1267  */
1268 extern JS_PUBLIC_API(JSObject *)
1269 JS_NewScriptObject(JSContext *cx, JSScript *script);
1271 /*
1272  * Infallible getter for a script's object.  If JS_NewScriptObject has not been
1273  * called on script yet, the return value will be null.
1274  */
1275 extern JS_PUBLIC_API(JSObject *)
1276 JS_GetScriptObject(JSScript *script);
1278 extern JS_PUBLIC_API(void)
1279 JS_DestroyScript(JSContext *cx, JSScript *script);
1281 extern JS_PUBLIC_API(JSFunction *)
1282 JS_CompileFunction(JSContext *cx, JSObject *obj, const char *name,
1283                    uintN nargs, const char **argnames,
1284                    const char *bytes, size_t length,
1285                    const char *filename, uintN lineno);
1287 extern JS_PUBLIC_API(JSFunction *)
1288 JS_CompileFunctionForPrincipals(JSContext *cx, JSObject *obj,
1289                                 JSPrincipals *principals, const char *name,
1290                                 uintN nargs, const char **argnames,
1291                                 const char *bytes, size_t length,
1292                                 const char *filename, uintN lineno);
1294 extern JS_PUBLIC_API(JSFunction *)
1295 JS_CompileUCFunction(JSContext *cx, JSObject *obj, const char *name,
1296                      uintN nargs, const char **argnames,
1297                      const jschar *chars, size_t length,
1298                      const char *filename, uintN lineno);
1300 extern JS_PUBLIC_API(JSFunction *)
1301 JS_CompileUCFunctionForPrincipals(JSContext *cx, JSObject *obj,
1302                                   JSPrincipals *principals, const char *name,
1303                                   uintN nargs, const char **argnames,
1304                                   const jschar *chars, size_t length,
1305                                   const char *filename, uintN lineno);
1307 extern JS_PUBLIC_API(JSString *)
1308 JS_DecompileScript(JSContext *cx, JSScript *script, const char *name,
1309                    uintN indent);
1311 /*
1312  * API extension: OR this into indent to avoid pretty-printing the decompiled
1313  * source resulting from JS_DecompileFunction{,Body}.
1314  */
1315 #define JS_DONT_PRETTY_PRINT    ((uintN)0x8000)
1317 extern JS_PUBLIC_API(JSString *)
1318 JS_DecompileFunction(JSContext *cx, JSFunction *fun, uintN indent);
1320 extern JS_PUBLIC_API(JSString *)
1321 JS_DecompileFunctionBody(JSContext *cx, JSFunction *fun, uintN indent);
1323 /*
1324  * NB: JS_ExecuteScript, JS_ExecuteScriptPart, and the JS_Evaluate*Script*
1325  * quadruplets all use the obj parameter as the initial scope chain header,
1326  * the 'this' keyword value, and the variables object (ECMA parlance for where
1327  * 'var' and 'function' bind names) of the execution context for script.
1328  *
1329  * Using obj as the variables object is problematic if obj's parent (which is
1330  * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
1331  * this case, variables created by 'var x = 0', e.g., go in obj, but variables
1332  * created by assignment to an unbound id, 'x = 0', go in the last object on
1333  * the scope chain linked by parent.
1334  *
1335  * ECMA calls that last scoping object the "global object", but note that many
1336  * embeddings have several such objects.  ECMA requires that "global code" be
1337  * executed with the variables object equal to this global object.  But these
1338  * JS API entry points provide freedom to execute code against a "sub-global",
1339  * i.e., a parented or scoped object, in which case the variables object will
1340  * differ from the last object on the scope chain, resulting in confusing and
1341  * non-ECMA explicit vs. implicit variable creation.
1342  *
1343  * Caveat embedders: unless you already depend on this buggy variables object
1344  * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
1345  * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
1346  * someone may have set other options on cx already -- for each context in the
1347  * application, if you pass parented objects as the obj parameter, or may ever
1348  * pass such objects in the future.
1349  *
1350  * Why a runtime option?  The alternative is to add six or so new API entry
1351  * points with signatures matching the following six, and that doesn't seem
1352  * worth the code bloat cost.  Such new entry points would probably have less
1353  * obvious names, too, so would not tend to be used.  The JS_SetOption call,
1354  * OTOH, can be more easily hacked into existing code that does not depend on
1355  * the bug; such code can continue to use the familiar JS_EvaluateScript,
1356  * etc., entry points.
1357  */
1358 extern JS_PUBLIC_API(JSBool)
1359 JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval);
1361 /*
1362  * Execute either the function-defining prolog of a script, or the script's
1363  * main body, but not both.
1364  */
1365 typedef enum JSExecPart { JSEXEC_PROLOG, JSEXEC_MAIN } JSExecPart;
1367 extern JS_PUBLIC_API(JSBool)
1368 JS_ExecuteScriptPart(JSContext *cx, JSObject *obj, JSScript *script,
1369                      JSExecPart part, jsval *rval);
1371 extern JS_PUBLIC_API(JSBool)
1372 JS_EvaluateScript(JSContext *cx, JSObject *obj,
1373                   const char *bytes, uintN length,
1374                   const char *filename, uintN lineno,
1375                   jsval *rval);
1377 extern JS_PUBLIC_API(JSBool)
1378 JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj,
1379                                JSPrincipals *principals,
1380                                const char *bytes, uintN length,
1381                                const char *filename, uintN lineno,
1382                                jsval *rval);
1384 extern JS_PUBLIC_API(JSBool)
1385 JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
1386                     const jschar *chars, uintN length,
1387                     const char *filename, uintN lineno,
1388                     jsval *rval);
1390 extern JS_PUBLIC_API(JSBool)
1391 JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj,
1392                                  JSPrincipals *principals,
1393                                  const jschar *chars, uintN length,
1394                                  const char *filename, uintN lineno,
1395                                  jsval *rval);
1397 extern JS_PUBLIC_API(JSBool)
1398 JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc,
1399                 jsval *argv, jsval *rval);
1401 extern JS_PUBLIC_API(JSBool)
1402 JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, uintN argc,
1403                     jsval *argv, jsval *rval);
1405 extern JS_PUBLIC_API(JSBool)
1406 JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc,
1407                      jsval *argv, jsval *rval);
1409 extern JS_PUBLIC_API(JSBranchCallback)
1410 JS_SetBranchCallback(JSContext *cx, JSBranchCallback cb);
1412 extern JS_PUBLIC_API(JSBool)
1413 JS_IsRunning(JSContext *cx);
1415 extern JS_PUBLIC_API(JSBool)
1416 JS_IsConstructing(JSContext *cx);
1418 /*
1419  * Returns true if a script is executing and its current bytecode is a set
1420  * (assignment) operation, even if there are native (no script) stack frames
1421  * between the script and the caller to JS_IsAssigning.
1422  */
1423 extern JS_FRIEND_API(JSBool)
1424 JS_IsAssigning(JSContext *cx);
1426 /*
1427  * Set the second return value, which should be a string or int jsval that
1428  * identifies a property in the returned object, to form an ECMA reference
1429  * type value (obj, id).  Only native methods can return reference types,
1430  * and if the returned value is used on the left-hand side of an assignment
1431  * op, the identified property will be set.  If the return value is in an
1432  * r-value, the interpreter just gets obj[id]'s value.
1433  */
1434 extern JS_PUBLIC_API(void)
1435 JS_SetCallReturnValue2(JSContext *cx, jsval v);
1437 /************************************************************************/
1439 /*
1440  * Strings.
1441  *
1442  * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
1443  * on error (signified by null return), it leaves bytes owned by the caller.
1444  * So the caller must free bytes in the error case, if it has no use for them.
1445  * In contrast, all the JS_New*StringCopy* functions do not take ownership of
1446  * the character memory passed to them -- they copy it.
1447  */
1448 extern JS_PUBLIC_API(JSString *)
1449 JS_NewString(JSContext *cx, char *bytes, size_t length);
1451 extern JS_PUBLIC_API(JSString *)
1452 JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);
1454 extern JS_PUBLIC_API(JSString *)
1455 JS_NewStringCopyZ(JSContext *cx, const char *s);
1457 extern JS_PUBLIC_API(JSString *)
1458 JS_InternString(JSContext *cx, const char *s);
1460 extern JS_PUBLIC_API(JSString *)
1461 JS_NewUCString(JSContext *cx, jschar *chars, size_t length);
1463 extern JS_PUBLIC_API(JSString *)
1464 JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n);
1466 extern JS_PUBLIC_API(JSString *)
1467 JS_NewUCStringCopyZ(JSContext *cx, const jschar *s);
1469 extern JS_PUBLIC_API(JSString *)
1470 JS_InternUCStringN(JSContext *cx, const jschar *s, size_t length);
1472 extern JS_PUBLIC_API(JSString *)
1473 JS_InternUCString(JSContext *cx, const jschar *s);
1475 extern JS_PUBLIC_API(char *)
1476 JS_GetStringBytes(JSString *str);
1478 extern JS_PUBLIC_API(jschar *)
1479 JS_GetStringChars(JSString *str);
1481 extern JS_PUBLIC_API(size_t)
1482 JS_GetStringLength(JSString *str);
1484 extern JS_PUBLIC_API(intN)
1485 JS_CompareStrings(JSString *str1, JSString *str2);
1487 /*
1488  * Mutable string support.  A string's characters are never mutable in this JS
1489  * implementation, but a growable string has a buffer that can be reallocated,
1490  * and a dependent string is a substring of another (growable, dependent, or
1491  * immutable) string.  The direct data members of the (opaque to API clients)
1492  * JSString struct may be changed in a single-threaded way for growable and
1493  * dependent strings.
1494  *
1495  * Therefore mutable strings cannot be used by more than one thread at a time.
1496  * You may call JS_MakeStringImmutable to convert the string from a mutable
1497  * (growable or dependent) string to an immutable (and therefore thread-safe)
1498  * string.  The engine takes care of converting growable and dependent strings
1499  * to immutable for you if you store strings in multi-threaded objects using
1500  * JS_SetProperty or kindred API entry points.
1501  *
1502  * If you store a JSString pointer in a native data structure that is (safely)
1503  * accessible to multiple threads, you must call JS_MakeStringImmutable before
1504  * retiring the store.
1505  */
1506 extern JS_PUBLIC_API(JSString *)
1507 JS_NewGrowableString(JSContext *cx, jschar *chars, size_t length);
1509 /*
1510  * Create a dependent string, i.e., a string that owns no character storage,
1511  * but that refers to a slice of another string's chars.  Dependent strings
1512  * are mutable by definition, so the thread safety comments above apply.
1513  */
1514 extern JS_PUBLIC_API(JSString *)
1515 JS_NewDependentString(JSContext *cx, JSString *str, size_t start,
1516                       size_t length);
1518 /*
1519  * Concatenate two strings, resulting in a new growable string.  If you create
1520  * the left string and pass it to JS_ConcatStrings on a single thread, try to
1521  * use JS_NewGrowableString to create the left string -- doing so helps Concat
1522  * avoid allocating a new buffer for the result and copying left's chars into
1523  * the new buffer.  See above for thread safety comments.
1524  */
1525 extern JS_PUBLIC_API(JSString *)
1526 JS_ConcatStrings(JSContext *cx, JSString *left, JSString *right);
1528 /*
1529  * Convert a dependent string into an independent one.  This function does not
1530  * change the string's mutability, so the thread safety comments above apply.
1531  */
1532 extern JS_PUBLIC_API(const jschar *)
1533 JS_UndependString(JSContext *cx, JSString *str);
1535 /*
1536  * Convert a mutable string (either growable or dependent) into an immutable,
1537  * thread-safe one.
1538  */
1539 extern JS_PUBLIC_API(JSBool)
1540 JS_MakeStringImmutable(JSContext *cx, JSString *str);
1542 /************************************************************************/
1544 /*
1545  * Locale specific string conversion callback.
1546  */
1547 struct JSLocaleCallbacks {
1548     JSLocaleToUpperCase     localeToUpperCase;
1549     JSLocaleToLowerCase     localeToLowerCase;
1550     JSLocaleCompare         localeCompare;
1551 };
1553 /*
1554  * Establish locale callbacks. The pointer must persist as long as the
1555  * JSContext.  Passing NULL restores the default behaviour.
1556  */
1557 extern JS_PUBLIC_API(void)
1558 JS_SetLocaleCallbacks(JSContext *cx, JSLocaleCallbacks *callbacks);
1560 /*
1561  * Return the address of the current locale callbacks struct, which may
1562  * be NULL.
1563  */
1564 extern JS_PUBLIC_API(JSLocaleCallbacks *)
1565 JS_GetLocaleCallbacks(JSContext *cx);
1567 /************************************************************************/
1569 /*
1570  * Error reporting.
1571  */
1573 /*
1574  * Report an exception represented by the sprintf-like conversion of format
1575  * and its arguments.  This exception message string is passed to a pre-set
1576  * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
1577  * the JSErrorReporter typedef).
1578  */
1579 extern JS_PUBLIC_API(void)
1580 JS_ReportError(JSContext *cx, const char *format, ...);
1582 /*
1583  * Use an errorNumber to retrieve the format string, args are char *
1584  */
1585 extern JS_PUBLIC_API(void)
1586 JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
1587                      void *userRef, const uintN errorNumber, ...);
1589 /*
1590  * Use an errorNumber to retrieve the format string, args are jschar *
1591  */
1592 extern JS_PUBLIC_API(void)
1593 JS_ReportErrorNumberUC(JSContext *cx, JSErrorCallback errorCallback,
1594                      void *userRef, const uintN errorNumber, ...);
1596 /*
1597  * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
1598  * Return true if there was no error trying to issue the warning, and if the
1599  * warning was not converted into an error due to the JSOPTION_WERROR option
1600  * being set, false otherwise.
1601  */
1602 extern JS_PUBLIC_API(JSBool)
1603 JS_ReportWarning(JSContext *cx, const char *format, ...);
1605 extern JS_PUBLIC_API(JSBool)
1606 JS_ReportErrorFlagsAndNumber(JSContext *cx, uintN flags,
1607                              JSErrorCallback errorCallback, void *userRef,
1608                              const uintN errorNumber, ...);
1610 extern JS_PUBLIC_API(JSBool)
1611 JS_ReportErrorFlagsAndNumberUC(JSContext *cx, uintN flags,
1612                                JSErrorCallback errorCallback, void *userRef,
1613                                const uintN errorNumber, ...);
1615 /*
1616  * Complain when out of memory.
1617  */
1618 extern JS_PUBLIC_API(void)
1619 JS_ReportOutOfMemory(JSContext *cx);
1621 struct JSErrorReport {
1622     const char      *filename;      /* source file name, URL, etc., or null */
1623     uintN           lineno;         /* source line number */
1624     const char      *linebuf;       /* offending source line without final \n */
1625     const char      *tokenptr;      /* pointer to error token in linebuf */
1626     const jschar    *uclinebuf;     /* unicode (original) line buffer */
1627     const jschar    *uctokenptr;    /* unicode (original) token pointer */
1628     uintN           flags;          /* error/warning, etc. */
1629     uintN           errorNumber;    /* the error number, e.g. see js.msg */
1630     const jschar    *ucmessage;     /* the (default) error message */
1631     const jschar    **messageArgs;  /* arguments for the error message */
1632 };
1634 /*
1635  * JSErrorReport flag values.  These may be freely composed.
1636  */
1637 #define JSREPORT_ERROR      0x0     /* pseudo-flag for default case */
1638 #define JSREPORT_WARNING    0x1     /* reported via JS_ReportWarning */
1639 #define JSREPORT_EXCEPTION  0x2     /* exception was thrown */
1640 #define JSREPORT_STRICT     0x4     /* error or warning due to strict option */
1642 /*
1643  * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
1644  * has been thrown for this runtime error, and the host should ignore it.
1645  * Exception-aware hosts should also check for JS_IsExceptionPending if
1646  * JS_ExecuteScript returns failure, and signal or propagate the exception, as
1647  * appropriate.
1648  */
1649 #define JSREPORT_IS_WARNING(flags)      (((flags) & JSREPORT_WARNING) != 0)
1650 #define JSREPORT_IS_EXCEPTION(flags)    (((flags) & JSREPORT_EXCEPTION) != 0)
1651 #define JSREPORT_IS_STRICT(flags)       (((flags) & JSREPORT_STRICT) != 0)
1653 extern JS_PUBLIC_API(JSErrorReporter)
1654 JS_SetErrorReporter(JSContext *cx, JSErrorReporter er);
1656 /************************************************************************/
1658 /*
1659  * Regular Expressions.
1660  */
1661 #define JSREG_FOLD      0x01    /* fold uppercase to lowercase */
1662 #define JSREG_GLOB      0x02    /* global exec, creates array of matches */
1663 #define JSREG_MULTILINE 0x04    /* treat ^ and $ as begin and end of line */
1665 extern JS_PUBLIC_API(JSObject *)
1666 JS_NewRegExpObject(JSContext *cx, char *bytes, size_t length, uintN flags);
1668 extern JS_PUBLIC_API(JSObject *)
1669 JS_NewUCRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags);
1671 extern JS_PUBLIC_API(void)
1672 JS_SetRegExpInput(JSContext *cx, JSString *input, JSBool multiline);
1674 extern JS_PUBLIC_API(void)
1675 JS_ClearRegExpStatics(JSContext *cx);
1677 extern JS_PUBLIC_API(void)
1678 JS_ClearRegExpRoots(JSContext *cx);
1680 /* TODO: compile, exec, get/set other statics... */
1682 /************************************************************************/
1684 extern JS_PUBLIC_API(JSBool)
1685 JS_IsExceptionPending(JSContext *cx);
1687 extern JS_PUBLIC_API(JSBool)
1688 JS_GetPendingException(JSContext *cx, jsval *vp);
1690 extern JS_PUBLIC_API(void)
1691 JS_SetPendingException(JSContext *cx, jsval v);
1693 extern JS_PUBLIC_API(void)
1694 JS_ClearPendingException(JSContext *cx);
1696 /*
1697  * Save the current exception state.  This takes a snapshot of cx's current
1698  * exception state without making any change to that state.
1699  *
1700  * The returned state pointer MUST be passed later to JS_RestoreExceptionState
1701  * (to restore that saved state, overriding any more recent state) or else to
1702  * JS_DropExceptionState (to free the state struct in case it is not correct
1703  * or desirable to restore it).  Both Restore and Drop free the state struct,
1704  * so callers must stop using the pointer returned from Save after calling the
1705  * Release or Drop API.
1706  */
1707 extern JS_PUBLIC_API(JSExceptionState *)
1708 JS_SaveExceptionState(JSContext *cx);
1710 extern JS_PUBLIC_API(void)
1711 JS_RestoreExceptionState(JSContext *cx, JSExceptionState *state);
1713 extern JS_PUBLIC_API(void)
1714 JS_DropExceptionState(JSContext *cx, JSExceptionState *state);
1716 /*
1717  * If the given value is an exception object that originated from an error,
1718  * the exception will contain an error report struct, and this API will return
1719  * the address of that struct.  Otherwise, it returns NULL.  The lifetime of
1720  * the error report struct that might be returned is the same as the lifetime
1721  * of the exception object.
1722  */
1723 extern JS_PUBLIC_API(JSErrorReport *)
1724 JS_ErrorFromException(JSContext *cx, jsval v);
1726 #ifdef JS_THREADSAFE
1728 /*
1729  * Associate the current thread with the given context.  This is done
1730  * implicitly by JS_NewContext.
1731  *
1732  * Returns the old thread id for this context, which should be treated as
1733  * an opaque value.  This value is provided for comparison to 0, which
1734  * indicates that ClearContextThread has been called on this context
1735  * since the last SetContextThread, or non-0, which indicates the opposite.
1736  */
1737 extern JS_PUBLIC_API(jsword)
1738 JS_GetContextThread(JSContext *cx);
1740 extern JS_PUBLIC_API(jsword)
1741 JS_SetContextThread(JSContext *cx);
1743 extern JS_PUBLIC_API(intN)
1744 JS_ClearContextThread(JSContext *cx);
1746 #endif /* JS_THREADSAFE */
1748 /************************************************************************/
1750 JS_END_EXTERN_C
1752 #endif /* jsapi_h___ */