forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsfriendapi.h
3155 lines (2669 loc) · 102 KB
/
jsfriendapi.h
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/. */
#ifndef jsfriendapi_h
#define jsfriendapi_h
#include "mozilla/Atomics.h"
#include "mozilla/Casting.h"
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/UniquePtr.h"
#include "jsapi.h" // For JSAutoByteString. See bug 1033916.
#include "jsbytecode.h"
#include "jspubtd.h"
#include "js/CallArgs.h"
#include "js/CallNonGenericMethod.h"
#include "js/Class.h"
#include "js/Utility.h"
#if JS_STACK_GROWTH_DIRECTION > 0
# define JS_CHECK_STACK_SIZE(limit, sp) (MOZ_LIKELY((uintptr_t)(sp) < (limit)))
#else
# define JS_CHECK_STACK_SIZE(limit, sp) (MOZ_LIKELY((uintptr_t)(sp) > (limit)))
#endif
class JSAtom;
struct JSErrorFormatString;
class JSLinearString;
struct JSJitInfo;
class JSErrorReport;
namespace JS {
template <class T>
class Heap;
} /* namespace JS */
namespace js {
class JS_FRIEND_API(BaseProxyHandler);
class InterpreterFrame;
} /* namespace js */
extern JS_FRIEND_API(void)
JS_SetGrayGCRootsTracer(JSContext* cx, JSTraceDataOp traceOp, void* data);
extern JS_FRIEND_API(JSObject*)
JS_FindCompilationScope(JSContext* cx, JS::HandleObject obj);
extern JS_FRIEND_API(JSFunction*)
JS_GetObjectFunction(JSObject* obj);
extern JS_FRIEND_API(bool)
JS_SplicePrototype(JSContext* cx, JS::HandleObject obj, JS::HandleObject proto);
extern JS_FRIEND_API(JSObject*)
JS_NewObjectWithUniqueType(JSContext* cx, const JSClass* clasp, JS::HandleObject proto);
/**
* Allocate an object in exactly the same way as JS_NewObjectWithGivenProto, but
* without invoking the metadata callback on it. This allows creation of
* internal bookkeeping objects that are guaranteed to not have metadata
* attached to them.
*/
extern JS_FRIEND_API(JSObject*)
JS_NewObjectWithoutMetadata(JSContext* cx, const JSClass* clasp, JS::Handle<JSObject*> proto);
extern JS_FRIEND_API(uint32_t)
JS_ObjectCountDynamicSlots(JS::HandleObject obj);
extern JS_FRIEND_API(size_t)
JS_SetProtoCalled(JSContext* cx);
extern JS_FRIEND_API(bool)
JS_NondeterministicGetWeakMapKeys(JSContext* cx, JS::HandleObject obj, JS::MutableHandleObject ret);
extern JS_FRIEND_API(bool)
JS_NondeterministicGetWeakSetKeys(JSContext* cx, JS::HandleObject obj, JS::MutableHandleObject ret);
// Raw JSScript* because this needs to be callable from a signal handler.
extern JS_FRIEND_API(unsigned)
JS_PCToLineNumber(JSScript* script, jsbytecode* pc, unsigned* columnp = nullptr);
/**
* Determine whether the given object is backed by a DeadObjectProxy.
*
* Such objects hold no other objects (they have no outgoing reference edges)
* and will throw if you touch them (e.g. by reading/writing a property).
*/
extern JS_FRIEND_API(bool)
JS_IsDeadWrapper(JSObject* obj);
/**
* Creates a new dead wrapper object in the given scope. To be used when
* attempting to wrap objects from scopes which are already dead.
*
* If origObject is passed, it must be an proxy object, and will be
* used to determine the characteristics of the new dead wrapper.
*/
extern JS_FRIEND_API(JSObject*)
JS_NewDeadWrapper(JSContext* cx, JSObject* origObject = nullptr);
/**
* Determine whether the given object is a ScriptSourceObject.
*/
extern JS_FRIEND_API(bool)
JS_IsScriptSourceObject(JSObject* obj);
/*
* Used by the cycle collector to trace through a shape or object group and
* all cycle-participating data it reaches, using bounded stack space.
*/
extern JS_FRIEND_API(void)
JS_TraceShapeCycleCollectorChildren(JS::CallbackTracer* trc, JS::GCCellPtr shape);
extern JS_FRIEND_API(void)
JS_TraceObjectGroupCycleCollectorChildren(JS::CallbackTracer* trc, JS::GCCellPtr group);
/*
* Telemetry reasons passed to the accumulate telemetry callback.
*
* It's OK for these enum values to change as they will be mapped to a fixed
* member of the mozilla::Telemetry::HistogramID enum by the callback.
*/
enum {
JS_TELEMETRY_GC_REASON,
JS_TELEMETRY_GC_IS_ZONE_GC,
JS_TELEMETRY_GC_MS,
JS_TELEMETRY_GC_BUDGET_MS,
JS_TELEMETRY_GC_BUDGET_OVERRUN,
JS_TELEMETRY_GC_ANIMATION_MS,
JS_TELEMETRY_GC_MAX_PAUSE_MS,
JS_TELEMETRY_GC_MAX_PAUSE_MS_2,
JS_TELEMETRY_GC_MARK_MS,
JS_TELEMETRY_GC_SWEEP_MS,
JS_TELEMETRY_GC_COMPACT_MS,
JS_TELEMETRY_GC_MARK_ROOTS_MS,
JS_TELEMETRY_GC_MARK_GRAY_MS,
JS_TELEMETRY_GC_SLICE_MS,
JS_TELEMETRY_GC_SLOW_PHASE,
JS_TELEMETRY_GC_SLOW_TASK,
JS_TELEMETRY_GC_MMU_50,
JS_TELEMETRY_GC_RESET,
JS_TELEMETRY_GC_RESET_REASON,
JS_TELEMETRY_GC_INCREMENTAL_DISABLED,
JS_TELEMETRY_GC_NON_INCREMENTAL,
JS_TELEMETRY_GC_NON_INCREMENTAL_REASON,
JS_TELEMETRY_GC_SCC_SWEEP_TOTAL_MS,
JS_TELEMETRY_GC_SCC_SWEEP_MAX_PAUSE_MS,
JS_TELEMETRY_GC_MINOR_REASON,
JS_TELEMETRY_GC_MINOR_REASON_LONG,
JS_TELEMETRY_GC_MINOR_US,
JS_TELEMETRY_GC_NURSERY_BYTES,
JS_TELEMETRY_GC_PRETENURE_COUNT,
JS_TELEMETRY_DEPRECATED_LANGUAGE_EXTENSIONS_IN_CONTENT,
JS_TELEMETRY_DEPRECATED_LANGUAGE_EXTENSIONS_IN_ADDONS,
JS_TELEMETRY_ADDON_EXCEPTIONS,
JS_TELEMETRY_AOT_USAGE,
JS_TELEMETRY_PRIVILEGED_PARSER_COMPILE_LAZY_AFTER_MS,
JS_TELEMETRY_WEB_PARSER_COMPILE_LAZY_AFTER_MS,
JS_TELEMETRY_END
};
typedef void
(*JSAccumulateTelemetryDataCallback)(int id, uint32_t sample, const char* key);
extern JS_FRIEND_API(void)
JS_SetAccumulateTelemetryCallback(JSContext* cx, JSAccumulateTelemetryDataCallback callback);
/*
* Use counter names passed to the accumulate use counter callback.
*
* It's OK to for these enum values to change as they will be mapped to a
* fixed member of the mozilla::UseCounter enum by the callback.
*/
enum class JSUseCounter {
ASMJS,
WASM
};
typedef void
(*JSSetUseCounterCallback)(JSObject* obj, JSUseCounter counter);
extern JS_FRIEND_API(void)
JS_SetSetUseCounterCallback(JSContext* cx, JSSetUseCounterCallback callback);
extern JS_FRIEND_API(bool)
JS_GetIsSecureContext(JSCompartment* compartment);
extern JS_FRIEND_API(JSPrincipals*)
JS_GetCompartmentPrincipals(JSCompartment* compartment);
extern JS_FRIEND_API(void)
JS_SetCompartmentPrincipals(JSCompartment* compartment, JSPrincipals* principals);
extern JS_FRIEND_API(JSPrincipals*)
JS_GetScriptPrincipals(JSScript* script);
extern JS_FRIEND_API(bool)
JS_ScriptHasMutedErrors(JSScript* script);
extern JS_FRIEND_API(JSObject*)
JS_CloneObject(JSContext* cx, JS::HandleObject obj, JS::HandleObject proto);
/**
* Copy the own properties of src to dst in a fast way. src and dst must both
* be native and must be in the compartment of cx. They must have the same
* class, the same parent, and the same prototype. Class reserved slots will
* NOT be copied.
*
* dst must not have any properties on it before this function is called.
*
* src must have been allocated via JS_NewObjectWithoutMetadata so that we can
* be sure it has no metadata that needs copying to dst. This also means that
* dst needs to have the compartment global as its parent. This function will
* preserve the existing metadata on dst, if any.
*/
extern JS_FRIEND_API(bool)
JS_InitializePropertiesFromCompatibleNativeObject(JSContext* cx,
JS::HandleObject dst,
JS::HandleObject src);
extern JS_FRIEND_API(JSString*)
JS_BasicObjectToString(JSContext* cx, JS::HandleObject obj);
namespace js {
JS_FRIEND_API(bool)
GetBuiltinClass(JSContext* cx, JS::HandleObject obj, ESClass* cls);
JS_FRIEND_API(const char*)
ObjectClassName(JSContext* cx, JS::HandleObject obj);
JS_FRIEND_API(void)
ReportOverRecursed(JSContext* maybecx);
JS_FRIEND_API(bool)
AddRawValueRoot(JSContext* cx, JS::Value* vp, const char* name);
JS_FRIEND_API(void)
RemoveRawValueRoot(JSContext* cx, JS::Value* vp);
JS_FRIEND_API(JSAtom*)
GetPropertyNameFromPC(JSScript* script, jsbytecode* pc);
#ifdef JS_DEBUG
/*
* Routines to print out values during debugging. These are FRIEND_API to help
* the debugger find them and to support temporarily hacking js::Dump* calls
* into other code. Note that there are overloads that do not require the FILE*
* parameter, which will default to stderr.
*/
extern JS_FRIEND_API(void)
DumpString(JSString* str, FILE* fp);
extern JS_FRIEND_API(void)
DumpAtom(JSAtom* atom, FILE* fp);
extern JS_FRIEND_API(void)
DumpObject(JSObject* obj, FILE* fp);
extern JS_FRIEND_API(void)
DumpChars(const char16_t* s, size_t n, FILE* fp);
extern JS_FRIEND_API(void)
DumpValue(const JS::Value& val, FILE* fp);
extern JS_FRIEND_API(void)
DumpId(jsid id, FILE* fp);
extern JS_FRIEND_API(void)
DumpInterpreterFrame(JSContext* cx, FILE* fp, InterpreterFrame* start = nullptr);
extern JS_FRIEND_API(bool)
DumpPC(JSContext* cx, FILE* fp);
extern JS_FRIEND_API(bool)
DumpScript(JSContext* cx, JSScript* scriptArg, FILE* fp);
// Versions for use directly in a debugger (default parameters are not handled
// well in gdb; built-in handles like stderr are not handled well in lldb.)
extern JS_FRIEND_API(void) DumpString(JSString* str);
extern JS_FRIEND_API(void) DumpAtom(JSAtom* atom);
extern JS_FRIEND_API(void) DumpObject(JSObject* obj);
extern JS_FRIEND_API(void) DumpChars(const char16_t* s, size_t n);
extern JS_FRIEND_API(void) DumpValue(const JS::Value& val);
extern JS_FRIEND_API(void) DumpId(jsid id);
extern JS_FRIEND_API(void) DumpInterpreterFrame(JSContext* cx, InterpreterFrame* start = nullptr);
extern JS_FRIEND_API(bool) DumpPC(JSContext* cx);
extern JS_FRIEND_API(bool) DumpScript(JSContext* cx, JSScript* scriptArg);
#endif
extern JS_FRIEND_API(void)
DumpBacktrace(JSContext* cx, FILE* fp);
extern JS_FRIEND_API(void)
DumpBacktrace(JSContext* cx);
} // namespace js
namespace JS {
/** Exposed for DumpJSStack */
extern JS_FRIEND_API(JS::UniqueChars)
FormatStackDump(JSContext* cx, JS::UniqueChars&& buf, bool showArgs, bool showLocals,
bool showThisProps);
/**
* Set all of the uninitialized lexicals on an object to undefined. Return
* true if any lexicals were initialized and false otherwise.
* */
extern JS_FRIEND_API(bool)
ForceLexicalInitialization(JSContext *cx, HandleObject obj);
/**
* Whether we are poisoning unused/released data for error detection. Governed
* by the JS_GC_POISONING #ifdef as well as the $JSGC_DISABLE_POISONING
* environment variable.
*/
extern JS_FRIEND_API(int)
IsGCPoisoning();
} // namespace JS
/**
* Copies all own properties from |obj| to |target|. |obj| must be a "native"
* object (that is to say, normal-ish - not an Array or a Proxy).
*
* This function immediately enters a compartment, and does not impose any
* restrictions on the compartment of |cx|.
*/
extern JS_FRIEND_API(bool)
JS_CopyPropertiesFrom(JSContext* cx, JS::HandleObject target, JS::HandleObject obj);
/*
* Single-property version of the above. This function asserts that an |own|
* property of the given name exists on |obj|.
*
* On entry, |cx| must be same-compartment with |obj|.
*
* The copyBehavior argument controls what happens with
* non-configurable properties.
*/
typedef enum {
MakeNonConfigurableIntoConfigurable,
CopyNonConfigurableAsIs
} PropertyCopyBehavior;
extern JS_FRIEND_API(bool)
JS_CopyPropertyFrom(JSContext* cx, JS::HandleId id, JS::HandleObject target,
JS::HandleObject obj,
PropertyCopyBehavior copyBehavior = CopyNonConfigurableAsIs);
extern JS_FRIEND_API(bool)
JS_WrapPropertyDescriptor(JSContext* cx, JS::MutableHandle<JS::PropertyDescriptor> desc);
struct JSFunctionSpecWithHelp {
const char* name;
JSNative call;
uint16_t nargs;
uint16_t flags;
const JSJitInfo* jitInfo;
const char* usage;
const char* help;
};
#define JS_FN_HELP(name,call,nargs,flags,usage,help) \
{name, call, nargs, (flags) | JSPROP_ENUMERATE, nullptr, usage, help}
#define JS_INLINABLE_FN_HELP(name,call,nargs,flags,native,usage,help) \
{name, call, nargs, (flags) | JSPROP_ENUMERATE, &js::jit::JitInfo_##native,\
usage, help}
#define JS_FS_HELP_END \
{nullptr, nullptr, 0, 0, nullptr, nullptr}
extern JS_FRIEND_API(bool)
JS_DefineFunctionsWithHelp(JSContext* cx, JS::HandleObject obj, const JSFunctionSpecWithHelp* fs);
namespace js {
extern JS_FRIEND_API(JSObject*)
proxy_WeakmapKeyDelegate(JSObject* obj);
/**
* A class of objects that return source code on demand.
*
* When code is compiled with setSourceIsLazy(true), SpiderMonkey doesn't
* retain the source code (and doesn't do lazy bytecode generation). If we ever
* need the source code, say, in response to a call to Function.prototype.
* toSource or Debugger.Source.prototype.text, then we call the 'load' member
* function of the instance of this class that has hopefully been registered
* with the runtime, passing the code's URL, and hope that it will be able to
* find the source.
*/
class SourceHook {
public:
virtual ~SourceHook() { }
/**
* Set |*src| and |*length| to refer to the source code for |filename|.
* On success, the caller owns the buffer to which |*src| points, and
* should use JS_free to free it.
*/
virtual bool load(JSContext* cx, const char* filename, char16_t** src, size_t* length) = 0;
};
/**
* Have |cx| use |hook| to retrieve lazily-retrieved source code. See the
* comments for SourceHook. The context takes ownership of the hook, and
* will delete it when the context itself is deleted, or when a new hook is
* set.
*/
extern JS_FRIEND_API(void)
SetSourceHook(JSContext* cx, mozilla::UniquePtr<SourceHook> hook);
/** Remove |cx|'s source hook, and return it. The caller now owns the hook. */
extern JS_FRIEND_API(mozilla::UniquePtr<SourceHook>)
ForgetSourceHook(JSContext* cx);
/**
* Use the runtime's internal handling of job queues for Promise jobs.
*
* Most embeddings, notably web browsers, will have their own task scheduling
* systems and need to integrate handling of Promise jobs into that, so they
* will want to manage job queues themselves. For basic embeddings such as the
* JS shell that don't have an event loop of their own, it's easier to have
* SpiderMonkey handle job queues internally.
*
* Note that the embedding still has to trigger processing of job queues at
* right time(s), such as after evaluation of a script has run to completion.
*/
extern JS_FRIEND_API(bool)
UseInternalJobQueues(JSContext* cx, bool cooperative = false);
/**
* Instruct the runtime to stop draining the internal job queue.
*
* Useful if the embedding is in the process of quitting in reaction to a
* builtin being called, or if it wants to resume executing jobs later on.
*/
extern JS_FRIEND_API(void)
StopDrainingJobQueue(JSContext* cx);
extern JS_FRIEND_API(void)
RunJobs(JSContext* cx);
extern JS_FRIEND_API(JS::Zone*)
GetCompartmentZone(JSCompartment* comp);
typedef bool
(* PreserveWrapperCallback)(JSContext* cx, JSObject* obj);
typedef enum {
CollectNurseryBeforeDump,
IgnoreNurseryObjects
} DumpHeapNurseryBehaviour;
/**
* Dump the complete object graph of heap-allocated things.
* fp is the file for the dump output.
*/
extern JS_FRIEND_API(void)
DumpHeap(JSContext* cx, FILE* fp, DumpHeapNurseryBehaviour nurseryBehaviour);
#ifdef JS_OLD_GETTER_SETTER_METHODS
JS_FRIEND_API(bool) obj_defineGetter(JSContext* cx, unsigned argc, JS::Value* vp);
JS_FRIEND_API(bool) obj_defineSetter(JSContext* cx, unsigned argc, JS::Value* vp);
#endif
extern JS_FRIEND_API(bool)
IsSystemCompartment(JSCompartment* comp);
extern JS_FRIEND_API(bool)
IsSystemZone(JS::Zone* zone);
extern JS_FRIEND_API(bool)
IsAtomsCompartment(JSCompartment* comp);
extern JS_FRIEND_API(bool)
IsAtomsZone(JS::Zone* zone);
struct WeakMapTracer
{
JSRuntime* runtime;
explicit WeakMapTracer(JSRuntime* rt) : runtime(rt) {}
// Weak map tracer callback, called once for every binding of every
// weak map that was live at the time of the last garbage collection.
//
// m will be nullptr if the weak map is not contained in a JS Object.
//
// The callback should not GC (and will assert in a debug build if it does so.)
virtual void trace(JSObject* m, JS::GCCellPtr key, JS::GCCellPtr value) = 0;
};
extern JS_FRIEND_API(void)
TraceWeakMaps(WeakMapTracer* trc);
extern JS_FRIEND_API(bool)
AreGCGrayBitsValid(JSRuntime* rt);
extern JS_FRIEND_API(bool)
ZoneGlobalsAreAllGray(JS::Zone* zone);
extern JS_FRIEND_API(bool)
IsObjectZoneSweepingOrCompacting(JSObject* obj);
typedef void
(*GCThingCallback)(void* closure, JS::GCCellPtr thing);
extern JS_FRIEND_API(void)
VisitGrayWrapperTargets(JS::Zone* zone, GCThingCallback callback, void* closure);
extern JS_FRIEND_API(JSObject*)
GetWeakmapKeyDelegate(JSObject* key);
/**
* Invoke cellCallback on every gray JSObject in the given zone.
*/
extern JS_FRIEND_API(void)
IterateGrayObjects(JS::Zone* zone, GCThingCallback cellCallback, void* data);
/**
* Invoke cellCallback on every gray JSObject in the given zone while cycle
* collection is in progress.
*/
extern JS_FRIEND_API(void)
IterateGrayObjectsUnderCC(JS::Zone* zone, GCThingCallback cellCallback, void* data);
#ifdef DEBUG
// Trace the heap and check there are no black to gray edges. These are
// not allowed since the cycle collector could throw away the gray thing and
// leave a dangling pointer.
//
// This doesn't trace weak maps as these are handled separately.
extern JS_FRIEND_API(bool)
CheckGrayMarkingState(JSRuntime* rt);
#endif
#ifdef JS_HAS_CTYPES
extern JS_FRIEND_API(size_t)
SizeOfDataIfCDataObject(mozilla::MallocSizeOf mallocSizeOf, JSObject* obj);
#endif
extern JS_FRIEND_API(JSCompartment*)
GetAnyCompartmentInZone(JS::Zone* zone);
/*
* Shadow declarations of JS internal structures, for access by inline access
* functions below. Do not use these structures in any other way. When adding
* new fields for access by inline methods, make sure to add static asserts to
* the original header file to ensure that offsets are consistent.
*/
namespace shadow {
struct ObjectGroup {
const Class* clasp;
JSObject* proto;
JSCompartment* compartment;
};
struct BaseShape {
const js::Class* clasp_;
JSObject* parent;
};
class Shape {
public:
shadow::BaseShape* base;
jsid _1;
uint32_t slotInfo;
static const uint32_t FIXED_SLOTS_SHIFT = 27;
};
/**
* This layout is shared by all native objects. For non-native objects, the
* group may always be accessed safely, and other members may be as well,
* depending on the object's specific layout.
*/
struct Object {
shadow::ObjectGroup* group;
shadow::Shape* shape;
JS::Value* slots;
void* _1;
static const size_t MAX_FIXED_SLOTS = 16;
size_t numFixedSlots() const { return shape->slotInfo >> Shape::FIXED_SLOTS_SHIFT; }
JS::Value* fixedSlots() const {
return (JS::Value*)(uintptr_t(this) + sizeof(shadow::Object));
}
JS::Value& slotRef(size_t slot) const {
size_t nfixed = numFixedSlots();
if (slot < nfixed)
return fixedSlots()[slot];
return slots[slot - nfixed];
}
};
struct Function {
Object base;
uint16_t nargs;
uint16_t flags;
/* Used only for natives */
JSNative native;
const JSJitInfo* jitinfo;
void* _1;
};
struct String
{
static const uint32_t INLINE_CHARS_BIT = JS_BIT(2);
static const uint32_t LATIN1_CHARS_BIT = JS_BIT(6);
static const uint32_t ROPE_FLAGS = 0;
static const uint32_t EXTERNAL_FLAGS = JS_BIT(5);
static const uint32_t TYPE_FLAGS_MASK = JS_BIT(6) - 1;
uint32_t flags;
uint32_t length;
union {
const JS::Latin1Char* nonInlineCharsLatin1;
const char16_t* nonInlineCharsTwoByte;
JS::Latin1Char inlineStorageLatin1[1];
char16_t inlineStorageTwoByte[1];
};
const JSStringFinalizer* externalFinalizer;
};
} /* namespace shadow */
// This is equal to |&JSObject::class_|. Use it in places where you don't want
// to #include jsobj.h.
extern JS_FRIEND_DATA(const js::Class* const) ObjectClassPtr;
inline const js::Class*
GetObjectClass(const JSObject* obj)
{
return reinterpret_cast<const shadow::Object*>(obj)->group->clasp;
}
inline const JSClass*
GetObjectJSClass(JSObject* obj)
{
return js::Jsvalify(GetObjectClass(obj));
}
JS_FRIEND_API(const Class*)
ProtoKeyToClass(JSProtoKey key);
// Returns the key for the class inherited by a given standard class (that
// is to say, the prototype of this standard class's prototype).
//
// You must be sure that this corresponds to a standard class with a cached
// JSProtoKey before calling this function. In general |key| will match the
// cached proto key, except in cases where multiple JSProtoKeys share a
// JSClass.
inline JSProtoKey
InheritanceProtoKeyForStandardClass(JSProtoKey key)
{
// [Object] has nothing to inherit from.
if (key == JSProto_Object)
return JSProto_Null;
// If we're ClassSpec defined return the proto key from that
if (ProtoKeyToClass(key)->specDefined())
return ProtoKeyToClass(key)->specInheritanceProtoKey();
// Otherwise, we inherit [Object].
return JSProto_Object;
}
JS_FRIEND_API(bool)
IsFunctionObject(JSObject* obj);
static MOZ_ALWAYS_INLINE JSCompartment*
GetObjectCompartment(JSObject* obj)
{
return reinterpret_cast<shadow::Object*>(obj)->group->compartment;
}
JS_FRIEND_API(JSObject*)
GetGlobalForObjectCrossCompartment(JSObject* obj);
JS_FRIEND_API(JSObject*)
GetPrototypeNoProxy(JSObject* obj);
JS_FRIEND_API(void)
AssertSameCompartment(JSContext* cx, JSObject* obj);
JS_FRIEND_API(void)
AssertSameCompartment(JSContext* cx, JS::HandleValue v);
#ifdef JS_DEBUG
JS_FRIEND_API(void)
AssertSameCompartment(JSObject* objA, JSObject* objB);
#else
inline void AssertSameCompartment(JSObject* objA, JSObject* objB) {}
#endif
JS_FRIEND_API(void)
NotifyAnimationActivity(JSObject* obj);
JS_FRIEND_API(JSFunction*)
DefineFunctionWithReserved(JSContext* cx, JSObject* obj, const char* name, JSNative call,
unsigned nargs, unsigned attrs);
JS_FRIEND_API(JSFunction*)
NewFunctionWithReserved(JSContext* cx, JSNative call, unsigned nargs, unsigned flags,
const char* name);
JS_FRIEND_API(JSFunction*)
NewFunctionByIdWithReserved(JSContext* cx, JSNative native, unsigned nargs, unsigned flags,
jsid id);
JS_FRIEND_API(const JS::Value&)
GetFunctionNativeReserved(JSObject* fun, size_t which);
JS_FRIEND_API(void)
SetFunctionNativeReserved(JSObject* fun, size_t which, const JS::Value& val);
JS_FRIEND_API(bool)
FunctionHasNativeReserved(JSObject* fun);
JS_FRIEND_API(bool)
GetObjectProto(JSContext* cx, JS::HandleObject obj, JS::MutableHandleObject proto);
extern JS_FRIEND_API(JSObject*)
GetStaticPrototype(JSObject* obj);
JS_FRIEND_API(bool)
GetOriginalEval(JSContext* cx, JS::HandleObject scope,
JS::MutableHandleObject eval);
inline void*
GetObjectPrivate(JSObject* obj)
{
MOZ_ASSERT(GetObjectClass(obj)->flags & JSCLASS_HAS_PRIVATE);
const shadow::Object* nobj = reinterpret_cast<const shadow::Object*>(obj);
void** addr = reinterpret_cast<void**>(&nobj->fixedSlots()[nobj->numFixedSlots()]);
return *addr;
}
/**
* Get the value stored in an object's reserved slot. This can be used with
* both native objects and proxies, but if |obj| is known to be a proxy
* GetProxyReservedSlot is a bit more efficient.
*/
inline const JS::Value&
GetReservedSlot(JSObject* obj, size_t slot)
{
MOZ_ASSERT(slot < JSCLASS_RESERVED_SLOTS(GetObjectClass(obj)));
return reinterpret_cast<const shadow::Object*>(obj)->slotRef(slot);
}
JS_FRIEND_API(void)
SetReservedSlotWithBarrier(JSObject* obj, size_t slot, const JS::Value& value);
/**
* Store a value in an object's reserved slot. This can be used with
* both native objects and proxies, but if |obj| is known to be a proxy
* SetProxyReservedSlot is a bit more efficient.
*/
inline void
SetReservedSlot(JSObject* obj, size_t slot, const JS::Value& value)
{
MOZ_ASSERT(slot < JSCLASS_RESERVED_SLOTS(GetObjectClass(obj)));
shadow::Object* sobj = reinterpret_cast<shadow::Object*>(obj);
if (sobj->slotRef(slot).isGCThing() || value.isGCThing())
SetReservedSlotWithBarrier(obj, slot, value);
else
sobj->slotRef(slot) = value;
}
JS_FRIEND_API(uint32_t)
GetObjectSlotSpan(JSObject* obj);
inline const JS::Value&
GetObjectSlot(JSObject* obj, size_t slot)
{
MOZ_ASSERT(slot < GetObjectSlotSpan(obj));
return reinterpret_cast<const shadow::Object*>(obj)->slotRef(slot);
}
MOZ_ALWAYS_INLINE size_t
GetAtomLength(JSAtom* atom)
{
return reinterpret_cast<shadow::String*>(atom)->length;
}
static const uint32_t MaxStringLength = (1 << 28) - 1;
MOZ_ALWAYS_INLINE size_t
GetStringLength(JSString* s)
{
return reinterpret_cast<shadow::String*>(s)->length;
}
MOZ_ALWAYS_INLINE size_t
GetFlatStringLength(JSFlatString* s)
{
return reinterpret_cast<shadow::String*>(s)->length;
}
MOZ_ALWAYS_INLINE size_t
GetLinearStringLength(JSLinearString* s)
{
return reinterpret_cast<shadow::String*>(s)->length;
}
MOZ_ALWAYS_INLINE bool
LinearStringHasLatin1Chars(JSLinearString* s)
{
return reinterpret_cast<shadow::String*>(s)->flags & shadow::String::LATIN1_CHARS_BIT;
}
MOZ_ALWAYS_INLINE bool
AtomHasLatin1Chars(JSAtom* atom)
{
return reinterpret_cast<shadow::String*>(atom)->flags & shadow::String::LATIN1_CHARS_BIT;
}
MOZ_ALWAYS_INLINE bool
StringHasLatin1Chars(JSString* s)
{
return reinterpret_cast<shadow::String*>(s)->flags & shadow::String::LATIN1_CHARS_BIT;
}
MOZ_ALWAYS_INLINE const JS::Latin1Char*
GetLatin1LinearStringChars(const JS::AutoRequireNoGC& nogc, JSLinearString* linear)
{
MOZ_ASSERT(LinearStringHasLatin1Chars(linear));
using shadow::String;
String* s = reinterpret_cast<String*>(linear);
if (s->flags & String::INLINE_CHARS_BIT)
return s->inlineStorageLatin1;
return s->nonInlineCharsLatin1;
}
MOZ_ALWAYS_INLINE const char16_t*
GetTwoByteLinearStringChars(const JS::AutoRequireNoGC& nogc, JSLinearString* linear)
{
MOZ_ASSERT(!LinearStringHasLatin1Chars(linear));
using shadow::String;
String* s = reinterpret_cast<String*>(linear);
if (s->flags & String::INLINE_CHARS_BIT)
return s->inlineStorageTwoByte;
return s->nonInlineCharsTwoByte;
}
MOZ_ALWAYS_INLINE JSLinearString*
AtomToLinearString(JSAtom* atom)
{
return reinterpret_cast<JSLinearString*>(atom);
}
MOZ_ALWAYS_INLINE JSFlatString*
AtomToFlatString(JSAtom* atom)
{
return reinterpret_cast<JSFlatString*>(atom);
}
MOZ_ALWAYS_INLINE JSLinearString*
FlatStringToLinearString(JSFlatString* s)
{
return reinterpret_cast<JSLinearString*>(s);
}
MOZ_ALWAYS_INLINE const JS::Latin1Char*
GetLatin1AtomChars(const JS::AutoRequireNoGC& nogc, JSAtom* atom)
{
return GetLatin1LinearStringChars(nogc, AtomToLinearString(atom));
}
MOZ_ALWAYS_INLINE const char16_t*
GetTwoByteAtomChars(const JS::AutoRequireNoGC& nogc, JSAtom* atom)
{
return GetTwoByteLinearStringChars(nogc, AtomToLinearString(atom));
}
MOZ_ALWAYS_INLINE bool
IsExternalString(JSString* str, const JSStringFinalizer** fin, const char16_t** chars)
{
using shadow::String;
String* s = reinterpret_cast<String*>(str);
if ((s->flags & String::TYPE_FLAGS_MASK) != String::EXTERNAL_FLAGS)
return false;
MOZ_ASSERT(JS_IsExternalString(str));
*fin = s->externalFinalizer;
*chars = s->nonInlineCharsTwoByte;
return true;
}
JS_FRIEND_API(JSLinearString*)
StringToLinearStringSlow(JSContext* cx, JSString* str);
MOZ_ALWAYS_INLINE JSLinearString*
StringToLinearString(JSContext* cx, JSString* str)
{
using shadow::String;
String* s = reinterpret_cast<String*>(str);
if (MOZ_UNLIKELY((s->flags & String::TYPE_FLAGS_MASK) == String::ROPE_FLAGS))
return StringToLinearStringSlow(cx, str);
return reinterpret_cast<JSLinearString*>(str);
}
template<typename CharType>
MOZ_ALWAYS_INLINE void
CopyLinearStringChars(CharType* dest, JSLinearString* s, size_t len, size_t start = 0);
MOZ_ALWAYS_INLINE void
CopyLinearStringChars(char16_t* dest, JSLinearString* s, size_t len, size_t start = 0)
{
MOZ_ASSERT(start + len <= GetLinearStringLength(s));
JS::AutoCheckCannotGC nogc;
if (LinearStringHasLatin1Chars(s)) {
const JS::Latin1Char* src = GetLatin1LinearStringChars(nogc, s);
for (size_t i = 0; i < len; i++)
dest[i] = src[start + i];
} else {
const char16_t* src = GetTwoByteLinearStringChars(nogc, s);
mozilla::PodCopy(dest, src + start, len);
}
}
MOZ_ALWAYS_INLINE void
CopyLinearStringChars(char* dest, JSLinearString* s, size_t len, size_t start = 0)
{
MOZ_ASSERT(start + len <= GetLinearStringLength(s));
JS::AutoCheckCannotGC nogc;
if (LinearStringHasLatin1Chars(s)) {
const JS::Latin1Char* src = GetLatin1LinearStringChars(nogc, s);
for (size_t i = 0; i < len; i++)
dest[i] = char(src[start + i]);
} else {
const char16_t* src = GetTwoByteLinearStringChars(nogc, s);
for (size_t i = 0; i < len; i++)
dest[i] = char(src[start + i]);
}
}
template<typename CharType>
inline bool
CopyStringChars(JSContext* cx, CharType* dest, JSString* s, size_t len, size_t start = 0)
{
JSLinearString* linear = StringToLinearString(cx, s);
if (!linear)
return false;
CopyLinearStringChars(dest, linear, len, start);
return true;
}
inline void
CopyFlatStringChars(char16_t* dest, JSFlatString* s, size_t len)
{
CopyLinearStringChars(dest, FlatStringToLinearString(s), len);
}
/**
* Add some or all property keys of obj to the id vector *props.
*
* The flags parameter controls which property keys are added. Pass a
* combination of the following bits:
*
* JSITER_OWNONLY - Don't also search the prototype chain; only consider
* obj's own properties.
*
* JSITER_HIDDEN - Include nonenumerable properties.
*
* JSITER_SYMBOLS - Include property keys that are symbols. The default
* behavior is to filter out symbols.
*
* JSITER_SYMBOLSONLY - Exclude non-symbol property keys.
*
* This is the closest C++ API we have to `Reflect.ownKeys(obj)`, or
* equivalently, the ES6 [[OwnPropertyKeys]] internal method. Pass
* `JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS` as flags to get
* results that match the output of Reflect.ownKeys.
*/
JS_FRIEND_API(bool)
GetPropertyKeys(JSContext* cx, JS::HandleObject obj, unsigned flags, JS::AutoIdVector* props);
JS_FRIEND_API(bool)
AppendUnique(JSContext* cx, JS::AutoIdVector& base, JS::AutoIdVector& others);
JS_FRIEND_API(bool)
StringIsArrayIndex(JSLinearString* str, uint32_t* indexp);
JS_FRIEND_API(void)