forked from 9miao/CrossApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsapi.h
More file actions
executable file
·5107 lines (4193 loc) · 167 KB
/
Copy pathjsapi.h
File metadata and controls
executable file
·5107 lines (4193 loc) · 167 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 sw=4 et tw=78:
*
* 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. */
#ifndef jsapi_h___
#define jsapi_h___
#include "mozilla/Attributes.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/GuardObjects.h"
#include "mozilla/RangedPtr.h"
#include "mozilla/StandardInteger.h"
#include "mozilla/ThreadLocal.h"
#include <stddef.h>
#include <stdio.h>
#include "js-config.h"
#include "jsalloc.h"
#include "jspubtd.h"
#include "jsutil.h"
#include "gc/Root.h"
#include "js/Anchor.h"
#include "js/CharacterEncoding.h"
#include "js/HashTable.h"
#include "js/PropertyKey.h"
#include "js/Utility.h"
#include "js/Value.h"
#include "js/Vector.h"
/************************************************************************/
namespace JS {
typedef mozilla::RangedPtr<const jschar> CharPtr;
class StableCharPtr : public CharPtr {
public:
StableCharPtr(const StableCharPtr &s) : CharPtr(s) {}
StableCharPtr(const mozilla::RangedPtr<const jschar> &s) : CharPtr(s) {}
StableCharPtr(const jschar *s, size_t len) : CharPtr(s, len) {}
StableCharPtr(const jschar *pos, const jschar *start, size_t len)
: CharPtr(pos, start, len)
{}
};
#if defined JS_THREADSAFE && defined DEBUG
class JS_PUBLIC_API(AutoCheckRequestDepth)
{
JSContext *cx;
public:
AutoCheckRequestDepth(JSContext *cx);
~AutoCheckRequestDepth();
};
# define CHECK_REQUEST(cx) \
JS::AutoCheckRequestDepth _autoCheckRequestDepth(cx)
#else
# define CHECK_REQUEST(cx) \
((void) 0)
#endif /* JS_THREADSAFE && DEBUG */
#ifdef DEBUG
/*
* Assert that we're not doing GC on cx, that we're in a request as
* needed, and that the compartments for cx and v are correct.
* Also check that GC would be safe at this point.
*/
JS_PUBLIC_API(void)
AssertArgumentsAreSane(JSContext *cx, const Value &v);
#else
inline void AssertArgumentsAreSane(JSContext *cx, const Value &v) {
/* Do nothing */
}
#endif /* DEBUG */
class JS_PUBLIC_API(AutoGCRooter) {
public:
AutoGCRooter(JSContext *cx, ptrdiff_t tag);
~AutoGCRooter() {
JS_ASSERT(this == *stackTop);
*stackTop = down;
}
/* Implemented in gc/RootMarking.cpp. */
inline void trace(JSTracer *trc);
static void traceAll(JSTracer *trc);
static void traceAllWrappers(JSTracer *trc);
protected:
AutoGCRooter * const down;
/*
* Discriminates actual subclass of this being used. If non-negative, the
* subclass roots an array of values of the length stored in this field.
* If negative, meaning is indicated by the corresponding value in the enum
* below. Any other negative value indicates some deeper problem such as
* memory corruption.
*/
ptrdiff_t tag_;
enum {
JSVAL = -1, /* js::AutoValueRooter */
VALARRAY = -2, /* js::AutoValueArray */
PARSER = -3, /* js::frontend::Parser */
SHAPEVECTOR = -4, /* js::AutoShapeVector */
IDARRAY = -6, /* js::AutoIdArray */
DESCRIPTORS = -7, /* js::AutoPropDescArrayRooter */
// UNUSED -8
// UNUSED -9
OBJECT = -10, /* js::AutoObjectRooter */
ID = -11, /* js::AutoIdRooter */
VALVECTOR = -12, /* js::AutoValueVector */
DESCRIPTOR = -13, /* js::AutoPropertyDescriptorRooter */
STRING = -14, /* js::AutoStringRooter */
IDVECTOR = -15, /* js::AutoIdVector */
OBJVECTOR = -16, /* js::AutoObjectVector */
STRINGVECTOR =-17, /* js::AutoStringVector */
SCRIPTVECTOR =-18, /* js::AutoScriptVector */
PROPDESC = -19, /* js::PropDesc::AutoRooter */
SHAPERANGE = -20, /* js::Shape::Range::AutoRooter */
STACKSHAPE = -21, /* js::StackShape::AutoRooter */
STACKBASESHAPE=-22,/* js::StackBaseShape::AutoRooter */
GETTERSETTER =-24, /* js::AutoRooterGetterSetter */
REGEXPSTATICS=-25, /* js::RegExpStatics::AutoRooter */
NAMEVECTOR = -26, /* js::AutoNameVector */
HASHABLEVALUE=-27,
IONMASM = -28, /* js::ion::MacroAssembler */
IONALLOC = -29, /* js::ion::AutoTempAllocatorRooter */
WRAPVECTOR = -30, /* js::AutoWrapperVector */
WRAPPER = -31, /* js::AutoWrapperRooter */
OBJOBJHASHMAP=-32, /* js::AutoObjectObjectHashMap */
OBJU32HASHMAP=-33, /* js::AutoObjectUnsigned32HashMap */
OBJHASHSET = -34 /* js::AutoObjectHashSet */
};
private:
AutoGCRooter ** const stackTop;
/* No copy or assignment semantics. */
AutoGCRooter(AutoGCRooter &ida) MOZ_DELETE;
void operator=(AutoGCRooter &ida) MOZ_DELETE;
};
class AutoValueRooter : private AutoGCRooter
{
public:
explicit AutoValueRooter(JSContext *cx
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, JSVAL), val(NullValue())
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
AutoValueRooter(JSContext *cx, const Value &v
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, JSVAL), val(v)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
/*
* If you are looking for Object* overloads, use AutoObjectRooter instead;
* rooting Object*s as a js::Value requires discerning whether or not it is
* a function object. Also, AutoObjectRooter is smaller.
*/
void set(Value v) {
JS_ASSERT(tag_ == JSVAL);
val = v;
}
const Value &value() const {
JS_ASSERT(tag_ == JSVAL);
return val;
}
Value *addr() {
JS_ASSERT(tag_ == JSVAL);
return &val;
}
const Value &jsval_value() const {
JS_ASSERT(tag_ == JSVAL);
return val;
}
Value *jsval_addr() {
JS_ASSERT(tag_ == JSVAL);
return &val;
}
friend void AutoGCRooter::trace(JSTracer *trc);
private:
Value val;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoObjectRooter : private AutoGCRooter
{
public:
AutoObjectRooter(JSContext *cx, JSObject *obj = NULL
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, OBJECT), obj_(obj)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
void setObject(JSObject *obj) {
obj_ = obj;
}
JSObject * object() const {
return obj_;
}
JSObject ** addr() {
return &obj_;
}
friend void AutoGCRooter::trace(JSTracer *trc);
private:
JSObject *obj_;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoStringRooter : private AutoGCRooter {
public:
AutoStringRooter(JSContext *cx, JSString *str = NULL
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, STRING), str_(str)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
void setString(JSString *str) {
str_ = str;
}
JSString * string() const {
return str_;
}
JSString ** addr() {
return &str_;
}
JSString * const * addr() const {
return &str_;
}
friend void AutoGCRooter::trace(JSTracer *trc);
private:
JSString *str_;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoArrayRooter : private AutoGCRooter {
public:
AutoArrayRooter(JSContext *cx, size_t len, Value *vec
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, len), array(vec), skip(cx, array, len)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
JS_ASSERT(tag_ >= 0);
}
void changeLength(size_t newLength) {
tag_ = ptrdiff_t(newLength);
JS_ASSERT(tag_ >= 0);
}
void changeArray(Value *newArray, size_t newLength) {
changeLength(newLength);
array = newArray;
}
Value *array;
MutableHandleValue handleAt(size_t i)
{
JS_ASSERT(i < size_t(tag_));
return MutableHandleValue::fromMarkedLocation(&array[i]);
}
HandleValue handleAt(size_t i) const
{
JS_ASSERT(i < size_t(tag_));
return HandleValue::fromMarkedLocation(&array[i]);
}
friend void AutoGCRooter::trace(JSTracer *trc);
private:
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
js::SkipRoot skip;
};
template<class T>
class AutoVectorRooter : protected AutoGCRooter
{
public:
explicit AutoVectorRooter(JSContext *cx, ptrdiff_t tag
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, tag), vector(cx), vectorRoot(cx, &vector)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
typedef T ElementType;
size_t length() const { return vector.length(); }
bool empty() const { return vector.empty(); }
bool append(const T &v) { return vector.append(v); }
bool append(const AutoVectorRooter<T> &other) {
return vector.append(other.vector);
}
/* For use when space has already been reserved. */
void infallibleAppend(const T &v) { vector.infallibleAppend(v); }
void popBack() { vector.popBack(); }
T popCopy() { return vector.popCopy(); }
bool growBy(size_t inc) {
size_t oldLength = vector.length();
if (!vector.growByUninitialized(inc))
return false;
makeRangeGCSafe(oldLength);
return true;
}
bool resize(size_t newLength) {
size_t oldLength = vector.length();
if (newLength <= oldLength) {
vector.shrinkBy(oldLength - newLength);
return true;
}
if (!vector.growByUninitialized(newLength - oldLength))
return false;
makeRangeGCSafe(oldLength);
return true;
}
void clear() { vector.clear(); }
bool reserve(size_t newLength) {
return vector.reserve(newLength);
}
T &operator[](size_t i) { return vector[i]; }
const T &operator[](size_t i) const { return vector[i]; }
JS::MutableHandle<T> handleAt(size_t i) { return JS::MutableHandle<T>::fromMarkedLocation(&vector[i]); }
JS::Handle<T> handleAt(size_t i) const { return JS::Handle<T>::fromMarkedLocation(&vector[i]); }
const T *begin() const { return vector.begin(); }
T *begin() { return vector.begin(); }
const T *end() const { return vector.end(); }
T *end() { return vector.end(); }
const T &back() const { return vector.back(); }
friend void AutoGCRooter::trace(JSTracer *trc);
private:
void makeRangeGCSafe(size_t oldLength) {
T *t = vector.begin() + oldLength;
for (size_t i = oldLength; i < vector.length(); ++i, ++t)
memset(t, 0, sizeof(T));
}
typedef js::Vector<T, 8> VectorImpl;
VectorImpl vector;
/* Prevent overwriting of inline elements in vector. */
js::SkipRoot vectorRoot;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
template<class Key, class Value>
class AutoHashMapRooter : protected AutoGCRooter
{
private:
typedef js::HashMap<Key, Value> HashMapImpl;
public:
explicit AutoHashMapRooter(JSContext *cx, ptrdiff_t tag
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, tag), map(cx)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
typedef Key KeyType;
typedef Value ValueType;
typedef typename HashMapImpl::Lookup Lookup;
typedef typename HashMapImpl::Ptr Ptr;
typedef typename HashMapImpl::AddPtr AddPtr;
bool init(uint32_t len = 16) {
return map.init(len);
}
bool initialized() const {
return map.initialized();
}
Ptr lookup(const Lookup &l) const {
return map.lookup(l);
}
void remove(Ptr p) {
map.remove(p);
}
AddPtr lookupForAdd(const Lookup &l) const {
return map.lookupForAdd(l);
}
template<typename KeyInput, typename ValueInput>
bool add(AddPtr &p, const KeyInput &k, const ValueInput &v) {
return map.add(p, k, v);
}
bool add(AddPtr &p, const Key &k) {
return map.add(p, k);
}
template<typename KeyInput, typename ValueInput>
bool relookupOrAdd(AddPtr &p, const KeyInput &k, const ValueInput &v) {
return map.relookupOrAdd(p, k, v);
}
typedef typename HashMapImpl::Range Range;
Range all() const {
return map.all();
}
typedef typename HashMapImpl::Enum Enum;
void clear() {
map.clear();
}
void finish() {
map.finish();
}
bool empty() const {
return map.empty();
}
uint32_t count() const {
return map.count();
}
size_t capacity() const {
return map.capacity();
}
size_t sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf) const {
return map.sizeOfExcludingThis(mallocSizeOf);
}
size_t sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf) const {
return map.sizeOfIncludingThis(mallocSizeOf);
}
unsigned generation() const {
return map.generation();
}
/************************************************** Shorthand operations */
bool has(const Lookup &l) const {
return map.has(l);
}
template<typename KeyInput, typename ValueInput>
bool put(const KeyInput &k, const ValueInput &v) {
return map.put(k, v);
}
template<typename KeyInput, typename ValueInput>
bool putNew(const KeyInput &k, const ValueInput &v) {
return map.putNew(k, v);
}
Ptr lookupWithDefault(const Key &k, const Value &defaultValue) {
return map.lookupWithDefault(k, defaultValue);
}
void remove(const Lookup &l) {
map.remove(l);
}
friend void AutoGCRooter::trace(JSTracer *trc);
private:
AutoHashMapRooter(const AutoHashMapRooter &hmr) MOZ_DELETE;
AutoHashMapRooter &operator=(const AutoHashMapRooter &hmr) MOZ_DELETE;
HashMapImpl map;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
template<class T>
class AutoHashSetRooter : protected AutoGCRooter
{
private:
typedef js::HashSet<T> HashSetImpl;
public:
explicit AutoHashSetRooter(JSContext *cx, ptrdiff_t tag
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, tag), set(cx)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
typedef typename HashSetImpl::Lookup Lookup;
typedef typename HashSetImpl::Ptr Ptr;
typedef typename HashSetImpl::AddPtr AddPtr;
bool init(uint32_t len = 16) {
return set.init(len);
}
bool initialized() const {
return set.initialized();
}
Ptr lookup(const Lookup &l) const {
return set.lookup(l);
}
void remove(Ptr p) {
set.remove(p);
}
AddPtr lookupForAdd(const Lookup &l) const {
return set.lookupForAdd(l);
}
bool add(AddPtr &p, const T &t) {
return set.add(p, t);
}
bool relookupOrAdd(AddPtr &p, const Lookup &l, const T &t) {
return set.relookupOrAdd(p, l, t);
}
typedef typename HashSetImpl::Range Range;
Range all() const {
return set.all();
}
typedef typename HashSetImpl::Enum Enum;
void clear() {
set.clear();
}
void finish() {
set.finish();
}
bool empty() const {
return set.empty();
}
uint32_t count() const {
return set.count();
}
size_t capacity() const {
return set.capacity();
}
size_t sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf) const {
return set.sizeOfExcludingThis(mallocSizeOf);
}
size_t sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf) const {
return set.sizeOfIncludingThis(mallocSizeOf);
}
unsigned generation() const {
return set.generation();
}
/************************************************** Shorthand operations */
bool has(const Lookup &l) const {
return set.has(l);
}
bool put(const T &t) {
return set.put(t);
}
bool putNew(const T &t) {
return set.putNew(t);
}
void remove(const Lookup &l) {
set.remove(l);
}
friend void AutoGCRooter::trace(JSTracer *trc);
private:
AutoHashSetRooter(const AutoHashSetRooter &hmr) MOZ_DELETE;
AutoHashSetRooter &operator=(const AutoHashSetRooter &hmr) MOZ_DELETE;
HashSetImpl set;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoValueVector : public AutoVectorRooter<Value>
{
public:
explicit AutoValueVector(JSContext *cx
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoVectorRooter<Value>(cx, VALVECTOR)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoIdVector : public AutoVectorRooter<jsid>
{
public:
explicit AutoIdVector(JSContext *cx
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoVectorRooter<jsid>(cx, IDVECTOR)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoScriptVector : public AutoVectorRooter<JSScript *>
{
public:
explicit AutoScriptVector(JSContext *cx
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoVectorRooter<JSScript *>(cx, SCRIPTVECTOR)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class CallReceiver
{
protected:
#ifdef DEBUG
mutable bool usedRval_;
void setUsedRval() const { usedRval_ = true; }
void clearUsedRval() const { usedRval_ = false; }
#else
void setUsedRval() const {}
void clearUsedRval() const {}
#endif
Value *argv_;
public:
friend CallReceiver CallReceiverFromVp(Value *);
friend CallReceiver CallReceiverFromArgv(Value *);
Value *base() const { return argv_ - 2; }
JSObject &callee() const { JS_ASSERT(!usedRval_); return argv_[-2].toObject(); }
JS::HandleValue calleev() const {
JS_ASSERT(!usedRval_);
return JS::HandleValue::fromMarkedLocation(&argv_[-2]);
}
JS::HandleValue thisv() const {
return JS::HandleValue::fromMarkedLocation(&argv_[-1]);
}
JS::MutableHandleValue mutableThisv() const {
return JS::MutableHandleValue::fromMarkedLocation(&argv_[-1]);
}
JS::MutableHandleValue rval() const {
setUsedRval();
return JS::MutableHandleValue::fromMarkedLocation(&argv_[-2]);
}
Value *spAfterCall() const {
setUsedRval();
return argv_ - 1;
}
void setCallee(Value aCalleev) const {
clearUsedRval();
argv_[-2] = aCalleev;
}
void setThis(Value aThisv) const {
argv_[-1] = aThisv;
}
};
JS_ALWAYS_INLINE CallReceiver
CallReceiverFromArgv(Value *argv)
{
CallReceiver receiver;
receiver.clearUsedRval();
receiver.argv_ = argv;
return receiver;
}
JS_ALWAYS_INLINE CallReceiver
CallReceiverFromVp(Value *vp)
{
return CallReceiverFromArgv(vp + 2);
}
/*****************************************************************************/
class CallArgs : public CallReceiver
{
protected:
unsigned argc_;
public:
friend CallArgs CallArgsFromVp(unsigned, Value *);
friend CallArgs CallArgsFromArgv(unsigned, Value *);
friend CallArgs CallArgsFromSp(unsigned, Value *);
Value &operator[](unsigned i) const { JS_ASSERT(i < argc_); return argv_[i]; }
MutableHandleValue handleAt(unsigned i)
{
JS_ASSERT(i < argc_);
return MutableHandleValue::fromMarkedLocation(&argv_[i]);
}
HandleValue handleAt(unsigned i) const
{
JS_ASSERT(i < argc_);
return HandleValue::fromMarkedLocation(&argv_[i]);
}
Value get(unsigned i) const
{
return i < length() ? argv_[i] : UndefinedValue();
}
Value *array() const { return argv_; }
unsigned length() const { return argc_; }
Value *end() const { return argv_ + argc_; }
bool hasDefined(unsigned i) const { return i < argc_ && !argv_[i].isUndefined(); }
};
JS_ALWAYS_INLINE CallArgs
CallArgsFromArgv(unsigned argc, Value *argv)
{
CallArgs args;
args.clearUsedRval();
args.argv_ = argv;
args.argc_ = argc;
return args;
}
JS_ALWAYS_INLINE CallArgs
CallArgsFromVp(unsigned argc, Value *vp)
{
return CallArgsFromArgv(argc, vp + 2);
}
JS_ALWAYS_INLINE CallArgs
CallArgsFromSp(unsigned argc, Value *sp)
{
return CallArgsFromArgv(argc, sp - argc);
}
/* Returns true if |v| is considered an acceptable this-value. */
typedef bool (*IsAcceptableThis)(const Value &v);
/*
* Implements the guts of a method; guaranteed to be provided an acceptable
* this-value, as determined by a corresponding IsAcceptableThis method.
*/
typedef bool (*NativeImpl)(JSContext *cx, CallArgs args);
namespace detail {
/* DON'T CALL THIS DIRECTLY. It's for use only by CallNonGenericMethod! */
extern JS_PUBLIC_API(bool)
CallMethodIfWrapped(JSContext *cx, IsAcceptableThis test, NativeImpl impl, CallArgs args);
} /* namespace detail */
/*
* Methods usually act upon |this| objects only from a single global object and
* compartment. Sometimes, however, a method must act upon |this| values from
* multiple global objects or compartments. In such cases the |this| value a
* method might see will be wrapped, such that various access to the object --
* to its class, its private data, its reserved slots, and so on -- will not
* work properly without entering that object's compartment. This method
* implements a solution to this problem.
*
* To implement a method that accepts |this| values from multiple compartments,
* define two functions. The first function matches the IsAcceptableThis type
* and indicates whether the provided value is an acceptable |this| for the
* method; it must be a pure function only of its argument.
*
* static JSClass AnswerClass = { ... };
*
* static bool
* IsAnswerObject(const Value &v)
* {
* if (!v.isObject())
* return false;
* return JS_GetClass(&v.toObject()) == &AnswerClass;
* }
*
* The second function implements the NativeImpl signature and defines the
* behavior of the method when it is provided an acceptable |this| value.
* Aside from some typing niceties -- see the CallArgs interface for details --
* its interface is the same as that of JSNative.
*
* static bool
* answer_getAnswer_impl(JSContext *cx, JS::CallArgs args)
* {
* args.rval().setInt32(42);
* return true;
* }
*
* The implementation function is guaranteed to be called *only* with a |this|
* value which is considered acceptable.
*
* Now to implement the actual method, write a JSNative that calls the method
* declared below, passing the appropriate template and runtime arguments.
*
* static JSBool
* answer_getAnswer(JSContext *cx, unsigned argc, JS::Value *vp)
* {
* JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
* return JS::CallNonGenericMethod<IsAnswerObject, answer_getAnswer_impl>(cx, args);
* }
*
* Note that, because they are used as template arguments, the predicate
* and implementation functions must have external linkage. (This is
* unfortunate, but GCC wasn't inlining things as one would hope when we
* passed them as function arguments.)
*
* JS::CallNonGenericMethod will test whether |args.thisv()| is acceptable. If
* it is, it will call the provided implementation function, which will return
* a value and indicate success. If it is not, it will attempt to unwrap
* |this| and call the implementation function on the unwrapped |this|. If
* that succeeds, all well and good. If it doesn't succeed, a TypeError will
* be thrown.
*
* Note: JS::CallNonGenericMethod will only work correctly if it's called in
* tail position in a JSNative. Do not call it from any other place.
*/
template<IsAcceptableThis Test, NativeImpl Impl>
JS_ALWAYS_INLINE bool
CallNonGenericMethod(JSContext *cx, CallArgs args)
{
const Value &thisv = args.thisv();
if (Test(thisv))
return Impl(cx, args);
return detail::CallMethodIfWrapped(cx, Test, Impl, args);
}
JS_ALWAYS_INLINE bool
CallNonGenericMethod(JSContext *cx, IsAcceptableThis Test, NativeImpl Impl, CallArgs args)
{
const Value &thisv = args.thisv();
if (Test(thisv))
return Impl(cx, args);
return detail::CallMethodIfWrapped(cx, Test, Impl, args);
}
} /* namespace JS */
/************************************************************************/
typedef JS::Handle<JSObject*> JSHandleObject;
typedef JS::Handle<JSString*> JSHandleString;
typedef JS::Handle<JS::Value> JSHandleValue;
typedef JS::Handle<jsid> JSHandleId;
typedef JS::MutableHandle<JSObject*> JSMutableHandleObject;
typedef JS::MutableHandle<JSFunction*> JSMutableHandleFunction;
typedef JS::MutableHandle<JSScript*> JSMutableHandleScript;
typedef JS::MutableHandle<JSString*> JSMutableHandleString;
typedef JS::MutableHandle<JS::Value> JSMutableHandleValue;
typedef JS::MutableHandle<jsid> JSMutableHandleId;
typedef js::RawObject JSRawObject;
typedef js::RawFunction JSRawFunction;
typedef js::RawScript JSRawScript;
typedef js::RawString JSRawString;
typedef js::RawId JSRawId;
typedef js::RawValue JSRawValue;
/* JSClass operation signatures. */
/*
* Add, delete, or get a property named by id in obj. Note the jsid id
* type -- id may be a string (Unicode property identifier) or an int (element
* index). The *vp out parameter, on success, is the new property value after
* an add or get. After a successful delete, *vp is JSVAL_FALSE iff
* obj[id] can't be deleted (because it's permanent).
*/
typedef JSBool
(* JSPropertyOp)(JSContext *cx, JSHandleObject obj, JSHandleId id, JSMutableHandleValue vp);
/*
* Set a property named by id in obj, treating the assignment as strict
* mode code if strict is true. Note the jsid id type -- id may be a string
* (Unicode property identifier) or an int (element index). The *vp out
* parameter, on success, is the new property value after the
* set.
*/
typedef JSBool
(* JSStrictPropertyOp)(JSContext *cx, JSHandleObject obj, JSHandleId id, JSBool strict, JSMutableHandleValue vp);
/*
* This function type is used for callbacks that enumerate the properties of
* a JSObject. The behavior depends on the value of enum_op:
*
* JSENUMERATE_INIT
* A new, opaque iterator state should be allocated and stored in *statep.
* (You can use PRIVATE_TO_JSVAL() to tag the pointer to be stored).
*
* The number of properties that will be enumerated should be returned as
* an integer jsval in *idp, if idp is non-null, and provided the number of
* enumerable properties is known. If idp is non-null and the number of
* enumerable properties can't be computed in advance, *idp should be set
* to JSVAL_ZERO.
*
* JSENUMERATE_INIT_ALL
* Used identically to JSENUMERATE_INIT, but exposes all properties of the
* object regardless of enumerability.
*
* JSENUMERATE_NEXT
* A previously allocated opaque iterator state is passed in via statep.
* Return the next jsid in the iteration using *idp. The opaque iterator
* state pointed at by statep is destroyed and *statep is set to JSVAL_NULL
* if there are no properties left to enumerate.
*
* JSENUMERATE_DESTROY
* Destroy the opaque iterator state previously allocated in *statep by a
* call to this function when enum_op was JSENUMERATE_INIT or
* JSENUMERATE_INIT_ALL.
*
* The return value is used to indicate success, with a value of JS_FALSE
* indicating failure.
*/
typedef JSBool
(* JSNewEnumerateOp)(JSContext *cx, JSHandleObject obj, JSIterateOp enum_op,
JSMutableHandleValue statep, JSMutableHandleId idp);
/*
* The old-style JSClass.enumerate op should define all lazy properties not
* yet reflected in obj.
*/
typedef JSBool
(* JSEnumerateOp)(JSContext *cx, JSHandleObject obj);
/*
* Resolve a lazy property named by id in obj by defining it directly in obj.
* Lazy properties are those reflected from some peer native property space
* (e.g., the DOM attributes for a given node reflected as obj) on demand.
*
* JS looks for a property in an object, and if not found, tries to resolve
* the given id. If resolve succeeds, the engine looks again in case resolve
* defined obj[id]. If no such property exists directly in obj, the process
* is repeated with obj's prototype, etc.
*
* NB: JSNewResolveOp provides a cheaper way to resolve lazy properties.
*/
typedef JSBool
(* JSResolveOp)(JSContext *cx, JSHandleObject obj, JSHandleId id);
/*
* Like JSResolveOp, but flags provide contextual information as follows:
*
* JSRESOLVE_ASSIGNING obj[id] is on the left-hand side of an assignment
*
* The *objp out parameter, on success, should be null to indicate that id
* was not resolved; and non-null, referring to obj or one of its prototypes,
* if id was resolved. The hook may assume *objp is null on entry.