forked from rnewman/services-central-old
/
jsapi.h
5363 lines (4441 loc) · 176 KB
/
jsapi.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 sw=4 et tw=78:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef jsapi_h___
#define jsapi_h___
/*
* JavaScript API.
*/
#include "mozilla/StdInt.h"
#include <stddef.h>
#include <stdio.h>
#include "js-config.h"
#include "jspubtd.h"
#include "jsval.h"
#include "js/Utility.h"
#ifdef __cplusplus
#include "mozilla/Attributes.h"
#endif
/************************************************************************/
/* JS::Value can store a full int32_t. */
#define JSVAL_INT_BITS 32
#define JSVAL_INT_MIN ((jsint)0x80000000)
#define JSVAL_INT_MAX ((jsint)0x7fffffff)
/************************************************************************/
#ifdef __cplusplus
namespace JS {
/*
* Protecting non-jsval, non-JSObject *, non-JSString * values from collection
*
* Most of the time, the garbage collector's conservative stack scanner works
* behind the scenes, finding all live values and protecting them from being
* collected. However, when JSAPI client code obtains a pointer to data the
* scanner does not know about, owned by an object the scanner does know about,
* Care Must Be Taken.
*
* The scanner recognizes only a select set of types: pointers to JSObjects and
* similar things (JSFunctions, and so on), pointers to JSStrings, and jsvals.
* So while the scanner finds all live |JSString| pointers, it does not notice
* |jschar| pointers.
*
* So suppose we have:
*
* void f(JSString *str) {
* const jschar *ch = JS_GetStringCharsZ(str);
* ... do stuff with ch, but no uses of str ...;
* }
*
* After the call to |JS_GetStringCharsZ|, there are no further uses of
* |str|, which means that the compiler is within its rights to not store
* it anywhere. But because the stack scanner will not notice |ch|, there
* is no longer any live value in this frame that would keep the string
* alive. If |str| is the last reference to that |JSString|, and the
* collector runs while we are using |ch|, the string's array of |jschar|s
* may be freed out from under us.
*
* Note that there is only an issue when 1) we extract a thing X the scanner
* doesn't recognize from 2) a thing Y the scanner does recognize, and 3) if Y
* gets garbage-collected, then X gets freed. If we have code like this:
*
* void g(JSObject *obj) {
* jsval x;
* JS_GetProperty(obj, "x", &x);
* ... do stuff with x ...
* }
*
* there's no problem, because the value we've extracted, x, is a jsval, a
* type that the conservative scanner recognizes.
*
* Conservative GC frees us from the obligation to explicitly root the types it
* knows about, but when we work with derived values like |ch|, we must root
* their owners, as the derived value alone won't keep them alive.
*
* A JS::Anchor is a kind of GC root that allows us to keep the owners of
* derived values like |ch| alive throughout the Anchor's lifetime. We could
* fix the above code as follows:
*
* void f(JSString *str) {
* JS::Anchor<JSString *> a_str(str);
* const jschar *ch = JS_GetStringCharsZ(str);
* ... do stuff with ch, but no uses of str ...;
* }
*
* This simply ensures that |str| will be live until |a_str| goes out of scope.
* As long as we don't retain a pointer to the string's characters for longer
* than that, we have avoided all garbage collection hazards.
*/
template<typename T> class AnchorPermitted;
template<> class AnchorPermitted<JSObject *> { };
template<> class AnchorPermitted<const JSObject *> { };
template<> class AnchorPermitted<JSFunction *> { };
template<> class AnchorPermitted<const JSFunction *> { };
template<> class AnchorPermitted<JSString *> { };
template<> class AnchorPermitted<const JSString *> { };
template<> class AnchorPermitted<Value> { };
template<typename T>
class Anchor: AnchorPermitted<T>
{
public:
Anchor() { }
explicit Anchor(T t) { hold = t; }
inline ~Anchor();
T &get() { return hold; }
const T &get() const { return hold; }
void set(const T &t) { hold = t; }
void clear() { hold = 0; }
private:
T hold;
/* Anchors should not be assigned or passed to functions. */
Anchor(const Anchor &);
const Anchor &operator=(const Anchor &);
};
#ifdef __GNUC__
template<typename T>
inline Anchor<T>::~Anchor()
{
/*
* No code is generated for this. But because this is marked 'volatile', G++ will
* assume it has important side-effects, and won't delete it. (G++ never looks at
* the actual text and notices it's empty.) And because we have passed |hold| to
* it, GCC will keep |hold| alive until this point.
*
* The "memory" clobber operand ensures that G++ will not move prior memory
* accesses after the asm --- it's a barrier. Unfortunately, it also means that
* G++ will assume that all memory has changed after the asm, as it would for a
* call to an unknown function. I don't know of a way to avoid that consequence.
*/
asm volatile("":: "g" (hold) : "memory");
}
#else
template<typename T>
inline Anchor<T>::~Anchor()
{
/*
* An adequate portable substitute, for non-structure types.
*
* The compiler promises that, by the end of an expression statement, the
* last-stored value to a volatile object is the same as it would be in an
* unoptimized, direct implementation (the "abstract machine" whose behavior the
* language spec describes). However, the compiler is still free to reorder
* non-volatile accesses across this store --- which is what we must prevent. So
* assigning the held value to a volatile variable, as we do here, is not enough.
*
* In our case, however, garbage collection only occurs at function calls, so it
* is sufficient to ensure that the destructor's store isn't moved earlier across
* any function calls that could collect. It is hard to imagine the compiler
* analyzing the program so thoroughly that it could prove that such motion was
* safe. In practice, compilers treat calls to the collector as opaque operations
* --- in particular, as operations which could access volatile variables, across
* which this destructor must not be moved.
*
* ("Objection, your honor! *Alleged* killer whale!")
*
* The disadvantage of this approach is that it does generate code for the store.
* We do need to use Anchors in some cases where cycles are tight.
*
* NB: there is a Anchor<Value>::~Anchor() specialization below.
*/
volatile T sink;
sink = hold;
}
#endif /* defined(__GNUC__) */
/*
* Methods for poisoning GC heap pointer words and checking for poisoned words.
* These are in this file for use in Value methods and so forth.
*
* If the moving GC hazard analysis is in use and detects a non-rooted stack
* pointer to a GC thing, one byte of that pointer is poisoned to refer to an
* invalid location. For both 32 bit and 64 bit systems, the fourth byte of the
* pointer is overwritten, to reduce the likelihood of accidentally changing
* a live integer value.
*/
inline void PoisonPtr(uintptr_t *v)
{
#if defined(JSGC_ROOT_ANALYSIS) && defined(DEBUG)
uint8_t *ptr = (uint8_t *) v + 3;
*ptr = JS_FREE_PATTERN;
#endif
}
template <typename T>
inline bool IsPoisonedPtr(T *v)
{
#if defined(JSGC_ROOT_ANALYSIS) && defined(DEBUG)
uint32_t mask = uintptr_t(v) & 0xff000000;
return mask == uint32_t(JS_FREE_PATTERN << 24);
#else
return false;
#endif
}
/*
* JS::Value is the C++ interface for a single JavaScript Engine value.
* A few general notes on JS::Value:
*
* - JS::Value has setX() and isX() members for X in
*
* { Int32, Double, String, Boolean, Undefined, Null, Object, Magic }
*
* JS::Value also contains toX() for each of the non-singleton types.
*
* - Magic is a singleton type whose payload contains a JSWhyMagic "reason" for
* the magic value. By providing JSWhyMagic values when creating and checking
* for magic values, it is possible to assert, at runtime, that only magic
* values with the expected reason flow through a particular value. For
* example, if cx->exception has a magic value, the reason must be
* JS_GENERATOR_CLOSING.
*
* - A key difference between JSVAL_* and JS::Value operations is that
* JS::Value gives null a separate type. Thus
*
* JSVAL_IS_OBJECT(v) === v.isObjectOrNull()
* !JSVAL_IS_PRIMITIVE(v) === v.isObject()
*
* To help prevent mistakenly boxing a nullable JSObject* as an object,
* Value::setObject takes a JSObject&. (Conversely, Value::asObject returns a
* JSObject&. A convenience member Value::setObjectOrNull is provided.
*
* - JSVAL_VOID is the same as the singleton value of the Undefined type.
*
* - Note that JS::Value is 8 bytes on 32 and 64-bit architectures. Thus, on
* 32-bit user code should avoid copying jsval/JS::Value as much as possible,
* preferring to pass by const Value &.
*/
class Value
{
public:
/*
* N.B. the default constructor leaves Value unitialized. Adding a default
* constructor prevents Value from being stored in a union.
*/
/*** Mutators ***/
JS_ALWAYS_INLINE
void setNull() {
data.asBits = BUILD_JSVAL(JSVAL_TAG_NULL, 0).asBits;
}
JS_ALWAYS_INLINE
void setUndefined() {
data.asBits = BUILD_JSVAL(JSVAL_TAG_UNDEFINED, 0).asBits;
}
JS_ALWAYS_INLINE
void setInt32(int32_t i) {
data = INT32_TO_JSVAL_IMPL(i);
}
JS_ALWAYS_INLINE
int32_t &getInt32Ref() {
JS_ASSERT(isInt32());
return data.s.payload.i32;
}
JS_ALWAYS_INLINE
void setDouble(double d) {
data = DOUBLE_TO_JSVAL_IMPL(d);
}
JS_ALWAYS_INLINE
double &getDoubleRef() {
JS_ASSERT(isDouble());
return data.asDouble;
}
JS_ALWAYS_INLINE
void setString(JSString *str) {
JS_ASSERT(!IsPoisonedPtr(str));
data = STRING_TO_JSVAL_IMPL(str);
}
JS_ALWAYS_INLINE
void setString(const JS::Anchor<JSString *> &str) {
setString(str.get());
}
JS_ALWAYS_INLINE
void setObject(JSObject &obj) {
JS_ASSERT(!IsPoisonedPtr(&obj));
data = OBJECT_TO_JSVAL_IMPL(&obj);
}
JS_ALWAYS_INLINE
void setBoolean(bool b) {
data = BOOLEAN_TO_JSVAL_IMPL(b);
}
JS_ALWAYS_INLINE
void setMagic(JSWhyMagic why) {
data = MAGIC_TO_JSVAL_IMPL(why);
}
JS_ALWAYS_INLINE
bool setNumber(uint32_t ui) {
if (ui > JSVAL_INT_MAX) {
setDouble((double)ui);
return false;
} else {
setInt32((int32_t)ui);
return true;
}
}
JS_ALWAYS_INLINE
bool setNumber(double d) {
int32_t i;
if (JSDOUBLE_IS_INT32(d, &i)) {
setInt32(i);
return true;
} else {
setDouble(d);
return false;
}
}
JS_ALWAYS_INLINE
void setObjectOrNull(JSObject *arg) {
if (arg)
setObject(*arg);
else
setNull();
}
JS_ALWAYS_INLINE
void swap(Value &rhs) {
uint64_t tmp = rhs.data.asBits;
rhs.data.asBits = data.asBits;
data.asBits = tmp;
}
/*** Value type queries ***/
JS_ALWAYS_INLINE
bool isUndefined() const {
return JSVAL_IS_UNDEFINED_IMPL(data);
}
JS_ALWAYS_INLINE
bool isNull() const {
return JSVAL_IS_NULL_IMPL(data);
}
JS_ALWAYS_INLINE
bool isNullOrUndefined() const {
return isNull() || isUndefined();
}
JS_ALWAYS_INLINE
bool isInt32() const {
return JSVAL_IS_INT32_IMPL(data);
}
JS_ALWAYS_INLINE
bool isInt32(int32_t i32) const {
return JSVAL_IS_SPECIFIC_INT32_IMPL(data, i32);
}
JS_ALWAYS_INLINE
bool isDouble() const {
return JSVAL_IS_DOUBLE_IMPL(data);
}
JS_ALWAYS_INLINE
bool isNumber() const {
return JSVAL_IS_NUMBER_IMPL(data);
}
JS_ALWAYS_INLINE
bool isString() const {
return JSVAL_IS_STRING_IMPL(data);
}
JS_ALWAYS_INLINE
bool isObject() const {
return JSVAL_IS_OBJECT_IMPL(data);
}
JS_ALWAYS_INLINE
bool isPrimitive() const {
return JSVAL_IS_PRIMITIVE_IMPL(data);
}
JS_ALWAYS_INLINE
bool isObjectOrNull() const {
return JSVAL_IS_OBJECT_OR_NULL_IMPL(data);
}
JS_ALWAYS_INLINE
bool isGCThing() const {
return JSVAL_IS_GCTHING_IMPL(data);
}
JS_ALWAYS_INLINE
bool isBoolean() const {
return JSVAL_IS_BOOLEAN_IMPL(data);
}
JS_ALWAYS_INLINE
bool isTrue() const {
return JSVAL_IS_SPECIFIC_BOOLEAN(data, true);
}
JS_ALWAYS_INLINE
bool isFalse() const {
return JSVAL_IS_SPECIFIC_BOOLEAN(data, false);
}
JS_ALWAYS_INLINE
bool isMagic() const {
return JSVAL_IS_MAGIC_IMPL(data);
}
JS_ALWAYS_INLINE
bool isMagic(JSWhyMagic why) const {
JS_ASSERT_IF(isMagic(), data.s.payload.why == why);
return JSVAL_IS_MAGIC_IMPL(data);
}
/*
* Although the Value class comment says 'magic' is a singleton type, it is
* technically possible to use the payload. This should be avoided to
* preserve the ability for the strong assertions in isMagic().
*/
JS_ALWAYS_INLINE
bool isParticularMagic(JSWhyMagic why) const {
return isMagic() && data.s.payload.why == why;
}
JS_ALWAYS_INLINE
bool isMarkable() const {
return JSVAL_IS_TRACEABLE_IMPL(data);
}
JS_ALWAYS_INLINE
JSGCTraceKind gcKind() const {
JS_ASSERT(isMarkable());
return JSGCTraceKind(JSVAL_TRACE_KIND_IMPL(data));
}
JS_ALWAYS_INLINE
JSWhyMagic whyMagic() const {
JS_ASSERT(isMagic());
return data.s.payload.why;
}
/*** Comparison ***/
JS_ALWAYS_INLINE
bool operator==(const Value &rhs) const {
return data.asBits == rhs.data.asBits;
}
JS_ALWAYS_INLINE
bool operator!=(const Value &rhs) const {
return data.asBits != rhs.data.asBits;
}
friend inline bool SameType(const Value &lhs, const Value &rhs);
/*** Extract the value's typed payload ***/
JS_ALWAYS_INLINE
int32_t toInt32() const {
JS_ASSERT(isInt32());
return JSVAL_TO_INT32_IMPL(data);
}
JS_ALWAYS_INLINE
double toDouble() const {
JS_ASSERT(isDouble());
return data.asDouble;
}
JS_ALWAYS_INLINE
double toNumber() const {
JS_ASSERT(isNumber());
return isDouble() ? toDouble() : double(toInt32());
}
JS_ALWAYS_INLINE
JSString *toString() const {
JS_ASSERT(isString());
return JSVAL_TO_STRING_IMPL(data);
}
JS_ALWAYS_INLINE
JSObject &toObject() const {
JS_ASSERT(isObject());
return *JSVAL_TO_OBJECT_IMPL(data);
}
JS_ALWAYS_INLINE
JSObject *toObjectOrNull() const {
JS_ASSERT(isObjectOrNull());
return JSVAL_TO_OBJECT_IMPL(data);
}
JS_ALWAYS_INLINE
void *toGCThing() const {
JS_ASSERT(isGCThing());
return JSVAL_TO_GCTHING_IMPL(data);
}
JS_ALWAYS_INLINE
bool toBoolean() const {
JS_ASSERT(isBoolean());
return JSVAL_TO_BOOLEAN_IMPL(data);
}
JS_ALWAYS_INLINE
uint32_t payloadAsRawUint32() const {
JS_ASSERT(!isDouble());
return data.s.payload.u32;
}
JS_ALWAYS_INLINE
JSValueType extractNonDoubleType() const {
return JSVAL_EXTRACT_NON_DOUBLE_TYPE_IMPL(data);
}
/*
* Private API
*
* Private setters/getters allow the caller to read/write arbitrary types
* that fit in the 64-bit payload. It is the caller's responsibility, after
* storing to a value with setPrivateX to read only using getPrivateX.
* Privates values are given a type type which ensures they are not marked.
*/
JS_ALWAYS_INLINE
void setPrivate(void *ptr) {
data = PRIVATE_PTR_TO_JSVAL_IMPL(ptr);
}
JS_ALWAYS_INLINE
void *toPrivate() const {
JS_ASSERT(JSVAL_IS_DOUBLE_IMPL(data));
return JSVAL_TO_PRIVATE_PTR_IMPL(data);
}
JS_ALWAYS_INLINE
void setPrivateUint32(uint32_t ui) {
data = PRIVATE_UINT32_TO_JSVAL_IMPL(ui);
}
JS_ALWAYS_INLINE
uint32_t toPrivateUint32() const {
JS_ASSERT(JSVAL_IS_DOUBLE_IMPL(data));
return JSVAL_TO_PRIVATE_UINT32_IMPL(data);
}
JS_ALWAYS_INLINE
uint32_t &getPrivateUint32Ref() {
JS_ASSERT(isDouble());
return data.s.payload.u32;
}
/*
* An unmarked value is just a void* cast as a Value. Thus, the Value is
* not safe for GC and must not be marked. This API avoids raw casts
* and the ensuing strict-aliasing warnings.
*/
JS_ALWAYS_INLINE
void setUnmarkedPtr(void *ptr) {
data.asPtr = ptr;
}
JS_ALWAYS_INLINE
void *toUnmarkedPtr() const {
return data.asPtr;
}
const size_t *payloadWord() const {
#if JS_BITS_PER_WORD == 32
return &data.s.payload.word;
#elif JS_BITS_PER_WORD == 64
return &data.asWord;
#endif
}
#ifndef _MSC_VER
/* To make jsval binary compatible when linking across C and C++ with MSVC,
* JS::Value needs to be POD. Otherwise, jsval will be passed in memory
* in C++ but by value in C (bug 645111).
*/
private:
#endif
jsval_layout data;
private:
void staticAssertions() {
JS_STATIC_ASSERT(sizeof(JSValueType) == 1);
JS_STATIC_ASSERT(sizeof(JSValueTag) == 4);
JS_STATIC_ASSERT(sizeof(JSBool) == 4);
JS_STATIC_ASSERT(sizeof(JSWhyMagic) <= 4);
JS_STATIC_ASSERT(sizeof(Value) == 8);
}
friend jsval_layout (::JSVAL_TO_IMPL)(Value);
friend Value (::IMPL_TO_JSVAL)(jsval_layout l);
} JSVAL_ALIGNMENT;
inline bool
IsPoisonedValue(const Value &v)
{
if (v.isString())
return IsPoisonedPtr(v.toString());
if (v.isObject())
return IsPoisonedPtr(&v.toObject());
return false;
}
/************************************************************************/
static JS_ALWAYS_INLINE Value
NullValue()
{
Value v;
v.setNull();
return v;
}
static JS_ALWAYS_INLINE Value
UndefinedValue()
{
Value v;
v.setUndefined();
return v;
}
static JS_ALWAYS_INLINE Value
Int32Value(int32_t i32)
{
Value v;
v.setInt32(i32);
return v;
}
static JS_ALWAYS_INLINE Value
DoubleValue(double dbl)
{
Value v;
v.setDouble(dbl);
return v;
}
static JS_ALWAYS_INLINE Value
StringValue(JSString *str)
{
Value v;
v.setString(str);
return v;
}
static JS_ALWAYS_INLINE Value
BooleanValue(bool boo)
{
Value v;
v.setBoolean(boo);
return v;
}
static JS_ALWAYS_INLINE Value
ObjectValue(JSObject &obj)
{
Value v;
v.setObject(obj);
return v;
}
static JS_ALWAYS_INLINE Value
MagicValue(JSWhyMagic why)
{
Value v;
v.setMagic(why);
return v;
}
static JS_ALWAYS_INLINE Value
NumberValue(double dbl)
{
Value v;
v.setNumber(dbl);
return v;
}
static JS_ALWAYS_INLINE Value
ObjectOrNullValue(JSObject *obj)
{
Value v;
v.setObjectOrNull(obj);
return v;
}
static JS_ALWAYS_INLINE Value
PrivateValue(void *ptr)
{
Value v;
v.setPrivate(ptr);
return v;
}
static JS_ALWAYS_INLINE Value
PrivateUint32Value(uint32_t ui)
{
Value v;
v.setPrivateUint32(ui);
return v;
}
JS_ALWAYS_INLINE bool
SameType(const Value &lhs, const Value &rhs)
{
return JSVAL_SAME_TYPE_IMPL(lhs.data, rhs.data);
}
/************************************************************************/
#ifndef __GNUC__
/*
* The default assignment operator for |struct C| has the signature:
*
* C& C::operator=(const C&)
*
* And in particular requires implicit conversion of |this| to type |C| for the
* return value. But |volatile C| cannot thus be converted to |C|, so just
* doing |sink = hold| as in the non-specialized version would fail to compile.
* Do the assignment on asBits instead, since I don't think we want to give
* jsval_layout an assignment operator returning |volatile jsval_layout|.
*/
template<>
inline Anchor<Value>::~Anchor()
{
volatile uint64_t bits;
bits = JSVAL_TO_IMPL(hold).asBits;
}
#endif
#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
extern void
MarkRuntime(JSTracer *trc);
class JS_PUBLIC_API(AutoGCRooter) {
public:
AutoGCRooter(JSContext *cx, ptrdiff_t tag);
~AutoGCRooter();
/* Implemented in jsgc.cpp. */
inline void trace(JSTracer *trc);
void traceAll(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;
JSContext * const context;
enum {
JSVAL = -1, /* js::AutoValueRooter */
VALARRAY = -2, /* js::AutoValueArrayRooter */
PARSER = -3, /* js::Parser */
SHAPEVECTOR = -4, /* js::AutoShapeVector */
ENUMERATOR = -5, /* js::AutoEnumStateRooter */
IDARRAY = -6, /* js::AutoIdArray */
DESCRIPTORS = -7, /* js::AutoPropDescArrayRooter */
NAMESPACES = -8, /* js::AutoNamespaceArray */
XML = -9, /* js::AutoXMLRooter */
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 */
};
private:
/* 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
JS_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, JSVAL), val(NullValue())
{
JS_GUARD_OBJECT_NOTIFIER_INIT;
}
AutoValueRooter(JSContext *cx, const Value &v
JS_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, JSVAL), val(v)
{
JS_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);
friend void MarkRuntime(JSTracer *trc);
private:
Value val;
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoObjectRooter : private AutoGCRooter {
public:
AutoObjectRooter(JSContext *cx, JSObject *obj = NULL
JS_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, OBJECT), obj(obj)
{
JS_GUARD_OBJECT_NOTIFIER_INIT;
}
void setObject(JSObject *obj) {
this->obj = obj;
}
JSObject * object() const {
return obj;
}
JSObject ** addr() {
return &obj;
}
friend void AutoGCRooter::trace(JSTracer *trc);
friend void MarkRuntime(JSTracer *trc);
private:
JSObject *obj;
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoStringRooter : private AutoGCRooter {
public:
AutoStringRooter(JSContext *cx, JSString *str = NULL
JS_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, STRING), str(str)
{
JS_GUARD_OBJECT_NOTIFIER_INIT;
}
void setString(JSString *str) {
this->str = str;
}
JSString * string() const {
return str;
}
JSString ** addr() {
return &str;
}
friend void AutoGCRooter::trace(JSTracer *trc);
private:
JSString *str;
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class AutoArrayRooter : private AutoGCRooter {
public:
AutoArrayRooter(JSContext *cx, size_t len, Value *vec
JS_GUARD_OBJECT_NOTIFIER_PARAM)
: AutoGCRooter(cx, len), array(vec)
{
JS_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;
friend void AutoGCRooter::trace(JSTracer *trc);