/[jscoverage]/trunk/js/jsapi.h
ViewVC logotype

Contents of /trunk/js/jsapi.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 332 - (show annotations)
Thu Oct 23 19:03:33 2008 UTC (13 years, 7 months ago) by siliconforks
File MIME type: text/plain
File size: 102357 byte(s)
Add SpiderMonkey from Firefox 3.1b1.

The following directories and files were removed:
correct/, correct.js
liveconnect/
nanojit/
t/
v8/
vprof/
xpconnect/
all JavaScript files (Y.js, call.js, if.js, math-partial-sums.js, md5.js, perfect.js, trace-test.js, trace.js)


1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
3 *
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
19 *
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
24 *
25 * Contributor(s):
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 #ifndef jsapi_h___
42 #define jsapi_h___
43 /*
44 * JavaScript API.
45 */
46 #include <stddef.h>
47 #include <stdio.h>
48 #include "jspubtd.h"
49 #include "jsutil.h"
50
51 JS_BEGIN_EXTERN_C
52
53 /*
54 * Type tags stored in the low bits of a jsval.
55 */
56 #define JSVAL_OBJECT 0x0 /* untagged reference to object */
57 #define JSVAL_INT 0x1 /* tagged 31-bit integer value */
58 #define JSVAL_DOUBLE 0x2 /* tagged reference to double */
59 #define JSVAL_STRING 0x4 /* tagged reference to string */
60 #define JSVAL_BOOLEAN 0x6 /* tagged boolean value */
61
62 /* Type tag bitfield length and derived macros. */
63 #define JSVAL_TAGBITS 3
64 #define JSVAL_TAGMASK JS_BITMASK(JSVAL_TAGBITS)
65 #define JSVAL_TAG(v) ((v) & JSVAL_TAGMASK)
66 #define JSVAL_SETTAG(v,t) ((v) | (t))
67 #define JSVAL_CLRTAG(v) ((v) & ~(jsval)JSVAL_TAGMASK)
68 #define JSVAL_ALIGN JS_BIT(JSVAL_TAGBITS)
69
70 /* Predicates for type testing. */
71 #define JSVAL_IS_OBJECT(v) (JSVAL_TAG(v) == JSVAL_OBJECT)
72 #define JSVAL_IS_NUMBER(v) (JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v))
73 #define JSVAL_IS_INT(v) ((v) & JSVAL_INT)
74 #define JSVAL_IS_DOUBLE(v) (JSVAL_TAG(v) == JSVAL_DOUBLE)
75 #define JSVAL_IS_STRING(v) (JSVAL_TAG(v) == JSVAL_STRING)
76 #define JSVAL_IS_BOOLEAN(v) (((v) & ~((jsval)1 << JSVAL_TAGBITS)) == \
77 JSVAL_BOOLEAN)
78 #define JSVAL_IS_NULL(v) ((v) == JSVAL_NULL)
79 #define JSVAL_IS_VOID(v) ((v) == JSVAL_VOID)
80 #define JSVAL_IS_PRIMITIVE(v) (!JSVAL_IS_OBJECT(v) || JSVAL_IS_NULL(v))
81
82 /* Objects, strings, and doubles are GC'ed. */
83 #define JSVAL_IS_GCTHING(v) (!((v) & JSVAL_INT) && \
84 JSVAL_TAG(v) != JSVAL_BOOLEAN)
85 #define JSVAL_TO_GCTHING(v) ((void *)JSVAL_CLRTAG(v))
86 #define JSVAL_TO_OBJECT(v) ((JSObject *)JSVAL_TO_GCTHING(v))
87 #define JSVAL_TO_DOUBLE(v) ((jsdouble *)JSVAL_TO_GCTHING(v))
88 #define JSVAL_TO_STRING(v) ((JSString *)JSVAL_TO_GCTHING(v))
89 #define OBJECT_TO_JSVAL(obj) ((jsval)(obj))
90 #define DOUBLE_TO_JSVAL(dp) JSVAL_SETTAG((jsval)(dp), JSVAL_DOUBLE)
91 #define STRING_TO_JSVAL(str) JSVAL_SETTAG((jsval)(str), JSVAL_STRING)
92
93 /* Lock and unlock the GC thing held by a jsval. */
94 #define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
95 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
96 : JS_TRUE)
97 #define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
98 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
99 : JS_TRUE)
100
101 /* Domain limits for the jsval int type. */
102 #define JSVAL_INT_BITS 31
103 #define JSVAL_INT_POW2(n) ((jsval)1 << (n))
104 #define JSVAL_INT_MIN (-JSVAL_INT_POW2(30))
105 #define JSVAL_INT_MAX (JSVAL_INT_POW2(30) - 1)
106 #define INT_FITS_IN_JSVAL(i) ((jsuint)(i) - (jsuint)JSVAL_INT_MIN <= \
107 (jsuint)(JSVAL_INT_MAX - JSVAL_INT_MIN))
108 #define JSVAL_TO_INT(v) ((jsint)(v) >> 1)
109 #define INT_TO_JSVAL(i) (((jsval)(i) << 1) | JSVAL_INT)
110
111 /* Convert between boolean and jsval. */
112 #define JSVAL_TO_BOOLEAN(v) ((JSBool)((v) >> JSVAL_TAGBITS))
113 #define BOOLEAN_TO_JSVAL(b) JSVAL_SETTAG((jsval)(b) << JSVAL_TAGBITS, \
114 JSVAL_BOOLEAN)
115
116 /* A private data pointer (2-byte-aligned) can be stored as an int jsval. */
117 #define JSVAL_TO_PRIVATE(v) ((void *)((v) & ~JSVAL_INT))
118 #define PRIVATE_TO_JSVAL(p) ((jsval)(p) | JSVAL_INT)
119
120 /* Property attributes, set in JSPropertySpec and passed to API functions. */
121 #define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
122 #define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
123 #define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
124 #define JSPROP_GETTER 0x10 /* property holds getter function */
125 #define JSPROP_SETTER 0x20 /* property holds setter function */
126 #define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
127 property; don't copy the property on
128 set of the same-named property in an
129 object that delegates to a prototype
130 containing this property */
131 #define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
132
133 /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
134 #define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
135 #define JSFUN_GETTER JSPROP_GETTER
136 #define JSFUN_SETTER JSPROP_SETTER
137 #define JSFUN_BOUND_METHOD 0x40 /* bind this to fun->object's parent */
138 #define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
139
140 #define JSFUN_DISJOINT_FLAGS(f) ((f) & 0x0f)
141 #define JSFUN_GSFLAGS(f) ((f) & (JSFUN_GETTER | JSFUN_SETTER))
142
143 #define JSFUN_GETTER_TEST(f) ((f) & JSFUN_GETTER)
144 #define JSFUN_SETTER_TEST(f) ((f) & JSFUN_SETTER)
145 #define JSFUN_BOUND_METHOD_TEST(f) ((f) & JSFUN_BOUND_METHOD)
146 #define JSFUN_HEAVYWEIGHT_TEST(f) ((f) & JSFUN_HEAVYWEIGHT)
147
148 #define JSFUN_GSFLAG2ATTR(f) JSFUN_GSFLAGS(f)
149
150 #define JSFUN_THISP_FLAGS(f) (f)
151 #define JSFUN_THISP_TEST(f,t) ((f) & t)
152
153 #define JSFUN_THISP_STRING 0x0100 /* |this| may be a primitive string */
154 #define JSFUN_THISP_NUMBER 0x0200 /* |this| may be a primitive number */
155 #define JSFUN_THISP_BOOLEAN 0x0400 /* |this| may be a primitive boolean */
156 #define JSFUN_THISP_PRIMITIVE 0x0700 /* |this| may be any primitive value */
157
158 #define JSFUN_FAST_NATIVE 0x0800 /* JSFastNative needs no JSStackFrame */
159
160 #define JSFUN_FLAGS_MASK 0x0ff8 /* overlay JSFUN_* attributes --
161 note that bit #15 is used internally
162 to flag interpreted functions */
163
164 #define JSFUN_STUB_GSOPS 0x1000 /* use JS_PropertyStub getter/setter
165 instead of defaulting to class gsops
166 for property holding function */
167
168 /*
169 * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
170 * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
171 * methods of a class prototype that are exposed as static methods taking an
172 * extra leading argument: the generic |this| parameter.
173 *
174 * If you set this flag in a JSFunctionSpec struct's flags initializer, then
175 * that struct must live at least as long as the native static method object
176 * created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
177 * JSFunctionSpec structs are allocated in static arrays.
178 */
179 #define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
180
181 /*
182 * Well-known JS values. The extern'd variables are initialized when the
183 * first JSContext is created by JS_NewContext (see below).
184 */
185 #define JSVAL_VOID BOOLEAN_TO_JSVAL(2)
186 #define JSVAL_NULL OBJECT_TO_JSVAL(0)
187 #define JSVAL_ZERO INT_TO_JSVAL(0)
188 #define JSVAL_ONE INT_TO_JSVAL(1)
189 #define JSVAL_FALSE BOOLEAN_TO_JSVAL(JS_FALSE)
190 #define JSVAL_TRUE BOOLEAN_TO_JSVAL(JS_TRUE)
191
192 /*
193 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
194 * comment in jstypes.h regarding safe int64 usage.
195 */
196 extern JS_PUBLIC_API(int64)
197 JS_Now(void);
198
199 /* Don't want to export data, so provide accessors for non-inline jsvals. */
200 extern JS_PUBLIC_API(jsval)
201 JS_GetNaNValue(JSContext *cx);
202
203 extern JS_PUBLIC_API(jsval)
204 JS_GetNegativeInfinityValue(JSContext *cx);
205
206 extern JS_PUBLIC_API(jsval)
207 JS_GetPositiveInfinityValue(JSContext *cx);
208
209 extern JS_PUBLIC_API(jsval)
210 JS_GetEmptyStringValue(JSContext *cx);
211
212 /*
213 * Format is a string of the following characters (spaces are insignificant),
214 * specifying the tabulated type conversions:
215 *
216 * b JSBool Boolean
217 * c uint16/jschar ECMA uint16, Unicode char
218 * i int32 ECMA int32
219 * u uint32 ECMA uint32
220 * j int32 Rounded int32 (coordinate)
221 * d jsdouble IEEE double
222 * I jsdouble Integral IEEE double
223 * s char * C string
224 * S JSString * Unicode string, accessed by a JSString pointer
225 * W jschar * Unicode character vector, 0-terminated (W for wide)
226 * o JSObject * Object reference
227 * f JSFunction * Function private
228 * v jsval Argument value (no conversion)
229 * * N/A Skip this argument (no vararg)
230 * / N/A End of required arguments
231 *
232 * The variable argument list after format must consist of &b, &c, &s, e.g.,
233 * where those variables have the types given above. For the pointer types
234 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
235 * to the JS runtime, not to the calling native code. The runtime promises
236 * to keep this memory valid so long as argv refers to allocated stack space
237 * (so long as the native function is active).
238 *
239 * Fewer arguments than format specifies may be passed only if there is a /
240 * in format after the last required argument specifier and argc is at least
241 * the number of required arguments. More arguments than format specifies
242 * may be passed without error; it is up to the caller to deal with trailing
243 * unconverted arguments.
244 */
245 extern JS_PUBLIC_API(JSBool)
246 JS_ConvertArguments(JSContext *cx, uintN argc, jsval *argv, const char *format,
247 ...);
248
249 #ifdef va_start
250 extern JS_PUBLIC_API(JSBool)
251 JS_ConvertArgumentsVA(JSContext *cx, uintN argc, jsval *argv,
252 const char *format, va_list ap);
253 #endif
254
255 /*
256 * Inverse of JS_ConvertArguments: scan format and convert trailing arguments
257 * into jsvals, GC-rooted if necessary by the JS stack. Return null on error,
258 * and a pointer to the new argument vector on success. Also return a stack
259 * mark on success via *markp, in which case the caller must eventually clean
260 * up by calling JS_PopArguments.
261 *
262 * Note that the number of actual arguments supplied is specified exclusively
263 * by format, so there is no argc parameter.
264 */
265 extern JS_PUBLIC_API(jsval *)
266 JS_PushArguments(JSContext *cx, void **markp, const char *format, ...);
267
268 #ifdef va_start
269 extern JS_PUBLIC_API(jsval *)
270 JS_PushArgumentsVA(JSContext *cx, void **markp, const char *format, va_list ap);
271 #endif
272
273 extern JS_PUBLIC_API(void)
274 JS_PopArguments(JSContext *cx, void *mark);
275
276 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
277
278 /*
279 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
280 * The handler function has this signature (see jspubtd.h):
281 *
282 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
283 * JSBool fromJS, jsval **vpp, va_list *app);
284 *
285 * It should return true on success, and return false after reporting an error
286 * or detecting an already-reported error.
287 *
288 * For a given format string, for example "AA", the formatter is called from
289 * JS_ConvertArgumentsVA like so:
290 *
291 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
292 *
293 * sp points into the arguments array on the JS stack, while ap points into
294 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
295 * the formatter to convert zero or more jsvals at sp to zero or more C values
296 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
297 * (via *app) to point past the converted arguments and their result pointers
298 * on the C stack.
299 *
300 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
301 *
302 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
303 *
304 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
305 * format specifier and store them at sp, updating ap and sp appropriately.
306 *
307 * The "..." after "AA" is the rest of the format string that was passed into
308 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
309 * in each Convert or PushArguments call is passed to the formatter, so that
310 * one such function may implement several formats, in order to share code.
311 *
312 * Remove just forgets about any handler associated with format. Add does not
313 * copy format, it points at the string storage allocated by the caller, which
314 * is typically a string constant. If format is in dynamic storage, it is up
315 * to the caller to keep the string alive until Remove is called.
316 */
317 extern JS_PUBLIC_API(JSBool)
318 JS_AddArgumentFormatter(JSContext *cx, const char *format,
319 JSArgumentFormatter formatter);
320
321 extern JS_PUBLIC_API(void)
322 JS_RemoveArgumentFormatter(JSContext *cx, const char *format);
323
324 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
325
326 extern JS_PUBLIC_API(JSBool)
327 JS_ConvertValue(JSContext *cx, jsval v, JSType type, jsval *vp);
328
329 extern JS_PUBLIC_API(JSBool)
330 JS_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
331
332 extern JS_PUBLIC_API(JSFunction *)
333 JS_ValueToFunction(JSContext *cx, jsval v);
334
335 extern JS_PUBLIC_API(JSFunction *)
336 JS_ValueToConstructor(JSContext *cx, jsval v);
337
338 extern JS_PUBLIC_API(JSString *)
339 JS_ValueToString(JSContext *cx, jsval v);
340
341 extern JS_PUBLIC_API(JSBool)
342 JS_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
343
344 /*
345 * Convert a value to a number, then to an int32, according to the ECMA rules
346 * for ToInt32.
347 */
348 extern JS_PUBLIC_API(JSBool)
349 JS_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip);
350
351 /*
352 * Convert a value to a number, then to a uint32, according to the ECMA rules
353 * for ToUint32.
354 */
355 extern JS_PUBLIC_API(JSBool)
356 JS_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip);
357
358 /*
359 * Convert a value to a number, then to an int32 if it fits by rounding to
360 * nearest; but failing with an error report if the double is out of range
361 * or unordered.
362 */
363 extern JS_PUBLIC_API(JSBool)
364 JS_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
365
366 /*
367 * ECMA ToUint16, for mapping a jsval to a Unicode point.
368 */
369 extern JS_PUBLIC_API(JSBool)
370 JS_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
371
372 extern JS_PUBLIC_API(JSBool)
373 JS_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp);
374
375 extern JS_PUBLIC_API(JSType)
376 JS_TypeOfValue(JSContext *cx, jsval v);
377
378 extern JS_PUBLIC_API(const char *)
379 JS_GetTypeName(JSContext *cx, JSType type);
380
381 /************************************************************************/
382
383 /*
384 * Initialization, locking, contexts, and memory allocation.
385 *
386 * It is important that the first runtime and first context be created in a
387 * single-threaded fashion, otherwise the behavior of the library is undefined.
388 * See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
389 */
390 #define JS_NewRuntime JS_Init
391 #define JS_DestroyRuntime JS_Finish
392 #define JS_LockRuntime JS_Lock
393 #define JS_UnlockRuntime JS_Unlock
394
395 extern JS_PUBLIC_API(JSRuntime *)
396 JS_NewRuntime(uint32 maxbytes);
397
398 extern JS_PUBLIC_API(void)
399 JS_DestroyRuntime(JSRuntime *rt);
400
401 extern JS_PUBLIC_API(void)
402 JS_ShutDown(void);
403
404 JS_PUBLIC_API(void *)
405 JS_GetRuntimePrivate(JSRuntime *rt);
406
407 JS_PUBLIC_API(void)
408 JS_SetRuntimePrivate(JSRuntime *rt, void *data);
409
410 extern JS_PUBLIC_API(void)
411 JS_BeginRequest(JSContext *cx);
412
413 extern JS_PUBLIC_API(void)
414 JS_EndRequest(JSContext *cx);
415
416 /* Yield to pending GC operations, regardless of request depth */
417 extern JS_PUBLIC_API(void)
418 JS_YieldRequest(JSContext *cx);
419
420 extern JS_PUBLIC_API(jsrefcount)
421 JS_SuspendRequest(JSContext *cx);
422
423 extern JS_PUBLIC_API(void)
424 JS_ResumeRequest(JSContext *cx, jsrefcount saveDepth);
425
426 #ifdef __cplusplus
427 JS_END_EXTERN_C
428
429 class JSAutoRequest {
430 public:
431 JSAutoRequest(JSContext *cx) : mContext(cx), mSaveDepth(0) {
432 JS_BeginRequest(mContext);
433 }
434 ~JSAutoRequest() {
435 JS_EndRequest(mContext);
436 }
437
438 void suspend() {
439 mSaveDepth = JS_SuspendRequest(mContext);
440 }
441 void resume() {
442 JS_ResumeRequest(mContext, mSaveDepth);
443 }
444
445 protected:
446 JSContext *mContext;
447 jsrefcount mSaveDepth;
448
449 #if 0
450 private:
451 static void *operator new(size_t) CPP_THROW_NEW { return 0; };
452 static void operator delete(void *, size_t) { };
453 #endif
454 };
455
456 class JSAutoSuspendRequest {
457 public:
458 JSAutoSuspendRequest(JSContext *cx) : mContext(cx), mSaveDepth(0) {
459 if (mContext) {
460 mSaveDepth = JS_SuspendRequest(mContext);
461 }
462 }
463 ~JSAutoSuspendRequest() {
464 resume();
465 }
466
467 void resume() {
468 if (mContext) {
469 JS_ResumeRequest(mContext, mSaveDepth);
470 mContext = 0;
471 }
472 }
473
474 protected:
475 JSContext *mContext;
476 jsrefcount mSaveDepth;
477
478 #if 0
479 private:
480 static void *operator new(size_t) CPP_THROW_NEW { return 0; };
481 static void operator delete(void *, size_t) { };
482 #endif
483 };
484
485 JS_BEGIN_EXTERN_C
486 #endif
487
488 extern JS_PUBLIC_API(void)
489 JS_Lock(JSRuntime *rt);
490
491 extern JS_PUBLIC_API(void)
492 JS_Unlock(JSRuntime *rt);
493
494 extern JS_PUBLIC_API(JSContextCallback)
495 JS_SetContextCallback(JSRuntime *rt, JSContextCallback cxCallback);
496
497 extern JS_PUBLIC_API(JSContext *)
498 JS_NewContext(JSRuntime *rt, size_t stackChunkSize);
499
500 extern JS_PUBLIC_API(void)
501 JS_DestroyContext(JSContext *cx);
502
503 extern JS_PUBLIC_API(void)
504 JS_DestroyContextNoGC(JSContext *cx);
505
506 extern JS_PUBLIC_API(void)
507 JS_DestroyContextMaybeGC(JSContext *cx);
508
509 extern JS_PUBLIC_API(void *)
510 JS_GetContextPrivate(JSContext *cx);
511
512 extern JS_PUBLIC_API(void)
513 JS_SetContextPrivate(JSContext *cx, void *data);
514
515 extern JS_PUBLIC_API(JSRuntime *)
516 JS_GetRuntime(JSContext *cx);
517
518 extern JS_PUBLIC_API(JSContext *)
519 JS_ContextIterator(JSRuntime *rt, JSContext **iterp);
520
521 extern JS_PUBLIC_API(JSVersion)
522 JS_GetVersion(JSContext *cx);
523
524 extern JS_PUBLIC_API(JSVersion)
525 JS_SetVersion(JSContext *cx, JSVersion version);
526
527 extern JS_PUBLIC_API(const char *)
528 JS_VersionToString(JSVersion version);
529
530 extern JS_PUBLIC_API(JSVersion)
531 JS_StringToVersion(const char *string);
532
533 /*
534 * JS options are orthogonal to version, and may be freely composed with one
535 * another as well as with version.
536 *
537 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
538 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
539 */
540 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
541 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
542 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
543 the last object on its 'obj'
544 param's scope chain as the
545 ECMA 'variables object' */
546 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
547 JS_BIT(3) /* context private data points
548 to an nsISupports subclass */
549 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
550 promises to execute compiled
551 script once only; enables
552 compile-time scope chain
553 resolution of consts. */
554 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
555 option supported for the
556 XUL preprocessor and kindred
557 beasts. */
558 #define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
559 parse <!-- --> as a token,
560 not backward compatible with
561 the comment-hiding hack used
562 in HTML script tags. */
563 #define JSOPTION_NATIVE_BRANCH_CALLBACK \
564 JS_BIT(7) /* the branch callback set by
565 JS_SetBranchCallback may be
566 called with a null script
567 parameter, by native code
568 that loops intensively.
569 Deprecated, use
570 JS_SetOperationCallback
571 instead */
572 #define JSOPTION_DONT_REPORT_UNCAUGHT \
573 JS_BIT(8) /* When returning from the
574 outermost API call, prevent
575 uncaught exceptions from
576 being converted to error
577 reports */
578
579 #define JSOPTION_RELIMIT JS_BIT(9) /* Throw exception on any
580 regular expression which
581 backtracks more than n^3
582 times, where n is length
583 of the input string */
584 #define JSOPTION_ANONFUNFIX JS_BIT(10) /* Disallow function () {} in
585 statement context per
586 ECMA-262 Edition 3. */
587
588 #define JSOPTION_JIT JS_BIT(11) /* Enable JIT compilation. */
589
590 #define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
591 that a null rval out-param
592 will be passed to each call
593 to JS_ExecuteScript. */
594
595 extern JS_PUBLIC_API(uint32)
596 JS_GetOptions(JSContext *cx);
597
598 extern JS_PUBLIC_API(uint32)
599 JS_SetOptions(JSContext *cx, uint32 options);
600
601 extern JS_PUBLIC_API(uint32)
602 JS_ToggleOptions(JSContext *cx, uint32 options);
603
604 extern JS_PUBLIC_API(const char *)
605 JS_GetImplementationVersion(void);
606
607 extern JS_PUBLIC_API(JSObject *)
608 JS_GetGlobalObject(JSContext *cx);
609
610 extern JS_PUBLIC_API(void)
611 JS_SetGlobalObject(JSContext *cx, JSObject *obj);
612
613 /*
614 * Initialize standard JS class constructors, prototypes, and any top-level
615 * functions and constants associated with the standard classes (e.g. isNaN
616 * for Number).
617 *
618 * NB: This sets cx's global object to obj if it was null.
619 */
620 extern JS_PUBLIC_API(JSBool)
621 JS_InitStandardClasses(JSContext *cx, JSObject *obj);
622
623 /*
624 * Resolve id, which must contain either a string or an int, to a standard
625 * class name in obj if possible, defining the class's constructor and/or
626 * prototype and storing true in *resolved. If id does not name a standard
627 * class or a top-level property induced by initializing a standard class,
628 * store false in *resolved and just return true. Return false on error,
629 * as usual for JSBool result-typed API entry points.
630 *
631 * This API can be called directly from a global object class's resolve op,
632 * to define standard classes lazily. The class's enumerate op should call
633 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
634 * loops any classes not yet resolved lazily.
635 */
636 extern JS_PUBLIC_API(JSBool)
637 JS_ResolveStandardClass(JSContext *cx, JSObject *obj, jsval id,
638 JSBool *resolved);
639
640 extern JS_PUBLIC_API(JSBool)
641 JS_EnumerateStandardClasses(JSContext *cx, JSObject *obj);
642
643 /*
644 * Enumerate any already-resolved standard class ids into ida, or into a new
645 * JSIdArray if ida is null. Return the augmented array on success, null on
646 * failure with ida (if it was non-null on entry) destroyed.
647 */
648 extern JS_PUBLIC_API(JSIdArray *)
649 JS_EnumerateResolvedStandardClasses(JSContext *cx, JSObject *obj,
650 JSIdArray *ida);
651
652 extern JS_PUBLIC_API(JSBool)
653 JS_GetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
654 JSObject **objp);
655
656 extern JS_PUBLIC_API(JSObject *)
657 JS_GetScopeChain(JSContext *cx);
658
659 extern JS_PUBLIC_API(JSObject *)
660 JS_GetGlobalForObject(JSContext *cx, JSObject *obj);
661
662 /*
663 * Macros to hide interpreter stack layout details from a JSFastNative using
664 * its jsval *vp parameter. The stack layout underlying invocation can't change
665 * without breaking source and binary compatibility (argv[-2] is well-known to
666 * be the callee jsval, and argv[-1] is as well known to be |this|).
667 *
668 * Note well: However, argv[-1] may be JSVAL_NULL where with slow natives it
669 * is the global object, so embeddings implementing fast natives *must* call
670 * JS_THIS or JS_THIS_OBJECT and test for failure indicated by a null return,
671 * which should propagate as a false return from native functions and hooks.
672 *
673 * To reduce boilerplace checks, JS_InstanceOf and JS_GetInstancePrivate now
674 * handle a null obj parameter by returning false (throwing a TypeError if
675 * given non-null argv), so most native functions that type-check their |this|
676 * parameter need not add null checking.
677 *
678 * NB: there is an anti-dependency between JS_CALLEE and JS_SET_RVAL: native
679 * methods that may inspect their callee must defer setting their return value
680 * until after any such possible inspection. Otherwise the return value will be
681 * inspected instead of the callee function object.
682 *
683 * WARNING: These are not (yet) mandatory macros, but new code outside of the
684 * engine should use them. In the Mozilla 2.0 milestone their definitions may
685 * change incompatibly.
686 */
687 #define JS_CALLEE(cx,vp) ((vp)[0])
688 #define JS_ARGV_CALLEE(argv) ((argv)[-2])
689 #define JS_THIS(cx,vp) JS_ComputeThis(cx, vp)
690 #define JS_THIS_OBJECT(cx,vp) ((JSObject *) JS_THIS(cx,vp))
691 #define JS_ARGV(cx,vp) ((vp) + 2)
692 #define JS_RVAL(cx,vp) (*(vp))
693 #define JS_SET_RVAL(cx,vp,v) (*(vp) = (v))
694
695 extern JS_PUBLIC_API(jsval)
696 JS_ComputeThis(JSContext *cx, jsval *vp);
697
698 extern JS_PUBLIC_API(void *)
699 JS_malloc(JSContext *cx, size_t nbytes);
700
701 extern JS_PUBLIC_API(void *)
702 JS_realloc(JSContext *cx, void *p, size_t nbytes);
703
704 extern JS_PUBLIC_API(void)
705 JS_free(JSContext *cx, void *p);
706
707 extern JS_PUBLIC_API(char *)
708 JS_strdup(JSContext *cx, const char *s);
709
710 extern JS_PUBLIC_API(jsdouble *)
711 JS_NewDouble(JSContext *cx, jsdouble d);
712
713 extern JS_PUBLIC_API(JSBool)
714 JS_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
715
716 extern JS_PUBLIC_API(JSBool)
717 JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
718
719 /*
720 * A JS GC root is a pointer to a JSObject *, JSString *, or jsdouble * that
721 * itself points into the GC heap (more recently, we support this extension:
722 * a root may be a pointer to a jsval v for which JSVAL_IS_GCTHING(v) is true).
723 *
724 * Therefore, you never pass JSObject *obj to JS_AddRoot(cx, obj). You always
725 * call JS_AddRoot(cx, &obj), passing obj by reference. And later, before obj
726 * or the structure it is embedded within goes out of scope or is freed, you
727 * must call JS_RemoveRoot(cx, &obj).
728 *
729 * Also, use JS_AddNamedRoot(cx, &structPtr->memberObj, "structPtr->memberObj")
730 * in preference to JS_AddRoot(cx, &structPtr->memberObj), in order to identify
731 * roots by their source callsites. This way, you can find the callsite while
732 * debugging if you should fail to do JS_RemoveRoot(cx, &structPtr->memberObj)
733 * before freeing structPtr's memory.
734 */
735 extern JS_PUBLIC_API(JSBool)
736 JS_AddRoot(JSContext *cx, void *rp);
737
738 #ifdef NAME_ALL_GC_ROOTS
739 #define JS_DEFINE_TO_TOKEN(def) #def
740 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
741 #define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
742 #endif
743
744 extern JS_PUBLIC_API(JSBool)
745 JS_AddNamedRoot(JSContext *cx, void *rp, const char *name);
746
747 extern JS_PUBLIC_API(JSBool)
748 JS_AddNamedRootRT(JSRuntime *rt, void *rp, const char *name);
749
750 extern JS_PUBLIC_API(JSBool)
751 JS_RemoveRoot(JSContext *cx, void *rp);
752
753 extern JS_PUBLIC_API(JSBool)
754 JS_RemoveRootRT(JSRuntime *rt, void *rp);
755
756 /*
757 * The last GC thing of each type (object, string, double, external string
758 * types) created on a given context is kept alive until another thing of the
759 * same type is created, using a newborn root in the context. These newborn
760 * roots help native code protect newly-created GC-things from GC invocations
761 * activated before those things can be rooted using local or global roots.
762 *
763 * However, the newborn roots can also entrain great gobs of garbage, so the
764 * JS_GC entry point clears them for the context on which GC is being forced.
765 * Embeddings may need to do likewise for all contexts.
766 *
767 * See the scoped local root API immediately below for a better way to manage
768 * newborns in cases where native hooks (functions, getters, setters, etc.)
769 * create many GC-things, potentially without connecting them to predefined
770 * local roots such as *rval or argv[i] in an active native function. Using
771 * JS_EnterLocalRootScope disables updating of the context's per-gc-thing-type
772 * newborn roots, until control flow unwinds and leaves the outermost nesting
773 * local root scope.
774 */
775 extern JS_PUBLIC_API(void)
776 JS_ClearNewbornRoots(JSContext *cx);
777
778 /*
779 * Scoped local root management allows native functions, getter/setters, etc.
780 * to avoid worrying about the newborn root pigeon-holes, overloading local
781 * roots allocated in argv and *rval, or ending up having to call JS_Add*Root
782 * and JS_RemoveRoot to manage global roots temporarily.
783 *
784 * Instead, calling JS_EnterLocalRootScope and JS_LeaveLocalRootScope around
785 * the body of the native hook causes the engine to allocate a local root for
786 * each newborn created in between the two API calls, using a local root stack
787 * associated with cx. For example:
788 *
789 * JSBool
790 * my_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
791 * {
792 * JSBool ok;
793 *
794 * if (!JS_EnterLocalRootScope(cx))
795 * return JS_FALSE;
796 * ok = my_GetPropertyBody(cx, obj, id, vp);
797 * JS_LeaveLocalRootScope(cx);
798 * return ok;
799 * }
800 *
801 * NB: JS_LeaveLocalRootScope must be called once for every prior successful
802 * call to JS_EnterLocalRootScope. If JS_EnterLocalRootScope fails, you must
803 * not make the matching JS_LeaveLocalRootScope call.
804 *
805 * JS_LeaveLocalRootScopeWithResult(cx, rval) is an alternative way to leave
806 * a local root scope that protects a result or return value, by effectively
807 * pushing it in the caller's local root scope.
808 *
809 * In case a native hook allocates many objects or other GC-things, but the
810 * native protects some of those GC-things by storing them as property values
811 * in an object that is itself protected, the hook can call JS_ForgetLocalRoot
812 * to free the local root automatically pushed for the now-protected GC-thing.
813 *
814 * JS_ForgetLocalRoot works on any GC-thing allocated in the current local
815 * root scope, but it's more time-efficient when called on references to more
816 * recently created GC-things. Calling it successively on other than the most
817 * recently allocated GC-thing will tend to average the time inefficiency, and
818 * may risk O(n^2) growth rate, but in any event, you shouldn't allocate too
819 * many local roots if you can root as you go (build a tree of objects from
820 * the top down, forgetting each latest-allocated GC-thing immediately upon
821 * linking it to its parent).
822 */
823 extern JS_PUBLIC_API(JSBool)
824 JS_EnterLocalRootScope(JSContext *cx);
825
826 extern JS_PUBLIC_API(void)
827 JS_LeaveLocalRootScope(JSContext *cx);
828
829 extern JS_PUBLIC_API(void)
830 JS_LeaveLocalRootScopeWithResult(JSContext *cx, jsval rval);
831
832 extern JS_PUBLIC_API(void)
833 JS_ForgetLocalRoot(JSContext *cx, void *thing);
834
835 #ifdef __cplusplus
836 JS_END_EXTERN_C
837
838 class JSAutoLocalRootScope {
839 public:
840 JSAutoLocalRootScope(JSContext *cx) : mContext(cx) {
841 JS_EnterLocalRootScope(mContext);
842 }
843 ~JSAutoLocalRootScope() {
844 JS_LeaveLocalRootScope(mContext);
845 }
846
847 void forget(void *thing) {
848 JS_ForgetLocalRoot(mContext, thing);
849 }
850
851 protected:
852 JSContext *mContext;
853
854 #if 0
855 private:
856 static void *operator new(size_t) CPP_THROW_NEW { return 0; };
857 static void operator delete(void *, size_t) { };
858 #endif
859 };
860
861 JS_BEGIN_EXTERN_C
862 #endif
863
864 #ifdef DEBUG
865 extern JS_PUBLIC_API(void)
866 JS_DumpNamedRoots(JSRuntime *rt,
867 void (*dump)(const char *name, void *rp, void *data),
868 void *data);
869 #endif
870
871 /*
872 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
873 * The root is pointed at by rp; if the root is unnamed, name is null; data is
874 * supplied from the third parameter to JS_MapGCRoots.
875 *
876 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
877 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
878 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
879 * constants are flags; you can OR them together.
880 *
881 * This function acquires and releases rt's GC lock around the mapping of the
882 * roots table, so the map function should run to completion in as few cycles
883 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
884 * or any JS API entry point that acquires locks, without double-tripping or
885 * deadlocking on the GC lock.
886 *
887 * JS_MapGCRoots returns the count of roots that were successfully mapped.
888 */
889 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
890 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
891 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
892
893 typedef intN
894 (* JSGCRootMapFun)(void *rp, const char *name, void *data);
895
896 extern JS_PUBLIC_API(uint32)
897 JS_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data);
898
899 extern JS_PUBLIC_API(JSBool)
900 JS_LockGCThing(JSContext *cx, void *thing);
901
902 extern JS_PUBLIC_API(JSBool)
903 JS_LockGCThingRT(JSRuntime *rt, void *thing);
904
905 extern JS_PUBLIC_API(JSBool)
906 JS_UnlockGCThing(JSContext *cx, void *thing);
907
908 extern JS_PUBLIC_API(JSBool)
909 JS_UnlockGCThingRT(JSRuntime *rt, void *thing);
910
911 /*
912 * Register externally maintained GC roots.
913 *
914 * traceOp: the trace operation. For each root the implementation should call
915 * JS_CallTracer whenever the root contains a traceable thing.
916 * data: the data argument to pass to each invocation of traceOp.
917 */
918 extern JS_PUBLIC_API(void)
919 JS_SetExtraGCRoots(JSRuntime *rt, JSTraceDataOp traceOp, void *data);
920
921 /*
922 * For implementors of JSMarkOp. All new code should implement JSTraceOp
923 * instead.
924 */
925 extern JS_PUBLIC_API(void)
926 JS_MarkGCThing(JSContext *cx, void *thing, const char *name, void *arg);
927
928 /*
929 * JS_CallTracer API and related macros for implementors of JSTraceOp, to
930 * enumerate all references to traceable things reachable via a property or
931 * other strong ref identified for debugging purposes by name or index or
932 * a naming callback.
933 *
934 * By definition references to traceable things include non-null pointers
935 * to JSObject, JSString and jsdouble and corresponding jsvals.
936 *
937 * See the JSTraceOp typedef in jspubtd.h.
938 */
939
940 /* Trace kinds to pass to JS_Tracing. */
941 #define JSTRACE_OBJECT 0
942 #define JSTRACE_DOUBLE 1
943 #define JSTRACE_STRING 2
944
945 /*
946 * Use the following macros to check if a particular jsval is a traceable
947 * thing and to extract the thing and its kind to pass to JS_CallTracer.
948 */
949 #define JSVAL_IS_TRACEABLE(v) (JSVAL_IS_GCTHING(v) && !JSVAL_IS_NULL(v))
950 #define JSVAL_TO_TRACEABLE(v) (JSVAL_TO_GCTHING(v))
951 #define JSVAL_TRACE_KIND(v) (JSVAL_TAG(v) >> 1)
952
953 JS_STATIC_ASSERT(JSVAL_TRACE_KIND(JSVAL_OBJECT) == JSTRACE_OBJECT);
954 JS_STATIC_ASSERT(JSVAL_TRACE_KIND(JSVAL_DOUBLE) == JSTRACE_DOUBLE);
955 JS_STATIC_ASSERT(JSVAL_TRACE_KIND(JSVAL_STRING) == JSTRACE_STRING);
956
957 struct JSTracer {
958 JSContext *context;
959 JSTraceCallback callback;
960 #ifdef DEBUG
961 JSTraceNamePrinter debugPrinter;
962 const void *debugPrintArg;
963 size_t debugPrintIndex;
964 #endif
965 };
966
967 /*
968 * The method to call on each reference to a traceable thing stored in a
969 * particular JSObject or other runtime structure. With DEBUG defined the
970 * caller before calling JS_CallTracer must initialize JSTracer fields
971 * describing the reference using the macros below.
972 */
973 extern JS_PUBLIC_API(void)
974 JS_CallTracer(JSTracer *trc, void *thing, uint32 kind);
975
976 /*
977 * Set debugging information about a reference to a traceable thing to prepare
978 * for the following call to JS_CallTracer.
979 *
980 * When printer is null, arg must be const char * or char * C string naming
981 * the reference and index must be either (size_t)-1 indicating that the name
982 * alone describes the reference or it must be an index into some array vector
983 * that stores the reference.
984 *
985 * When printer callback is not null, the arg and index arguments are
986 * available to the callback as debugPrinterArg and debugPrintIndex fields
987 * of JSTracer.
988 *
989 * The storage for name or callback's arguments needs to live only until
990 * the following call to JS_CallTracer returns.
991 */
992 #ifdef DEBUG
993 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
994 JS_BEGIN_MACRO \
995 (trc)->debugPrinter = (printer); \
996 (trc)->debugPrintArg = (arg); \
997 (trc)->debugPrintIndex = (index); \
998 JS_END_MACRO
999 #else
1000 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1001 JS_BEGIN_MACRO \
1002 JS_END_MACRO
1003 #endif
1004
1005 /*
1006 * Convenience macro to describe the argument of JS_CallTracer using C string
1007 * and index.
1008 */
1009 # define JS_SET_TRACING_INDEX(trc, name, index) \
1010 JS_SET_TRACING_DETAILS(trc, NULL, name, index)
1011
1012 /*
1013 * Convenience macro to describe the argument of JS_CallTracer using C string.
1014 */
1015 # define JS_SET_TRACING_NAME(trc, name) \
1016 JS_SET_TRACING_DETAILS(trc, NULL, name, (size_t)-1)
1017
1018 /*
1019 * Convenience macro to invoke JS_CallTracer using C string as the name for
1020 * the reference to a traceable thing.
1021 */
1022 # define JS_CALL_TRACER(trc, thing, kind, name) \
1023 JS_BEGIN_MACRO \
1024 JS_SET_TRACING_NAME(trc, name); \
1025 JS_CallTracer((trc), (thing), (kind)); \
1026 JS_END_MACRO
1027
1028 /*
1029 * Convenience macros to invoke JS_CallTracer when jsval represents a
1030 * reference to a traceable thing.
1031 */
1032 #define JS_CALL_VALUE_TRACER(trc, val, name) \
1033 JS_BEGIN_MACRO \
1034 if (JSVAL_IS_TRACEABLE(val)) { \
1035 JS_CALL_TRACER((trc), JSVAL_TO_GCTHING(val), \
1036 JSVAL_TRACE_KIND(val), name); \
1037 } \
1038 JS_END_MACRO
1039
1040 #define JS_CALL_OBJECT_TRACER(trc, object, name) \
1041 JS_BEGIN_MACRO \
1042 JSObject *obj_ = (object); \
1043 JS_ASSERT(obj_); \
1044 JS_CALL_TRACER((trc), obj_, JSTRACE_OBJECT, name); \
1045 JS_END_MACRO
1046
1047 #define JS_CALL_STRING_TRACER(trc, string, name) \
1048 JS_BEGIN_MACRO \
1049 JSString *str_ = (string); \
1050 JS_ASSERT(str_); \
1051 JS_CALL_TRACER((trc), str_, JSTRACE_STRING, name); \
1052 JS_END_MACRO
1053
1054 #define JS_CALL_DOUBLE_TRACER(trc, number, name) \
1055 JS_BEGIN_MACRO \
1056 jsdouble *num_ = (number); \
1057 JS_ASSERT(num_); \
1058 JS_CALL_TRACER((trc), num_, JSTRACE_DOUBLE, name); \
1059 JS_END_MACRO
1060
1061 /*
1062 * API for JSTraceCallback implementations.
1063 */
1064 # define JS_TRACER_INIT(trc, cx_, callback_) \
1065 JS_BEGIN_MACRO \
1066 (trc)->context = (cx_); \
1067 (trc)->callback = (callback_); \
1068 JS_SET_TRACING_DETAILS(trc, NULL, NULL, (size_t)-1); \
1069 JS_END_MACRO
1070
1071 extern JS_PUBLIC_API(void)
1072 JS_TraceChildren(JSTracer *trc, void *thing, uint32 kind);
1073
1074 extern JS_PUBLIC_API(void)
1075 JS_TraceRuntime(JSTracer *trc);
1076
1077 #ifdef DEBUG
1078
1079 extern JS_PUBLIC_API(void)
1080 JS_PrintTraceThingInfo(char *buf, size_t bufsize, JSTracer *trc,
1081 void *thing, uint32 kind, JSBool includeDetails);
1082
1083 /*
1084 * DEBUG-only method to dump the object graph of heap-allocated things.
1085 *
1086 * fp: file for the dump output.
1087 * start: when non-null, dump only things reachable from start
1088 * thing. Otherwise dump all things reachable from the
1089 * runtime roots.
1090 * startKind: trace kind of start if start is not null. Must be 0 when
1091 * start is null.
1092 * thingToFind: dump only paths in the object graph leading to thingToFind
1093 * when non-null.
1094 * maxDepth: the upper bound on the number of edges to descend from the
1095 * graph roots.
1096 * thingToIgnore: thing to ignore during the graph traversal when non-null.
1097 */
1098 extern JS_PUBLIC_API(JSBool)
1099 JS_DumpHeap(JSContext *cx, FILE *fp, void* startThing, uint32 startKind,
1100 void *thingToFind, size_t maxDepth, void *thingToIgnore);
1101
1102 #endif
1103
1104 /*
1105 * Garbage collector API.
1106 */
1107 extern JS_PUBLIC_API(void)
1108 JS_GC(JSContext *cx);
1109
1110 extern JS_PUBLIC_API(void)
1111 JS_MaybeGC(JSContext *cx);
1112
1113 extern JS_PUBLIC_API(JSGCCallback)
1114 JS_SetGCCallback(JSContext *cx, JSGCCallback cb);
1115
1116 extern JS_PUBLIC_API(JSGCCallback)
1117 JS_SetGCCallbackRT(JSRuntime *rt, JSGCCallback cb);
1118
1119 extern JS_PUBLIC_API(JSBool)
1120 JS_IsGCMarkingTracer(JSTracer *trc);
1121
1122 extern JS_PUBLIC_API(JSBool)
1123 JS_IsAboutToBeFinalized(JSContext *cx, void *thing);
1124
1125 typedef enum JSGCParamKey {
1126 /* Maximum nominal heap before last ditch GC. */
1127 JSGC_MAX_BYTES = 0,
1128
1129 /* Number of JS_malloc bytes before last ditch GC. */
1130 JSGC_MAX_MALLOC_BYTES = 1,
1131
1132 /* Hoard stackPools for this long, in ms, default is 30 seconds. */
1133 JSGC_STACKPOOL_LIFESPAN = 2
1134 } JSGCParamKey;
1135
1136 extern JS_PUBLIC_API(void)
1137 JS_SetGCParameter(JSRuntime *rt, JSGCParamKey key, uint32 value);
1138
1139 /*
1140 * Add a finalizer for external strings created by JS_NewExternalString (see
1141 * below) using a type-code returned from this function, and that understands
1142 * how to free or release the memory pointed at by JS_GetStringChars(str).
1143 *
1144 * Return a nonnegative type index if there is room for finalizer in the
1145 * global GC finalizers table, else return -1. If the engine is compiled
1146 * JS_THREADSAFE and used in a multi-threaded environment, this function must
1147 * be invoked on the primordial thread only, at startup -- or else the entire
1148 * program must single-thread itself while loading a module that calls this
1149 * function.
1150 */
1151 extern JS_PUBLIC_API(intN)
1152 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer);
1153
1154 /*
1155 * Remove finalizer from the global GC finalizers table, returning its type
1156 * code if found, -1 if not found.
1157 *
1158 * As with JS_AddExternalStringFinalizer, there is a threading restriction
1159 * if you compile the engine JS_THREADSAFE: this function may be called for a
1160 * given finalizer pointer on only one thread; different threads may call to
1161 * remove distinct finalizers safely.
1162 *
1163 * You must ensure that all strings with finalizer's type have been collected
1164 * before calling this function. Otherwise, string data will be leaked by the
1165 * GC, for want of a finalizer to call.
1166 */
1167 extern JS_PUBLIC_API(intN)
1168 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer);
1169
1170 /*
1171 * Create a new JSString whose chars member refers to external memory, i.e.,
1172 * memory requiring special, type-specific finalization. The type code must
1173 * be a nonnegative return value from JS_AddExternalStringFinalizer.
1174 */
1175 extern JS_PUBLIC_API(JSString *)
1176 JS_NewExternalString(JSContext *cx, jschar *chars, size_t length, intN type);
1177
1178 /*
1179 * Returns the external-string finalizer index for this string, or -1 if it is
1180 * an "internal" (native to JS engine) string.
1181 */
1182 extern JS_PUBLIC_API(intN)
1183 JS_GetExternalStringGCType(JSRuntime *rt, JSString *str);
1184
1185 /*
1186 * Sets maximum (if stack grows upward) or minimum (downward) legal stack byte
1187 * address in limitAddr for the thread or process stack used by cx. To disable
1188 * stack size checking, pass 0 for limitAddr.
1189 */
1190 extern JS_PUBLIC_API(void)
1191 JS_SetThreadStackLimit(JSContext *cx, jsuword limitAddr);
1192
1193 /*
1194 * Set the quota on the number of bytes that stack-like data structures can
1195 * use when the runtime compiles and executes scripts. These structures
1196 * consume heap space, so JS_SetThreadStackLimit does not bound their size.
1197 * The default quota is 32MB which is quite generous.
1198 *
1199 * The function must be called before any script compilation or execution API
1200 * calls, i.e. either immediately after JS_NewContext or from JSCONTEXT_NEW
1201 * context callback.
1202 */
1203 extern JS_PUBLIC_API(void)
1204 JS_SetScriptStackQuota(JSContext *cx, size_t quota);
1205
1206 #define JS_DEFAULT_SCRIPT_STACK_QUOTA ((size_t) 0x2000000)
1207
1208 /************************************************************************/
1209
1210 /*
1211 * Classes, objects, and properties.
1212 */
1213
1214 /* For detailed comments on the function pointer types, see jspubtd.h. */
1215 struct JSClass {
1216 const char *name;
1217 uint32 flags;
1218
1219 /* Mandatory non-null function pointer members. */
1220 JSPropertyOp addProperty;
1221 JSPropertyOp delProperty;
1222 JSPropertyOp getProperty;
1223 JSPropertyOp setProperty;
1224 JSEnumerateOp enumerate;
1225 JSResolveOp resolve;
1226 JSConvertOp convert;
1227 JSFinalizeOp finalize;
1228
1229 /* Optionally non-null members start here. */
1230 JSGetObjectOps getObjectOps;
1231 JSCheckAccessOp checkAccess;
1232 JSNative call;
1233 JSNative construct;
1234 JSXDRObjectOp xdrObject;
1235 JSHasInstanceOp hasInstance;
1236 JSMarkOp mark;
1237 JSReserveSlotsOp reserveSlots;
1238 };
1239
1240 struct JSExtendedClass {
1241 JSClass base;
1242 JSEqualityOp equality;
1243 JSObjectOp outerObject;
1244 JSObjectOp innerObject;
1245 JSIteratorOp iteratorObject;
1246 JSObjectOp wrappedObject; /* NB: infallible, null
1247 returns are treated as
1248 the original object */
1249 void (*reserved0)(void);
1250 void (*reserved1)(void);
1251 void (*reserved2)(void);
1252 };
1253
1254 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
1255 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
1256 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
1257 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
1258 #define JSCLASS_SHARE_ALL_PROPERTIES (1<<4) /* all properties are SHARED */
1259 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
1260 object in prototype chain
1261 passed in via *objp in/out
1262 parameter */
1263 #define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
1264 prototype */
1265 #define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
1266
1267 /*
1268 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
1269 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
1270 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
1271 */
1272 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
1273 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
1274 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
1275 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
1276 << JSCLASS_RESERVED_SLOTS_SHIFT)
1277 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
1278 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
1279 & JSCLASS_RESERVED_SLOTS_MASK)
1280
1281 #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
1282 JSCLASS_RESERVED_SLOTS_WIDTH)
1283
1284 /* True if JSClass is really a JSExtendedClass. */
1285 #define JSCLASS_IS_EXTENDED (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
1286 #define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1))
1287 #define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2))
1288
1289 /* Indicates that JSClass.mark is a tracer with JSTraceOp type. */
1290 #define JSCLASS_MARK_IS_TRACE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3))
1291
1292 /*
1293 * ECMA-262 requires that most constructors used internally create objects
1294 * with "the original Foo.prototype value" as their [[Prototype]] (__proto__)
1295 * member initial value. The "original ... value" verbiage is there because
1296 * in ECMA-262, global properties naming class objects are read/write and
1297 * deleteable, for the most part.
1298 *
1299 * Implementing this efficiently requires that global objects have classes
1300 * with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS won't break
1301 * anything except the ECMA-262 "original prototype value" behavior, which was
1302 * broken for years in SpiderMonkey. In other words, without these flags you
1303 * get backward compatibility.
1304 */
1305 #define JSCLASS_GLOBAL_FLAGS \
1306 (JSCLASS_IS_GLOBAL | JSCLASS_HAS_RESERVED_SLOTS(JSProto_LIMIT))
1307
1308 /* Fast access to the original value of each standard class's prototype. */
1309 #define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 8)
1310 #define JSCLASS_CACHED_PROTO_WIDTH 8
1311 #define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH)
1312 #define JSCLASS_HAS_CACHED_PROTO(key) ((key) << JSCLASS_CACHED_PROTO_SHIFT)
1313 #define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \
1314 (((clasp)->flags \
1315 >> JSCLASS_CACHED_PROTO_SHIFT) \
1316 & JSCLASS_CACHED_PROTO_MASK))
1317
1318 /* Initializer for unused members of statically initialized JSClass structs. */
1319 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,0
1320 #define JSCLASS_NO_RESERVED_MEMBERS 0,0,0
1321
1322 /* For detailed comments on these function pointer types, see jspubtd.h. */
1323 struct JSObjectOps {
1324 /* Mandatory non-null function pointer members. */
1325 JSNewObjectMapOp newObjectMap;
1326 JSObjectMapOp destroyObjectMap;
1327 JSLookupPropOp lookupProperty;
1328 JSDefinePropOp defineProperty;
1329 JSPropertyIdOp getProperty;
1330 JSPropertyIdOp setProperty;
1331 JSAttributesOp getAttributes;
1332 JSAttributesOp setAttributes;
1333 JSPropertyIdOp deleteProperty;
1334 JSConvertOp defaultValue;
1335 JSNewEnumerateOp enumerate;
1336 JSCheckAccessIdOp checkAccess;
1337
1338 /* Optionally non-null members start here. */
1339 JSObjectOp thisObject;
1340 JSPropertyRefOp dropProperty;
1341 JSNative call;
1342 JSNative construct;
1343 JSXDRObjectOp xdrObject;
1344 JSHasInstanceOp hasInstance;
1345 JSSetObjectSlotOp setProto;
1346 JSSetObjectSlotOp setParent;
1347 JSTraceOp trace;
1348 JSFinalizeOp clear;
1349 JSGetRequiredSlotOp getRequiredSlot;
1350 JSSetRequiredSlotOp setRequiredSlot;
1351 };
1352
1353 struct JSXMLObjectOps {
1354 JSObjectOps base;
1355 JSGetMethodOp getMethod;
1356 JSSetMethodOp setMethod;
1357 JSEnumerateValuesOp enumerateValues;
1358 JSEqualityOp equality;
1359 JSConcatenateOp concatenate;
1360 };
1361
1362 /*
1363 * Classes that expose JSObjectOps via a non-null getObjectOps class hook may
1364 * derive a property structure from this struct, return a pointer to it from
1365 * lookupProperty and defineProperty, and use the pointer to avoid rehashing
1366 * in getAttributes and setAttributes.
1367 *
1368 * The jsid type contains either an int jsval (see JSVAL_IS_INT above), or an
1369 * internal pointer that is opaque to users of this API, but which users may
1370 * convert from and to a jsval using JS_ValueToId and JS_IdToValue.
1371 */
1372 struct JSProperty {
1373 jsid id;
1374 };
1375
1376 struct JSIdArray {
1377 jsint length;
1378 jsid vector[1]; /* actually, length jsid words */
1379 };
1380
1381 extern JS_PUBLIC_API(void)
1382 JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
1383
1384 extern JS_PUBLIC_API(JSBool)
1385 JS_ValueToId(JSContext *cx, jsval v, jsid *idp);
1386
1387 extern JS_PUBLIC_API(JSBool)
1388 JS_IdToValue(JSContext *cx, jsid id, jsval *vp);
1389
1390 /*
1391 * The magic XML namespace id is int-tagged, but not a valid integer jsval.
1392 * Global object classes in embeddings that enable JS_HAS_XML_SUPPORT (E4X)
1393 * should handle this id specially before converting id via JSVAL_TO_INT.
1394 */
1395 #define JS_DEFAULT_XML_NAMESPACE_ID ((jsid) JSVAL_VOID)
1396
1397 /*
1398 * JSNewResolveOp flag bits.
1399 */
1400 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
1401 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
1402 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
1403 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
1404 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
1405
1406 extern JS_PUBLIC_API(JSBool)
1407 JS_PropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
1408
1409 extern JS_PUBLIC_API(JSBool)
1410 JS_EnumerateStub(JSContext *cx, JSObject *obj);
1411
1412 extern JS_PUBLIC_API(JSBool)
1413 JS_ResolveStub(JSContext *cx, JSObject *obj, jsval id);
1414
1415 extern JS_PUBLIC_API(JSBool)
1416 JS_ConvertStub(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
1417
1418 extern JS_PUBLIC_API(void)
1419 JS_FinalizeStub(JSContext *cx, JSObject *obj);
1420
1421 struct JSConstDoubleSpec {
1422 jsdouble dval;
1423 const char *name;
1424 uint8 flags;
1425 uint8 spare[3];
1426 };
1427
1428 /*
1429 * To define an array element rather than a named property member, cast the
1430 * element's index to (const char *) and initialize name with it, and set the
1431 * JSPROP_INDEX bit in flags.
1432 */
1433 struct JSPropertySpec {
1434 const char *name;
1435 int8 tinyid;
1436 uint8 flags;
1437 JSPropertyOp getter;
1438 JSPropertyOp setter;
1439 };
1440
1441 struct JSFunctionSpec {
1442 const char *name;
1443 JSNative call;
1444 uint16 nargs;
1445 uint16 flags;
1446
1447 /*
1448 * extra & 0xFFFF: Number of extra argument slots for local GC roots.
1449 * If fast native, must be zero.
1450 * extra >> 16: If slow native, reserved for future use (must be 0).
1451 * If fast native, minimum required argc.
1452 */
1453 uint32 extra;
1454 };
1455
1456 /*
1457 * Terminating sentinel initializer to put at the end of a JSFunctionSpec array
1458 * that's passed to JS_DefineFunctions or JS_InitClass.
1459 */
1460 #define JS_FS_END JS_FS(NULL,NULL,0,0,0)
1461
1462 /*
1463 * Initializer macro for a JSFunctionSpec array element. This is the original
1464 * kind of native function specifier initializer. Use JS_FN ("fast native", see
1465 * JSFastNative in jspubtd.h) for all functions that do not need a stack frame
1466 * when activated.
1467 */
1468 #define JS_FS(name,call,nargs,flags,extra) \
1469 {name, call, nargs, flags, extra}
1470
1471 /*
1472 * "Fast native" initializer macro for a JSFunctionSpec array element. Use this
1473 * in preference to JS_FS if the native in question does not need its own stack
1474 * frame when activated.
1475 */
1476 #define JS_FN(name,fastcall,nargs,flags) \
1477 {name, (JSNative)(fastcall), nargs, \
1478 (flags) | JSFUN_FAST_NATIVE | JSFUN_STUB_GSOPS, 0}
1479
1480 extern JS_PUBLIC_API(JSObject *)
1481 JS_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
1482 JSClass *clasp, JSNative constructor, uintN nargs,
1483 JSPropertySpec *ps, JSFunctionSpec *fs,
1484 JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
1485
1486 #ifdef JS_THREADSAFE
1487 extern JS_PUBLIC_API(JSClass *)
1488 JS_GetClass(JSContext *cx, JSObject *obj);
1489
1490 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
1491 #else
1492 extern JS_PUBLIC_API(JSClass *)
1493 JS_GetClass(JSObject *obj);
1494
1495 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
1496 #endif
1497
1498 extern JS_PUBLIC_API(JSBool)
1499 JS_InstanceOf(JSContext *cx, JSObject *obj, JSClass *clasp, jsval *argv);
1500
1501 extern JS_PUBLIC_API(JSBool)
1502 JS_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
1503
1504 extern JS_PUBLIC_API(void *)
1505 JS_GetPrivate(JSContext *cx, JSObject *obj);
1506
1507 extern JS_PUBLIC_API(JSBool)
1508 JS_SetPrivate(JSContext *cx, JSObject *obj, void *data);
1509
1510 extern JS_PUBLIC_API(void *)
1511 JS_GetInstancePrivate(JSContext *cx, JSObject *obj, JSClass *clasp,
1512 jsval *argv);
1513
1514 extern JS_PUBLIC_API(JSObject *)
1515 JS_GetPrototype(JSContext *cx, JSObject *obj);
1516
1517 extern JS_PUBLIC_API(JSBool)
1518 JS_SetPrototype(JSContext *cx, JSObject *obj, JSObject *proto);
1519
1520 extern JS_PUBLIC_API(JSObject *)
1521 JS_GetParent(JSContext *cx, JSObject *obj);
1522
1523 extern JS_PUBLIC_API(JSBool)
1524 JS_SetParent(JSContext *cx, JSObject *obj, JSObject *parent);
1525
1526 extern JS_PUBLIC_API(JSObject *)
1527 JS_GetConstructor(JSContext *cx, JSObject *proto);
1528
1529 /*
1530 * Get a unique identifier for obj, good for the lifetime of obj (even if it
1531 * is moved by a copying GC). Return false on failure (likely out of memory),
1532 * and true with *idp containing the unique id on success.
1533 */
1534 extern JS_PUBLIC_API(JSBool)
1535 JS_GetObjectId(JSContext *cx, JSObject *obj, jsid *idp);
1536
1537 extern JS_PUBLIC_API(JSObject *)
1538 JS_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent);
1539
1540 /*
1541 * Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
1542 * proto if proto's actual parameter value is null.
1543 */
1544 extern JS_PUBLIC_API(JSObject *)
1545 JS_NewObjectWithGivenProto(JSContext *cx, JSClass *clasp, JSObject *proto,
1546 JSObject *parent);
1547
1548 extern JS_PUBLIC_API(JSBool)
1549 JS_SealObject(JSContext *cx, JSObject *obj, JSBool deep);
1550
1551 extern JS_PUBLIC_API(JSObject *)
1552 JS_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
1553 JSObject *parent);
1554
1555 extern JS_PUBLIC_API(JSObject *)
1556 JS_ConstructObjectWithArguments(JSContext *cx, JSClass *clasp, JSObject *proto,
1557 JSObject *parent, uintN argc, jsval *argv);
1558
1559 extern JS_PUBLIC_API(JSObject *)
1560 JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, JSClass *clasp,
1561 JSObject *proto, uintN attrs);
1562
1563 extern JS_PUBLIC_API(JSBool)
1564 JS_DefineConstDoubles(JSContext *cx, JSObject *obj, JSConstDoubleSpec *cds);
1565
1566 extern JS_PUBLIC_API(JSBool)
1567 JS_DefineProperties(JSContext *cx, JSObject *obj, JSPropertySpec *ps);
1568
1569 extern JS_PUBLIC_API(JSBool)
1570 JS_DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
1571 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
1572
1573 /*
1574 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1575 *
1576 * If the object does not have a property by that name, *foundp will be
1577 * JS_FALSE and the value of *attrsp is undefined.
1578 */
1579 extern JS_PUBLIC_API(JSBool)
1580 JS_GetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
1581 uintN *attrsp, JSBool *foundp);
1582
1583 /*
1584 * The same, but if the property is native, return its getter and setter via
1585 * *getterp and *setterp, respectively (and only if the out parameter pointer
1586 * is not null).
1587 */
1588 extern JS_PUBLIC_API(JSBool)
1589 JS_GetPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
1590 const char *name,
1591 uintN *attrsp, JSBool *foundp,
1592 JSPropertyOp *getterp,
1593 JSPropertyOp *setterp);
1594
1595 /*
1596 * Set the attributes of a property on a given object.
1597 *
1598 * If the object does not have a property by that name, *foundp will be
1599 * JS_FALSE and nothing will be altered.
1600 */
1601 extern JS_PUBLIC_API(JSBool)
1602 JS_SetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
1603 uintN attrs, JSBool *foundp);
1604
1605 extern JS_PUBLIC_API(JSBool)
1606 JS_DefinePropertyWithTinyId(JSContext *cx, JSObject *obj, const char *name,
1607 int8 tinyid, jsval value,
1608 JSPropertyOp getter, JSPropertyOp setter,
1609 uintN attrs);
1610
1611 extern JS_PUBLIC_API(JSBool)
1612 JS_AliasProperty(JSContext *cx, JSObject *obj, const char *name,
1613 const char *alias);
1614
1615 extern JS_PUBLIC_API(JSBool)
1616 JS_AlreadyHasOwnProperty(JSContext *cx, JSObject *obj, const char *name,
1617 JSBool *foundp);
1618
1619 extern JS_PUBLIC_API(JSBool)
1620 JS_HasProperty(JSContext *cx, JSObject *obj, const char *name, JSBool *foundp);
1621
1622 extern JS_PUBLIC_API(JSBool)
1623 JS_LookupProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1624
1625 extern JS_PUBLIC_API(JSBool)
1626 JS_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, const char *name,
1627 uintN flags, jsval *vp);
1628
1629 extern JS_PUBLIC_API(JSBool)
1630 JS_LookupPropertyByIdWithFlags(JSContext *cx, JSObject *obj, jsid id,
1631 uintN flags, JSObject **objp, jsval *vp);
1632
1633 extern JS_PUBLIC_API(JSBool)
1634 JS_GetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1635
1636 extern JS_PUBLIC_API(JSBool)
1637 JS_GetMethodById(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
1638 jsval *vp);
1639
1640 extern JS_PUBLIC_API(JSBool)
1641 JS_GetMethod(JSContext *cx, JSObject *obj, const char *name, JSObject **objp,
1642 jsval *vp);
1643
1644 extern JS_PUBLIC_API(JSBool)
1645 JS_SetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
1646
1647 extern JS_PUBLIC_API(JSBool)
1648 JS_DeleteProperty(JSContext *cx, JSObject *obj, const char *name);
1649
1650 extern JS_PUBLIC_API(JSBool)
1651 JS_DeleteProperty2(JSContext *cx, JSObject *obj, const char *name,
1652 jsval *rval);
1653
1654 extern JS_PUBLIC_API(JSBool)
1655 JS_DefineUCProperty(JSContext *cx, JSObject *obj,
1656 const jschar *name, size_t namelen, jsval value,
1657 JSPropertyOp getter, JSPropertyOp setter,
1658 uintN attrs);
1659
1660 /*
1661 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1662 *
1663 * If the object does not have a property by that name, *foundp will be
1664 * JS_FALSE and the value of *attrsp is undefined.
1665 */
1666 extern JS_PUBLIC_API(JSBool)
1667 JS_GetUCPropertyAttributes(JSContext *cx, JSObject *obj,
1668 const jschar *name, size_t namelen,
1669 uintN *attrsp, JSBool *foundp);
1670
1671 /*
1672 * The same, but if the property is native, return its getter and setter via
1673 * *getterp and *setterp, respectively (and only if the out parameter pointer
1674 * is not null).
1675 */
1676 extern JS_PUBLIC_API(JSBool)
1677 JS_GetUCPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
1678 const jschar *name, size_t namelen,
1679 uintN *attrsp, JSBool *foundp,
1680 JSPropertyOp *getterp,
1681 JSPropertyOp *setterp);
1682
1683 /*
1684 * Set the attributes of a property on a given object.
1685 *
1686 * If the object does not have a property by that name, *foundp will be
1687 * JS_FALSE and nothing will be altered.
1688 */
1689 extern JS_PUBLIC_API(JSBool)
1690 JS_SetUCPropertyAttributes(JSContext *cx, JSObject *obj,
1691 const jschar *name, size_t namelen,
1692 uintN attrs, JSBool *foundp);
1693
1694
1695 extern JS_PUBLIC_API(JSBool)
1696 JS_DefineUCPropertyWithTinyId(JSContext *cx, JSObject *obj,
1697 const jschar *name, size_t namelen,
1698 int8 tinyid, jsval value,
1699 JSPropertyOp getter, JSPropertyOp setter,
1700 uintN attrs);
1701
1702 extern JS_PUBLIC_API(JSBool)
1703 JS_AlreadyHasOwnUCProperty(JSContext *cx, JSObject *obj, const jschar *name,
1704 size_t namelen, JSBool *foundp);
1705
1706 extern JS_PUBLIC_API(JSBool)
1707 JS_HasUCProperty(JSContext *cx, JSObject *obj,
1708 const jschar *name, size_t namelen,
1709 JSBool *vp);
1710
1711 extern JS_PUBLIC_API(JSBool)
1712 JS_LookupUCProperty(JSContext *cx, JSObject *obj,
1713 const jschar *name, size_t namelen,
1714 jsval *vp);
1715
1716 extern JS_PUBLIC_API(JSBool)
1717 JS_GetUCProperty(JSContext *cx, JSObject *obj,
1718 const jschar *name, size_t namelen,
1719 jsval *vp);
1720
1721 extern JS_PUBLIC_API(JSBool)
1722 JS_SetUCProperty(JSContext *cx, JSObject *obj,
1723 const jschar *name, size_t namelen,
1724 jsval *vp);
1725
1726 extern JS_PUBLIC_API(JSBool)
1727 JS_DeleteUCProperty2(JSContext *cx, JSObject *obj,
1728 const jschar *name, size_t namelen,
1729 jsval *rval);
1730
1731 extern JS_PUBLIC_API(JSObject *)
1732 JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector);
1733
1734 extern JS_PUBLIC_API(JSBool)
1735 JS_IsArrayObject(JSContext *cx, JSObject *obj);
1736
1737 extern JS_PUBLIC_API(JSBool)
1738 JS_GetArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1739
1740 extern JS_PUBLIC_API(JSBool)
1741 JS_SetArrayLength(JSContext *cx, JSObject *obj, jsuint length);
1742
1743 extern JS_PUBLIC_API(JSBool)
1744 JS_HasArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1745
1746 extern JS_PUBLIC_API(JSBool)
1747 JS_DefineElement(JSContext *cx, JSObject *obj, jsint index, jsval value,
1748 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
1749
1750 extern JS_PUBLIC_API(JSBool)
1751 JS_AliasElement(JSContext *cx, JSObject *obj, const char *name, jsint alias);
1752
1753 extern JS_PUBLIC_API(JSBool)
1754 JS_AlreadyHasOwnElement(JSContext *cx, JSObject *obj, jsint index,
1755 JSBool *foundp);
1756
1757 extern JS_PUBLIC_API(JSBool)
1758 JS_HasElement(JSContext *cx, JSObject *obj, jsint index, JSBool *foundp);
1759
1760 extern JS_PUBLIC_API(JSBool)
1761 JS_LookupElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1762
1763 extern JS_PUBLIC_API(JSBool)
1764 JS_GetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1765
1766 extern JS_PUBLIC_API(JSBool)
1767 JS_SetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
1768
1769 extern JS_PUBLIC_API(JSBool)
1770 JS_DeleteElement(JSContext *cx, JSObject *obj, jsint index);
1771
1772 extern JS_PUBLIC_API(JSBool)
1773 JS_DeleteElement2(JSContext *cx, JSObject *obj, jsint index, jsval *rval);
1774
1775 extern JS_PUBLIC_API(void)
1776 JS_ClearScope(JSContext *cx, JSObject *obj);
1777
1778 extern JS_PUBLIC_API(JSIdArray *)
1779 JS_Enumerate(JSContext *cx, JSObject *obj);
1780
1781 /*
1782 * Create an object to iterate over enumerable properties of obj, in arbitrary
1783 * property definition order. NB: This differs from longstanding for..in loop
1784 * order, which uses order of property definition in obj.
1785 */
1786 extern JS_PUBLIC_API(JSObject *)
1787 JS_NewPropertyIterator(JSContext *cx, JSObject *obj);
1788
1789 /*
1790 * Return true on success with *idp containing the id of the next enumerable
1791 * property to visit using iterobj, or JSVAL_VOID if there is no such property
1792 * left to visit. Return false on error.
1793 */
1794 extern JS_PUBLIC_API(JSBool)
1795 JS_NextProperty(JSContext *cx, JSObject *iterobj, jsid *idp);
1796
1797 extern JS_PUBLIC_API(JSBool)
1798 JS_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
1799 jsval *vp, uintN *attrsp);
1800
1801 extern JS_PUBLIC_API(JSBool)
1802 JS_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
1803
1804 extern JS_PUBLIC_API(JSBool)
1805 JS_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
1806
1807 /************************************************************************/
1808
1809 /*
1810 * Security protocol.
1811 */
1812 struct JSPrincipals {
1813 char *codebase;
1814
1815 /* XXX unspecified and unused by Mozilla code -- can we remove these? */
1816 void * (* getPrincipalArray)(JSContext *cx, JSPrincipals *);
1817 JSBool (* globalPrivilegesEnabled)(JSContext *cx, JSPrincipals *);
1818
1819 /* Don't call "destroy"; use reference counting macros below. */
1820 jsrefcount refcount;
1821
1822 void (* destroy)(JSContext *cx, JSPrincipals *);
1823 JSBool (* subsume)(JSPrincipals *, JSPrincipals *);
1824 };
1825
1826 #ifdef JS_THREADSAFE
1827 #define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
1828 #define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
1829
1830 extern JS_PUBLIC_API(jsrefcount)
1831 JS_HoldPrincipals(JSContext *cx, JSPrincipals *principals);
1832
1833 extern JS_PUBLIC_API(jsrefcount)
1834 JS_DropPrincipals(JSContext *cx, JSPrincipals *principals);
1835
1836 #else
1837 #define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
1838 #define JSPRINCIPALS_DROP(cx, principals) \
1839 ((--(principals)->refcount == 0) \
1840 ? ((*(principals)->destroy)((cx), (principals)), 0) \
1841 : (principals)->refcount)
1842 #endif
1843
1844
1845 struct JSSecurityCallbacks {
1846 JSCheckAccessOp checkObjectAccess;
1847 JSPrincipalsTranscoder principalsTranscoder;
1848 JSObjectPrincipalsFinder findObjectPrincipals;
1849 };
1850
1851 extern JS_PUBLIC_API(JSSecurityCallbacks *)
1852 JS_SetRuntimeSecurityCallbacks(JSRuntime *rt, JSSecurityCallbacks *callbacks);
1853
1854 extern JS_PUBLIC_API(JSSecurityCallbacks *)
1855 JS_GetRuntimeSecurityCallbacks(JSRuntime *rt);
1856
1857 extern JS_PUBLIC_API(JSSecurityCallbacks *)
1858 JS_SetContextSecurityCallbacks(JSContext *cx, JSSecurityCallbacks *callbacks);
1859
1860 extern JS_PUBLIC_API(JSSecurityCallbacks *)
1861 JS_GetSecurityCallbacks(JSContext *cx);
1862
1863 /************************************************************************/
1864
1865 /*
1866 * Functions and scripts.
1867 */
1868 extern JS_PUBLIC_API(JSFunction *)
1869 JS_NewFunction(JSContext *cx, JSNative call, uintN nargs, uintN flags,
1870 JSObject *parent, const char *name);
1871
1872 extern JS_PUBLIC_API(JSObject *)
1873 JS_GetFunctionObject(JSFunction *fun);
1874
1875 /*
1876 * Deprecated, useful only for diagnostics. Use JS_GetFunctionId instead for
1877 * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
1878 */
1879 extern JS_PUBLIC_API(const char *)
1880 JS_GetFunctionName(JSFunction *fun);
1881
1882 /*
1883 * Return the function's identifier as a JSString, or null if fun is unnamed.
1884 * The returned string lives as long as fun, so you don't need to root a saved
1885 * reference to it if fun is well-connected or rooted, and provided you bound
1886 * the use of the saved reference by fun's lifetime.
1887 *
1888 * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
1889 * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
1890 * from UTF-16-ish jschars.
1891 */
1892 extern JS_PUBLIC_API(JSString *)
1893 JS_GetFunctionId(JSFunction *fun);
1894
1895 /*
1896 * Return JSFUN_* flags for fun.
1897 */
1898 extern JS_PUBLIC_API(uintN)
1899 JS_GetFunctionFlags(JSFunction *fun);
1900
1901 /*
1902 * Return the arity (length) of fun.
1903 */
1904 extern JS_PUBLIC_API(uint16)
1905 JS_GetFunctionArity(JSFunction *fun);
1906
1907 /*
1908 * Infallible predicate to test whether obj is a function object (faster than
1909 * comparing obj's class name to "Function", but equivalent unless someone has
1910 * overwritten the "Function" identifier with a different constructor and then
1911 * created instances using that constructor that might be passed in as obj).
1912 */
1913 extern JS_PUBLIC_API(JSBool)
1914 JS_ObjectIsFunction(JSContext *cx, JSObject *obj);
1915
1916 extern JS_PUBLIC_API(JSBool)
1917 JS_DefineFunctions(JSContext *cx, JSObject *obj, JSFunctionSpec *fs);
1918
1919 extern JS_PUBLIC_API(JSFunction *)
1920 JS_DefineFunction(JSContext *cx, JSObject *obj, const char *name, JSNative call,
1921 uintN nargs, uintN attrs);
1922
1923 extern JS_PUBLIC_API(JSFunction *)
1924 JS_DefineUCFunction(JSContext *cx, JSObject *obj,
1925 const jschar *name, size_t namelen, JSNative call,
1926 uintN nargs, uintN attrs);
1927
1928 extern JS_PUBLIC_API(JSObject *)
1929 JS_CloneFunctionObject(JSContext *cx, JSObject *funobj, JSObject *parent);
1930
1931 /*
1932 * Given a buffer, return JS_FALSE if the buffer might become a valid
1933 * javascript statement with the addition of more lines. Otherwise return
1934 * JS_TRUE. The intent is to support interactive compilation - accumulate
1935 * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
1936 * the compiler.
1937 */
1938 extern JS_PUBLIC_API(JSBool)
1939 JS_BufferIsCompilableUnit(JSContext *cx, JSObject *obj,
1940 const char *bytes, size_t length);
1941
1942 /*
1943 * The JSScript objects returned by the following functions refer to string and
1944 * other kinds of literals, including doubles and RegExp objects. These
1945 * literals are vulnerable to garbage collection; to root script objects and
1946 * prevent literals from being collected, create a rootable object using
1947 * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
1948 */
1949 extern JS_PUBLIC_API(JSScript *)
1950 JS_CompileScript(JSContext *cx, JSObject *obj,
1951 const char *bytes, size_t length,
1952 const char *filename, uintN lineno);
1953
1954 extern JS_PUBLIC_API(JSScript *)
1955 JS_CompileScriptForPrincipals(JSContext *cx, JSObject *obj,
1956 JSPrincipals *principals,
1957 const char *bytes, size_t length,
1958 const char *filename, uintN lineno);
1959
1960 extern JS_PUBLIC_API(JSScript *)
1961 JS_CompileUCScript(JSContext *cx, JSObject *obj,
1962 const jschar *chars, size_t length,
1963 const char *filename, uintN lineno);
1964
1965 extern JS_PUBLIC_API(JSScript *)
1966 JS_CompileUCScriptForPrincipals(JSContext *cx, JSObject *obj,
1967 JSPrincipals *principals,
1968 const jschar *chars, size_t length,
1969 const char *filename, uintN lineno);
1970
1971 extern JS_PUBLIC_API(JSScript *)
1972 JS_CompileFile(JSContext *cx, JSObject *obj, const char *filename);
1973
1974 extern JS_PUBLIC_API(JSScript *)
1975 JS_CompileFileHandle(JSContext *cx, JSObject *obj, const char *filename,
1976 FILE *fh);
1977
1978 extern JS_PUBLIC_API(JSScript *)
1979 JS_CompileFileHandleForPrincipals(JSContext *cx, JSObject *obj,
1980 const char *filename, FILE *fh,
1981 JSPrincipals *principals);
1982
1983 /*
1984 * NB: you must use JS_NewScriptObject and root a pointer to its return value
1985 * in order to keep a JSScript and its atoms safe from garbage collection after
1986 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
1987 * E.g., and without error checks:
1988 *
1989 * JSScript *script = JS_CompileFile(cx, global, filename);
1990 * JSObject *scrobj = JS_NewScriptObject(cx, script);
1991 * JS_AddNamedRoot(cx, &scrobj, "scrobj");
1992 * do {
1993 * jsval result;
1994 * JS_ExecuteScript(cx, global, script, &result);
1995 * JS_GC();
1996 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
1997 * JS_RemoveRoot(cx, &scrobj);
1998 */
1999 extern JS_PUBLIC_API(JSObject *)
2000 JS_NewScriptObject(JSContext *cx, JSScript *script);
2001
2002 /*
2003 * Infallible getter for a script's object. If JS_NewScriptObject has not been
2004 * called on script yet, the return value will be null.
2005 */
2006 extern JS_PUBLIC_API(JSObject *)
2007 JS_GetScriptObject(JSScript *script);
2008
2009 extern JS_PUBLIC_API(void)
2010 JS_DestroyScript(JSContext *cx, JSScript *script);
2011
2012 extern JS_PUBLIC_API(JSFunction *)
2013 JS_CompileFunction(JSContext *cx, JSObject *obj, const char *name,
2014 uintN nargs, const char **argnames,
2015 const char *bytes, size_t length,
2016 const char *filename, uintN lineno);
2017
2018 extern JS_PUBLIC_API(JSFunction *)
2019 JS_CompileFunctionForPrincipals(JSContext *cx, JSObject *obj,
2020 JSPrincipals *principals, const char *name,
2021 uintN nargs, const char **argnames,
2022 const char *bytes, size_t length,
2023 const char *filename, uintN lineno);
2024
2025 extern JS_PUBLIC_API(JSFunction *)
2026 JS_CompileUCFunction(JSContext *cx, JSObject *obj, const char *name,
2027 uintN nargs, const char **argnames,
2028 const jschar *chars, size_t length,
2029 const char *filename, uintN lineno);
2030
2031 extern JS_PUBLIC_API(JSFunction *)
2032 JS_CompileUCFunctionForPrincipals(JSContext *cx, JSObject *obj,
2033 JSPrincipals *principals, const char *name,
2034 uintN nargs, const char **argnames,
2035 const jschar *chars, size_t length,
2036 const char *filename, uintN lineno);
2037
2038 extern JS_PUBLIC_API(JSString *)
2039 JS_DecompileScript(JSContext *cx, JSScript *script, const char *name,
2040 uintN indent);
2041
2042 /*
2043 * API extension: OR this into indent to avoid pretty-printing the decompiled
2044 * source resulting from JS_DecompileFunction{,Body}.
2045 */
2046 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
2047
2048 extern JS_PUBLIC_API(JSString *)
2049 JS_DecompileFunction(JSContext *cx, JSFunction *fun, uintN indent);
2050
2051 extern JS_PUBLIC_API(JSString *)
2052 JS_DecompileFunctionBody(JSContext *cx, JSFunction *fun, uintN indent);
2053
2054 /*
2055 * NB: JS_ExecuteScript, JS_ExecuteScriptPart, and the JS_Evaluate*Script*
2056 * quadruplets all use the obj parameter as the initial scope chain header,
2057 * the 'this' keyword value, and the variables object (ECMA parlance for where
2058 * 'var' and 'function' bind names) of the execution context for script.
2059 *
2060 * Using obj as the variables object is problematic if obj's parent (which is
2061 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
2062 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
2063 * created by assignment to an unbound id, 'x = 0', go in the last object on
2064 * the scope chain linked by parent.
2065 *
2066 * ECMA calls that last scoping object the "global object", but note that many
2067 * embeddings have several such objects. ECMA requires that "global code" be
2068 * executed with the variables object equal to this global object. But these
2069 * JS API entry points provide freedom to execute code against a "sub-global",
2070 * i.e., a parented or scoped object, in which case the variables object will
2071 * differ from the last object on the scope chain, resulting in confusing and
2072 * non-ECMA explicit vs. implicit variable creation.
2073 *
2074 * Caveat embedders: unless you already depend on this buggy variables object
2075 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
2076 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
2077 * someone may have set other options on cx already -- for each context in the
2078 * application, if you pass parented objects as the obj parameter, or may ever
2079 * pass such objects in the future.
2080 *
2081 * Why a runtime option? The alternative is to add six or so new API entry
2082 * points with signatures matching the following six, and that doesn't seem
2083 * worth the code bloat cost. Such new entry points would probably have less
2084 * obvious names, too, so would not tend to be used. The JS_SetOption call,
2085 * OTOH, can be more easily hacked into existing code that does not depend on
2086 * the bug; such code can continue to use the familiar JS_EvaluateScript,
2087 * etc., entry points.
2088 */
2089 extern JS_PUBLIC_API(JSBool)
2090 JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval);
2091
2092 /*
2093 * Execute either the function-defining prolog of a script, or the script's
2094 * main body, but not both.
2095 */
2096 typedef enum JSExecPart { JSEXEC_PROLOG, JSEXEC_MAIN } JSExecPart;
2097
2098 extern JS_PUBLIC_API(JSBool)
2099 JS_ExecuteScriptPart(JSContext *cx, JSObject *obj, JSScript *script,
2100 JSExecPart part, jsval *rval);
2101
2102 extern JS_PUBLIC_API(JSBool)
2103 JS_EvaluateScript(JSContext *cx, JSObject *obj,
2104 const char *bytes, uintN length,
2105 const char *filename, uintN lineno,
2106 jsval *rval);
2107
2108 extern JS_PUBLIC_API(JSBool)
2109 JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj,
2110 JSPrincipals *principals,
2111 const char *bytes, uintN length,
2112 const char *filename, uintN lineno,
2113 jsval *rval);
2114
2115 extern JS_PUBLIC_API(JSBool)
2116 JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
2117 const jschar *chars, uintN length,
2118 const char *filename, uintN lineno,
2119 jsval *rval);
2120
2121 extern JS_PUBLIC_API(JSBool)
2122 JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj,
2123 JSPrincipals *principals,
2124 const jschar *chars, uintN length,
2125 const char *filename, uintN lineno,
2126 jsval *rval);
2127
2128 extern JS_PUBLIC_API(JSBool)
2129 JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc,
2130 jsval *argv, jsval *rval);
2131
2132 extern JS_PUBLIC_API(JSBool)
2133 JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, uintN argc,
2134 jsval *argv, jsval *rval);
2135
2136 extern JS_PUBLIC_API(JSBool)
2137 JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc,
2138 jsval *argv, jsval *rval);
2139
2140 /*
2141 * The maximum value of the operation limit to pass to JS_SetOperationCallback
2142 * and JS_SetOperationLimit.
2143 */
2144 #define JS_MAX_OPERATION_LIMIT ((uint32) 0x7FFFFFFF)
2145
2146 #define JS_OPERATION_WEIGHT_BASE 4096
2147
2148 /*
2149 * Set the operation callback that the engine calls periodically after
2150 * the internal operation count reaches the specified limit.
2151 *
2152 * When operationLimit is JS_OPERATION_WEIGHT_BASE, the callback will be
2153 * called at least after each backward jump in the interpreter. To minimize
2154 * the overhead of the callback invocation we suggest at least
2155 *
2156 * 100 * JS_OPERATION_WEIGHT_BASE
2157 *
2158 * as a value for operationLimit.
2159 */
2160 extern JS_PUBLIC_API(void)
2161 JS_SetOperationCallback(JSContext *cx, JSOperationCallback callback,
2162 uint32 operationLimit);
2163
2164 extern JS_PUBLIC_API(void)
2165 JS_ClearOperationCallback(JSContext *cx);
2166
2167 extern JS_PUBLIC_API(JSOperationCallback)
2168 JS_GetOperationCallback(JSContext *cx);
2169
2170 /*
2171 * Get the operation limit associated with the operation callback. This API
2172 * function may be called only when the result of JS_GetOperationCallback(cx)
2173 * is not null.
2174 */
2175 extern JS_PUBLIC_API(uint32)
2176 JS_GetOperationLimit(JSContext *cx);
2177
2178 /*
2179 * Change the operation limit associated with the operation callback. This API
2180 * function may be called only when the result of JS_GetOperationCallback(cx)
2181 * is not null.
2182 */
2183 extern JS_PUBLIC_API(void)
2184 JS_SetOperationLimit(JSContext *cx, uint32 operationLimit);
2185
2186 /*
2187 * Note well: JS_SetBranchCallback is deprecated. It is similar to
2188 *
2189 * JS_SetOperationCallback(cx, callback, 4096, NULL);
2190 *
2191 * except that the callback will not be called from a long-running native
2192 * function when JSOPTION_NATIVE_BRANCH_CALLBACK is not set and the top-most
2193 * frame is native.
2194 */
2195 extern JS_PUBLIC_API(JSBranchCallback)
2196 JS_SetBranchCallback(JSContext *cx, JSBranchCallback cb);
2197
2198 extern JS_PUBLIC_API(JSBool)
2199 JS_IsRunning(JSContext *cx);
2200
2201 extern JS_PUBLIC_API(JSBool)
2202 JS_IsConstructing(JSContext *cx);
2203
2204 /*
2205 * Returns true if a script is executing and its current bytecode is a set
2206 * (assignment) operation, even if there are native (no script) stack frames
2207 * between the script and the caller to JS_IsAssigning.
2208 */
2209 extern JS_FRIEND_API(JSBool)
2210 JS_IsAssigning(JSContext *cx);
2211
2212 /*
2213 * Set the second return value, which should be a string or int jsval that
2214 * identifies a property in the returned object, to form an ECMA reference
2215 * type value (obj, id). Only native methods can return reference types,
2216 * and if the returned value is used on the left-hand side of an assignment
2217 * op, the identified property will be set. If the return value is in an
2218 * r-value, the interpreter just gets obj[id]'s value.
2219 */
2220 extern JS_PUBLIC_API(void)
2221 JS_SetCallReturnValue2(JSContext *cx, jsval v);
2222
2223 /*
2224 * Saving and restoring frame chains.
2225 *
2226 * These two functions are used to set aside cx's call stack while that stack
2227 * is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
2228 * code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
2229 * must be balanced and all nested calls to JS_SaveFrameChain must have had
2230 * matching JS_RestoreFrameChain calls.
2231 *
2232 * JS_SaveFrameChain deals with cx not having any code running on it. A null
2233 * return does not signify an error, and JS_RestoreFrameChain handles a null
2234 * frame pointer argument safely.
2235 */
2236 extern JS_PUBLIC_API(JSStackFrame *)
2237 JS_SaveFrameChain(JSContext *cx);
2238
2239 extern JS_PUBLIC_API(void)
2240 JS_RestoreFrameChain(JSContext *cx, JSStackFrame *fp);
2241
2242 /************************************************************************/
2243
2244 /*
2245 * Strings.
2246 *
2247 * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
2248 * on error (signified by null return), it leaves bytes owned by the caller.
2249 * So the caller must free bytes in the error case, if it has no use for them.
2250 * In contrast, all the JS_New*StringCopy* functions do not take ownership of
2251 * the character memory passed to them -- they copy it.
2252 */
2253 extern JS_PUBLIC_API(JSString *)
2254 JS_NewString(JSContext *cx, char *bytes, size_t length);
2255
2256 extern JS_PUBLIC_API(JSString *)
2257 JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);
2258
2259 extern JS_PUBLIC_API(JSString *)
2260 JS_NewStringCopyZ(JSContext *cx, const char *s);
2261
2262 extern JS_PUBLIC_API(JSString *)
2263 JS_InternString(JSContext *cx, const char *s);
2264
2265 extern JS_PUBLIC_API(JSString *)
2266 JS_NewUCString(JSContext *cx, jschar *chars, size_t length);
2267
2268 extern JS_PUBLIC_API(JSString *)
2269 JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n);
2270
2271 extern JS_PUBLIC_API(JSString *)
2272 JS_NewUCStringCopyZ(JSContext *cx, const jschar *s);
2273
2274 extern JS_PUBLIC_API(JSString *)
2275 JS_InternUCStringN(JSContext *cx, const jschar *s, size_t length);
2276
2277 extern JS_PUBLIC_API(JSString *)
2278 JS_InternUCString(JSContext *cx, const jschar *s);
2279
2280 extern JS_PUBLIC_API(char *)
2281 JS_GetStringBytes(JSString *str);
2282
2283 extern JS_PUBLIC_API(jschar *)
2284 JS_GetStringChars(JSString *str);
2285
2286 extern JS_PUBLIC_API(size_t)
2287 JS_GetStringLength(JSString *str);
2288
2289 extern JS_PUBLIC_API(intN)
2290 JS_CompareStrings(JSString *str1, JSString *str2);
2291
2292 /*
2293 * Mutable string support. A string's characters are never mutable in this JS
2294 * implementation, but a growable string has a buffer that can be reallocated,
2295 * and a dependent string is a substring of another (growable, dependent, or
2296 * immutable) string. The direct data members of the (opaque to API clients)
2297 * JSString struct may be changed in a single-threaded way for growable and
2298 * dependent strings.
2299 *
2300 * Therefore mutable strings cannot be used by more than one thread at a time.
2301 * You may call JS_MakeStringImmutable to convert the string from a mutable
2302 * (growable or dependent) string to an immutable (and therefore thread-safe)
2303 * string. The engine takes care of converting growable and dependent strings
2304 * to immutable for you if you store strings in multi-threaded objects using
2305 * JS_SetProperty or kindred API entry points.
2306 *
2307 * If you store a JSString pointer in a native data structure that is (safely)
2308 * accessible to multiple threads, you must call JS_MakeStringImmutable before
2309 * retiring the store.
2310 */
2311 extern JS_PUBLIC_API(JSString *)
2312 JS_NewGrowableString(JSContext *cx, jschar *chars, size_t length);
2313
2314 /*
2315 * Create a dependent string, i.e., a string that owns no character storage,
2316 * but that refers to a slice of another string's chars. Dependent strings
2317 * are mutable by definition, so the thread safety comments above apply.
2318 */
2319 extern JS_PUBLIC_API(JSString *)
2320 JS_NewDependentString(JSContext *cx, JSString *str, size_t start,
2321 size_t length);
2322
2323 /*
2324 * Concatenate two strings, resulting in a new growable string. If you create
2325 * the left string and pass it to JS_ConcatStrings on a single thread, try to
2326 * use JS_NewGrowableString to create the left string -- doing so helps Concat
2327 * avoid allocating a new buffer for the result and copying left's chars into
2328 * the new buffer. See above for thread safety comments.
2329 */
2330 extern JS_PUBLIC_API(JSString *)
2331 JS_ConcatStrings(JSContext *cx, JSString *left, JSString *right);
2332
2333 /*
2334 * Convert a dependent string into an independent one. This function does not
2335 * change the string's mutability, so the thread safety comments above apply.
2336 */
2337 extern JS_PUBLIC_API(const jschar *)
2338 JS_UndependString(JSContext *cx, JSString *str);
2339
2340 /*
2341 * Convert a mutable string (either growable or dependent) into an immutable,
2342 * thread-safe one.
2343 */
2344 extern JS_PUBLIC_API(JSBool)
2345 JS_MakeStringImmutable(JSContext *cx, JSString *str);
2346
2347 /*
2348 * Return JS_TRUE if C (char []) strings passed via the API and internally
2349 * are UTF-8.
2350 */
2351 JS_PUBLIC_API(JSBool)
2352 JS_CStringsAreUTF8(void);
2353
2354 /*
2355 * Update the value to be returned by JS_CStringsAreUTF8(). Once set, it
2356 * can never be changed. This API must be called before the first call to
2357 * JS_NewRuntime.
2358 */
2359 JS_PUBLIC_API(void)
2360 JS_SetCStringsAreUTF8(void);
2361
2362 /*
2363 * Character encoding support.
2364 *
2365 * For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
2366 * of the destination buffer before the call; on return, *dstlenp contains the
2367 * number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
2368 * stored. To determine the necessary destination buffer size, make a sizing
2369 * call that passes NULL for dst.
2370 *
2371 * On errors, the functions report the error. In that case, *dstlenp contains
2372 * the number of characters or bytes transferred so far. If cx is NULL, no
2373 * error is reported on failure, and the functions simply return JS_FALSE.
2374 *
2375 * NB: Neither function stores an additional zero byte or jschar after the
2376 * transcoded string.
2377 *
2378 * If JS_CStringsAreUTF8() is true then JS_EncodeCharacters encodes to
2379 * UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create additional
2380 * errors if the character sequence is malformed. If UTF-8 support is
2381 * disabled, the functions deflate and inflate, respectively.
2382 */
2383 JS_PUBLIC_API(JSBool)
2384 JS_EncodeCharacters(JSContext *cx, const jschar *src, size_t srclen, char *dst,
2385 size_t *dstlenp);
2386
2387 JS_PUBLIC_API(JSBool)
2388 JS_DecodeBytes(JSContext *cx, const char *src, size_t srclen, jschar *dst,
2389 size_t *dstlenp);
2390
2391 /*
2392 * A variation on JS_EncodeCharacters where a null terminated string is
2393 * returned that you are expected to call JS_free on when done.
2394 */
2395 JS_PUBLIC_API(char *)
2396 JS_EncodeString(JSContext *cx, JSString *str);
2397
2398 /************************************************************************/
2399 /*
2400 * JSON functions
2401 */
2402 typedef JSBool (* JSONWriteCallback)(const jschar *buf, uint32 len, void *data);
2403
2404 /*
2405 * JSON.stringify as specificed by ES3.1 (draft)
2406 */
2407 JS_PUBLIC_API(JSBool)
2408 JS_Stringify(JSContext *cx, jsval *vp, JSObject *replacer,
2409 JSONWriteCallback callback, void *data);
2410
2411 /*
2412 * Retrieve a toJSON function. If found, set vp to its result.
2413 */
2414 JS_PUBLIC_API(JSBool)
2415 JS_TryJSON(JSContext *cx, jsval *vp);
2416
2417 /*
2418 * JSON.parse as specificed by ES3.1 (draft)
2419 */
2420 JS_PUBLIC_API(JSONParser *)
2421 JS_BeginJSONParse(JSContext *cx, jsval *vp);
2422
2423 JS_PUBLIC_API(JSBool)
2424 JS_ConsumeJSONText(JSContext *cx, JSONParser *jp, const jschar *data, uint32 len);
2425
2426 JS_PUBLIC_API(JSBool)
2427 JS_FinishJSONParse(JSContext *cx, JSONParser *jp);
2428
2429 /************************************************************************/
2430
2431 /*
2432 * Locale specific string conversion and error message callbacks.
2433 */
2434 struct JSLocaleCallbacks {
2435 JSLocaleToUpperCase localeToUpperCase;
2436 JSLocaleToLowerCase localeToLowerCase;
2437 JSLocaleCompare localeCompare;
2438 JSLocaleToUnicode localeToUnicode;
2439 JSErrorCallback localeGetErrorMessage;
2440 };
2441
2442 /*
2443 * Establish locale callbacks. The pointer must persist as long as the
2444 * JSContext. Passing NULL restores the default behaviour.
2445 */
2446 extern JS_PUBLIC_API(void)
2447 JS_SetLocaleCallbacks(JSContext *cx, JSLocaleCallbacks *callbacks);
2448
2449 /*
2450 * Return the address of the current locale callbacks struct, which may
2451 * be NULL.
2452 */
2453 extern JS_PUBLIC_API(JSLocaleCallbacks *)
2454 JS_GetLocaleCallbacks(JSContext *cx);
2455
2456 /************************************************************************/
2457
2458 /*
2459 * Error reporting.
2460 */
2461
2462 /*
2463 * Report an exception represented by the sprintf-like conversion of format
2464 * and its arguments. This exception message string is passed to a pre-set
2465 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
2466 * the JSErrorReporter typedef).
2467 */
2468 extern JS_PUBLIC_API(void)
2469 JS_ReportError(JSContext *cx, const char *format, ...);
2470
2471 /*
2472 * Use an errorNumber to retrieve the format string, args are char *
2473 */
2474 extern JS_PUBLIC_API(void)
2475 JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
2476 void *userRef, const uintN errorNumber, ...);
2477
2478 /*
2479 * Use an errorNumber to retrieve the format string, args are jschar *
2480 */
2481 extern JS_PUBLIC_API(void)
2482 JS_ReportErrorNumberUC(JSContext *cx, JSErrorCallback errorCallback,
2483 void *userRef, const uintN errorNumber, ...);
2484
2485 /*
2486 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
2487 * Return true if there was no error trying to issue the warning, and if the
2488 * warning was not converted into an error due to the JSOPTION_WERROR option
2489 * being set, false otherwise.
2490 */
2491 extern JS_PUBLIC_API(JSBool)
2492 JS_ReportWarning(JSContext *cx, const char *format, ...);
2493
2494 extern JS_PUBLIC_API(JSBool)
2495 JS_ReportErrorFlagsAndNumber(JSContext *cx, uintN flags,
2496 JSErrorCallback errorCallback, void *userRef,
2497 const uintN errorNumber, ...);
2498
2499 extern JS_PUBLIC_API(JSBool)
2500 JS_ReportErrorFlagsAndNumberUC(JSContext *cx, uintN flags,
2501 JSErrorCallback errorCallback, void *userRef,
2502 const uintN errorNumber, ...);
2503
2504 /*
2505 * Complain when out of memory.
2506 */
2507 extern JS_PUBLIC_API(void)
2508 JS_ReportOutOfMemory(JSContext *cx);
2509
2510 /*
2511 * Complain when an allocation size overflows the maximum supported limit.
2512 */
2513 extern JS_PUBLIC_API(void)
2514 JS_ReportAllocationOverflow(JSContext *cx);
2515
2516 struct JSErrorReport {
2517 const char *filename; /* source file name, URL, etc., or null */
2518 uintN lineno; /* source line number */
2519 const char *linebuf; /* offending source line without final \n */
2520 const char *tokenptr; /* pointer to error token in linebuf */
2521 const jschar *uclinebuf; /* unicode (original) line buffer */
2522 const jschar *uctokenptr; /* unicode (original) token pointer */
2523 uintN flags; /* error/warning, etc. */
2524 uintN errorNumber; /* the error number, e.g. see js.msg */
2525 const jschar *ucmessage; /* the (default) error message */
2526 const jschar **messageArgs; /* arguments for the error message */
2527 };
2528
2529 /*
2530 * JSErrorReport flag values. These may be freely composed.
2531 */
2532 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
2533 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
2534 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
2535 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
2536
2537 /*
2538 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
2539 * has been thrown for this runtime error, and the host should ignore it.
2540 * Exception-aware hosts should also check for JS_IsExceptionPending if
2541 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
2542 * appropriate.
2543 */
2544 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
2545 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
2546 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
2547
2548 extern JS_PUBLIC_API(JSErrorReporter)
2549 JS_SetErrorReporter(JSContext *cx, JSErrorReporter er);
2550
2551 /************************************************************************/
2552
2553 /*
2554 * Regular Expressions.
2555 */
2556 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
2557 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
2558 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
2559 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
2560
2561 extern JS_PUBLIC_API(JSObject *)
2562 JS_NewRegExpObject(JSContext *cx, char *bytes, size_t length, uintN flags);
2563
2564 extern JS_PUBLIC_API(JSObject *)
2565 JS_NewUCRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags);
2566
2567 extern JS_PUBLIC_API(void)
2568 JS_SetRegExpInput(JSContext *cx, JSString *input, JSBool multiline);
2569
2570 extern JS_PUBLIC_API(void)
2571 JS_ClearRegExpStatics(JSContext *cx);
2572
2573 extern JS_PUBLIC_API(void)
2574 JS_ClearRegExpRoots(JSContext *cx);
2575
2576 /* TODO: compile, exec, get/set other statics... */
2577
2578 /************************************************************************/
2579
2580 extern JS_PUBLIC_API(JSBool)
2581 JS_IsExceptionPending(JSContext *cx);
2582
2583 extern JS_PUBLIC_API(JSBool)
2584 JS_GetPendingException(JSContext *cx, jsval *vp);
2585
2586 extern JS_PUBLIC_API(void)
2587 JS_SetPendingException(JSContext *cx, jsval v);
2588
2589 extern JS_PUBLIC_API(void)
2590 JS_ClearPendingException(JSContext *cx);
2591
2592 extern JS_PUBLIC_API(JSBool)
2593 JS_ReportPendingException(JSContext *cx);
2594
2595 /*
2596 * Save the current exception state. This takes a snapshot of cx's current
2597 * exception state without making any change to that state.
2598 *
2599 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
2600 * (to restore that saved state, overriding any more recent state) or else to
2601 * JS_DropExceptionState (to free the state struct in case it is not correct
2602 * or desirable to restore it). Both Restore and Drop free the state struct,
2603 * so callers must stop using the pointer returned from Save after calling the
2604 * Release or Drop API.
2605 */
2606 extern JS_PUBLIC_API(JSExceptionState *)
2607 JS_SaveExceptionState(JSContext *cx);
2608
2609 extern JS_PUBLIC_API(void)
2610 JS_RestoreExceptionState(JSContext *cx, JSExceptionState *state);
2611
2612 extern JS_PUBLIC_API(void)
2613 JS_DropExceptionState(JSContext *cx, JSExceptionState *state);
2614
2615 /*
2616 * If the given value is an exception object that originated from an error,
2617 * the exception will contain an error report struct, and this API will return
2618 * the address of that struct. Otherwise, it returns NULL. The lifetime of
2619 * the error report struct that might be returned is the same as the lifetime
2620 * of the exception object.
2621 */
2622 extern JS_PUBLIC_API(JSErrorReport *)
2623 JS_ErrorFromException(JSContext *cx, jsval v);
2624
2625 /*
2626 * Given a reported error's message and JSErrorReport struct pointer, throw
2627 * the corresponding exception on cx.
2628 */
2629 extern JS_PUBLIC_API(JSBool)
2630 JS_ThrowReportedError(JSContext *cx, const char *message,
2631 JSErrorReport *reportp);
2632
2633 /*
2634 * Throws a StopIteration exception on cx.
2635 */
2636 extern JS_PUBLIC_API(JSBool)
2637 JS_ThrowStopIteration(JSContext *cx);
2638
2639 /*
2640 * Associate the current thread with the given context. This is done
2641 * implicitly by JS_NewContext.
2642 *
2643 * Returns the old thread id for this context, which should be treated as
2644 * an opaque value. This value is provided for comparison to 0, which
2645 * indicates that ClearContextThread has been called on this context
2646 * since the last SetContextThread, or non-0, which indicates the opposite.
2647 */
2648 extern JS_PUBLIC_API(jsword)
2649 JS_GetContextThread(JSContext *cx);
2650
2651 extern JS_PUBLIC_API(jsword)
2652 JS_SetContextThread(JSContext *cx);
2653
2654 extern JS_PUBLIC_API(jsword)
2655 JS_ClearContextThread(JSContext *cx);
2656
2657 /************************************************************************/
2658
2659 #ifdef DEBUG
2660 #define JS_GC_ZEAL 1
2661 #endif
2662
2663 #ifdef JS_GC_ZEAL
2664 extern JS_PUBLIC_API(void)
2665 JS_SetGCZeal(JSContext *cx, uint8 zeal);
2666 #endif
2667
2668 JS_END_EXTERN_C
2669
2670 #endif /* jsapi_h___ */

  ViewVC Help
Powered by ViewVC 1.1.24