CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / js / src / jspubtd.h
blob249bedd6ab0a3676d764ce386dbfce31fa14e7c9
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jspubtd_h___
41 #define jspubtd_h___
43 * JS public API typedefs.
45 #include "jstypes.h"
46 #include "jscompat.h"
47 #include "jsval.h"
49 JS_BEGIN_EXTERN_C
51 /* Scalar typedefs. */
52 typedef JSInt32 jsint;
53 typedef JSUint32 jsuint;
54 typedef float64 jsdouble;
55 typedef JSInt32 jsrefcount; /* PRInt32 if JS_THREADSAFE, see jslock.h */
57 #ifdef WIN32
58 typedef wchar_t jschar;
59 #else
60 typedef JSUint16 jschar;
61 #endif
65 * Run-time version enumeration. See jsversion.h for compile-time counterparts
66 * to these values that may be selected by the JS_VERSION macro, and tested by
67 * #if expressions.
69 typedef enum JSVersion {
70 JSVERSION_1_0 = 100,
71 JSVERSION_1_1 = 110,
72 JSVERSION_1_2 = 120,
73 JSVERSION_1_3 = 130,
74 JSVERSION_1_4 = 140,
75 JSVERSION_ECMA_3 = 148,
76 JSVERSION_1_5 = 150,
77 JSVERSION_1_6 = 160,
78 JSVERSION_1_7 = 170,
79 JSVERSION_1_8 = 180,
80 JSVERSION_ECMA_5 = 185,
81 JSVERSION_DEFAULT = 0,
82 JSVERSION_UNKNOWN = -1,
83 JSVERSION_LATEST = JSVERSION_ECMA_5
84 } JSVersion;
86 #define JSVERSION_IS_ECMA(version) \
87 ((version) == JSVERSION_DEFAULT || (version) >= JSVERSION_1_3)
89 /* Result of typeof operator enumeration. */
90 typedef enum JSType {
91 JSTYPE_VOID, /* undefined */
92 JSTYPE_OBJECT, /* object */
93 JSTYPE_FUNCTION, /* function */
94 JSTYPE_STRING, /* string */
95 JSTYPE_NUMBER, /* number */
96 JSTYPE_BOOLEAN, /* boolean */
97 JSTYPE_NULL, /* null */
98 JSTYPE_XML, /* xml object */
99 JSTYPE_LIMIT
100 } JSType;
102 /* Dense index into cached prototypes and class atoms for standard objects. */
103 typedef enum JSProtoKey {
104 #define JS_PROTO(name,code,init) JSProto_##name = code,
105 #include "jsproto.tbl"
106 #undef JS_PROTO
107 JSProto_LIMIT
108 } JSProtoKey;
110 /* js_CheckAccess mode enumeration. */
111 typedef enum JSAccessMode {
112 JSACC_PROTO = 0, /* XXXbe redundant w.r.t. id */
113 JSACC_PARENT = 1, /* XXXbe redundant w.r.t. id */
116 * enum value #2 formerly called JSACC_IMPORT,
117 * gap preserved for ABI compatibility.
120 JSACC_WATCH = 3, /* a watchpoint on object foo for id 'bar' */
121 JSACC_READ = 4, /* a "get" of foo.bar */
122 JSACC_WRITE = 8, /* a "set" of foo.bar = baz */
123 JSACC_LIMIT
124 } JSAccessMode;
126 #define JSACC_TYPEMASK (JSACC_WRITE - 1)
129 * This enum type is used to control the behavior of a JSObject property
130 * iterator function that has type JSNewEnumerate.
132 typedef enum JSIterateOp {
133 /* Create new iterator state over enumerable properties. */
134 JSENUMERATE_INIT,
136 /* Create new iterator state over all properties. */
137 JSENUMERATE_INIT_ALL,
139 /* Iterate once. */
140 JSENUMERATE_NEXT,
142 /* Destroy iterator state. */
143 JSENUMERATE_DESTROY
144 } JSIterateOp;
146 /* Struct typedefs. */
147 typedef struct JSClass JSClass;
148 typedef struct JSConstDoubleSpec JSConstDoubleSpec;
149 typedef struct JSContext JSContext;
150 typedef struct JSErrorReport JSErrorReport;
151 typedef struct JSFunction JSFunction;
152 typedef struct JSFunctionSpec JSFunctionSpec;
153 typedef struct JSTracer JSTracer;
154 typedef struct JSIdArray JSIdArray;
155 typedef struct JSPropertyDescriptor JSPropertyDescriptor;
156 typedef struct JSPropertySpec JSPropertySpec;
157 typedef struct JSObjectMap JSObjectMap;
158 typedef struct JSRuntime JSRuntime;
159 typedef struct JSScript JSScript;
160 typedef struct JSStackFrame JSStackFrame;
161 typedef struct JSXDRState JSXDRState;
162 typedef struct JSExceptionState JSExceptionState;
163 typedef struct JSLocaleCallbacks JSLocaleCallbacks;
164 typedef struct JSSecurityCallbacks JSSecurityCallbacks;
165 typedef struct JSONParser JSONParser;
166 typedef struct JSCompartment JSCompartment;
167 typedef struct JSCrossCompartmentCall JSCrossCompartmentCall;
168 typedef struct JSStructuredCloneWriter JSStructuredCloneWriter;
169 typedef struct JSStructuredCloneReader JSStructuredCloneReader;
170 typedef struct JSStructuredCloneCallbacks JSStructuredCloneCallbacks;
172 #ifdef __cplusplus
173 typedef class JSWrapper JSWrapper;
174 typedef class JSCrossCompartmentWrapper JSCrossCompartmentWrapper;
175 #endif
177 /* JSClass (and js::ObjectOps where appropriate) function pointer typedefs. */
180 * Add, delete, or get a property named by id in obj. Note the jsid id
181 * type -- id may be a string (Unicode property identifier) or an int (element
182 * index). The *vp out parameter, on success, is the new property value after
183 * an add or get. After a successful delete, *vp is JSVAL_FALSE iff
184 * obj[id] can't be deleted (because it's permanent).
186 typedef JSBool
187 (* JSPropertyOp)(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
190 * Set a property named by id in obj, treating the assignment as strict
191 * mode code if strict is true. Note the jsid id type -- id may be a string
192 * (Unicode property identifier) or an int (element index). The *vp out
193 * parameter, on success, is the new property value after the
194 * set.
196 typedef JSBool
197 (* JSStrictPropertyOp)(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp);
200 * This function type is used for callbacks that enumerate the properties of
201 * a JSObject. The behavior depends on the value of enum_op:
203 * JSENUMERATE_INIT
204 * A new, opaque iterator state should be allocated and stored in *statep.
205 * (You can use PRIVATE_TO_JSVAL() to tag the pointer to be stored).
207 * The number of properties that will be enumerated should be returned as
208 * an integer jsval in *idp, if idp is non-null, and provided the number of
209 * enumerable properties is known. If idp is non-null and the number of
210 * enumerable properties can't be computed in advance, *idp should be set
211 * to JSVAL_ZERO.
213 * JSENUMERATE_INIT_ALL
214 * Used identically to JSENUMERATE_INIT, but exposes all properties of the
215 * object regardless of enumerability.
217 * JSENUMERATE_NEXT
218 * A previously allocated opaque iterator state is passed in via statep.
219 * Return the next jsid in the iteration using *idp. The opaque iterator
220 * state pointed at by statep is destroyed and *statep is set to JSVAL_NULL
221 * if there are no properties left to enumerate.
223 * JSENUMERATE_DESTROY
224 * Destroy the opaque iterator state previously allocated in *statep by a
225 * call to this function when enum_op was JSENUMERATE_INIT or
226 * JSENUMERATE_INIT_ALL.
228 * The return value is used to indicate success, with a value of JS_FALSE
229 * indicating failure.
231 typedef JSBool
232 (* JSNewEnumerateOp)(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
233 jsval *statep, jsid *idp);
236 * The old-style JSClass.enumerate op should define all lazy properties not
237 * yet reflected in obj.
239 typedef JSBool
240 (* JSEnumerateOp)(JSContext *cx, JSObject *obj);
243 * Resolve a lazy property named by id in obj by defining it directly in obj.
244 * Lazy properties are those reflected from some peer native property space
245 * (e.g., the DOM attributes for a given node reflected as obj) on demand.
247 * JS looks for a property in an object, and if not found, tries to resolve
248 * the given id. If resolve succeeds, the engine looks again in case resolve
249 * defined obj[id]. If no such property exists directly in obj, the process
250 * is repeated with obj's prototype, etc.
252 * NB: JSNewResolveOp provides a cheaper way to resolve lazy properties.
254 typedef JSBool
255 (* JSResolveOp)(JSContext *cx, JSObject *obj, jsid id);
258 * Like JSResolveOp, but flags provide contextual information as follows:
260 * JSRESOLVE_QUALIFIED a qualified property id: obj.id or obj[id], not id
261 * JSRESOLVE_ASSIGNING obj[id] is on the left-hand side of an assignment
262 * JSRESOLVE_DETECTING 'if (o.p)...' or similar detection opcode sequence
263 * JSRESOLVE_DECLARING var, const, or function prolog declaration opcode
264 * JSRESOLVE_CLASSNAME class name used when constructing
266 * The *objp out parameter, on success, should be null to indicate that id
267 * was not resolved; and non-null, referring to obj or one of its prototypes,
268 * if id was resolved.
270 * This hook instead of JSResolveOp is called via the JSClass.resolve member
271 * if JSCLASS_NEW_RESOLVE is set in JSClass.flags.
273 * Setting JSCLASS_NEW_RESOLVE and JSCLASS_NEW_RESOLVE_GETS_START further
274 * extends this hook by passing in the starting object on the prototype chain
275 * via *objp. Thus a resolve hook implementation may define the property id
276 * being resolved in the object in which the id was first sought, rather than
277 * in a prototype object whose class led to the resolve hook being called.
279 * When using JSCLASS_NEW_RESOLVE_GETS_START, the resolve hook must therefore
280 * null *objp to signify "not resolved". With only JSCLASS_NEW_RESOLVE and no
281 * JSCLASS_NEW_RESOLVE_GETS_START, the hook can assume *objp is null on entry.
282 * This is not good practice, but enough existing hook implementations count
283 * on it that we can't break compatibility by passing the starting object in
284 * *objp without a new JSClass flag.
286 typedef JSBool
287 (* JSNewResolveOp)(JSContext *cx, JSObject *obj, jsid id, uintN flags,
288 JSObject **objp);
291 * Convert obj to the given type, returning true with the resulting value in
292 * *vp on success, and returning false on error or exception.
294 typedef JSBool
295 (* JSConvertOp)(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
298 * Delegate typeof to an object so it can cloak a primitive or another object.
300 typedef JSType
301 (* JSTypeOfOp)(JSContext *cx, JSObject *obj);
304 * Finalize obj, which the garbage collector has determined to be unreachable
305 * from other live objects or from GC roots. Obviously, finalizers must never
306 * store a reference to obj.
308 typedef void
309 (* JSFinalizeOp)(JSContext *cx, JSObject *obj);
312 * Used by JS_AddExternalStringFinalizer and JS_RemoveExternalStringFinalizer
313 * to extend and reduce the set of string types finalized by the GC.
315 typedef void
316 (* JSStringFinalizeOp)(JSContext *cx, JSString *str);
319 * JSClass.checkAccess type: check whether obj[id] may be accessed per mode,
320 * returning false on error/exception, true on success with obj[id]'s last-got
321 * value in *vp, and its attributes in *attrsp. As for JSPropertyOp above, id
322 * is either a string or an int jsval.
324 typedef JSBool
325 (* JSCheckAccessOp)(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
326 jsval *vp);
329 * Encode or decode an object, given an XDR state record representing external
330 * data. See jsxdrapi.h.
332 typedef JSBool
333 (* JSXDRObjectOp)(JSXDRState *xdr, JSObject **objp);
336 * Check whether v is an instance of obj. Return false on error or exception,
337 * true on success with JS_TRUE in *bp if v is an instance of obj, JS_FALSE in
338 * *bp otherwise.
340 typedef JSBool
341 (* JSHasInstanceOp)(JSContext *cx, JSObject *obj, const jsval *v, JSBool *bp);
344 * Deprecated function type for JSClass.mark. All new code should define
345 * JSTraceOp instead to ensure the traversal of traceable things stored in
346 * the native structures.
348 typedef uint32
349 (* JSMarkOp)(JSContext *cx, JSObject *obj, void *arg);
352 * Function type for trace operation of the class called to enumerate all
353 * traceable things reachable from obj's private data structure. For each such
354 * thing, a trace implementation must call
356 * JS_CallTracer(trc, thing, kind);
358 * or one of its convenience macros as described in jsapi.h.
360 * JSTraceOp implementation can assume that no other threads mutates object
361 * state. It must not change state of the object or corresponding native
362 * structures. The only exception for this rule is the case when the embedding
363 * needs a tight integration with GC. In that case the embedding can check if
364 * the traversal is a part of the marking phase through calling
365 * JS_IsGCMarkingTracer and apply a special code like emptying caches or
366 * marking its native structures.
368 * To define the tracer for a JSClass, the implementation must add
369 * JSCLASS_MARK_IS_TRACE to class flags and use JS_CLASS_TRACE(method)
370 * macro below to convert JSTraceOp to JSMarkOp when initializing or
371 * assigning JSClass.mark field.
373 typedef void
374 (* JSTraceOp)(JSTracer *trc, JSObject *obj);
376 #if defined __GNUC__ && __GNUC__ >= 4 && !defined __cplusplus
377 # define JS_CLASS_TRACE(method) \
378 (__builtin_types_compatible_p(JSTraceOp, __typeof(&(method))) \
379 ? (JSMarkOp)(method) \
380 : js_WrongTypeForClassTracer)
382 extern JSMarkOp js_WrongTypeForClassTracer;
384 #else
385 # define JS_CLASS_TRACE(method) ((JSMarkOp)(method))
386 #endif
389 * Tracer callback, called for each traceable thing directly referenced by a
390 * particular object or runtime structure. It is the callback responsibility
391 * to ensure the traversal of the full object graph via calling eventually
392 * JS_TraceChildren on the passed thing. In this case the callback must be
393 * prepared to deal with cycles in the traversal graph.
395 * kind argument is one of JSTRACE_OBJECT, JSTRACE_STRING or a tag denoting
396 * internal implementation-specific traversal kind. In the latter case the only
397 * operations on thing that the callback can do is to call JS_TraceChildren or
398 * DEBUG-only JS_PrintTraceThingInfo.
400 typedef void
401 (* JSTraceCallback)(JSTracer *trc, void *thing, uint32 kind);
404 * DEBUG only callback that JSTraceOp implementation can provide to return
405 * a string describing the reference traced with JS_CallTracer.
407 typedef void
408 (* JSTraceNamePrinter)(JSTracer *trc, char *buf, size_t bufsize);
410 typedef JSBool
411 (* JSEqualityOp)(JSContext *cx, JSObject *obj, const jsval *v, JSBool *bp);
414 * Typedef for native functions called by the JS VM.
416 * See jsapi.h, the JS_CALLEE, JS_THIS, etc. macros.
419 typedef JSBool
420 (* JSNative)(JSContext *cx, uintN argc, jsval *vp);
422 /* Callbacks and their arguments. */
424 typedef enum JSContextOp {
425 JSCONTEXT_NEW,
426 JSCONTEXT_DESTROY
427 } JSContextOp;
430 * The possible values for contextOp when the runtime calls the callback are:
431 * JSCONTEXT_NEW JS_NewContext successfully created a new JSContext
432 * instance. The callback can initialize the instance as
433 * required. If the callback returns false, the instance
434 * will be destroyed and JS_NewContext returns null. In
435 * this case the callback is not called again.
436 * JSCONTEXT_DESTROY One of JS_DestroyContext* methods is called. The
437 * callback may perform its own cleanup and must always
438 * return true.
439 * Any other value For future compatibility the callback must do nothing
440 * and return true in this case.
442 typedef JSBool
443 (* JSContextCallback)(JSContext *cx, uintN contextOp);
445 #ifndef JS_THREADSAFE
446 typedef void
447 (* JSHeartbeatCallback)(JSRuntime *rt);
448 #endif
450 typedef enum JSGCStatus {
451 JSGC_BEGIN,
452 JSGC_END,
453 JSGC_MARK_END,
454 JSGC_FINALIZE_END
455 } JSGCStatus;
457 typedef JSBool
458 (* JSGCCallback)(JSContext *cx, JSGCStatus status);
461 * Generic trace operation that calls JS_CallTracer on each traceable thing
462 * stored in data.
464 typedef void
465 (* JSTraceDataOp)(JSTracer *trc, void *data);
467 typedef JSBool
468 (* JSOperationCallback)(JSContext *cx);
471 * Deprecated form of JSOperationCallback.
473 typedef JSBool
474 (* JSBranchCallback)(JSContext *cx, JSScript *script);
476 typedef void
477 (* JSErrorReporter)(JSContext *cx, const char *message, JSErrorReport *report);
480 * Possible exception types. These types are part of a JSErrorFormatString
481 * structure. They define which error to throw in case of a runtime error.
482 * JSEXN_NONE marks an unthrowable error.
484 typedef enum JSExnType {
485 JSEXN_NONE = -1,
486 JSEXN_ERR,
487 JSEXN_INTERNALERR,
488 JSEXN_EVALERR,
489 JSEXN_RANGEERR,
490 JSEXN_REFERENCEERR,
491 JSEXN_SYNTAXERR,
492 JSEXN_TYPEERR,
493 JSEXN_URIERR,
494 JSEXN_LIMIT
495 } JSExnType;
497 typedef struct JSErrorFormatString {
498 /* The error format string (UTF-8 if js_CStringsAreUTF8). */
499 const char *format;
501 /* The number of arguments to expand in the formatted error message. */
502 uint16 argCount;
504 /* One of the JSExnType constants above. */
505 int16 exnType;
506 } JSErrorFormatString;
508 typedef const JSErrorFormatString *
509 (* JSErrorCallback)(void *userRef, const char *locale,
510 const uintN errorNumber);
512 #ifdef va_start
513 #define JS_ARGUMENT_FORMATTER_DEFINED 1
515 typedef JSBool
516 (* JSArgumentFormatter)(JSContext *cx, const char *format, JSBool fromJS,
517 jsval **vpp, va_list *app);
518 #endif
520 typedef JSBool
521 (* JSLocaleToUpperCase)(JSContext *cx, JSString *src, jsval *rval);
523 typedef JSBool
524 (* JSLocaleToLowerCase)(JSContext *cx, JSString *src, jsval *rval);
526 typedef JSBool
527 (* JSLocaleCompare)(JSContext *cx, JSString *src1, JSString *src2,
528 jsval *rval);
530 typedef JSBool
531 (* JSLocaleToUnicode)(JSContext *cx, const char *src, jsval *rval);
534 * Security protocol types.
536 typedef struct JSPrincipals JSPrincipals;
539 * XDR-encode or -decode a principals instance, based on whether xdr->mode is
540 * JSXDR_ENCODE, in which case *principalsp should be encoded; or JSXDR_DECODE,
541 * in which case implementations must return a held (via JSPRINCIPALS_HOLD),
542 * non-null *principalsp out parameter. Return true on success, false on any
543 * error, which the implementation must have reported.
545 typedef JSBool
546 (* JSPrincipalsTranscoder)(JSXDRState *xdr, JSPrincipals **principalsp);
549 * Return a weak reference to the principals associated with obj, possibly via
550 * the immutable parent chain leading from obj to a top-level container (e.g.,
551 * a window object in the DOM level 0). If there are no principals associated
552 * with obj, return null. Therefore null does not mean an error was reported;
553 * in no event should an error be reported or an exception be thrown by this
554 * callback's implementation.
556 typedef JSPrincipals *
557 (* JSObjectPrincipalsFinder)(JSContext *cx, JSObject *obj);
560 * Used to check if a CSP instance wants to disable eval() and friends.
561 * See js_CheckCSPPermitsJSAction() in jsobj.
563 typedef JSBool
564 (* JSCSPEvalChecker)(JSContext *cx);
567 * Callback used to ask the embedding for the cross compartment wrapper handler
568 * that implements the desired prolicy for this kind of object in the
569 * destination compartment.
571 typedef JSObject *
572 (* JSWrapObjectCallback)(JSContext *cx, JSObject *obj, JSObject *proto, JSObject *parent,
573 uintN flags);
576 * Callback used by the wrap hook to ask the embedding to prepare an object
577 * for wrapping in a context. This might include unwrapping other wrappers
578 * or even finding a more suitable object for the new compartment.
580 typedef JSObject *
581 (* JSPreWrapCallback)(JSContext *cx, JSObject *scope, JSObject *obj, uintN flags);
583 typedef enum {
584 JSCOMPARTMENT_NEW, /* XXX Does it make sense to have a NEW? */
585 JSCOMPARTMENT_DESTROY
586 } JSCompartmentOp;
588 typedef JSBool
589 (* JSCompartmentCallback)(JSContext *cx, JSCompartment *compartment, uintN compartmentOp);
592 * Read structured data from the reader r. This hook is used to read a value
593 * previously serialized by a call to the WriteStructuredCloneOp hook.
595 * tag and data are the pair of uint32 values from the header. The callback may
596 * use the JS_Read* APIs to read any other relevant parts of the object from
597 * the reader r. closure is any value passed to the JS_ReadStructuredClone
598 * function. Return the new object on success, NULL on error/exception.
600 typedef JSObject *(*ReadStructuredCloneOp)(JSContext *cx, JSStructuredCloneReader *r,
601 uint32 tag, uint32 data, void *closure);
604 * Structured data serialization hook. The engine can write primitive values,
605 * Objects, Arrays, Dates, RegExps, TypedArrays, and ArrayBuffers. Any other
606 * type of object requires application support. This callback must first use
607 * the JS_WriteUint32Pair API to write an object header, passing a value
608 * greater than JS_SCTAG_USER to the tag parameter. Then it can use the
609 * JS_Write* APIs to write any other relevant parts of the value v to the
610 * writer w. closure is any value passed to the JS_WriteStructuredCLone function.
612 * Return true on success, false on error/exception.
614 typedef JSBool (*WriteStructuredCloneOp)(JSContext *cx, JSStructuredCloneWriter *w,
615 JSObject *obj, void *closure);
618 * This is called when JS_WriteStructuredClone finds that the object to be
619 * written is recursive. To follow HTML5, the application must throw a
620 * DATA_CLONE_ERR DOMException. errorid is always JS_SCERR_RECURSION.
622 typedef void (*StructuredCloneErrorOp)(JSContext *cx, uint32 errorid);
624 JS_END_EXTERN_C
626 #endif /* jspubtd_h___ */