forked from classilla/tenfourfox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsapi.cpp
More file actions
6315 lines (5429 loc) · 181 KB
/
jsapi.cpp
File metadata and controls
6315 lines (5429 loc) · 181 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* JavaScript API.
*/
#include "jsapi.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/PodOperations.h"
#include "mozilla/UniquePtr.h"
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#include "jsarray.h"
#include "jsatom.h"
#include "jsbool.h"
#include "jscntxt.h"
#include "jsdate.h"
#include "jsexn.h"
#include "jsfriendapi.h"
#include "jsfun.h"
#include "jsgc.h"
#include "jsiter.h"
#include "jslock.h"
#include "jsmath.h"
#include "jsnum.h"
#include "jsobj.h"
#include "json.h"
#include "jsprf.h"
#include "jsscript.h"
#include "jsstr.h"
#include "jstypes.h"
#include "jsutil.h"
#include "jswatchpoint.h"
#include "jsweakmap.h"
#include "jswrapper.h"
#include "asmjs/AsmJSLink.h"
#include "builtin/AtomicsObject.h"
#include "builtin/Eval.h"
#include "builtin/Intl.h"
#include "builtin/MapObject.h"
#include "builtin/RegExp.h"
#include "builtin/SymbolObject.h"
#ifdef ENABLE_BINARYDATA
#include "builtin/SIMD.h"
#include "builtin/TypedObject.h"
#endif
#include "frontend/BytecodeCompiler.h"
#include "frontend/FullParseHandler.h" // for JS_BufferIsCompileableUnit
#include "frontend/Parser.h" // for JS_BufferIsCompileableUnit
#include "gc/Marking.h"
#include "jit/JitCommon.h"
#include "js/CharacterEncoding.h"
#include "js/Conversions.h"
#include "js/Date.h"
#include "js/Initialization.h"
#include "js/Proxy.h"
#include "js/SliceBudget.h"
#include "js/StructuredClone.h"
#include "vm/DateObject.h"
#include "vm/Debugger.h"
#include "vm/ErrorObject.h"
#include "vm/HelperThreads.h"
#include "vm/Interpreter.h"
#include "vm/RegExpStatics.h"
#include "vm/Runtime.h"
#include "vm/SavedStacks.h"
#include "vm/ScopeObject.h"
#include "vm/Shape.h"
#include "vm/StopIterationObject.h"
#include "vm/StringBuffer.h"
#include "vm/Symbol.h"
#include "vm/TypedArrayCommon.h"
#include "vm/WrapperObject.h"
#include "vm/Xdr.h"
#include "jsatominlines.h"
#include "jsfuninlines.h"
#include "jsscriptinlines.h"
#include "vm/Interpreter-inl.h"
#include "vm/NativeObject-inl.h"
#include "vm/SavedStacks-inl.h"
#include "vm/String-inl.h"
using namespace js;
using namespace js::gc;
using mozilla::Maybe;
using mozilla::PodCopy;
using mozilla::PodZero;
using mozilla::UniquePtr;
using JS::AutoGCRooter;
using JS::ToInt32;
using JS::ToInteger;
using JS::ToUint32;
using js::frontend::Parser;
#ifdef HAVE_VA_LIST_AS_ARRAY
#define JS_ADDRESSOF_VA_LIST(ap) ((va_list*)(ap))
#else
#define JS_ADDRESSOF_VA_LIST(ap) (&(ap))
#endif
bool
JS::CallArgs::requireAtLeast(JSContext* cx, const char* fnname, unsigned required) const
{
if (length() < required) {
char numArgsStr[40];
JS_snprintf(numArgsStr, sizeof numArgsStr, "%u", required - 1);
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_MORE_ARGS_NEEDED,
fnname, numArgsStr, required == 2 ? "" : "s");
return false;
}
return true;
}
static bool
ErrorTakesArguments(unsigned msg)
{
MOZ_ASSERT(msg < JSErr_Limit);
unsigned argCount = js_ErrorFormatString[msg].argCount;
MOZ_ASSERT(argCount <= 2);
return argCount == 1 || argCount == 2;
}
static bool
ErrorTakesObjectArgument(unsigned msg)
{
MOZ_ASSERT(msg < JSErr_Limit);
unsigned argCount = js_ErrorFormatString[msg].argCount;
MOZ_ASSERT(argCount <= 2);
return argCount == 2;
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::reportStrictErrorOrWarning(JSContext* cx, HandleObject obj, HandleId id,
bool strict)
{
static_assert(unsigned(OkCode) == unsigned(JSMSG_NOT_AN_ERROR),
"unsigned value of OkCode must not be an error code");
MOZ_ASSERT(code_ != Uninitialized);
MOZ_ASSERT(!ok());
unsigned flags = strict ? JSREPORT_ERROR : (JSREPORT_WARNING | JSREPORT_STRICT);
if (code_ == JSMSG_OBJECT_NOT_EXTENSIBLE || code_ == JSMSG_SET_NON_OBJECT_RECEIVER) {
RootedValue val(cx, ObjectValue(*obj));
return ReportValueErrorFlags(cx, flags, code_, JSDVG_IGNORE_STACK, val,
nullptr, nullptr, nullptr);
}
if (ErrorTakesArguments(code_)) {
RootedValue idv(cx, IdToValue(id));
RootedString str(cx, ValueToSource(cx, idv));
if (!str)
return false;
JSAutoByteString propName(cx, str);
if (!propName)
return false;
if (ErrorTakesObjectArgument(code_)) {
return JS_ReportErrorFlagsAndNumber(cx, flags, GetErrorMessage, nullptr, code_,
obj->getClass()->name, propName.ptr());
}
return JS_ReportErrorFlagsAndNumber(cx, flags, GetErrorMessage, nullptr, code_,
propName.ptr());
}
return JS_ReportErrorFlagsAndNumber(cx, flags, GetErrorMessage, nullptr, code_);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::reportStrictErrorOrWarning(JSContext* cx, HandleObject obj, bool strict)
{
MOZ_ASSERT(code_ != Uninitialized);
MOZ_ASSERT(!ok());
MOZ_ASSERT(!ErrorTakesArguments(code_));
unsigned flags = strict ? JSREPORT_ERROR : (JSREPORT_WARNING | JSREPORT_STRICT);
return JS_ReportErrorFlagsAndNumber(cx, flags, GetErrorMessage, nullptr, code_);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failCantRedefineProp()
{
return fail(JSMSG_CANT_REDEFINE_PROP);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failReadOnly()
{
return fail(JSMSG_READ_ONLY);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failGetterOnly()
{
return fail(JSMSG_GETTER_ONLY);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failCantDelete()
{
return fail(JSMSG_CANT_DELETE);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failCantSetInterposed()
{
return fail(JSMSG_CANT_SET_INTERPOSED);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failCantDefineWindowElement()
{
return fail(JSMSG_CANT_DEFINE_WINDOW_ELEMENT);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failCantDeleteWindowElement()
{
return fail(JSMSG_CANT_DELETE_WINDOW_ELEMENT);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failCantDeleteWindowNamedProperty()
{
return fail(JSMSG_CANT_DELETE_WINDOW_NAMED_PROPERTY);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failCantPreventExtensions()
{
return fail(JSMSG_CANT_PREVENT_EXTENSIONS);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failCantSetProto()
{
return fail(JSMSG_CANT_SET_PROTO);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failNoNamedSetter()
{
return fail(JSMSG_NO_NAMED_SETTER);
}
JS_PUBLIC_API(bool)
JS::ObjectOpResult::failNoIndexedSetter()
{
return fail(JSMSG_NO_INDEXED_SETTER);
}
JS_PUBLIC_API(int64_t)
JS_Now()
{
return PRMJ_Now();
}
JS_PUBLIC_API(Value)
JS_GetNaNValue(JSContext* cx)
{
return cx->runtime()->NaNValue;
}
JS_PUBLIC_API(Value)
JS_GetNegativeInfinityValue(JSContext* cx)
{
return cx->runtime()->negativeInfinityValue;
}
JS_PUBLIC_API(Value)
JS_GetPositiveInfinityValue(JSContext* cx)
{
return cx->runtime()->positiveInfinityValue;
}
JS_PUBLIC_API(Value)
JS_GetEmptyStringValue(JSContext* cx)
{
return StringValue(cx->runtime()->emptyString);
}
JS_PUBLIC_API(JSString*)
JS_GetEmptyString(JSRuntime* rt)
{
MOZ_ASSERT(rt->hasContexts());
return rt->emptyString;
}
namespace js {
void
AssertHeapIsIdle(JSRuntime* rt)
{
MOZ_ASSERT(!rt->isHeapBusy());
}
void
AssertHeapIsIdle(JSContext* cx)
{
AssertHeapIsIdle(cx->runtime());
}
} // namespace js
static void
AssertHeapIsIdleOrIterating(JSRuntime* rt)
{
MOZ_ASSERT(!rt->isHeapCollecting());
}
static void
AssertHeapIsIdleOrIterating(JSContext* cx)
{
AssertHeapIsIdleOrIterating(cx->runtime());
}
static void
AssertHeapIsIdleOrStringIsFlat(JSContext* cx, JSString* str)
{
/*
* We allow some functions to be called during a GC as long as the argument
* is a flat string, since that will not cause allocation.
*/
MOZ_ASSERT_IF(cx->runtime()->isHeapBusy(), str->isFlat());
}
JS_PUBLIC_API(bool)
JS_ValueToObject(JSContext* cx, HandleValue value, MutableHandleObject objp)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, value);
if (value.isNullOrUndefined()) {
objp.set(nullptr);
return true;
}
JSObject* obj = ToObject(cx, value);
if (!obj)
return false;
objp.set(obj);
return true;
}
JS_PUBLIC_API(JSFunction*)
JS_ValueToFunction(JSContext* cx, HandleValue value)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, value);
return ReportIfNotFunction(cx, value);
}
JS_PUBLIC_API(JSFunction*)
JS_ValueToConstructor(JSContext* cx, HandleValue value)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, value);
return ReportIfNotFunction(cx, value);
}
JS_PUBLIC_API(JSString*)
JS_ValueToSource(JSContext* cx, HandleValue value)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, value);
return ValueToSource(cx, value);
}
JS_PUBLIC_API(bool)
JS_DoubleIsInt32(double d, int32_t* ip)
{
return mozilla::NumberIsInt32(d, ip);
}
JS_PUBLIC_API(JSType)
JS_TypeOfValue(JSContext* cx, HandleValue value)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, value);
return TypeOfValue(value);
}
JS_PUBLIC_API(bool)
JS_StrictlyEqual(JSContext* cx, HandleValue value1, HandleValue value2, bool* equal)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, value1, value2);
MOZ_ASSERT(equal);
return StrictlyEqual(cx, value1, value2, equal);
}
JS_PUBLIC_API(bool)
JS_LooselyEqual(JSContext* cx, HandleValue value1, HandleValue value2, bool* equal)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, value1, value2);
MOZ_ASSERT(equal);
return LooselyEqual(cx, value1, value2, equal);
}
JS_PUBLIC_API(bool)
JS_SameValue(JSContext* cx, HandleValue value1, HandleValue value2, bool* same)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, value1, value2);
MOZ_ASSERT(same);
return SameValue(cx, value1, value2, same);
}
JS_PUBLIC_API(bool)
JS_IsBuiltinEvalFunction(JSFunction* fun)
{
return IsAnyBuiltinEval(fun);
}
JS_PUBLIC_API(bool)
JS_IsBuiltinFunctionConstructor(JSFunction* fun)
{
return fun->isBuiltinFunctionConstructor();
}
/************************************************************************/
#ifdef DEBUG
JS_FRIEND_API(bool)
JS::isGCEnabled()
{
return !TlsPerThreadData.get()->suppressGC;
}
#else
JS_FRIEND_API(bool) JS::isGCEnabled() { return true; }
#endif
JS_PUBLIC_API(JSRuntime*)
JS_NewRuntime(uint32_t maxbytes, uint32_t maxNurseryBytes, JSRuntime* parentRuntime)
{
MOZ_ASSERT(JS::detail::libraryInitState == JS::detail::InitState::Running,
"must call JS_Init prior to creating any JSRuntimes");
// Make sure that all parent runtimes are the topmost parent.
while (parentRuntime && parentRuntime->parentRuntime)
parentRuntime = parentRuntime->parentRuntime;
JSRuntime* rt = js_new<JSRuntime>(parentRuntime);
if (!rt)
return nullptr;
if (!rt->init(maxbytes, maxNurseryBytes)) {
JS_DestroyRuntime(rt);
return nullptr;
}
return rt;
}
JS_PUBLIC_API(void)
JS_DestroyRuntime(JSRuntime* rt)
{
js_delete(rt);
}
static JS_CurrentEmbedderTimeFunction currentEmbedderTimeFunction;
JS_PUBLIC_API(void)
JS_SetCurrentEmbedderTimeFunction(JS_CurrentEmbedderTimeFunction timeFn)
{
currentEmbedderTimeFunction = timeFn;
}
JS_PUBLIC_API(double)
JS_GetCurrentEmbedderTime()
{
if (currentEmbedderTimeFunction)
return currentEmbedderTimeFunction();
return PRMJ_Now() / static_cast<double>(PRMJ_USEC_PER_MSEC);
}
JS_PUBLIC_API(void*)
JS_GetRuntimePrivate(JSRuntime* rt)
{
return rt->data;
}
JS_PUBLIC_API(void)
JS_SetRuntimePrivate(JSRuntime* rt, void* data)
{
rt->data = data;
}
static void
StartRequest(JSContext* cx)
{
JSRuntime* rt = cx->runtime();
MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt));
if (rt->requestDepth) {
rt->requestDepth++;
} else {
/* Indicate that a request is running. */
rt->requestDepth = 1;
rt->triggerActivityCallback(true);
}
}
static void
StopRequest(JSContext* cx)
{
JSRuntime* rt = cx->runtime();
MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt));
MOZ_ASSERT(rt->requestDepth != 0);
if (rt->requestDepth != 1) {
rt->requestDepth--;
} else {
rt->requestDepth = 0;
rt->triggerActivityCallback(false);
}
}
JS_PUBLIC_API(void)
JS_BeginRequest(JSContext* cx)
{
cx->outstandingRequests++;
StartRequest(cx);
}
JS_PUBLIC_API(void)
JS_EndRequest(JSContext* cx)
{
MOZ_ASSERT(cx->outstandingRequests != 0);
cx->outstandingRequests--;
StopRequest(cx);
}
JS_PUBLIC_API(void)
JS_SetContextCallback(JSRuntime* rt, JSContextCallback cxCallback, void* data)
{
rt->cxCallback = cxCallback;
rt->cxCallbackData = data;
}
JS_PUBLIC_API(JSContext*)
JS_NewContext(JSRuntime* rt, size_t stackChunkSize)
{
return NewContext(rt, stackChunkSize);
}
JS_PUBLIC_API(void)
JS_DestroyContext(JSContext* cx)
{
MOZ_ASSERT(!cx->compartment());
DestroyContext(cx, DCM_FORCE_GC);
}
JS_PUBLIC_API(void)
JS_DestroyContextNoGC(JSContext* cx)
{
MOZ_ASSERT(!cx->compartment());
DestroyContext(cx, DCM_NO_GC);
}
JS_PUBLIC_API(void*)
JS_GetContextPrivate(JSContext* cx)
{
return cx->data;
}
JS_PUBLIC_API(void)
JS_SetContextPrivate(JSContext* cx, void* data)
{
cx->data = data;
}
JS_PUBLIC_API(void*)
JS_GetSecondContextPrivate(JSContext* cx)
{
return cx->data2;
}
JS_PUBLIC_API(void)
JS_SetSecondContextPrivate(JSContext* cx, void* data)
{
cx->data2 = data;
}
JS_PUBLIC_API(JSRuntime*)
JS_GetRuntime(JSContext* cx)
{
return cx->runtime();
}
JS_PUBLIC_API(JSRuntime*)
JS_GetParentRuntime(JSContext* cx)
{
JSRuntime* rt = cx->runtime();
return rt->parentRuntime ? rt->parentRuntime : rt;
}
JS_PUBLIC_API(JSContext*)
JS_ContextIterator(JSRuntime* rt, JSContext** iterp)
{
JSContext* cx = *iterp;
cx = cx ? cx->getNext() : rt->contextList.getFirst();
*iterp = cx;
return cx;
}
JS_PUBLIC_API(JSVersion)
JS_GetVersion(JSContext* cx)
{
return VersionNumber(cx->findVersion());
}
JS_PUBLIC_API(void)
JS_SetVersionForCompartment(JSCompartment* compartment, JSVersion version)
{
compartment->options().setVersion(version);
}
static const struct v2smap {
JSVersion version;
const char* string;
} v2smap[] = {
{JSVERSION_ECMA_3, "ECMAv3"},
{JSVERSION_1_6, "1.6"},
{JSVERSION_1_7, "1.7"},
{JSVERSION_1_8, "1.8"},
{JSVERSION_ECMA_5, "ECMAv5"},
{JSVERSION_DEFAULT, js_default_str},
{JSVERSION_DEFAULT, "1.0"},
{JSVERSION_DEFAULT, "1.1"},
{JSVERSION_DEFAULT, "1.2"},
{JSVERSION_DEFAULT, "1.3"},
{JSVERSION_DEFAULT, "1.4"},
{JSVERSION_DEFAULT, "1.5"},
{JSVERSION_UNKNOWN, nullptr}, /* must be last, nullptr is sentinel */
};
JS_PUBLIC_API(const char*)
JS_VersionToString(JSVersion version)
{
int i;
for (i = 0; v2smap[i].string; i++)
if (v2smap[i].version == version)
return v2smap[i].string;
return "unknown";
}
JS_PUBLIC_API(JSVersion)
JS_StringToVersion(const char* string)
{
int i;
for (i = 0; v2smap[i].string; i++)
if (strcmp(v2smap[i].string, string) == 0)
return v2smap[i].version;
return JSVERSION_UNKNOWN;
}
JS_PUBLIC_API(JS::RuntimeOptions&)
JS::RuntimeOptionsRef(JSRuntime* rt)
{
return rt->options();
}
JS_PUBLIC_API(JS::RuntimeOptions&)
JS::RuntimeOptionsRef(JSContext* cx)
{
return cx->runtime()->options();
}
JS_PUBLIC_API(JS::ContextOptions&)
JS::ContextOptionsRef(JSContext* cx)
{
return cx->options();
}
JS_PUBLIC_API(const char*)
JS_GetImplementationVersion(void)
{
return "JavaScript-C" MOZILLA_VERSION;
}
JS_PUBLIC_API(void)
JS_SetDestroyCompartmentCallback(JSRuntime* rt, JSDestroyCompartmentCallback callback)
{
rt->destroyCompartmentCallback = callback;
}
JS_PUBLIC_API(void)
JS_SetDestroyZoneCallback(JSRuntime* rt, JSZoneCallback callback)
{
rt->destroyZoneCallback = callback;
}
JS_PUBLIC_API(void)
JS_SetSweepZoneCallback(JSRuntime* rt, JSZoneCallback callback)
{
rt->sweepZoneCallback = callback;
}
JS_PUBLIC_API(void)
JS_SetCompartmentNameCallback(JSRuntime* rt, JSCompartmentNameCallback callback)
{
rt->compartmentNameCallback = callback;
}
JS_PUBLIC_API(void)
JS_SetWrapObjectCallbacks(JSRuntime* rt, const JSWrapObjectCallbacks* callbacks)
{
rt->wrapObjectCallbacks = callbacks;
}
JS_PUBLIC_API(JSCompartment*)
JS_EnterCompartment(JSContext* cx, JSObject* target)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
JSCompartment* oldCompartment = cx->compartment();
cx->enterCompartment(target->compartment());
return oldCompartment;
}
JS_PUBLIC_API(void)
JS_LeaveCompartment(JSContext* cx, JSCompartment* oldCompartment)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
cx->leaveCompartment(oldCompartment);
}
JSAutoCompartment::JSAutoCompartment(JSContext* cx, JSObject* target
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
: cx_(cx),
oldCompartment_(cx->compartment())
{
AssertHeapIsIdleOrIterating(cx_);
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
cx_->enterCompartment(target->compartment());
}
JSAutoCompartment::JSAutoCompartment(JSContext* cx, JSScript* target
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
: cx_(cx),
oldCompartment_(cx->compartment())
{
AssertHeapIsIdleOrIterating(cx_);
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
cx_->enterCompartment(target->compartment());
}
JSAutoCompartment::~JSAutoCompartment()
{
cx_->leaveCompartment(oldCompartment_);
}
JSAutoNullableCompartment::JSAutoNullableCompartment(JSContext* cx,
JSObject* targetOrNull
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
: cx_(cx),
oldCompartment_(cx->compartment())
{
AssertHeapIsIdleOrIterating(cx_);
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
if (targetOrNull) {
cx_->enterCompartment(targetOrNull->compartment());
} else {
cx_->enterNullCompartment();
}
}
JSAutoNullableCompartment::~JSAutoNullableCompartment()
{
cx_->leaveCompartment(oldCompartment_);
}
JS_PUBLIC_API(void)
JS_SetCompartmentPrivate(JSCompartment* compartment, void* data)
{
compartment->data = data;
}
JS_PUBLIC_API(void*)
JS_GetCompartmentPrivate(JSCompartment* compartment)
{
return compartment->data;
}
JS_PUBLIC_API(JSAddonId*)
JS::NewAddonId(JSContext* cx, HandleString str)
{
return static_cast<JSAddonId*>(JS_AtomizeAndPinJSString(cx, str));
}
JS_PUBLIC_API(JSString*)
JS::StringOfAddonId(JSAddonId* id)
{
return id;
}
JS_PUBLIC_API(JSAddonId*)
JS::AddonIdOfObject(JSObject* obj)
{
return obj->compartment()->addonId;
}
JS_PUBLIC_API(void)
JS_SetZoneUserData(JS::Zone* zone, void* data)
{
zone->data = data;
}
JS_PUBLIC_API(void*)
JS_GetZoneUserData(JS::Zone* zone)
{
return zone->data;
}
JS_PUBLIC_API(bool)
JS_WrapObject(JSContext* cx, MutableHandleObject objp)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
if (objp)
JS::ExposeObjectToActiveJS(objp);
return cx->compartment()->wrap(cx, objp);
}
JS_PUBLIC_API(bool)
JS_WrapValue(JSContext* cx, MutableHandleValue vp)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
JS::ExposeValueToActiveJS(vp);
return cx->compartment()->wrap(cx, vp);
}
/*
* Identity remapping. Not for casual consumers.
*
* Normally, an object's contents and its identity are inextricably linked.
* Identity is determined by the address of the JSObject* in the heap, and
* the contents are what is located at that address. Transplanting allows these
* concepts to be separated through a combination of swapping (exchanging the
* contents of two same-compartment objects) and remapping cross-compartment
* identities by altering wrappers.
*
* The |origobj| argument should be the object whose identity needs to be
* remapped, usually to another compartment. The contents of |origobj| are
* destroyed.
*
* The |target| argument serves two purposes:
*
* First, |target| serves as a hint for the new identity of the object. The new
* identity object will always be in the same compartment as |target|, but
* if that compartment already had an object representing |origobj| (either a
* cross-compartment wrapper for it, or |origobj| itself if the two arguments
* are same-compartment), the existing object is used. Otherwise, |target|
* itself is used. To avoid ambiguity, JS_TransplantObject always returns the
* new identity.
*
* Second, the new identity object's contents will be those of |target|. A swap()
* is used to make this happen if an object other than |target| is used.
*
* We don't have a good way to recover from failure in this function, so
* we intentionally crash instead.
*/
JS_PUBLIC_API(JSObject*)
JS_TransplantObject(JSContext* cx, HandleObject origobj, HandleObject target)
{
AssertHeapIsIdle(cx);
MOZ_ASSERT(origobj != target);
MOZ_ASSERT(!origobj->is<CrossCompartmentWrapperObject>());
MOZ_ASSERT(!target->is<CrossCompartmentWrapperObject>());
RootedValue origv(cx, ObjectValue(*origobj));
RootedObject newIdentity(cx);
{
AutoDisableProxyCheck adpc(cx->runtime());
JSCompartment* destination = target->compartment();
if (origobj->compartment() == destination) {
// If the original object is in the same compartment as the
// destination, then we know that we won't find a wrapper in the
// destination's cross compartment map and that the same
// object will continue to work.
if (!JSObject::swap(cx, origobj, target))
MOZ_CRASH();
newIdentity = origobj;
} else if (WrapperMap::Ptr p = destination->lookupWrapper(origv)) {
// There might already be a wrapper for the original object in
// the new compartment. If there is, we use its identity and swap
// in the contents of |target|.
newIdentity = &p->value().get().toObject();
// When we remove origv from the wrapper map, its wrapper, newIdentity,
// must immediately cease to be a cross-compartment wrapper. Neuter it.
destination->removeWrapper(p);
NukeCrossCompartmentWrapper(cx, newIdentity);
if (!JSObject::swap(cx, newIdentity, target))
MOZ_CRASH();
} else {
// Otherwise, we use |target| for the new identity object.
newIdentity = target;
}
// Now, iterate through other scopes looking for references to the
// old object, and update the relevant cross-compartment wrappers.
if (!RemapAllWrappersForObject(cx, origobj, newIdentity))
MOZ_CRASH();
// Lastly, update the original object to point to the new one.
if (origobj->compartment() != destination) {
RootedObject newIdentityWrapper(cx, newIdentity);
AutoCompartment ac(cx, origobj);
if (!JS_WrapObject(cx, &newIdentityWrapper))
MOZ_CRASH();
MOZ_ASSERT(Wrapper::wrappedObject(newIdentityWrapper) == newIdentity);
if (!JSObject::swap(cx, origobj, newIdentityWrapper))
MOZ_CRASH();
origobj->compartment()->putWrapper(cx, CrossCompartmentKey(newIdentity), origv);
}
}
// The new identity object might be one of several things. Return it to avoid
// ambiguity.
return newIdentity;
}
/*
* Recompute all cross-compartment wrappers for an object, resetting state.
* Gecko uses this to clear Xray wrappers when doing a navigation that reuses
* the inner window and global object.
*/
JS_PUBLIC_API(bool)
JS_RefreshCrossCompartmentWrappers(JSContext* cx, HandleObject obj)
{
return RemapAllWrappersForObject(cx, obj, obj);
}
JS_PUBLIC_API(bool)
JS_InitStandardClasses(JSContext* cx, HandleObject obj)
{
MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, obj);
Rooted<GlobalObject*> global(cx, &obj->global());
return GlobalObject::initStandardClasses(cx, global);
}
#define EAGER_ATOM(name) NAME_OFFSET(name)
typedef struct JSStdName {
size_t atomOffset; /* offset of atom pointer in JSAtomState */
JSProtoKey key;
bool isDummy() const { return key == JSProto_Null; }
bool isSentinel() const { return key == JSProto_LIMIT; }
} JSStdName;
static const JSStdName*
LookupStdName(const JSAtomState& names, JSAtom* name, const JSStdName* table)
{
for (unsigned i = 0; !table[i].isSentinel(); i++) {
if (table[i].isDummy())
continue;
JSAtom* atom = AtomStateOffsetToName(names, table[i].atomOffset);
MOZ_ASSERT(atom);
if (name == atom)
return &table[i];