1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
* vim: set ts=8 sw=4 et tw=80:
4
* ***** BEGIN LICENSE BLOCK *****
5
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
17
* The Original Code is Mozilla Communicator client code, released
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.
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.
39
* ***** END LICENSE BLOCK ***** */
50
#include "jsarena.h" /* Added by JSIFY */
51
#include "jsutil.h" /* Added by JSIFY */
81
#if JS_HAS_FILE_OBJECT
85
#if JS_HAS_XML_SUPPORT
89
#ifdef HAVE_VA_LIST_AS_ARRAY
90
#define JS_ADDRESSOF_VA_LIST(ap) ((va_list *)(ap))
92
#define JS_ADDRESSOF_VA_LIST(ap) (&(ap))
95
#if defined(JS_PARANOID_REQUEST) && defined(JS_THREADSAFE)
96
#define CHECK_REQUEST(cx) JS_ASSERT(cx->requestDepth)
98
#define CHECK_REQUEST(cx) ((void)0)
108
JS_GetNaNValue(JSContext *cx)
110
return DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
114
JS_GetNegativeInfinityValue(JSContext *cx)
116
return DOUBLE_TO_JSVAL(cx->runtime->jsNegativeInfinity);
120
JS_GetPositiveInfinityValue(JSContext *cx)
122
return DOUBLE_TO_JSVAL(cx->runtime->jsPositiveInfinity);
126
JS_GetEmptyStringValue(JSContext *cx)
128
return STRING_TO_JSVAL(cx->runtime->emptyString);
132
TryArgumentFormatter(JSContext *cx, const char **formatp, JSBool fromJS,
133
jsval **vpp, va_list *app)
136
JSArgumentFormatMap *map;
139
for (map = cx->argumentFormatMap; map; map = map->next) {
140
if (!strncmp(format, map->format, map->length)) {
141
*formatp = format + map->length;
142
return map->formatter(cx, format, fromJS, vpp, app);
145
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_CHAR, format);
149
JS_PUBLIC_API(JSBool)
150
JS_ConvertArguments(JSContext *cx, uintN argc, jsval *argv, const char *format,
156
va_start(ap, format);
157
ok = JS_ConvertArgumentsVA(cx, argc, argv, format, ap);
162
JS_PUBLIC_API(JSBool)
163
JS_ConvertArgumentsVA(JSContext *cx, uintN argc, jsval *argv,
164
const char *format, va_list ap)
177
while ((c = *format++) != '\0') {
184
if (sp == argv + argc) {
186
fun = js_ValueToFunction(cx, &argv[-2], 0);
189
JS_snprintf(numBuf, sizeof numBuf, "%u", argc);
190
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
191
JSMSG_MORE_ARGS_NEEDED,
192
JS_GetFunctionName(fun), numBuf,
193
(argc == 1) ? "" : "s");
201
if (!js_ValueToBoolean(cx, *sp, va_arg(ap, JSBool *)))
205
if (!js_ValueToUint16(cx, *sp, va_arg(ap, uint16 *)))
209
if (!js_ValueToECMAInt32(cx, *sp, va_arg(ap, int32 *)))
213
if (!js_ValueToECMAUint32(cx, *sp, va_arg(ap, uint32 *)))
217
if (!js_ValueToInt32(cx, *sp, va_arg(ap, int32 *)))
221
if (!js_ValueToNumber(cx, *sp, va_arg(ap, jsdouble *)))
225
if (!js_ValueToNumber(cx, *sp, &d))
227
*va_arg(ap, jsdouble *) = js_DoubleToInteger(d);
232
str = js_ValueToString(cx, *sp);
235
*sp = STRING_TO_JSVAL(str);
237
*va_arg(ap, char **) = JS_GetStringBytes(str);
239
*va_arg(ap, jschar **) = JS_GetStringChars(str);
241
*va_arg(ap, JSString **) = str;
244
if (!js_ValueToObject(cx, *sp, &obj))
246
*sp = OBJECT_TO_JSVAL(obj);
247
*va_arg(ap, JSObject **) = obj;
250
obj = js_ValueToFunctionObject(cx, sp, 0);
253
*va_arg(ap, JSFunction **) = (JSFunction *) JS_GetPrivate(cx, obj);
256
*va_arg(ap, jsval *) = *sp;
262
if (!TryArgumentFormatter(cx, &format, JS_TRUE, &sp,
263
JS_ADDRESSOF_VA_LIST(ap))) {
266
/* NB: the formatter already updated sp, so we continue here. */
274
JS_PUBLIC_API(jsval *)
275
JS_PushArguments(JSContext *cx, void **markp, const char *format, ...)
280
va_start(ap, format);
281
argv = JS_PushArgumentsVA(cx, markp, format, ap);
286
JS_PUBLIC_API(jsval *)
287
JS_PushArgumentsVA(JSContext *cx, void **markp, const char *format, va_list ap)
300
for (cp = format; (c = *cp) != '\0'; cp++) {
302
* Count non-space non-star characters as individual jsval arguments.
303
* This may over-allocate stack, but we'll fix below.
305
if (isspace(c) || c == '*')
309
sp = js_AllocStack(cx, argc, markp);
313
while ((c = *format++) != '\0') {
314
if (isspace(c) || c == '*')
318
*sp = BOOLEAN_TO_JSVAL((JSBool) va_arg(ap, int));
321
*sp = INT_TO_JSVAL((uint16) va_arg(ap, unsigned int));
325
if (!js_NewNumberValue(cx, (jsdouble) va_arg(ap, int32), sp))
329
if (!js_NewNumberValue(cx, (jsdouble) va_arg(ap, uint32), sp))
334
if (!js_NewDoubleValue(cx, va_arg(ap, jsdouble), sp))
338
str = JS_NewStringCopyZ(cx, va_arg(ap, char *));
341
*sp = STRING_TO_JSVAL(str);
344
str = JS_NewUCStringCopyZ(cx, va_arg(ap, jschar *));
347
*sp = STRING_TO_JSVAL(str);
350
str = va_arg(ap, JSString *);
351
*sp = STRING_TO_JSVAL(str);
354
*sp = OBJECT_TO_JSVAL(va_arg(ap, JSObject *));
357
fun = va_arg(ap, JSFunction *);
358
*sp = fun ? OBJECT_TO_JSVAL(fun->object) : JSVAL_NULL;
361
*sp = va_arg(ap, jsval);
365
if (!TryArgumentFormatter(cx, &format, JS_FALSE, &sp,
366
JS_ADDRESSOF_VA_LIST(ap))) {
369
/* NB: the formatter already updated sp, so we continue here. */
376
* We may have overallocated stack due to a multi-character format code
377
* handled by a JSArgumentFormatter. Give back that stack space!
379
JS_ASSERT(sp <= argv + argc);
380
if (sp < argv + argc) {
381
/* Return slots not pushed to the current stack arena. */
382
cx->stackPool.current->avail = (jsuword)sp;
384
/* Reduce the count of slots the GC will scan in this stack segment. */
385
sh = cx->stackHeaders;
386
JS_ASSERT(JS_STACK_SEGMENT(sh) + sh->nslots == argv + argc);
387
sh->nslots -= argc - (sp - argv);
392
js_FreeStack(cx, *markp);
397
JS_PopArguments(JSContext *cx, void *mark)
400
js_FreeStack(cx, mark);
403
JS_PUBLIC_API(JSBool)
404
JS_AddArgumentFormatter(JSContext *cx, const char *format,
405
JSArgumentFormatter formatter)
408
JSArgumentFormatMap **mpp, *map;
410
length = strlen(format);
411
mpp = &cx->argumentFormatMap;
412
while ((map = *mpp) != NULL) {
413
/* Insert before any shorter string to match before prefixes. */
414
if (map->length < length)
416
if (map->length == length && !strcmp(map->format, format))
420
map = (JSArgumentFormatMap *) JS_malloc(cx, sizeof *map);
423
map->format = format;
424
map->length = length;
428
map->formatter = formatter;
433
JS_RemoveArgumentFormatter(JSContext *cx, const char *format)
436
JSArgumentFormatMap **mpp, *map;
438
length = strlen(format);
439
mpp = &cx->argumentFormatMap;
440
while ((map = *mpp) != NULL) {
441
if (map->length == length && !strcmp(map->format, format)) {
450
JS_PUBLIC_API(JSBool)
451
JS_ConvertValue(JSContext *cx, jsval v, JSType type, jsval *vp)
465
ok = js_ValueToObject(cx, v, &obj);
467
*vp = OBJECT_TO_JSVAL(obj);
469
case JSTYPE_FUNCTION:
471
obj = js_ValueToFunctionObject(cx, vp, JSV2F_SEARCH_STACK);
475
str = js_ValueToString(cx, v);
478
*vp = STRING_TO_JSVAL(str);
481
ok = js_ValueToNumber(cx, v, &d);
483
dp = js_NewDouble(cx, d, 0);
486
*vp = DOUBLE_TO_JSVAL(dp);
490
ok = js_ValueToBoolean(cx, v, &b);
492
*vp = BOOLEAN_TO_JSVAL(b);
496
JS_snprintf(numBuf, sizeof numBuf, "%d", (int)type);
497
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_TYPE,
506
JS_PUBLIC_API(JSBool)
507
JS_ValueToObject(JSContext *cx, jsval v, JSObject **objp)
510
return js_ValueToObject(cx, v, objp);
513
JS_PUBLIC_API(JSFunction *)
514
JS_ValueToFunction(JSContext *cx, jsval v)
517
return js_ValueToFunction(cx, &v, JSV2F_SEARCH_STACK);
520
JS_PUBLIC_API(JSFunction *)
521
JS_ValueToConstructor(JSContext *cx, jsval v)
524
return js_ValueToFunction(cx, &v, JSV2F_SEARCH_STACK);
527
JS_PUBLIC_API(JSString *)
528
JS_ValueToString(JSContext *cx, jsval v)
531
return js_ValueToString(cx, v);
534
JS_PUBLIC_API(JSBool)
535
JS_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp)
538
return js_ValueToNumber(cx, v, dp);
541
JS_PUBLIC_API(JSBool)
542
JS_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip)
545
return js_ValueToECMAInt32(cx, v, ip);
548
JS_PUBLIC_API(JSBool)
549
JS_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip)
552
return js_ValueToECMAUint32(cx, v, ip);
555
JS_PUBLIC_API(JSBool)
556
JS_ValueToInt32(JSContext *cx, jsval v, int32 *ip)
559
return js_ValueToInt32(cx, v, ip);
562
JS_PUBLIC_API(JSBool)
563
JS_ValueToUint16(JSContext *cx, jsval v, uint16 *ip)
566
return js_ValueToUint16(cx, v, ip);
569
JS_PUBLIC_API(JSBool)
570
JS_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
573
return js_ValueToBoolean(cx, v, bp);
576
JS_PUBLIC_API(JSType)
577
JS_TypeOfValue(JSContext *cx, jsval v)
585
if (JSVAL_IS_OBJECT(v)) {
586
type = JSTYPE_OBJECT; /* XXXbe JSTYPE_NULL for JS2 */
587
obj = JSVAL_TO_OBJECT(v);
590
#if JS_HAS_XML_SUPPORT
591
if (ops == &js_XMLObjectOps.base) {
597
* ECMA 262, 11.4.3 says that any native object that implements
598
* [[Call]] should be of type "function". Note that RegExp and
599
* Script are both of type "function" for compatibility with
600
* older SpiderMonkeys.
602
clasp = OBJ_GET_CLASS(cx, obj);
603
if ((ops == &js_ObjectOps)
605
? (clasp == &js_RegExpClass || clasp == &js_ScriptClass)
606
: clasp == &js_FunctionClass)
607
: ops->call != NULL) {
608
type = JSTYPE_FUNCTION;
611
if (!OBJ_GET_PROPERTY(cx, obj,
612
ATOM_TO_JSID(cx->runtime->atomState
615
JS_ClearPendingException(cx);
616
} else if (JSVAL_IS_FUNCTION(cx, v)) {
617
type = JSTYPE_FUNCTION;
623
} else if (JSVAL_IS_NUMBER(v)) {
624
type = JSTYPE_NUMBER;
625
} else if (JSVAL_IS_STRING(v)) {
626
type = JSTYPE_STRING;
627
} else if (JSVAL_IS_BOOLEAN(v)) {
628
type = JSTYPE_BOOLEAN;
635
JS_PUBLIC_API(const char *)
636
JS_GetTypeName(JSContext *cx, JSType type)
638
if ((uintN)type >= (uintN)JSTYPE_LIMIT)
640
return js_type_str[type];
643
/************************************************************************/
645
JS_PUBLIC_API(JSRuntime *)
646
JS_NewRuntime(uint32 maxbytes)
653
* This code asserts that the numbers associated with the error names in
654
* jsmsg.def are monotonically increasing. It uses values for the error
655
* names enumerated in jscntxt.c. It's not a compiletime check, but it's
656
* better than nothing.
659
#define MSG_DEF(name, number, count, exception, format) \
660
JS_ASSERT(name == errorNumber++);
666
if (!js_InitStringGlobals())
668
rt = (JSRuntime *) malloc(sizeof(JSRuntime));
672
/* Initialize infallibly first, so we can goto bad and JS_DestroyRuntime. */
673
memset(rt, 0, sizeof(JSRuntime));
674
JS_INIT_CLIST(&rt->contextList);
675
JS_INIT_CLIST(&rt->trapList);
676
JS_INIT_CLIST(&rt->watchPointList);
678
if (!js_InitGC(rt, maxbytes))
681
rt->gcLock = JS_NEW_LOCK();
684
rt->gcDone = JS_NEW_CONDVAR(rt->gcLock);
687
rt->requestDone = JS_NEW_CONDVAR(rt->gcLock);
688
if (!rt->requestDone)
690
/* this is asymmetric with JS_ShutDown: */
691
if (!js_SetupLocks(8, 16))
693
rt->rtLock = JS_NEW_LOCK();
696
rt->stateChange = JS_NEW_CONDVAR(rt->gcLock);
697
if (!rt->stateChange)
699
rt->setSlotLock = JS_NEW_LOCK();
700
if (!rt->setSlotLock)
702
rt->setSlotDone = JS_NEW_CONDVAR(rt->setSlotLock);
703
if (!rt->setSlotDone)
705
rt->scopeSharingDone = JS_NEW_CONDVAR(rt->gcLock);
706
if (!rt->scopeSharingDone)
708
rt->scopeSharingTodo = NO_SCOPE_SHARING_TODO;
710
rt->propertyCache.empty = JS_TRUE;
711
if (!js_InitPropertyTree(rt))
716
JS_DestroyRuntime(rt);
721
JS_DestroyRuntime(JSRuntime *rt)
724
/* Don't hurt everyone in leaky ol' Mozilla with a fatal JS_ASSERT! */
725
if (!JS_CLIST_IS_EMPTY(&rt->contextList)) {
726
JSContext *cx, *iter = NULL;
728
while ((cx = js_ContextIterator(rt, JS_TRUE, &iter)) != NULL)
731
"JS API usage error: %u contexts left in runtime upon JS_DestroyRuntime.\n",
736
js_FreeRuntimeScriptState(rt);
737
js_FinishAtomState(&rt->atomState);
741
JS_DESTROY_LOCK(rt->gcLock);
743
JS_DESTROY_CONDVAR(rt->gcDone);
745
JS_DESTROY_CONDVAR(rt->requestDone);
747
JS_DESTROY_LOCK(rt->rtLock);
749
JS_DESTROY_CONDVAR(rt->stateChange);
751
JS_DESTROY_LOCK(rt->setSlotLock);
753
JS_DESTROY_CONDVAR(rt->setSlotDone);
754
if (rt->scopeSharingDone)
755
JS_DESTROY_CONDVAR(rt->scopeSharingDone);
757
js_FinishPropertyTree(rt);
766
js_FreeStringGlobals();
772
JS_PUBLIC_API(void *)
773
JS_GetRuntimePrivate(JSRuntime *rt)
779
JS_SetRuntimePrivate(JSRuntime *rt, void *data)
787
JS_BeginRequest(JSContext *cx)
791
JS_ASSERT(cx->thread);
792
if (!cx->requestDepth) {
793
/* Wait until the GC is finished. */
797
/* NB: we use cx->thread here, not js_CurrentThreadId(). */
798
if (rt->gcThread != cx->thread) {
799
while (rt->gcLevel > 0)
800
JS_AWAIT_GC_DONE(rt);
803
/* Indicate that a request is running. */
805
cx->requestDepth = 1;
813
JS_EndRequest(JSContext *cx)
816
JSScope *scope, **todop;
820
JS_ASSERT(cx->requestDepth > 0);
821
if (cx->requestDepth == 1) {
822
/* Lock before clearing to interlock with ClaimScope, in jslock.c. */
825
cx->requestDepth = 0;
827
/* See whether cx has any single-threaded scopes to start sharing. */
828
todop = &rt->scopeSharingTodo;
830
while ((scope = *todop) != NO_SCOPE_SHARING_TODO) {
831
if (scope->ownercx != cx) {
832
todop = &scope->u.link;
835
*todop = scope->u.link;
836
scope->u.link = NULL; /* null u.link for sanity ASAP */
839
* If js_DropObjectMap returns null, we held the last ref to scope.
840
* The waiting thread(s) must have been killed, after which the GC
841
* collected the object that held this scope. Unlikely, because it
842
* requires that the GC ran (e.g., from a branch callback) during
843
* this request, but possible.
845
if (js_DropObjectMap(cx, &scope->map, NULL)) {
846
js_InitLock(&scope->lock);
847
scope->u.count = 0; /* NULL may not pun as 0 */
848
js_FinishSharingScope(rt, scope); /* set ownercx = NULL */
853
JS_NOTIFY_ALL_CONDVAR(rt->scopeSharingDone);
855
/* Give the GC a chance to run if this was the last request running. */
856
JS_ASSERT(rt->requestCount > 0);
858
if (rt->requestCount == 0)
859
JS_NOTIFY_REQUEST_DONE(rt);
868
/* Yield to pending GC operations, regardless of request depth */
870
JS_YieldRequest(JSContext *cx)
874
JS_ASSERT(cx->thread);
879
JS_ASSERT(rt->requestCount > 0);
881
if (rt->requestCount == 0)
882
JS_NOTIFY_REQUEST_DONE(rt);
884
/* XXXbe give the GC or another request calling it a chance to run here?
885
Assumes FIFO scheduling */
891
JS_PUBLIC_API(jsrefcount)
892
JS_SuspendRequest(JSContext *cx)
894
jsrefcount saveDepth = cx->requestDepth;
896
while (cx->requestDepth)
902
JS_ResumeRequest(JSContext *cx, jsrefcount saveDepth)
904
JS_ASSERT(!cx->requestDepth);
905
while (--saveDepth >= 0)
909
#endif /* JS_THREADSAFE */
912
JS_Lock(JSRuntime *rt)
918
JS_Unlock(JSRuntime *rt)
920
JS_UNLOCK_RUNTIME(rt);
923
JS_PUBLIC_API(JSContext *)
924
JS_NewContext(JSRuntime *rt, size_t stackChunkSize)
926
return js_NewContext(rt, stackChunkSize);
930
JS_DestroyContext(JSContext *cx)
932
js_DestroyContext(cx, JS_FORCE_GC);
936
JS_DestroyContextNoGC(JSContext *cx)
938
js_DestroyContext(cx, JS_NO_GC);
942
JS_DestroyContextMaybeGC(JSContext *cx)
944
js_DestroyContext(cx, JS_MAYBE_GC);
947
JS_PUBLIC_API(void *)
948
JS_GetContextPrivate(JSContext *cx)
954
JS_SetContextPrivate(JSContext *cx, void *data)
959
JS_PUBLIC_API(JSRuntime *)
960
JS_GetRuntime(JSContext *cx)
965
JS_PUBLIC_API(JSContext *)
966
JS_ContextIterator(JSRuntime *rt, JSContext **iterp)
968
return js_ContextIterator(rt, JS_TRUE, iterp);
971
JS_PUBLIC_API(JSVersion)
972
JS_GetVersion(JSContext *cx)
974
return cx->version & JSVERSION_MASK;
977
JS_PUBLIC_API(JSVersion)
978
JS_SetVersion(JSContext *cx, JSVersion version)
980
JSVersion oldVersion;
982
JS_ASSERT(version != JSVERSION_UNKNOWN);
983
JS_ASSERT((version & ~JSVERSION_MASK) == 0);
985
oldVersion = cx->version & JSVERSION_MASK;
986
if (version == oldVersion)
989
cx->version = (cx->version & ~JSVERSION_MASK) | version;
990
js_OnVersionChange(cx);
994
static struct v2smap {
998
{JSVERSION_1_0, "1.0"},
999
{JSVERSION_1_1, "1.1"},
1000
{JSVERSION_1_2, "1.2"},
1001
{JSVERSION_1_3, "1.3"},
1002
{JSVERSION_1_4, "1.4"},
1003
{JSVERSION_ECMA_3, "ECMAv3"},
1004
{JSVERSION_1_5, "1.5"},
1005
{JSVERSION_1_6, "1.6"},
1006
{JSVERSION_DEFAULT, js_default_str},
1007
{JSVERSION_UNKNOWN, NULL}, /* must be last, NULL is sentinel */
1010
JS_PUBLIC_API(const char *)
1011
JS_VersionToString(JSVersion version)
1015
for (i = 0; v2smap[i].string; i++)
1016
if (v2smap[i].version == version)
1017
return v2smap[i].string;
1021
JS_PUBLIC_API(JSVersion)
1022
JS_StringToVersion(const char *string)
1026
for (i = 0; v2smap[i].string; i++)
1027
if (strcmp(v2smap[i].string, string) == 0)
1028
return v2smap[i].version;
1029
return JSVERSION_UNKNOWN;
1032
JS_PUBLIC_API(uint32)
1033
JS_GetOptions(JSContext *cx)
1038
#define SYNC_OPTIONS_TO_VERSION(cx) \
1040
if ((cx)->options & JSOPTION_XML) \
1041
(cx)->version |= JSVERSION_HAS_XML; \
1043
(cx)->version &= ~JSVERSION_HAS_XML; \
1046
JS_PUBLIC_API(uint32)
1047
JS_SetOptions(JSContext *cx, uint32 options)
1049
uint32 oldopts = cx->options;
1050
cx->options = options;
1051
SYNC_OPTIONS_TO_VERSION(cx);
1055
JS_PUBLIC_API(uint32)
1056
JS_ToggleOptions(JSContext *cx, uint32 options)
1058
uint32 oldopts = cx->options;
1059
cx->options ^= options;
1060
SYNC_OPTIONS_TO_VERSION(cx);
1064
JS_PUBLIC_API(const char *)
1065
JS_GetImplementationVersion(void)
1067
return "JavaScript-C 1.6.1 2007-04-01";
1071
JS_PUBLIC_API(JSObject *)
1072
JS_GetGlobalObject(JSContext *cx)
1074
return cx->globalObject;
1078
JS_SetGlobalObject(JSContext *cx, JSObject *obj)
1080
cx->globalObject = obj;
1081
#if JS_HAS_XML_SUPPORT
1082
cx->xmlSettingFlags = 0;
1087
InitFunctionAndObjectClasses(JSContext *cx, JSObject *obj)
1089
JSDHashTable *table;
1093
JSResolvingEntry *entry;
1094
JSObject *fun_proto, *obj_proto;
1096
/* If cx has no global object, use obj so prototypes can be found. */
1097
if (!cx->globalObject)
1098
JS_SetGlobalObject(cx, obj);
1100
/* Record Function and Object in cx->resolvingTable, if we are resolving. */
1101
table = cx->resolvingTable;
1102
resolving = (table && table->entryCount);
1106
key.id = ATOM_TO_JSID(rt->atomState.FunctionAtom);
1107
entry = (JSResolvingEntry *)
1108
JS_DHashTableOperate(table, &key, JS_DHASH_ADD);
1109
if (entry && entry->key.obj && (entry->flags & JSRESFLAG_LOOKUP)) {
1110
/* Already resolving Function, record Object too. */
1111
JS_ASSERT(entry->key.obj == obj);
1112
key.id = ATOM_TO_JSID(rt->atomState.ObjectAtom);
1113
entry = (JSResolvingEntry *)
1114
JS_DHashTableOperate(table, &key, JS_DHASH_ADD);
1117
JS_ReportOutOfMemory(cx);
1120
JS_ASSERT(!entry->key.obj && entry->flags == 0);
1122
entry->flags = JSRESFLAG_LOOKUP;
1125
/* Initialize the function class first so constructors can be made. */
1126
fun_proto = js_InitFunctionClass(cx, obj);
1130
/* Initialize the object class next so Object.prototype works. */
1131
obj_proto = js_InitObjectClass(cx, obj);
1137
/* Function.prototype and the global object delegate to Object.prototype. */
1138
OBJ_SET_PROTO(cx, fun_proto, obj_proto);
1139
if (!OBJ_GET_PROTO(cx, obj))
1140
OBJ_SET_PROTO(cx, obj, obj_proto);
1143
/* If resolving, remove the other entry (Object or Function) from table. */
1145
JS_DHashTableOperate(table, &key, JS_DHASH_REMOVE);
1149
JS_PUBLIC_API(JSBool)
1150
JS_InitStandardClasses(JSContext *cx, JSObject *obj)
1154
#if JS_HAS_UNDEFINED
1156
/* Define a top-level property 'undefined' with the undefined value. */
1157
JSAtom *atom = cx->runtime->atomState.typeAtoms[JSTYPE_VOID];
1158
if (!OBJ_DEFINE_PROPERTY(cx, obj, ATOM_TO_JSID(atom), JSVAL_VOID,
1159
NULL, NULL, JSPROP_PERMANENT, NULL)) {
1165
/* Function and Object require cooperative bootstrapping magic. */
1166
if (!InitFunctionAndObjectClasses(cx, obj))
1169
/* Initialize the rest of the standard objects and functions. */
1170
return js_InitArrayClass(cx, obj) &&
1171
js_InitBooleanClass(cx, obj) &&
1172
js_InitMathClass(cx, obj) &&
1173
js_InitNumberClass(cx, obj) &&
1174
js_InitStringClass(cx, obj) &&
1175
#if JS_HAS_CALL_OBJECT
1176
js_InitCallClass(cx, obj) &&
1179
js_InitRegExpClass(cx, obj) &&
1181
#if JS_HAS_SCRIPT_OBJECT
1182
js_InitScriptClass(cx, obj) &&
1184
#if JS_HAS_ERROR_EXCEPTIONS
1185
js_InitExceptionClasses(cx, obj) &&
1187
#if JS_HAS_XML_SUPPORT
1188
js_InitXMLClasses(cx, obj) &&
1190
#if JS_HAS_FILE_OBJECT
1191
js_InitFileClass(cx, obj) &&
1193
js_InitDateClass(cx, obj);
1196
#define ATOM_OFFSET(name) offsetof(JSAtomState, name##Atom)
1197
#define OFFSET_TO_ATOM(rt,off) (*(JSAtom **)((char*)&(rt)->atomState + (off)))
1200
* Table of class initializers and their atom offsets in rt->atomState.
1201
* If you add a "standard" class, remember to update this table.
1206
} standard_class_atoms[] = {
1207
{InitFunctionAndObjectClasses, ATOM_OFFSET(Function)},
1208
{InitFunctionAndObjectClasses, ATOM_OFFSET(Object)},
1209
{js_InitArrayClass, ATOM_OFFSET(Array)},
1210
{js_InitBooleanClass, ATOM_OFFSET(Boolean)},
1211
{js_InitDateClass, ATOM_OFFSET(Date)},
1212
{js_InitMathClass, ATOM_OFFSET(Math)},
1213
{js_InitNumberClass, ATOM_OFFSET(Number)},
1214
{js_InitStringClass, ATOM_OFFSET(String)},
1215
#if JS_HAS_CALL_OBJECT
1216
{js_InitCallClass, ATOM_OFFSET(Call)},
1218
#if JS_HAS_ERROR_EXCEPTIONS
1219
{js_InitExceptionClasses, ATOM_OFFSET(Error)},
1222
{js_InitRegExpClass, ATOM_OFFSET(RegExp)},
1224
#if JS_HAS_SCRIPT_OBJECT
1225
{js_InitScriptClass, ATOM_OFFSET(Script)},
1227
#if JS_HAS_XML_SUPPORT
1228
{js_InitXMLClass, ATOM_OFFSET(XML)},
1229
{js_InitNamespaceClass, ATOM_OFFSET(Namespace)},
1230
{js_InitQNameClass, ATOM_OFFSET(QName)},
1232
#if JS_HAS_FILE_OBJECT
1233
{js_InitFileClass, ATOM_OFFSET(File)},
1239
* Table of top-level function and constant names and their init functions.
1240
* If you add a "standard" global function or property, remember to update
1243
typedef struct JSStdName {
1245
size_t atomOffset; /* offset of atom pointer in JSAtomState */
1246
const char *name; /* null if atom is pre-pinned, else name */
1250
StdNameToAtom(JSContext *cx, JSStdName *stdn)
1256
offset = stdn->atomOffset;
1257
atom = OFFSET_TO_ATOM(cx->runtime, offset);
1261
atom = js_Atomize(cx, name, strlen(name), ATOM_PINNED);
1262
OFFSET_TO_ATOM(cx->runtime, offset) = atom;
1268
#define EAGERLY_PINNED_ATOM(name) ATOM_OFFSET(name), NULL
1269
#define LAZILY_PINNED_ATOM(name) ATOM_OFFSET(lazy.name), js_##name##_str
1271
static JSStdName standard_class_names[] = {
1272
/* ECMA requires that eval be a direct property of the global object. */
1273
{js_InitObjectClass, EAGERLY_PINNED_ATOM(eval)},
1275
/* Global properties and functions defined by the Number class. */
1276
{js_InitNumberClass, LAZILY_PINNED_ATOM(NaN)},
1277
{js_InitNumberClass, LAZILY_PINNED_ATOM(Infinity)},
1278
{js_InitNumberClass, LAZILY_PINNED_ATOM(isNaN)},
1279
{js_InitNumberClass, LAZILY_PINNED_ATOM(isFinite)},
1280
{js_InitNumberClass, LAZILY_PINNED_ATOM(parseFloat)},
1281
{js_InitNumberClass, LAZILY_PINNED_ATOM(parseInt)},
1283
/* String global functions. */
1284
{js_InitStringClass, LAZILY_PINNED_ATOM(escape)},
1285
{js_InitStringClass, LAZILY_PINNED_ATOM(unescape)},
1286
{js_InitStringClass, LAZILY_PINNED_ATOM(decodeURI)},
1287
{js_InitStringClass, LAZILY_PINNED_ATOM(encodeURI)},
1288
{js_InitStringClass, LAZILY_PINNED_ATOM(decodeURIComponent)},
1289
{js_InitStringClass, LAZILY_PINNED_ATOM(encodeURIComponent)},
1291
{js_InitStringClass, LAZILY_PINNED_ATOM(uneval)},
1294
/* Exception constructors. */
1295
#if JS_HAS_ERROR_EXCEPTIONS
1296
{js_InitExceptionClasses, EAGERLY_PINNED_ATOM(Error)},
1297
{js_InitExceptionClasses, LAZILY_PINNED_ATOM(InternalError)},
1298
{js_InitExceptionClasses, LAZILY_PINNED_ATOM(EvalError)},
1299
{js_InitExceptionClasses, LAZILY_PINNED_ATOM(RangeError)},
1300
{js_InitExceptionClasses, LAZILY_PINNED_ATOM(ReferenceError)},
1301
{js_InitExceptionClasses, LAZILY_PINNED_ATOM(SyntaxError)},
1302
{js_InitExceptionClasses, LAZILY_PINNED_ATOM(TypeError)},
1303
{js_InitExceptionClasses, LAZILY_PINNED_ATOM(URIError)},
1306
#if JS_HAS_XML_SUPPORT
1307
{js_InitAnyNameClass, LAZILY_PINNED_ATOM(AnyName)},
1308
{js_InitAttributeNameClass, LAZILY_PINNED_ATOM(AttributeName)},
1309
{js_InitXMLClass, LAZILY_PINNED_ATOM(XMLList)},
1310
{js_InitXMLClass, LAZILY_PINNED_ATOM(isXMLName)},
1316
static JSStdName object_prototype_names[] = {
1317
/* Object.prototype properties (global delegates to Object.prototype). */
1318
{js_InitObjectClass, EAGERLY_PINNED_ATOM(proto)},
1319
{js_InitObjectClass, EAGERLY_PINNED_ATOM(parent)},
1320
{js_InitObjectClass, EAGERLY_PINNED_ATOM(count)},
1322
{js_InitObjectClass, EAGERLY_PINNED_ATOM(toSource)},
1324
{js_InitObjectClass, EAGERLY_PINNED_ATOM(toString)},
1325
{js_InitObjectClass, EAGERLY_PINNED_ATOM(toLocaleString)},
1326
{js_InitObjectClass, EAGERLY_PINNED_ATOM(valueOf)},
1327
#if JS_HAS_OBJ_WATCHPOINT
1328
{js_InitObjectClass, LAZILY_PINNED_ATOM(watch)},
1329
{js_InitObjectClass, LAZILY_PINNED_ATOM(unwatch)},
1331
#if JS_HAS_NEW_OBJ_METHODS
1332
{js_InitObjectClass, LAZILY_PINNED_ATOM(hasOwnProperty)},
1333
{js_InitObjectClass, LAZILY_PINNED_ATOM(isPrototypeOf)},
1334
{js_InitObjectClass, LAZILY_PINNED_ATOM(propertyIsEnumerable)},
1336
#if JS_HAS_GETTER_SETTER
1337
{js_InitObjectClass, LAZILY_PINNED_ATOM(defineGetter)},
1338
{js_InitObjectClass, LAZILY_PINNED_ATOM(defineSetter)},
1339
{js_InitObjectClass, LAZILY_PINNED_ATOM(lookupGetter)},
1340
{js_InitObjectClass, LAZILY_PINNED_ATOM(lookupSetter)},
1346
#undef EAGERLY_PINNED_ATOM
1347
#undef LAZILY_PINNED_ATOM
1349
JS_PUBLIC_API(JSBool)
1350
JS_ResolveStandardClass(JSContext *cx, JSObject *obj, jsval id,
1360
*resolved = JS_FALSE;
1362
if (!JSVAL_IS_STRING(id))
1364
idstr = JSVAL_TO_STRING(id);
1367
#if JS_HAS_UNDEFINED
1368
/* Check whether we're resolving 'undefined', and define it if so. */
1369
atom = rt->atomState.typeAtoms[JSTYPE_VOID];
1370
if (idstr == ATOM_TO_STRING(atom)) {
1371
*resolved = JS_TRUE;
1372
return OBJ_DEFINE_PROPERTY(cx, obj, ATOM_TO_JSID(atom), JSVAL_VOID,
1373
NULL, NULL, JSPROP_PERMANENT, NULL);
1377
/* Try for class constructors/prototypes named by well-known atoms. */
1379
for (i = 0; standard_class_atoms[i].init; i++) {
1380
atom = OFFSET_TO_ATOM(rt, standard_class_atoms[i].atomOffset);
1381
if (idstr == ATOM_TO_STRING(atom)) {
1382
init = standard_class_atoms[i].init;
1388
/* Try less frequently used top-level functions and constants. */
1389
for (i = 0; standard_class_names[i].init; i++) {
1390
atom = StdNameToAtom(cx, &standard_class_names[i]);
1393
if (idstr == ATOM_TO_STRING(atom)) {
1394
init = standard_class_names[i].init;
1399
if (!init && !OBJ_GET_PROTO(cx, obj)) {
1401
* Try even less frequently used names delegated from the global
1402
* object to Object.prototype, but only if the Object class hasn't
1403
* yet been initialized.
1405
for (i = 0; object_prototype_names[i].init; i++) {
1406
atom = StdNameToAtom(cx, &object_prototype_names[i]);
1409
if (idstr == ATOM_TO_STRING(atom)) {
1410
init = standard_class_names[i].init;
1420
*resolved = JS_TRUE;
1426
AlreadyHasOwnProperty(JSObject *obj, JSAtom *atom)
1428
JS_ASSERT(OBJ_IS_NATIVE(obj));
1429
return SCOPE_GET_PROPERTY(OBJ_SCOPE(obj), ATOM_TO_JSID(atom)) != NULL;
1432
JS_PUBLIC_API(JSBool)
1433
JS_EnumerateStandardClasses(JSContext *cx, JSObject *obj)
1442
#if JS_HAS_UNDEFINED
1443
/* Check whether we need to bind 'undefined' and define it if so. */
1444
atom = rt->atomState.typeAtoms[JSTYPE_VOID];
1445
if (!AlreadyHasOwnProperty(obj, atom) &&
1446
!OBJ_DEFINE_PROPERTY(cx, obj, ATOM_TO_JSID(atom), JSVAL_VOID,
1447
NULL, NULL, JSPROP_PERMANENT, NULL)) {
1452
/* Initialize any classes that have not been resolved yet. */
1453
for (i = 0; standard_class_atoms[i].init; i++) {
1454
atom = OFFSET_TO_ATOM(rt, standard_class_atoms[i].atomOffset);
1455
if (!AlreadyHasOwnProperty(obj, atom) &&
1456
!standard_class_atoms[i].init(cx, obj)) {
1465
AddAtomToArray(JSContext *cx, JSAtom *atom, JSIdArray *ida, jsint *ip)
1470
length = ida->length;
1472
ida = js_SetIdArrayLength(cx, ida, JS_MAX(length * 2, 8));
1475
JS_ASSERT(i < ida->length);
1477
ida->vector[i] = ATOM_TO_JSID(atom);
1483
EnumerateIfResolved(JSContext *cx, JSObject *obj, JSAtom *atom, JSIdArray *ida,
1484
jsint *ip, JSBool *foundp)
1486
*foundp = AlreadyHasOwnProperty(obj, atom);
1488
ida = AddAtomToArray(cx, atom, ida, ip);
1492
JS_PUBLIC_API(JSIdArray *)
1493
JS_EnumerateResolvedStandardClasses(JSContext *cx, JSObject *obj,
1507
ida = js_NewIdArray(cx, 8);
1513
#if JS_HAS_UNDEFINED
1514
/* Check whether 'undefined' has been resolved and enumerate it if so. */
1515
atom = rt->atomState.typeAtoms[JSTYPE_VOID];
1516
ida = EnumerateIfResolved(cx, obj, atom, ida, &i, &found);
1521
/* Enumerate only classes that *have* been resolved. */
1522
for (j = 0; standard_class_atoms[j].init; j++) {
1523
atom = OFFSET_TO_ATOM(rt, standard_class_atoms[j].atomOffset);
1524
ida = EnumerateIfResolved(cx, obj, atom, ida, &i, &found);
1529
init = standard_class_atoms[j].init;
1531
for (k = 0; standard_class_names[k].init; k++) {
1532
if (standard_class_names[k].init == init) {
1533
atom = StdNameToAtom(cx, &standard_class_names[k]);
1534
ida = AddAtomToArray(cx, atom, ida, &i);
1540
if (init == js_InitObjectClass) {
1541
for (k = 0; object_prototype_names[k].init; k++) {
1542
atom = StdNameToAtom(cx, &object_prototype_names[k]);
1543
ida = AddAtomToArray(cx, atom, ida, &i);
1551
/* Trim to exact length via js_SetIdArrayLength. */
1552
return js_SetIdArrayLength(cx, ida, i);
1556
#undef OFFSET_TO_ATOM
1558
JS_PUBLIC_API(JSObject *)
1559
JS_GetScopeChain(JSContext *cx)
1561
return cx->fp ? cx->fp->scopeChain : NULL;
1564
JS_PUBLIC_API(void *)
1565
JS_malloc(JSContext *cx, size_t nbytes)
1569
JS_ASSERT(nbytes != 0);
1572
cx->runtime->gcMallocBytes += nbytes;
1575
JS_ReportOutOfMemory(cx);
1579
JS_PUBLIC_API(void *)
1580
JS_realloc(JSContext *cx, void *p, size_t nbytes)
1582
p = realloc(p, nbytes);
1584
JS_ReportOutOfMemory(cx);
1589
JS_free(JSContext *cx, void *p)
1595
JS_PUBLIC_API(char *)
1596
JS_strdup(JSContext *cx, const char *s)
1602
p = JS_malloc(cx, n);
1605
return (char *)memcpy(p, s, n);
1608
JS_PUBLIC_API(jsdouble *)
1609
JS_NewDouble(JSContext *cx, jsdouble d)
1612
return js_NewDouble(cx, d, 0);
1615
JS_PUBLIC_API(JSBool)
1616
JS_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval)
1619
return js_NewDoubleValue(cx, d, rval);
1622
JS_PUBLIC_API(JSBool)
1623
JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval)
1626
return js_NewNumberValue(cx, d, rval);
1630
JS_PUBLIC_API(JSBool)
1631
JS_AddRoot(JSContext *cx, void *rp)
1634
return js_AddRoot(cx, rp, NULL);
1637
JS_PUBLIC_API(JSBool)
1638
JS_AddNamedRootRT(JSRuntime *rt, void *rp, const char *name)
1640
return js_AddRootRT(rt, rp, name);
1643
JS_PUBLIC_API(JSBool)
1644
JS_RemoveRoot(JSContext *cx, void *rp)
1647
return js_RemoveRoot(cx->runtime, rp);
1650
JS_PUBLIC_API(JSBool)
1651
JS_RemoveRootRT(JSRuntime *rt, void *rp)
1653
return js_RemoveRoot(rt, rp);
1656
JS_PUBLIC_API(JSBool)
1657
JS_AddNamedRoot(JSContext *cx, void *rp, const char *name)
1660
return js_AddRoot(cx, rp, name);
1664
JS_ClearNewbornRoots(JSContext *cx)
1666
JS_CLEAR_WEAK_ROOTS(&cx->weakRoots);
1669
JS_PUBLIC_API(JSBool)
1670
JS_EnterLocalRootScope(JSContext *cx)
1673
return js_EnterLocalRootScope(cx);
1677
JS_LeaveLocalRootScope(JSContext *cx)
1680
js_LeaveLocalRootScope(cx);
1684
JS_ForgetLocalRoot(JSContext *cx, void *thing)
1687
js_ForgetLocalRoot(cx, (jsval) thing);
1690
#include "jshash.h" /* Added by JSIFY */
1694
typedef struct NamedRootDumpArgs {
1695
void (*dump)(const char *name, void *rp, void *data);
1697
} NamedRootDumpArgs;
1699
JS_STATIC_DLL_CALLBACK(JSDHashOperator)
1700
js_named_root_dumper(JSDHashTable *table, JSDHashEntryHdr *hdr, uint32 number,
1703
NamedRootDumpArgs *args = (NamedRootDumpArgs *) arg;
1704
JSGCRootHashEntry *rhe = (JSGCRootHashEntry *)hdr;
1707
args->dump(rhe->name, rhe->root, args->data);
1708
return JS_DHASH_NEXT;
1712
JS_DumpNamedRoots(JSRuntime *rt,
1713
void (*dump)(const char *name, void *rp, void *data),
1716
NamedRootDumpArgs args;
1720
JS_DHashTableEnumerate(&rt->gcRootsHash, js_named_root_dumper, &args);
1725
typedef struct GCRootMapArgs {
1730
JS_STATIC_DLL_CALLBACK(JSDHashOperator)
1731
js_gcroot_mapper(JSDHashTable *table, JSDHashEntryHdr *hdr, uint32 number,
1734
GCRootMapArgs *args = (GCRootMapArgs *) arg;
1735
JSGCRootHashEntry *rhe = (JSGCRootHashEntry *)hdr;
1739
mapflags = args->map(rhe->root, rhe->name, args->data);
1741
#if JS_MAP_GCROOT_NEXT == JS_DHASH_NEXT && \
1742
JS_MAP_GCROOT_STOP == JS_DHASH_STOP && \
1743
JS_MAP_GCROOT_REMOVE == JS_DHASH_REMOVE
1744
op = (JSDHashOperator)mapflags;
1747
if (mapflags & JS_MAP_GCROOT_STOP)
1748
op |= JS_DHASH_STOP;
1749
if (mapflags & JS_MAP_GCROOT_REMOVE)
1750
op |= JS_DHASH_REMOVE;
1756
JS_PUBLIC_API(uint32)
1757
JS_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data)
1765
rv = JS_DHashTableEnumerate(&rt->gcRootsHash, js_gcroot_mapper, &args);
1770
JS_PUBLIC_API(JSBool)
1771
JS_LockGCThing(JSContext *cx, void *thing)
1776
ok = js_LockGCThing(cx, thing);
1778
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_LOCK);
1782
JS_PUBLIC_API(JSBool)
1783
JS_LockGCThingRT(JSRuntime *rt, void *thing)
1785
return js_LockGCThingRT(rt, thing);
1788
JS_PUBLIC_API(JSBool)
1789
JS_UnlockGCThing(JSContext *cx, void *thing)
1794
ok = js_UnlockGCThingRT(cx->runtime, thing);
1796
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_UNLOCK);
1800
JS_PUBLIC_API(JSBool)
1801
JS_UnlockGCThingRT(JSRuntime *rt, void *thing)
1803
return js_UnlockGCThingRT(rt, thing);
1807
JS_MarkGCThing(JSContext *cx, void *thing, const char *name, void *arg)
1809
JS_ASSERT(cx->runtime->gcLevel > 0);
1810
#ifdef JS_THREADSAFE
1811
JS_ASSERT(cx->runtime->gcThread == js_CurrentThreadId());
1814
GC_MARK(cx, thing, name, arg);
1818
JS_GC(JSContext *cx)
1820
/* Don't nuke active arenas if executing or compiling. */
1821
if (cx->stackPool.current == &cx->stackPool.first)
1822
JS_FinishArenaPool(&cx->stackPool);
1823
if (cx->tempPool.current == &cx->tempPool.first)
1824
JS_FinishArenaPool(&cx->tempPool);
1829
JS_MaybeGC(JSContext *cx)
1831
#ifdef WAY_TOO_MUCH_GC
1835
uint32 bytes, lastBytes;
1838
bytes = rt->gcBytes;
1839
lastBytes = rt->gcLastBytes;
1840
if ((bytes > 8192 && bytes > lastBytes + lastBytes / 2) ||
1841
rt->gcMallocBytes > rt->gcMaxMallocBytes) {
1843
* Run the GC if we have half again as many bytes of GC-things as
1844
* the last time we GC'd, or if we have malloc'd more bytes through
1845
* JS_malloc than we were told to allocate by JS_NewRuntime.
1852
JS_PUBLIC_API(JSGCCallback)
1853
JS_SetGCCallback(JSContext *cx, JSGCCallback cb)
1855
return JS_SetGCCallbackRT(cx->runtime, cb);
1858
JS_PUBLIC_API(JSGCCallback)
1859
JS_SetGCCallbackRT(JSRuntime *rt, JSGCCallback cb)
1863
oldcb = rt->gcCallback;
1864
rt->gcCallback = cb;
1868
JS_PUBLIC_API(JSBool)
1869
JS_IsAboutToBeFinalized(JSContext *cx, void *thing)
1872
return js_IsAboutToBeFinalized(cx, thing);
1876
JS_SetGCParameter(JSRuntime *rt, JSGCParamKey key, uint32 value)
1879
case JSGC_MAX_BYTES:
1880
rt->gcMaxBytes = value;
1882
case JSGC_MAX_MALLOC_BYTES:
1883
rt->gcMaxMallocBytes = value;
1889
JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer)
1891
return js_ChangeExternalStringFinalizer(NULL, finalizer);
1895
JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer)
1897
return js_ChangeExternalStringFinalizer(finalizer, NULL);
1900
JS_PUBLIC_API(JSString *)
1901
JS_NewExternalString(JSContext *cx, jschar *chars, size_t length, intN type)
1906
JS_ASSERT(GCX_EXTERNAL_STRING <= type && type < (intN) GCX_NTYPES);
1908
str = (JSString *) js_NewGCThing(cx, (uintN) type, sizeof(JSString));
1911
str->length = length;
1917
JS_GetExternalStringGCType(JSRuntime *rt, JSString *str)
1919
uint8 type = (uint8) (*js_GetGCThingFlags(str) & GCF_TYPEMASK);
1921
if (type >= GCX_EXTERNAL_STRING)
1923
JS_ASSERT(type == GCX_STRING || type == GCX_MUTABLE_STRING);
1928
JS_SetThreadStackLimit(JSContext *cx, jsuword limitAddr)
1930
#if JS_STACK_GROWTH_DIRECTION > 0
1932
limitAddr = (jsuword)-1;
1934
cx->stackLimit = limitAddr;
1937
/************************************************************************/
1940
JS_DestroyIdArray(JSContext *cx, JSIdArray *ida)
1945
JS_PUBLIC_API(JSBool)
1946
JS_ValueToId(JSContext *cx, jsval v, jsid *idp)
1951
if (JSVAL_IS_INT(v)) {
1954
atom = js_ValueToStringAtom(cx, v);
1957
*idp = ATOM_TO_JSID(atom);
1962
JS_PUBLIC_API(JSBool)
1963
JS_IdToValue(JSContext *cx, jsid id, jsval *vp)
1966
*vp = ID_TO_VALUE(id);
1970
JS_PUBLIC_API(JSBool)
1971
JS_PropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
1976
JS_PUBLIC_API(JSBool)
1977
JS_EnumerateStub(JSContext *cx, JSObject *obj)
1982
JS_PUBLIC_API(JSBool)
1983
JS_ResolveStub(JSContext *cx, JSObject *obj, jsval id)
1988
JS_PUBLIC_API(JSBool)
1989
JS_ConvertStub(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
1991
#if JS_BUG_EAGER_TOSTRING
1992
if (type == JSTYPE_STRING)
1995
js_TryValueOf(cx, obj, type, vp);
2000
JS_FinalizeStub(JSContext *cx, JSObject *obj)
2004
JS_PUBLIC_API(JSObject *)
2005
JS_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
2006
JSClass *clasp, JSNative constructor, uintN nargs,
2007
JSPropertySpec *ps, JSFunctionSpec *fs,
2008
JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
2011
JSObject *proto, *ctor;
2012
JSTempValueRooter tvr;
2018
atom = js_Atomize(cx, clasp->name, strlen(clasp->name), 0);
2022
/* Create a prototype object for this class. */
2023
proto = js_NewObject(cx, clasp, parent_proto, obj);
2027
/* After this point, control must exit via label bad or out. */
2028
JS_PUSH_SINGLE_TEMP_ROOT(cx, OBJECT_TO_JSVAL(proto), &tvr);
2031
/* Lacking a constructor, name the prototype (e.g., Math). */
2032
named = OBJ_DEFINE_PROPERTY(cx, obj, ATOM_TO_JSID(atom),
2033
OBJECT_TO_JSVAL(proto),
2034
NULL, NULL, 0, NULL);
2039
/* Define the constructor function in obj's scope. */
2040
fun = js_DefineFunction(cx, obj, atom, constructor, nargs, 0);
2041
named = (fun != NULL);
2046
* Remember the class this function is a constructor for so that
2047
* we know to create an object of this class when we call the
2053
* Optionally construct the prototype object, before the class has
2054
* been fully initialized. Allow the ctor to replace proto with a
2055
* different object, as is done for operator new -- and as at least
2056
* XML support requires.
2059
if (clasp->flags & JSCLASS_CONSTRUCT_PROTOTYPE) {
2060
cval = OBJECT_TO_JSVAL(ctor);
2061
if (!js_InternalConstruct(cx, proto, cval, 0, NULL, &rval))
2063
if (!JSVAL_IS_PRIMITIVE(rval) && JSVAL_TO_OBJECT(rval) != proto)
2064
proto = JSVAL_TO_OBJECT(rval);
2067
/* Connect constructor and prototype by named properties. */
2068
if (!js_SetClassPrototype(cx, ctor, proto,
2069
JSPROP_READONLY | JSPROP_PERMANENT)) {
2073
/* Bootstrap Function.prototype (see also JS_InitStandardClasses). */
2074
if (OBJ_GET_CLASS(cx, ctor) == clasp) {
2075
/* XXXMLM - this fails in framesets that are writing over
2077
* JS_ASSERT(!OBJ_GET_PROTO(cx, ctor));
2079
OBJ_SET_PROTO(cx, ctor, proto);
2083
/* Add properties and methods to the prototype and the constructor. */
2084
if ((ps && !JS_DefineProperties(cx, proto, ps)) ||
2085
(fs && !JS_DefineFunctions(cx, proto, fs)) ||
2086
(static_ps && !JS_DefineProperties(cx, ctor, static_ps)) ||
2087
(static_fs && !JS_DefineFunctions(cx, ctor, static_fs))) {
2092
JS_POP_TEMP_ROOT(cx, &tvr);
2097
(void) OBJ_DELETE_PROPERTY(cx, obj, ATOM_TO_JSID(atom), &rval);
2102
#ifdef JS_THREADSAFE
2103
JS_PUBLIC_API(JSClass *)
2104
JS_GetClass(JSContext *cx, JSObject *obj)
2107
JSVAL_TO_PRIVATE(GC_AWARE_GET_SLOT(cx, obj, JSSLOT_CLASS));
2110
JS_PUBLIC_API(JSClass *)
2111
JS_GetClass(JSObject *obj)
2113
return LOCKED_OBJ_GET_CLASS(obj);
2117
JS_PUBLIC_API(JSBool)
2118
JS_InstanceOf(JSContext *cx, JSObject *obj, JSClass *clasp, jsval *argv)
2123
if (OBJ_GET_CLASS(cx, obj) == clasp)
2126
fun = js_ValueToFunction(cx, &argv[-2], 0);
2128
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
2129
JSMSG_INCOMPATIBLE_PROTO,
2130
clasp->name, JS_GetFunctionName(fun),
2131
OBJ_GET_CLASS(cx, obj)->name);
2137
JS_PUBLIC_API(JSBool)
2138
JS_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp)
2140
return js_HasInstance(cx, obj, v, bp);
2143
JS_PUBLIC_API(void *)
2144
JS_GetPrivate(JSContext *cx, JSObject *obj)
2148
JS_ASSERT(OBJ_GET_CLASS(cx, obj)->flags & JSCLASS_HAS_PRIVATE);
2149
v = GC_AWARE_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
2150
if (!JSVAL_IS_INT(v))
2152
return JSVAL_TO_PRIVATE(v);
2155
JS_PUBLIC_API(JSBool)
2156
JS_SetPrivate(JSContext *cx, JSObject *obj, void *data)
2158
JS_ASSERT(OBJ_GET_CLASS(cx, obj)->flags & JSCLASS_HAS_PRIVATE);
2159
OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, PRIVATE_TO_JSVAL(data));
2163
JS_PUBLIC_API(void *)
2164
JS_GetInstancePrivate(JSContext *cx, JSObject *obj, JSClass *clasp,
2167
if (!JS_InstanceOf(cx, obj, clasp, argv))
2169
return JS_GetPrivate(cx, obj);
2172
JS_PUBLIC_API(JSObject *)
2173
JS_GetPrototype(JSContext *cx, JSObject *obj)
2178
proto = JSVAL_TO_OBJECT(GC_AWARE_GET_SLOT(cx, obj, JSSLOT_PROTO));
2180
/* Beware ref to dead object (we may be called from obj's finalizer). */
2181
return proto && proto->map ? proto : NULL;
2184
JS_PUBLIC_API(JSBool)
2185
JS_SetPrototype(JSContext *cx, JSObject *obj, JSObject *proto)
2188
if (obj->map->ops->setProto)
2189
return obj->map->ops->setProto(cx, obj, JSSLOT_PROTO, proto);
2190
OBJ_SET_SLOT(cx, obj, JSSLOT_PROTO, OBJECT_TO_JSVAL(proto));
2194
JS_PUBLIC_API(JSObject *)
2195
JS_GetParent(JSContext *cx, JSObject *obj)
2199
parent = JSVAL_TO_OBJECT(GC_AWARE_GET_SLOT(cx, obj, JSSLOT_PARENT));
2201
/* Beware ref to dead object (we may be called from obj's finalizer). */
2202
return parent && parent->map ? parent : NULL;
2205
JS_PUBLIC_API(JSBool)
2206
JS_SetParent(JSContext *cx, JSObject *obj, JSObject *parent)
2209
if (obj->map->ops->setParent)
2210
return obj->map->ops->setParent(cx, obj, JSSLOT_PARENT, parent);
2211
OBJ_SET_SLOT(cx, obj, JSSLOT_PARENT, OBJECT_TO_JSVAL(parent));
2215
JS_PUBLIC_API(JSObject *)
2216
JS_GetConstructor(JSContext *cx, JSObject *proto)
2221
if (!OBJ_GET_PROPERTY(cx, proto,
2222
ATOM_TO_JSID(cx->runtime->atomState.constructorAtom),
2226
if (!JSVAL_IS_FUNCTION(cx, cval)) {
2227
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NO_CONSTRUCTOR,
2228
OBJ_GET_CLASS(cx, proto)->name);
2231
return JSVAL_TO_OBJECT(cval);
2234
JS_PUBLIC_API(JSBool)
2235
JS_GetObjectId(JSContext *cx, JSObject *obj, jsid *idp)
2237
JS_ASSERT(((jsid)obj & JSID_TAGMASK) == 0);
2238
*idp = OBJECT_TO_JSID(obj);
2242
JS_PUBLIC_API(JSObject *)
2243
JS_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent)
2247
clasp = &js_ObjectClass; /* default class is Object */
2248
return js_NewObject(cx, clasp, proto, parent);
2251
JS_PUBLIC_API(JSBool)
2252
JS_SealObject(JSContext *cx, JSObject *obj, JSBool deep)
2259
if (!OBJ_IS_NATIVE(obj)) {
2260
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
2261
JSMSG_CANT_SEAL_OBJECT,
2262
OBJ_GET_CLASS(cx, obj)->name);
2266
scope = OBJ_SCOPE(obj);
2268
#if defined JS_THREADSAFE && defined DEBUG
2269
/* Insist on scope being used exclusively by cx's thread. */
2270
if (scope->ownercx != cx) {
2271
JS_LOCK_OBJ(cx, obj);
2272
JS_ASSERT(OBJ_SCOPE(obj) == scope);
2273
JS_ASSERT(scope->ownercx == cx);
2274
JS_UNLOCK_SCOPE(cx, scope);
2278
/* Nothing to do if obj's scope is already sealed. */
2279
if (SCOPE_IS_SEALED(scope))
2282
/* XXX Enumerate lazy properties now, as they can't be added later. */
2283
ida = JS_Enumerate(cx, obj);
2286
JS_DestroyIdArray(cx, ida);
2288
/* Ensure that obj has its own, mutable scope, and seal that scope. */
2289
JS_LOCK_OBJ(cx, obj);
2290
scope = js_GetMutableScope(cx, obj);
2292
SCOPE_SET_SEALED(scope);
2293
JS_UNLOCK_SCOPE(cx, scope);
2297
/* If we are not sealing an entire object graph, we're done. */
2301
/* Walk obj->slots and if any value is a non-null object, seal it. */
2302
nslots = JS_MIN(scope->map.freeslot, scope->map.nslots);
2303
for (vp = obj->slots, end = vp + nslots; vp < end; vp++) {
2305
if (JSVAL_IS_PRIMITIVE(v))
2307
if (!JS_SealObject(cx, JSVAL_TO_OBJECT(v), deep))
2313
JS_PUBLIC_API(JSObject *)
2314
JS_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
2319
clasp = &js_ObjectClass; /* default class is Object */
2320
return js_ConstructObject(cx, clasp, proto, parent, 0, NULL);
2323
JS_PUBLIC_API(JSObject *)
2324
JS_ConstructObjectWithArguments(JSContext *cx, JSClass *clasp, JSObject *proto,
2325
JSObject *parent, uintN argc, jsval *argv)
2329
clasp = &js_ObjectClass; /* default class is Object */
2330
return js_ConstructObject(cx, clasp, proto, parent, argc, argv);
2334
DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
2335
JSPropertyOp getter, JSPropertyOp setter, uintN attrs,
2336
uintN flags, intN tinyid)
2341
if (attrs & JSPROP_INDEX) {
2342
id = INT_TO_JSID(JS_PTR_TO_INT32(name));
2344
attrs &= ~JSPROP_INDEX;
2346
atom = js_Atomize(cx, name, strlen(name), 0);
2349
id = ATOM_TO_JSID(atom);
2351
if (flags != 0 && OBJ_IS_NATIVE(obj)) {
2352
return js_DefineNativeProperty(cx, obj, id, value, getter, setter,
2353
attrs, flags, tinyid, NULL);
2355
return OBJ_DEFINE_PROPERTY(cx, obj, id, value, getter, setter, attrs,
2359
#define AUTO_NAMELEN(s,n) (((n) == (size_t)-1) ? js_strlen(s) : (n))
2362
DefineUCProperty(JSContext *cx, JSObject *obj,
2363
const jschar *name, size_t namelen, jsval value,
2364
JSPropertyOp getter, JSPropertyOp setter, uintN attrs,
2365
uintN flags, intN tinyid)
2369
atom = js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0);
2372
if (flags != 0 && OBJ_IS_NATIVE(obj)) {
2373
return js_DefineNativeProperty(cx, obj, ATOM_TO_JSID(atom), value,
2374
getter, setter, attrs, flags, tinyid,
2377
return OBJ_DEFINE_PROPERTY(cx, obj, ATOM_TO_JSID(atom), value,
2378
getter, setter, attrs, NULL);
2381
JS_PUBLIC_API(JSObject *)
2382
JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, JSClass *clasp,
2383
JSObject *proto, uintN attrs)
2389
clasp = &js_ObjectClass; /* default class is Object */
2390
nobj = js_NewObject(cx, clasp, proto, obj);
2393
if (!DefineProperty(cx, obj, name, OBJECT_TO_JSVAL(nobj), NULL, NULL, attrs,
2395
cx->weakRoots.newborn[GCX_OBJECT] = NULL;
2401
JS_PUBLIC_API(JSBool)
2402
JS_DefineConstDoubles(JSContext *cx, JSObject *obj, JSConstDoubleSpec *cds)
2409
for (ok = JS_TRUE; cds->name; cds++) {
2410
ok = js_NewNumberValue(cx, cds->dval, &value);
2415
flags = JSPROP_READONLY | JSPROP_PERMANENT;
2416
ok = DefineProperty(cx, obj, cds->name, value, NULL, NULL, flags, 0, 0);
2423
JS_PUBLIC_API(JSBool)
2424
JS_DefineProperties(JSContext *cx, JSObject *obj, JSPropertySpec *ps)
2429
for (ok = JS_TRUE; ps->name; ps++) {
2430
ok = DefineProperty(cx, obj, ps->name, JSVAL_VOID,
2431
ps->getter, ps->setter, ps->flags,
2432
SPROP_HAS_SHORTID, ps->tinyid);
2439
JS_PUBLIC_API(JSBool)
2440
JS_DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
2441
JSPropertyOp getter, JSPropertyOp setter, uintN attrs)
2444
return DefineProperty(cx, obj, name, value, getter, setter, attrs, 0, 0);
2447
JS_PUBLIC_API(JSBool)
2448
JS_DefinePropertyWithTinyId(JSContext *cx, JSObject *obj, const char *name,
2449
int8 tinyid, jsval value,
2450
JSPropertyOp getter, JSPropertyOp setter,
2454
return DefineProperty(cx, obj, name, value, getter, setter, attrs,
2455
SPROP_HAS_SHORTID, tinyid);
2459
LookupProperty(JSContext *cx, JSObject *obj, const char *name, JSObject **objp,
2464
atom = js_Atomize(cx, name, strlen(name), 0);
2467
return OBJ_LOOKUP_PROPERTY(cx, obj, ATOM_TO_JSID(atom), objp, propp);
2471
LookupUCProperty(JSContext *cx, JSObject *obj,
2472
const jschar *name, size_t namelen,
2473
JSObject **objp, JSProperty **propp)
2477
atom = js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0);
2480
return OBJ_LOOKUP_PROPERTY(cx, obj, ATOM_TO_JSID(atom), objp, propp);
2483
JS_PUBLIC_API(JSBool)
2484
JS_AliasProperty(JSContext *cx, JSObject *obj, const char *name,
2491
JSScopeProperty *sprop;
2494
if (!LookupProperty(cx, obj, name, &obj2, &prop))
2497
js_ReportIsNotDefined(cx, name);
2500
if (obj2 != obj || !OBJ_IS_NATIVE(obj)) {
2501
OBJ_DROP_PROPERTY(cx, obj2, prop);
2502
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_ALIAS,
2503
alias, name, OBJ_GET_CLASS(cx, obj2)->name);
2506
atom = js_Atomize(cx, alias, strlen(alias), 0);
2510
sprop = (JSScopeProperty *)prop;
2511
ok = (js_AddNativeProperty(cx, obj, ATOM_TO_JSID(atom),
2512
sprop->getter, sprop->setter, sprop->slot,
2513
sprop->attrs, sprop->flags | SPROP_IS_ALIAS,
2517
OBJ_DROP_PROPERTY(cx, obj, prop);
2522
LookupResult(JSContext *cx, JSObject *obj, JSObject *obj2, JSProperty *prop)
2524
JSScopeProperty *sprop;
2528
/* XXX bad API: no way to tell "not defined" from "void value" */
2531
if (OBJ_IS_NATIVE(obj2)) {
2532
/* Peek at the native property's slot value, without doing a Get. */
2533
sprop = (JSScopeProperty *)prop;
2534
rval = SPROP_HAS_VALID_SLOT(sprop, OBJ_SCOPE(obj2))
2535
? LOCKED_OBJ_GET_SLOT(obj2, sprop->slot)
2538
/* XXX bad API: no way to return "defined but value unknown" */
2541
OBJ_DROP_PROPERTY(cx, obj2, prop);
2546
GetPropertyAttributes(JSContext *cx, JSObject *obj, JSAtom *atom,
2547
uintN *attrsp, JSBool *foundp,
2548
JSPropertyOp *getterp, JSPropertyOp *setterp)
2556
if (!OBJ_LOOKUP_PROPERTY(cx, obj, ATOM_TO_JSID(atom), &obj2, &prop))
2559
if (!prop || obj != obj2) {
2567
OBJ_DROP_PROPERTY(cx, obj2, prop);
2572
ok = OBJ_GET_ATTRIBUTES(cx, obj, ATOM_TO_JSID(atom), prop, attrsp);
2573
if (ok && OBJ_IS_NATIVE(obj)) {
2574
JSScopeProperty *sprop = (JSScopeProperty *) prop;
2577
*getterp = sprop->getter;
2579
*setterp = sprop->setter;
2581
OBJ_DROP_PROPERTY(cx, obj, prop);
2586
SetPropertyAttributes(JSContext *cx, JSObject *obj, JSAtom *atom,
2587
uintN attrs, JSBool *foundp)
2595
if (!OBJ_LOOKUP_PROPERTY(cx, obj, ATOM_TO_JSID(atom), &obj2, &prop))
2597
if (!prop || obj != obj2) {
2600
OBJ_DROP_PROPERTY(cx, obj2, prop);
2605
ok = OBJ_SET_ATTRIBUTES(cx, obj, ATOM_TO_JSID(atom), prop, &attrs);
2606
OBJ_DROP_PROPERTY(cx, obj, prop);
2610
JS_PUBLIC_API(JSBool)
2611
JS_GetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
2612
uintN *attrsp, JSBool *foundp)
2615
return GetPropertyAttributes(cx, obj,
2616
js_Atomize(cx, name, strlen(name), 0),
2617
attrsp, foundp, NULL, NULL);
2620
JS_PUBLIC_API(JSBool)
2621
JS_GetPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
2623
uintN *attrsp, JSBool *foundp,
2624
JSPropertyOp *getterp,
2625
JSPropertyOp *setterp)
2628
return GetPropertyAttributes(cx, obj,
2629
js_Atomize(cx, name, strlen(name), 0),
2630
attrsp, foundp, getterp, setterp);
2633
JS_PUBLIC_API(JSBool)
2634
JS_SetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
2635
uintN attrs, JSBool *foundp)
2638
return SetPropertyAttributes(cx, obj,
2639
js_Atomize(cx, name, strlen(name), 0),
2643
JS_PUBLIC_API(JSBool)
2644
JS_HasProperty(JSContext *cx, JSObject *obj, const char *name, JSBool *foundp)
2651
ok = LookupProperty(cx, obj, name, &obj2, &prop);
2653
*foundp = (prop != NULL);
2655
OBJ_DROP_PROPERTY(cx, obj2, prop);
2660
JS_PUBLIC_API(JSBool)
2661
JS_LookupProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp)
2668
ok = LookupProperty(cx, obj, name, &obj2, &prop);
2670
*vp = LookupResult(cx, obj, obj2, prop);
2674
JS_PUBLIC_API(JSBool)
2675
JS_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, const char *name,
2676
uintN flags, jsval *vp)
2684
atom = js_Atomize(cx, name, strlen(name), 0);
2687
ok = OBJ_IS_NATIVE(obj)
2688
? js_LookupPropertyWithFlags(cx, obj, ATOM_TO_JSID(atom), flags,
2690
: OBJ_LOOKUP_PROPERTY(cx, obj, ATOM_TO_JSID(atom), &obj2, &prop);
2692
*vp = LookupResult(cx, obj, obj2, prop);
2696
JS_PUBLIC_API(JSBool)
2697
JS_GetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp)
2702
atom = js_Atomize(cx, name, strlen(name), 0);
2705
return OBJ_GET_PROPERTY(cx, obj, ATOM_TO_JSID(atom), vp);
2708
JS_PUBLIC_API(JSBool)
2709
JS_GetMethod(JSContext *cx, JSObject *obj, const char *name, JSObject **objp,
2716
atom = js_Atomize(cx, name, strlen(name), 0);
2719
id = ATOM_TO_JSID(atom);
2721
#if JS_HAS_XML_SUPPORT
2722
if (OBJECT_IS_XML(cx, obj)) {
2723
JSXMLObjectOps *ops;
2725
ops = (JSXMLObjectOps *) obj->map->ops;
2726
obj = ops->getMethod(cx, obj, id, vp);
2732
if (!OBJ_GET_PROPERTY(cx, obj, id, vp))
2740
JS_PUBLIC_API(JSBool)
2741
JS_SetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp)
2746
atom = js_Atomize(cx, name, strlen(name), 0);
2749
return OBJ_SET_PROPERTY(cx, obj, ATOM_TO_JSID(atom), vp);
2752
JS_PUBLIC_API(JSBool)
2753
JS_DeleteProperty(JSContext *cx, JSObject *obj, const char *name)
2758
return JS_DeleteProperty2(cx, obj, name, &junk);
2761
JS_PUBLIC_API(JSBool)
2762
JS_DeleteProperty2(JSContext *cx, JSObject *obj, const char *name,
2768
atom = js_Atomize(cx, name, strlen(name), 0);
2771
return OBJ_DELETE_PROPERTY(cx, obj, ATOM_TO_JSID(atom), rval);
2774
JS_PUBLIC_API(JSBool)
2775
JS_DefineUCProperty(JSContext *cx, JSObject *obj,
2776
const jschar *name, size_t namelen, jsval value,
2777
JSPropertyOp getter, JSPropertyOp setter,
2781
return DefineUCProperty(cx, obj, name, namelen, value, getter, setter,
2785
JS_PUBLIC_API(JSBool)
2786
JS_GetUCPropertyAttributes(JSContext *cx, JSObject *obj,
2787
const jschar *name, size_t namelen,
2788
uintN *attrsp, JSBool *foundp)
2791
return GetPropertyAttributes(cx, obj,
2792
js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0),
2793
attrsp, foundp, NULL, NULL);
2796
JS_PUBLIC_API(JSBool)
2797
JS_GetUCPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
2798
const jschar *name, size_t namelen,
2799
uintN *attrsp, JSBool *foundp,
2800
JSPropertyOp *getterp,
2801
JSPropertyOp *setterp)
2804
return GetPropertyAttributes(cx, obj,
2805
js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0),
2806
attrsp, foundp, getterp, setterp);
2809
JS_PUBLIC_API(JSBool)
2810
JS_SetUCPropertyAttributes(JSContext *cx, JSObject *obj,
2811
const jschar *name, size_t namelen,
2812
uintN attrs, JSBool *foundp)
2815
return SetPropertyAttributes(cx, obj,
2816
js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0),
2820
JS_PUBLIC_API(JSBool)
2821
JS_DefineUCPropertyWithTinyId(JSContext *cx, JSObject *obj,
2822
const jschar *name, size_t namelen,
2823
int8 tinyid, jsval value,
2824
JSPropertyOp getter, JSPropertyOp setter,
2828
return DefineUCProperty(cx, obj, name, namelen, value, getter, setter,
2829
attrs, SPROP_HAS_SHORTID, tinyid);
2832
JS_PUBLIC_API(JSBool)
2833
JS_HasUCProperty(JSContext *cx, JSObject *obj,
2834
const jschar *name, size_t namelen,
2842
ok = LookupUCProperty(cx, obj, name, namelen, &obj2, &prop);
2844
*vp = (prop != NULL);
2846
OBJ_DROP_PROPERTY(cx, obj2, prop);
2851
JS_PUBLIC_API(JSBool)
2852
JS_LookupUCProperty(JSContext *cx, JSObject *obj,
2853
const jschar *name, size_t namelen,
2861
ok = LookupUCProperty(cx, obj, name, namelen, &obj2, &prop);
2863
*vp = LookupResult(cx, obj, obj2, prop);
2867
JS_PUBLIC_API(JSBool)
2868
JS_GetUCProperty(JSContext *cx, JSObject *obj,
2869
const jschar *name, size_t namelen,
2875
atom = js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0);
2878
return OBJ_GET_PROPERTY(cx, obj, ATOM_TO_JSID(atom), vp);
2881
JS_PUBLIC_API(JSBool)
2882
JS_SetUCProperty(JSContext *cx, JSObject *obj,
2883
const jschar *name, size_t namelen,
2889
atom = js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0);
2892
return OBJ_SET_PROPERTY(cx, obj, ATOM_TO_JSID(atom), vp);
2895
JS_PUBLIC_API(JSBool)
2896
JS_DeleteUCProperty2(JSContext *cx, JSObject *obj,
2897
const jschar *name, size_t namelen,
2903
atom = js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0);
2906
return OBJ_DELETE_PROPERTY(cx, obj, ATOM_TO_JSID(atom), rval);
2909
JS_PUBLIC_API(JSObject *)
2910
JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector)
2913
/* NB: jsuint cast does ToUint32. */
2914
return js_NewArrayObject(cx, (jsuint)length, vector);
2917
JS_PUBLIC_API(JSBool)
2918
JS_IsArrayObject(JSContext *cx, JSObject *obj)
2920
return OBJ_GET_CLASS(cx, obj) == &js_ArrayClass;
2923
JS_PUBLIC_API(JSBool)
2924
JS_GetArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp)
2927
return js_GetLengthProperty(cx, obj, lengthp);
2930
JS_PUBLIC_API(JSBool)
2931
JS_SetArrayLength(JSContext *cx, JSObject *obj, jsuint length)
2934
return js_SetLengthProperty(cx, obj, length);
2937
JS_PUBLIC_API(JSBool)
2938
JS_HasArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp)
2941
return js_HasLengthProperty(cx, obj, lengthp);
2944
JS_PUBLIC_API(JSBool)
2945
JS_DefineElement(JSContext *cx, JSObject *obj, jsint index, jsval value,
2946
JSPropertyOp getter, JSPropertyOp setter, uintN attrs)
2949
return OBJ_DEFINE_PROPERTY(cx, obj, INT_TO_JSID(index), value,
2950
getter, setter, attrs, NULL);
2953
JS_PUBLIC_API(JSBool)
2954
JS_AliasElement(JSContext *cx, JSObject *obj, const char *name, jsint alias)
2958
JSScopeProperty *sprop;
2962
if (!LookupProperty(cx, obj, name, &obj2, &prop))
2965
js_ReportIsNotDefined(cx, name);
2968
if (obj2 != obj || !OBJ_IS_NATIVE(obj)) {
2970
OBJ_DROP_PROPERTY(cx, obj2, prop);
2971
JS_snprintf(numBuf, sizeof numBuf, "%ld", (long)alias);
2972
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_ALIAS,
2973
numBuf, name, OBJ_GET_CLASS(cx, obj2)->name);
2976
sprop = (JSScopeProperty *)prop;
2977
ok = (js_AddNativeProperty(cx, obj, INT_TO_JSID(alias),
2978
sprop->getter, sprop->setter, sprop->slot,
2979
sprop->attrs, sprop->flags | SPROP_IS_ALIAS,
2982
OBJ_DROP_PROPERTY(cx, obj, prop);
2986
JS_PUBLIC_API(JSBool)
2987
JS_HasElement(JSContext *cx, JSObject *obj, jsint index, JSBool *foundp)
2994
ok = OBJ_LOOKUP_PROPERTY(cx, obj, INT_TO_JSID(index), &obj2, &prop);
2996
*foundp = (prop != NULL);
2998
OBJ_DROP_PROPERTY(cx, obj2, prop);
3003
JS_PUBLIC_API(JSBool)
3004
JS_LookupElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp)
3011
ok = OBJ_LOOKUP_PROPERTY(cx, obj, INT_TO_JSID(index), &obj2, &prop);
3013
*vp = LookupResult(cx, obj, obj2, prop);
3017
JS_PUBLIC_API(JSBool)
3018
JS_GetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp)
3021
return OBJ_GET_PROPERTY(cx, obj, INT_TO_JSID(index), vp);
3024
JS_PUBLIC_API(JSBool)
3025
JS_SetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp)
3028
return OBJ_SET_PROPERTY(cx, obj, INT_TO_JSID(index), vp);
3031
JS_PUBLIC_API(JSBool)
3032
JS_DeleteElement(JSContext *cx, JSObject *obj, jsint index)
3037
return JS_DeleteElement2(cx, obj, index, &junk);
3040
JS_PUBLIC_API(JSBool)
3041
JS_DeleteElement2(JSContext *cx, JSObject *obj, jsint index, jsval *rval)
3044
return OBJ_DELETE_PROPERTY(cx, obj, INT_TO_JSID(index), rval);
3048
JS_ClearScope(JSContext *cx, JSObject *obj)
3052
if (obj->map->ops->clear)
3053
obj->map->ops->clear(cx, obj);
3056
JS_PUBLIC_API(JSIdArray *)
3057
JS_Enumerate(JSContext *cx, JSObject *obj)
3060
jsval iter_state, num_properties;
3068
iter_state = JSVAL_NULL;
3070
/* Get the number of properties to enumerate. */
3071
if (!OBJ_ENUMERATE(cx, obj, JSENUMERATE_INIT, &iter_state, &num_properties))
3073
if (!JSVAL_IS_INT(num_properties)) {
3078
/* Grow as needed if we don't know the exact amount ahead of time. */
3079
n = JSVAL_TO_INT(num_properties);
3083
/* Create an array of jsids large enough to hold all the properties */
3084
ida = js_NewIdArray(cx, n);
3089
vector = &ida->vector[0];
3091
if (!OBJ_ENUMERATE(cx, obj, JSENUMERATE_NEXT, &iter_state, &id))
3094
/* No more jsid's to enumerate ? */
3095
if (iter_state == JSVAL_NULL)
3098
if (i == ida->length) {
3099
ida = js_SetIdArrayLength(cx, ida, ida->length * 2);
3102
vector = &ida->vector[0];
3106
return js_SetIdArrayLength(cx, ida, i);
3109
if (iter_state != JSVAL_NULL)
3110
OBJ_ENUMERATE(cx, obj, JSENUMERATE_DESTROY, &iter_state, 0);
3112
JS_DestroyIdArray(cx, ida);
3117
* XXX reverse iterator for properties, unreverse and meld with jsinterp.c's
3118
* prop_iterator_class somehow...
3119
* + preserve the OBJ_ENUMERATE API while optimizing the native object case
3120
* + native case here uses a JSScopeProperty *, but that iterates in reverse!
3121
* + so we make non-native match, by reverse-iterating after JS_Enumerating
3123
#define JSSLOT_ITER_INDEX (JSSLOT_PRIVATE + 1)
3125
#if JSSLOT_ITER_INDEX >= JS_INITIAL_NSLOTS
3126
# error "JSSLOT_ITER_INDEX botch!"
3130
prop_iter_finalize(JSContext *cx, JSObject *obj)
3136
v = GC_AWARE_GET_SLOT(cx, obj, JSSLOT_ITER_INDEX);
3137
if (JSVAL_IS_VOID(v))
3140
i = JSVAL_TO_INT(v);
3142
/* Non-native case: destroy the ida enumerated when obj was created. */
3143
ida = (JSIdArray *) JS_GetPrivate(cx, obj);
3145
JS_DestroyIdArray(cx, ida);
3150
prop_iter_mark(JSContext *cx, JSObject *obj, void *arg)
3154
JSScopeProperty *sprop;
3158
v = GC_AWARE_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
3159
JS_ASSERT(!JSVAL_IS_VOID(v));
3161
i = JSVAL_TO_INT(OBJ_GET_SLOT(cx, obj, JSSLOT_ITER_INDEX));
3163
/* Native case: just mark the next property to visit. */
3164
sprop = (JSScopeProperty *) JSVAL_TO_PRIVATE(v);
3166
MARK_SCOPE_PROPERTY(cx, sprop);
3168
/* Non-native case: mark each id in the JSIdArray private. */
3169
ida = (JSIdArray *) JSVAL_TO_PRIVATE(v);
3170
for (i = 0, n = ida->length; i < n; i++) {
3171
id = ida->vector[i];
3178
static JSClass prop_iter_class = {
3180
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(1),
3181
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
3182
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, prop_iter_finalize,
3183
NULL, NULL, NULL, NULL,
3184
NULL, NULL, prop_iter_mark, NULL
3187
JS_PUBLIC_API(JSObject *)
3188
JS_NewPropertyIterator(JSContext *cx, JSObject *obj)
3197
iterobj = js_NewObject(cx, &prop_iter_class, NULL, obj);
3201
if (OBJ_IS_NATIVE(obj)) {
3202
/* Native case: start with the last property in obj's own scope. */
3203
scope = OBJ_SCOPE(obj);
3204
pdata = (scope->object == obj) ? scope->lastProp : NULL;
3207
JSTempValueRooter tvr;
3210
* Non-native case: enumerate a JSIdArray and keep it via private.
3212
* Note: we have to make sure that we root obj around the call to
3213
* JS_Enumerate to protect against multiple allocations under it.
3215
JS_PUSH_SINGLE_TEMP_ROOT(cx, OBJECT_TO_JSVAL(iterobj), &tvr);
3216
ida = JS_Enumerate(cx, obj);
3217
JS_POP_TEMP_ROOT(cx, &tvr);
3221
index = ida->length;
3224
/* iterobj can not escape to other threads here. */
3225
iterobj->slots[JSSLOT_PRIVATE] = PRIVATE_TO_JSVAL(pdata);
3226
iterobj->slots[JSSLOT_ITER_INDEX] = INT_TO_JSVAL(index);
3230
cx->weakRoots.newborn[GCX_OBJECT] = NULL;
3234
JS_PUBLIC_API(JSBool)
3235
JS_NextProperty(JSContext *cx, JSObject *iterobj, jsid *idp)
3240
JSScopeProperty *sprop;
3244
i = JSVAL_TO_INT(OBJ_GET_SLOT(cx, iterobj, JSSLOT_ITER_INDEX));
3246
/* Native case: private data is a property tree node pointer. */
3247
obj = OBJ_GET_PARENT(cx, iterobj);
3248
JS_ASSERT(OBJ_IS_NATIVE(obj));
3249
scope = OBJ_SCOPE(obj);
3250
JS_ASSERT(scope->object == obj);
3251
sprop = (JSScopeProperty *) JS_GetPrivate(cx, iterobj);
3254
* If the next property mapped by scope in the property tree ancestor
3255
* line is not enumerable, or it's an alias, or one or more properties
3256
* were deleted from the "middle" of the scope-mapped ancestor line
3257
* and the next property was among those deleted, skip it and keep on
3258
* trying to find an enumerable property that is still in scope.
3261
(!(sprop->attrs & JSPROP_ENUMERATE) ||
3262
(sprop->flags & SPROP_IS_ALIAS) ||
3263
(SCOPE_HAD_MIDDLE_DELETE(scope) &&
3264
!SCOPE_HAS_PROPERTY(scope, sprop)))) {
3265
sprop = sprop->parent;
3271
if (!JS_SetPrivate(cx, iterobj, sprop->parent))
3276
/* Non-native case: use the ida enumerated when iterobj was created. */
3277
ida = (JSIdArray *) JS_GetPrivate(cx, iterobj);
3278
JS_ASSERT(i <= ida->length);
3282
*idp = ida->vector[--i];
3283
OBJ_SET_SLOT(cx, iterobj, JSSLOT_ITER_INDEX, INT_TO_JSVAL(i));
3289
JS_PUBLIC_API(JSBool)
3290
JS_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
3291
jsval *vp, uintN *attrsp)
3294
return OBJ_CHECK_ACCESS(cx, obj, id, mode, vp, attrsp);
3297
JS_PUBLIC_API(JSCheckAccessOp)
3298
JS_SetCheckObjectAccessCallback(JSRuntime *rt, JSCheckAccessOp acb)
3300
JSCheckAccessOp oldacb;
3302
oldacb = rt->checkObjectAccess;
3303
rt->checkObjectAccess = acb;
3308
ReservedSlotIndexOK(JSContext *cx, JSObject *obj, JSClass *clasp,
3309
uint32 index, uint32 limit)
3311
/* Check the computed, possibly per-instance, upper bound. */
3312
if (clasp->reserveSlots)
3313
JS_LOCK_OBJ_VOID(cx, obj, limit += clasp->reserveSlots(cx, obj));
3314
if (index >= limit) {
3315
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
3316
JSMSG_RESERVED_SLOT_RANGE);
3322
JS_PUBLIC_API(JSBool)
3323
JS_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp)
3329
clasp = OBJ_GET_CLASS(cx, obj);
3330
limit = JSCLASS_RESERVED_SLOTS(clasp);
3331
if (index >= limit && !ReservedSlotIndexOK(cx, obj, clasp, index, limit))
3333
slot = JSSLOT_START(clasp) + index;
3334
*vp = OBJ_GET_REQUIRED_SLOT(cx, obj, slot);
3338
JS_PUBLIC_API(JSBool)
3339
JS_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v)
3345
clasp = OBJ_GET_CLASS(cx, obj);
3346
limit = JSCLASS_RESERVED_SLOTS(clasp);
3347
if (index >= limit && !ReservedSlotIndexOK(cx, obj, clasp, index, limit))
3349
slot = JSSLOT_START(clasp) + index;
3350
return OBJ_SET_REQUIRED_SLOT(cx, obj, slot, v);
3353
#ifdef JS_THREADSAFE
3354
JS_PUBLIC_API(jsrefcount)
3355
JS_HoldPrincipals(JSContext *cx, JSPrincipals *principals)
3357
return JS_ATOMIC_INCREMENT(&principals->refcount);
3360
JS_PUBLIC_API(jsrefcount)
3361
JS_DropPrincipals(JSContext *cx, JSPrincipals *principals)
3363
jsrefcount rc = JS_ATOMIC_DECREMENT(&principals->refcount);
3365
principals->destroy(cx, principals);
3370
JS_PUBLIC_API(JSPrincipalsTranscoder)
3371
JS_SetPrincipalsTranscoder(JSRuntime *rt, JSPrincipalsTranscoder px)
3373
JSPrincipalsTranscoder oldpx;
3375
oldpx = rt->principalsTranscoder;
3376
rt->principalsTranscoder = px;
3380
JS_PUBLIC_API(JSObjectPrincipalsFinder)
3381
JS_SetObjectPrincipalsFinder(JSRuntime *rt, JSObjectPrincipalsFinder fop)
3383
JSObjectPrincipalsFinder oldfop;
3385
oldfop = rt->findObjectPrincipals;
3386
rt->findObjectPrincipals = fop;
3390
JS_PUBLIC_API(JSFunction *)
3391
JS_NewFunction(JSContext *cx, JSNative native, uintN nargs, uintN flags,
3392
JSObject *parent, const char *name)
3401
atom = js_Atomize(cx, name, strlen(name), 0);
3405
return js_NewFunction(cx, NULL, native, nargs, flags, parent, atom);
3408
JS_PUBLIC_API(JSObject *)
3409
JS_CloneFunctionObject(JSContext *cx, JSObject *funobj, JSObject *parent)
3412
if (OBJ_GET_CLASS(cx, funobj) != &js_FunctionClass) {
3413
/* Indicate we cannot clone this object. */
3416
return js_CloneFunctionObject(cx, funobj, parent);
3419
JS_PUBLIC_API(JSObject *)
3420
JS_GetFunctionObject(JSFunction *fun)
3425
JS_PUBLIC_API(const char *)
3426
JS_GetFunctionName(JSFunction *fun)
3429
? JS_GetStringBytes(ATOM_TO_STRING(fun->atom))
3433
JS_PUBLIC_API(JSString *)
3434
JS_GetFunctionId(JSFunction *fun)
3436
return fun->atom ? ATOM_TO_STRING(fun->atom) : NULL;
3439
JS_PUBLIC_API(uintN)
3440
JS_GetFunctionFlags(JSFunction *fun)
3445
JS_PUBLIC_API(uint16)
3446
JS_GetFunctionArity(JSFunction *fun)
3451
JS_PUBLIC_API(JSBool)
3452
JS_ObjectIsFunction(JSContext *cx, JSObject *obj)
3454
return OBJ_GET_CLASS(cx, obj) == &js_FunctionClass;
3457
JS_STATIC_DLL_CALLBACK(JSBool)
3458
js_generic_native_method_dispatcher(JSContext *cx, JSObject *obj,
3459
uintN argc, jsval *argv, jsval *rval)
3465
if (!JS_GetReservedSlot(cx, JSVAL_TO_OBJECT(argv[-2]), 0, &fsv))
3467
fs = (JSFunctionSpec *) JSVAL_TO_PRIVATE(fsv);
3470
* We know that argv[0] is valid because JS_DefineFunctions, which is our
3471
* only (indirect) referrer, defined us as requiring at least one argument
3472
* (notice how it passes fs->nargs + 1 as the next-to-last argument to
3473
* JS_DefineFunction).
3475
if (JSVAL_IS_PRIMITIVE(argv[0])) {
3477
* Make sure that this is an object or null, as required by the generic
3480
if (!js_ValueToObject(cx, argv[0], &tmp))
3482
argv[0] = OBJECT_TO_JSVAL(tmp);
3486
* Copy all actual (argc) and required but missing (fs->nargs + 1 - argc)
3487
* args down over our |this| parameter, argv[-1], which is almost always
3488
* the class constructor object, e.g. Array. Then call the corresponding
3489
* prototype native method with our first argument passed as |this|.
3491
memmove(argv - 1, argv, JS_MAX(fs->nargs + 1U, argc) * sizeof(jsval));
3494
* Follow Function.prototype.apply and .call by using the global object as
3495
* the 'this' param if no args.
3497
JS_ASSERT(cx->fp->argv == argv);
3498
if (!js_ComputeThis(cx, JSVAL_TO_OBJECT(argv[-1]), cx->fp))
3502
* Protect against argc - 1 underflowing below. By calling js_ComputeThis,
3503
* we made it as if the static was called with one parameter.
3508
return fs->call(cx, JSVAL_TO_OBJECT(argv[-1]), argc - 1, argv, rval);
3511
JS_PUBLIC_API(JSBool)
3512
JS_DefineFunctions(JSContext *cx, JSObject *obj, JSFunctionSpec *fs)
3520
for (; fs->name; fs++) {
3524
* Define a generic arity N+1 static method for the arity N prototype
3525
* method if flags contains JSFUN_GENERIC_NATIVE.
3527
if (flags & JSFUN_GENERIC_NATIVE) {
3529
ctor = JS_GetConstructor(cx, obj);
3534
flags &= ~JSFUN_GENERIC_NATIVE;
3535
fun = JS_DefineFunction(cx, ctor, fs->name,
3536
js_generic_native_method_dispatcher,
3537
fs->nargs + 1, flags);
3540
fun->extra = fs->extra;
3543
* As jsapi.h notes, fs must point to storage that lives as long
3544
* as fun->object lives.
3546
if (!JS_SetReservedSlot(cx, fun->object, 0, PRIVATE_TO_JSVAL(fs)))
3550
fun = JS_DefineFunction(cx, obj, fs->name, fs->call, fs->nargs, flags);
3553
fun->extra = fs->extra;
3558
JS_PUBLIC_API(JSFunction *)
3559
JS_DefineFunction(JSContext *cx, JSObject *obj, const char *name, JSNative call,
3560
uintN nargs, uintN attrs)
3565
atom = js_Atomize(cx, name, strlen(name), 0);
3568
return js_DefineFunction(cx, obj, atom, call, nargs, attrs);
3571
JS_PUBLIC_API(JSFunction *)
3572
JS_DefineUCFunction(JSContext *cx, JSObject *obj,
3573
const jschar *name, size_t namelen, JSNative call,
3574
uintN nargs, uintN attrs)
3578
atom = js_AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen), 0);
3581
return js_DefineFunction(cx, obj, atom, call, nargs, attrs);
3585
CompileTokenStream(JSContext *cx, JSObject *obj, JSTokenStream *ts,
3586
void *tempMark, JSBool *eofp)
3589
JSArenaPool codePool, notePool;
3595
JS_InitArenaPool(&codePool, "code", 1024, sizeof(jsbytecode));
3596
JS_InitArenaPool(¬ePool, "note", 1024, sizeof(jssrcnote));
3597
if (!js_InitCodeGenerator(cx, &cg, &codePool, ¬ePool,
3598
ts->filename, ts->lineno,
3601
} else if (!js_CompileTokenStream(cx, obj, ts, &cg)) {
3603
eof = (ts->flags & TSF_EOF) != 0;
3605
script = js_NewScriptFromCG(cx, &cg, NULL);
3609
if (!js_CloseTokenStream(cx, ts)) {
3611
js_DestroyScript(cx, script);
3614
cg.tempMark = tempMark;
3615
js_FinishCodeGenerator(cx, &cg);
3616
JS_FinishArenaPool(&codePool);
3617
JS_FinishArenaPool(¬ePool);
3621
JS_PUBLIC_API(JSScript *)
3622
JS_CompileScript(JSContext *cx, JSObject *obj,
3623
const char *bytes, size_t length,
3624
const char *filename, uintN lineno)
3630
chars = js_InflateString(cx, bytes, &length);
3633
script = JS_CompileUCScript(cx, obj, chars, length, filename, lineno);
3638
JS_PUBLIC_API(JSScript *)
3639
JS_CompileScriptForPrincipals(JSContext *cx, JSObject *obj,
3640
JSPrincipals *principals,
3641
const char *bytes, size_t length,
3642
const char *filename, uintN lineno)
3648
chars = js_InflateString(cx, bytes, &length);
3651
script = JS_CompileUCScriptForPrincipals(cx, obj, principals,
3652
chars, length, filename, lineno);
3657
JS_PUBLIC_API(JSScript *)
3658
JS_CompileUCScript(JSContext *cx, JSObject *obj,
3659
const jschar *chars, size_t length,
3660
const char *filename, uintN lineno)
3663
return JS_CompileUCScriptForPrincipals(cx, obj, NULL, chars, length,
3667
#if JS_HAS_EXCEPTIONS
3668
# define LAST_FRAME_EXCEPTION_CHECK(cx,result) \
3671
js_ReportUncaughtException(cx); \
3674
# define LAST_FRAME_EXCEPTION_CHECK(cx,result) /* nothing */
3677
#define LAST_FRAME_CHECKS(cx,result) \
3680
(cx)->weakRoots.lastInternalResult = JSVAL_NULL; \
3681
LAST_FRAME_EXCEPTION_CHECK(cx, result); \
3685
JS_PUBLIC_API(JSScript *)
3686
JS_CompileUCScriptForPrincipals(JSContext *cx, JSObject *obj,
3687
JSPrincipals *principals,
3688
const jschar *chars, size_t length,
3689
const char *filename, uintN lineno)
3696
mark = JS_ARENA_MARK(&cx->tempPool);
3697
ts = js_NewTokenStream(cx, chars, length, filename, lineno, principals);
3700
script = CompileTokenStream(cx, obj, ts, mark, NULL);
3701
LAST_FRAME_CHECKS(cx, script);
3705
JS_PUBLIC_API(JSBool)
3706
JS_BufferIsCompilableUnit(JSContext *cx, JSObject *obj,
3707
const char *bytes, size_t length)
3711
JSExceptionState *exnState;
3714
JSErrorReporter older;
3717
chars = js_InflateString(cx, bytes, &length);
3722
* Return true on any out-of-memory error, so our caller doesn't try to
3723
* collect more buffered source.
3726
exnState = JS_SaveExceptionState(cx);
3727
tempMark = JS_ARENA_MARK(&cx->tempPool);
3728
ts = js_NewTokenStream(cx, chars, length, NULL, 0, NULL);
3730
older = JS_SetErrorReporter(cx, NULL);
3731
if (!js_ParseTokenStream(cx, obj, ts) &&
3732
(ts->flags & TSF_UNEXPECTED_EOF)) {
3734
* We ran into an error. If it was because we ran out of source,
3735
* we return false, so our caller will know to try to collect more
3741
JS_SetErrorReporter(cx, older);
3742
js_CloseTokenStream(cx, ts);
3743
JS_ARENA_RELEASE(&cx->tempPool, tempMark);
3747
JS_RestoreExceptionState(cx, exnState);
3751
JS_PUBLIC_API(JSScript *)
3752
JS_CompileFile(JSContext *cx, JSObject *obj, const char *filename)
3759
mark = JS_ARENA_MARK(&cx->tempPool);
3760
ts = js_NewFileTokenStream(cx, filename, stdin);
3763
script = CompileTokenStream(cx, obj, ts, mark, NULL);
3764
LAST_FRAME_CHECKS(cx, script);
3768
JS_PUBLIC_API(JSScript *)
3769
JS_CompileFileHandle(JSContext *cx, JSObject *obj, const char *filename,
3772
return JS_CompileFileHandleForPrincipals(cx, obj, filename, file, NULL);
3775
JS_PUBLIC_API(JSScript *)
3776
JS_CompileFileHandleForPrincipals(JSContext *cx, JSObject *obj,
3777
const char *filename, FILE *file,
3778
JSPrincipals *principals)
3785
mark = JS_ARENA_MARK(&cx->tempPool);
3786
ts = js_NewFileTokenStream(cx, NULL, file);
3789
ts->filename = filename;
3790
/* XXXshaver js_NewFileTokenStream should do this, because it drops */
3792
ts->principals = principals;
3793
JSPRINCIPALS_HOLD(cx, ts->principals);
3795
script = CompileTokenStream(cx, obj, ts, mark, NULL);
3796
LAST_FRAME_CHECKS(cx, script);
3800
JS_PUBLIC_API(JSObject *)
3801
JS_NewScriptObject(JSContext *cx, JSScript *script)
3805
obj = js_NewObject(cx, &js_ScriptClass, NULL, NULL);
3810
if (!JS_SetPrivate(cx, obj, script))
3812
script->object = obj;
3817
JS_PUBLIC_API(JSObject *)
3818
JS_GetScriptObject(JSScript *script)
3820
return script->object;
3824
JS_DestroyScript(JSContext *cx, JSScript *script)
3827
js_DestroyScript(cx, script);
3830
JS_PUBLIC_API(JSFunction *)
3831
JS_CompileFunction(JSContext *cx, JSObject *obj, const char *name,
3832
uintN nargs, const char **argnames,
3833
const char *bytes, size_t length,
3834
const char *filename, uintN lineno)
3840
chars = js_InflateString(cx, bytes, &length);
3843
fun = JS_CompileUCFunction(cx, obj, name, nargs, argnames, chars, length,
3849
JS_PUBLIC_API(JSFunction *)
3850
JS_CompileFunctionForPrincipals(JSContext *cx, JSObject *obj,
3851
JSPrincipals *principals, const char *name,
3852
uintN nargs, const char **argnames,
3853
const char *bytes, size_t length,
3854
const char *filename, uintN lineno)
3860
chars = js_InflateString(cx, bytes, &length);
3863
fun = JS_CompileUCFunctionForPrincipals(cx, obj, principals, name,
3864
nargs, argnames, chars, length,
3870
JS_PUBLIC_API(JSFunction *)
3871
JS_CompileUCFunction(JSContext *cx, JSObject *obj, const char *name,
3872
uintN nargs, const char **argnames,
3873
const jschar *chars, size_t length,
3874
const char *filename, uintN lineno)
3877
return JS_CompileUCFunctionForPrincipals(cx, obj, NULL, name,
3883
JS_PUBLIC_API(JSFunction *)
3884
JS_CompileUCFunctionForPrincipals(JSContext *cx, JSObject *obj,
3885
JSPrincipals *principals, const char *name,
3886
uintN nargs, const char **argnames,
3887
const jschar *chars, size_t length,
3888
const char *filename, uintN lineno)
3893
JSAtom *funAtom, *argAtom;
3897
mark = JS_ARENA_MARK(&cx->tempPool);
3898
ts = js_NewTokenStream(cx, chars, length, filename, lineno, principals);
3906
funAtom = js_Atomize(cx, name, strlen(name), 0);
3912
fun = js_NewFunction(cx, NULL, NULL, nargs, 0, obj, funAtom);
3916
for (i = 0; i < nargs; i++) {
3917
argAtom = js_Atomize(cx, argnames[i], strlen(argnames[i]), 0);
3920
if (!js_AddHiddenProperty(cx, fun->object, ATOM_TO_JSID(argAtom),
3921
js_GetArgument, js_SetArgument,
3923
JSPROP_PERMANENT | JSPROP_SHARED,
3924
SPROP_HAS_SHORTID, i)) {
3933
if (!js_CompileFunctionBody(cx, ts, fun)) {
3937
if (obj && funAtom) {
3938
if (!OBJ_DEFINE_PROPERTY(cx, obj, ATOM_TO_JSID(funAtom),
3939
OBJECT_TO_JSVAL(fun->object),
3940
NULL, NULL, JSPROP_ENUMERATE, NULL)) {
3946
js_CloseTokenStream(cx, ts);
3947
JS_ARENA_RELEASE(&cx->tempPool, mark);
3948
LAST_FRAME_CHECKS(cx, fun);
3952
JS_PUBLIC_API(JSString *)
3953
JS_DecompileScript(JSContext *cx, JSScript *script, const char *name,
3960
jp = js_NewPrinter(cx, name,
3961
indent & ~JS_DONT_PRETTY_PRINT,
3962
!(indent & JS_DONT_PRETTY_PRINT));
3965
if (js_DecompileScript(jp, script))
3966
str = js_GetPrinterOutput(jp);
3969
js_DestroyPrinter(jp);
3973
JS_PUBLIC_API(JSString *)
3974
JS_DecompileFunction(JSContext *cx, JSFunction *fun, uintN indent)
3980
jp = js_NewPrinter(cx, JS_GetFunctionName(fun),
3981
indent & ~JS_DONT_PRETTY_PRINT,
3982
!(indent & JS_DONT_PRETTY_PRINT));
3985
if (js_DecompileFunction(jp, fun))
3986
str = js_GetPrinterOutput(jp);
3989
js_DestroyPrinter(jp);
3993
JS_PUBLIC_API(JSString *)
3994
JS_DecompileFunctionBody(JSContext *cx, JSFunction *fun, uintN indent)
4000
jp = js_NewPrinter(cx, JS_GetFunctionName(fun),
4001
indent & ~JS_DONT_PRETTY_PRINT,
4002
!(indent & JS_DONT_PRETTY_PRINT));
4005
if (js_DecompileFunctionBody(jp, fun))
4006
str = js_GetPrinterOutput(jp);
4009
js_DestroyPrinter(jp);
4013
JS_PUBLIC_API(JSBool)
4014
JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval)
4019
ok = js_Execute(cx, obj, script, NULL, 0, rval);
4020
LAST_FRAME_CHECKS(cx, ok);
4024
JS_PUBLIC_API(JSBool)
4025
JS_ExecuteScriptPart(JSContext *cx, JSObject *obj, JSScript *script,
4026
JSExecPart part, jsval *rval)
4032
/* Make a temporary copy of the JSScript structure and farble it a bit. */
4034
if (part == JSEXEC_PROLOG) {
4035
tmp.length = PTRDIFF(tmp.main, tmp.code, jsbytecode);
4037
tmp.length -= PTRDIFF(tmp.main, tmp.code, jsbytecode);
4038
tmp.code = tmp.main;
4041
/* Tell the debugger about our temporary copy of the script structure. */
4043
if (rt->newScriptHook) {
4044
rt->newScriptHook(cx, tmp.filename, tmp.lineno, &tmp, NULL,
4045
rt->newScriptHookData);
4048
/* Execute the farbled struct and tell the debugger to forget about it. */
4049
ok = JS_ExecuteScript(cx, obj, &tmp, rval);
4050
if (rt->destroyScriptHook)
4051
rt->destroyScriptHook(cx, &tmp, rt->destroyScriptHookData);
4055
JS_PUBLIC_API(JSBool)
4056
JS_EvaluateScript(JSContext *cx, JSObject *obj,
4057
const char *bytes, uintN nbytes,
4058
const char *filename, uintN lineno,
4061
size_t length = nbytes;
4066
chars = js_InflateString(cx, bytes, &length);
4069
ok = JS_EvaluateUCScript(cx, obj, chars, length, filename, lineno, rval);
4074
JS_PUBLIC_API(JSBool)
4075
JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj,
4076
JSPrincipals *principals,
4077
const char *bytes, uintN nbytes,
4078
const char *filename, uintN lineno,
4081
size_t length = nbytes;
4086
chars = js_InflateString(cx, bytes, &length);
4089
ok = JS_EvaluateUCScriptForPrincipals(cx, obj, principals, chars, length,
4090
filename, lineno, rval);
4095
JS_PUBLIC_API(JSBool)
4096
JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
4097
const jschar *chars, uintN length,
4098
const char *filename, uintN lineno,
4102
return JS_EvaluateUCScriptForPrincipals(cx, obj, NULL, chars, length,
4103
filename, lineno, rval);
4106
JS_PUBLIC_API(JSBool)
4107
JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj,
4108
JSPrincipals *principals,
4109
const jschar *chars, uintN length,
4110
const char *filename, uintN lineno,
4118
options = cx->options;
4119
cx->options = options | JSOPTION_COMPILE_N_GO;
4120
script = JS_CompileUCScriptForPrincipals(cx, obj, principals, chars, length,
4122
cx->options = options;
4125
ok = js_Execute(cx, obj, script, NULL, 0, rval);
4126
LAST_FRAME_CHECKS(cx, ok);
4127
JS_DestroyScript(cx, script);
4131
JS_PUBLIC_API(JSBool)
4132
JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc,
4133
jsval *argv, jsval *rval)
4138
ok = js_InternalCall(cx, obj, OBJECT_TO_JSVAL(fun->object), argc, argv,
4140
LAST_FRAME_CHECKS(cx, ok);
4144
JS_PUBLIC_API(JSBool)
4145
JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, uintN argc,
4146
jsval *argv, jsval *rval)
4152
#if JS_HAS_XML_SUPPORT
4153
if (OBJECT_IS_XML(cx, obj)) {
4154
JSXMLObjectOps *ops;
4157
ops = (JSXMLObjectOps *) obj->map->ops;
4158
atom = js_Atomize(cx, name, strlen(name), 0);
4161
obj = ops->getMethod(cx, obj, ATOM_TO_JSID(atom), &fval);
4166
if (!JS_GetProperty(cx, obj, name, &fval))
4168
ok = js_InternalCall(cx, obj, fval, argc, argv, rval);
4169
LAST_FRAME_CHECKS(cx, ok);
4173
JS_PUBLIC_API(JSBool)
4174
JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc,
4175
jsval *argv, jsval *rval)
4180
ok = js_InternalCall(cx, obj, fval, argc, argv, rval);
4181
LAST_FRAME_CHECKS(cx, ok);
4185
JS_PUBLIC_API(JSBranchCallback)
4186
JS_SetBranchCallback(JSContext *cx, JSBranchCallback cb)
4188
JSBranchCallback oldcb;
4190
oldcb = cx->branchCallback;
4191
cx->branchCallback = cb;
4195
JS_PUBLIC_API(JSBool)
4196
JS_IsRunning(JSContext *cx)
4198
return cx->fp != NULL;
4201
JS_PUBLIC_API(JSBool)
4202
JS_IsConstructing(JSContext *cx)
4204
return cx->fp && (cx->fp->flags & JSFRAME_CONSTRUCTING);
4207
JS_FRIEND_API(JSBool)
4208
JS_IsAssigning(JSContext *cx)
4213
for (fp = cx->fp; fp && !fp->script; fp = fp->down)
4215
if (!fp || !(pc = fp->pc))
4217
return (js_CodeSpec[*pc].format & JOF_ASSIGNING) != 0;
4221
JS_SetCallReturnValue2(JSContext *cx, jsval v)
4223
#if JS_HAS_LVALUE_RETURN
4225
cx->rval2set = JS_TRUE;
4229
/************************************************************************/
4231
JS_PUBLIC_API(JSString *)
4232
JS_NewString(JSContext *cx, char *bytes, size_t length)
4236
size_t charsLength = length;
4239
/* Make a Unicode vector from the 8-bit char codes in bytes. */
4240
chars = js_InflateString(cx, bytes, &charsLength);
4244
/* Free chars (but not bytes, which caller frees on error) if we fail. */
4245
str = js_NewString(cx, chars, charsLength, 0);
4251
/* Hand off bytes to the deflated string cache, if possible. */
4252
if (!js_SetStringBytes(str, bytes, length))
4257
JS_PUBLIC_API(JSString *)
4258
JS_NewStringCopyN(JSContext *cx, const char *s, size_t n)
4264
js = js_InflateString(cx, s, &n);
4267
str = js_NewString(cx, js, n, 0);
4273
JS_PUBLIC_API(JSString *)
4274
JS_NewStringCopyZ(JSContext *cx, const char *s)
4282
return cx->runtime->emptyString;
4284
js = js_InflateString(cx, s, &n);
4287
str = js_NewString(cx, js, n, 0);
4293
JS_PUBLIC_API(JSString *)
4294
JS_InternString(JSContext *cx, const char *s)
4299
atom = js_Atomize(cx, s, strlen(s), ATOM_INTERNED);
4302
return ATOM_TO_STRING(atom);
4305
JS_PUBLIC_API(JSString *)
4306
JS_NewUCString(JSContext *cx, jschar *chars, size_t length)
4309
return js_NewString(cx, chars, length, 0);
4312
JS_PUBLIC_API(JSString *)
4313
JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n)
4316
return js_NewStringCopyN(cx, s, n, 0);
4319
JS_PUBLIC_API(JSString *)
4320
JS_NewUCStringCopyZ(JSContext *cx, const jschar *s)
4324
return cx->runtime->emptyString;
4325
return js_NewStringCopyZ(cx, s, 0);
4328
JS_PUBLIC_API(JSString *)
4329
JS_InternUCStringN(JSContext *cx, const jschar *s, size_t length)
4334
atom = js_AtomizeChars(cx, s, length, ATOM_INTERNED);
4337
return ATOM_TO_STRING(atom);
4340
JS_PUBLIC_API(JSString *)
4341
JS_InternUCString(JSContext *cx, const jschar *s)
4343
return JS_InternUCStringN(cx, s, js_strlen(s));
4346
JS_PUBLIC_API(char *)
4347
JS_GetStringBytes(JSString *str)
4351
bytes = js_GetStringBytes(str);
4352
return bytes ? bytes : "";
4355
JS_PUBLIC_API(jschar *)
4356
JS_GetStringChars(JSString *str)
4359
* API botch (again, shades of JS_GetStringBytes): we have no cx to pass
4360
* to js_UndependString (called by js_GetStringChars) for out-of-memory
4361
* error reports, so js_UndependString passes NULL and suppresses errors.
4362
* If it fails to convert a dependent string into an independent one, our
4363
* caller will not be guaranteed a \u0000 terminator as a backstop. This
4364
* may break some clients who already misbehave on embedded NULs.
4366
* The gain of dependent strings, which cure quadratic and cubic growth
4367
* rate bugs in string concatenation, is worth this slight loss in API
4372
chars = js_GetStringChars(str);
4373
return chars ? chars : JSSTRING_CHARS(str);
4376
JS_PUBLIC_API(size_t)
4377
JS_GetStringLength(JSString *str)
4379
return JSSTRING_LENGTH(str);
4383
JS_CompareStrings(JSString *str1, JSString *str2)
4385
return js_CompareStrings(str1, str2);
4388
JS_PUBLIC_API(JSString *)
4389
JS_NewGrowableString(JSContext *cx, jschar *chars, size_t length)
4392
return js_NewString(cx, chars, length, GCF_MUTABLE);
4395
JS_PUBLIC_API(JSString *)
4396
JS_NewDependentString(JSContext *cx, JSString *str, size_t start,
4400
return js_NewDependentString(cx, str, start, length, 0);
4403
JS_PUBLIC_API(JSString *)
4404
JS_ConcatStrings(JSContext *cx, JSString *left, JSString *right)
4407
return js_ConcatStrings(cx, left, right);
4410
JS_PUBLIC_API(const jschar *)
4411
JS_UndependString(JSContext *cx, JSString *str)
4414
return js_UndependString(cx, str);
4417
JS_PUBLIC_API(JSBool)
4418
JS_MakeStringImmutable(JSContext *cx, JSString *str)
4421
if (!js_UndependString(cx, str))
4424
*js_GetGCThingFlags(str) &= ~GCF_MUTABLE;
4428
JS_PUBLIC_API(JSBool)
4429
JS_EncodeCharacters(JSContext *cx, const jschar *src, size_t srclen, char *dst,
4432
return js_DeflateStringToBuffer(cx, src, srclen, dst, dstlenp);
4435
JS_PUBLIC_API(JSBool)
4436
JS_DecodeBytes(JSContext *cx, const char *src, size_t srclen, jschar *dst,
4439
return js_InflateStringToBuffer(cx, src, srclen, dst, dstlenp);
4442
JS_PUBLIC_API(JSBool)
4443
JS_StringsAreUTF8 ()
4445
#ifdef JS_C_STRINGS_ARE_UTF8
4452
/************************************************************************/
4455
JS_ReportError(JSContext *cx, const char *format, ...)
4459
va_start(ap, format);
4460
js_ReportErrorVA(cx, JSREPORT_ERROR, format, ap);
4465
JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
4466
void *userRef, const uintN errorNumber, ...)
4470
va_start(ap, errorNumber);
4471
js_ReportErrorNumberVA(cx, JSREPORT_ERROR, errorCallback, userRef,
4472
errorNumber, JS_TRUE, ap);
4477
JS_ReportErrorNumberUC(JSContext *cx, JSErrorCallback errorCallback,
4478
void *userRef, const uintN errorNumber, ...)
4482
va_start(ap, errorNumber);
4483
js_ReportErrorNumberVA(cx, JSREPORT_ERROR, errorCallback, userRef,
4484
errorNumber, JS_FALSE, ap);
4488
JS_PUBLIC_API(JSBool)
4489
JS_ReportWarning(JSContext *cx, const char *format, ...)
4494
va_start(ap, format);
4495
ok = js_ReportErrorVA(cx, JSREPORT_WARNING, format, ap);
4500
JS_PUBLIC_API(JSBool)
4501
JS_ReportErrorFlagsAndNumber(JSContext *cx, uintN flags,
4502
JSErrorCallback errorCallback, void *userRef,
4503
const uintN errorNumber, ...)
4508
va_start(ap, errorNumber);
4509
ok = js_ReportErrorNumberVA(cx, flags, errorCallback, userRef,
4510
errorNumber, JS_TRUE, ap);
4515
JS_PUBLIC_API(JSBool)
4516
JS_ReportErrorFlagsAndNumberUC(JSContext *cx, uintN flags,
4517
JSErrorCallback errorCallback, void *userRef,
4518
const uintN errorNumber, ...)
4523
va_start(ap, errorNumber);
4524
ok = js_ReportErrorNumberVA(cx, flags, errorCallback, userRef,
4525
errorNumber, JS_FALSE, ap);
4531
JS_ReportOutOfMemory(JSContext *cx)
4533
js_ReportOutOfMemory(cx, js_GetErrorMessage);
4536
JS_PUBLIC_API(JSErrorReporter)
4537
JS_SetErrorReporter(JSContext *cx, JSErrorReporter er)
4539
JSErrorReporter older;
4541
older = cx->errorReporter;
4542
cx->errorReporter = er;
4546
/************************************************************************/
4549
* Regular Expressions.
4551
JS_PUBLIC_API(JSObject *)
4552
JS_NewRegExpObject(JSContext *cx, char *bytes, size_t length, uintN flags)
4559
chars = js_InflateString(cx, bytes, &length);
4562
obj = js_NewRegExpObject(cx, NULL, chars, length, flags);
4566
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NO_REG_EXPS);
4571
JS_PUBLIC_API(JSObject *)
4572
JS_NewUCRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags)
4576
return js_NewRegExpObject(cx, NULL, chars, length, flags);
4578
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NO_REG_EXPS);
4584
JS_SetRegExpInput(JSContext *cx, JSString *input, JSBool multiline)
4586
JSRegExpStatics *res;
4589
/* No locking required, cx is thread-private and input must be live. */
4590
res = &cx->regExpStatics;
4592
res->multiline = multiline;
4593
cx->runtime->gcPoke = JS_TRUE;
4597
JS_ClearRegExpStatics(JSContext *cx)
4599
JSRegExpStatics *res;
4601
/* No locking required, cx is thread-private and input must be live. */
4602
res = &cx->regExpStatics;
4604
res->multiline = JS_FALSE;
4605
res->parenCount = 0;
4606
res->lastMatch = res->lastParen = js_EmptySubString;
4607
res->leftContext = res->rightContext = js_EmptySubString;
4608
cx->runtime->gcPoke = JS_TRUE;
4612
JS_ClearRegExpRoots(JSContext *cx)
4614
JSRegExpStatics *res;
4616
/* No locking required, cx is thread-private and input must be live. */
4617
res = &cx->regExpStatics;
4619
cx->runtime->gcPoke = JS_TRUE;
4622
/* TODO: compile, execute, get/set other statics... */
4624
/************************************************************************/
4627
JS_SetLocaleCallbacks(JSContext *cx, JSLocaleCallbacks *callbacks)
4629
cx->localeCallbacks = callbacks;
4632
JS_PUBLIC_API(JSLocaleCallbacks *)
4633
JS_GetLocaleCallbacks(JSContext *cx)
4635
return cx->localeCallbacks;
4638
/************************************************************************/
4640
JS_PUBLIC_API(JSBool)
4641
JS_IsExceptionPending(JSContext *cx)
4643
#if JS_HAS_EXCEPTIONS
4644
return (JSBool) cx->throwing;
4650
JS_PUBLIC_API(JSBool)
4651
JS_GetPendingException(JSContext *cx, jsval *vp)
4653
#if JS_HAS_EXCEPTIONS
4657
*vp = cx->exception;
4665
JS_SetPendingException(JSContext *cx, jsval v)
4668
#if JS_HAS_EXCEPTIONS
4669
cx->throwing = JS_TRUE;
4675
JS_ClearPendingException(JSContext *cx)
4677
#if JS_HAS_EXCEPTIONS
4678
cx->throwing = JS_FALSE;
4679
cx->exception = JSVAL_VOID;
4683
JS_PUBLIC_API(JSBool)
4684
JS_ReportPendingException(JSContext *cx)
4686
#if JS_HAS_EXCEPTIONS
4692
* Set cx->creatingException to suppress the standard error-to-exception
4693
* conversion done by all {js,JS}_Report* functions except for OOM. The
4694
* cx->creatingException flag was added to suppress recursive divergence
4695
* under js_ErrorToException, but it serves for our purposes here too.
4697
save = cx->creatingException;
4698
cx->creatingException = JS_TRUE;
4699
ok = js_ReportUncaughtException(cx);
4700
cx->creatingException = save;
4707
#if JS_HAS_EXCEPTIONS
4708
struct JSExceptionState {
4714
JS_PUBLIC_API(JSExceptionState *)
4715
JS_SaveExceptionState(JSContext *cx)
4717
#if JS_HAS_EXCEPTIONS
4718
JSExceptionState *state;
4721
state = (JSExceptionState *) JS_malloc(cx, sizeof(JSExceptionState));
4723
state->throwing = JS_GetPendingException(cx, &state->exception);
4724
if (state->throwing && JSVAL_IS_GCTHING(state->exception))
4725
js_AddRoot(cx, &state->exception, "JSExceptionState.exception");
4734
JS_RestoreExceptionState(JSContext *cx, JSExceptionState *state)
4736
#if JS_HAS_EXCEPTIONS
4739
if (state->throwing)
4740
JS_SetPendingException(cx, state->exception);
4742
JS_ClearPendingException(cx);
4743
JS_DropExceptionState(cx, state);
4749
JS_DropExceptionState(JSContext *cx, JSExceptionState *state)
4751
#if JS_HAS_EXCEPTIONS
4754
if (state->throwing && JSVAL_IS_GCTHING(state->exception))
4755
JS_RemoveRoot(cx, &state->exception);
4761
JS_PUBLIC_API(JSErrorReport *)
4762
JS_ErrorFromException(JSContext *cx, jsval v)
4764
#if JS_HAS_ERROR_EXCEPTIONS
4766
return js_ErrorFromException(cx, v);
4772
JS_PUBLIC_API(JSBool)
4773
JS_ThrowReportedError(JSContext *cx, const char *message,
4774
JSErrorReport *reportp)
4776
return js_ErrorToException(cx, message, reportp);
4779
#ifdef JS_THREADSAFE
4780
JS_PUBLIC_API(jsword)
4781
JS_GetContextThread(JSContext *cx)
4786
JS_PUBLIC_API(jsword)
4787
JS_SetContextThread(JSContext *cx)
4789
jsword old = cx->thread;
4790
cx->thread = js_CurrentThreadId();
4794
JS_PUBLIC_API(jsword)
4795
JS_ClearContextThread(JSContext *cx)
4797
jsword old = cx->thread;
4803
/************************************************************************/
4806
#include <windows.h>
4808
* Initialization routine for the JS DLL...
4812
* Global Instance handle...
4813
* In Win32 this is the module handle of the DLL.
4815
* In Win16 this is the instance handle of the application
4816
* which loaded the DLL.
4820
BOOL WINAPI DllMain (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
4827
int CALLBACK LibMain( HINSTANCE hInst, WORD wDataSeg,
4828
WORD cbHeapSize, LPSTR lpszCmdLine )
4833
BOOL CALLBACK __loadds WEP(BOOL fSystemExit)
4838
#endif /* !_WIN32 */