|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #if !defined(JSGC_USE_EXACT_ROOTING) |
|
6 |
|
7 #include "jsobj.h" |
|
8 |
|
9 #include "jsapi-tests/tests.h" |
|
10 #include "vm/String.h" |
|
11 |
|
12 BEGIN_TEST(testConservativeGC) |
|
13 { |
|
14 JS::RootedValue v2(cx); |
|
15 EVAL("({foo: 'bar'});", &v2); |
|
16 CHECK(v2.isObject()); |
|
17 char objCopy[sizeof(JSObject)]; |
|
18 js_memcpy(&objCopy, JSVAL_TO_OBJECT(v2), sizeof(JSObject)); |
|
19 |
|
20 JS::RootedValue v3(cx); |
|
21 EVAL("String(Math.PI);", &v3); |
|
22 CHECK(JSVAL_IS_STRING(v3)); |
|
23 char strCopy[sizeof(JSString)]; |
|
24 js_memcpy(&strCopy, JSVAL_TO_STRING(v3), sizeof(JSString)); |
|
25 |
|
26 JS::RootedValue tmp(cx); |
|
27 EVAL("({foo2: 'bar2'});", &tmp); |
|
28 CHECK(tmp.isObject()); |
|
29 JS::RootedObject obj2(cx, JSVAL_TO_OBJECT(tmp)); |
|
30 char obj2Copy[sizeof(JSObject)]; |
|
31 js_memcpy(&obj2Copy, obj2, sizeof(JSObject)); |
|
32 |
|
33 EVAL("String(Math.sqrt(3));", &tmp); |
|
34 CHECK(JSVAL_IS_STRING(tmp)); |
|
35 JS::RootedString str2(cx, JSVAL_TO_STRING(tmp)); |
|
36 char str2Copy[sizeof(JSString)]; |
|
37 js_memcpy(&str2Copy, str2, sizeof(JSString)); |
|
38 |
|
39 tmp = JSVAL_NULL; |
|
40 |
|
41 JS_GC(rt); |
|
42 |
|
43 EVAL("var a = [];\n" |
|
44 "for (var i = 0; i != 10000; ++i) {\n" |
|
45 "a.push(i + 0.1, [1, 2], String(Math.sqrt(i)), {a: i});\n" |
|
46 "}", &tmp); |
|
47 |
|
48 JS_GC(rt); |
|
49 |
|
50 checkObjectFields((JSObject *)objCopy, JSVAL_TO_OBJECT(v2)); |
|
51 CHECK(!memcmp(strCopy, JSVAL_TO_STRING(v3), sizeof(strCopy))); |
|
52 |
|
53 checkObjectFields((JSObject *)obj2Copy, obj2); |
|
54 CHECK(!memcmp(str2Copy, str2, sizeof(str2Copy))); |
|
55 |
|
56 return true; |
|
57 } |
|
58 |
|
59 bool checkObjectFields(JSObject *savedCopy, JSObject *obj) |
|
60 { |
|
61 /* Ignore fields which are unstable across GCs. */ |
|
62 CHECK(savedCopy->lastProperty() == obj->lastProperty()); |
|
63 return true; |
|
64 } |
|
65 |
|
66 END_TEST(testConservativeGC) |
|
67 |
|
68 BEGIN_TEST(testDerivedValues) |
|
69 { |
|
70 JSString *str = JS_NewStringCopyZ(cx, "once upon a midnight dreary"); |
|
71 JS::Anchor<JSString *> str_anchor(str); |
|
72 static const jschar expected[] = { 'o', 'n', 'c', 'e' }; |
|
73 const jschar *ch = JS_GetStringCharsZ(cx, str); |
|
74 str = nullptr; |
|
75 |
|
76 /* Do a lot of allocation and collection. */ |
|
77 for (int i = 0; i < 3; i++) { |
|
78 for (int j = 0; j < 1000; j++) |
|
79 JS_NewStringCopyZ(cx, "as I pondered weak and weary"); |
|
80 JS_GC(rt); |
|
81 } |
|
82 |
|
83 CHECK(!memcmp(ch, expected, sizeof(expected))); |
|
84 return true; |
|
85 } |
|
86 END_TEST(testDerivedValues) |
|
87 |
|
88 #endif /* !defined(JSGC_USE_EXACT_ROOTING) */ |