|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 */ |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include "jsapi-tests/tests.h" |
|
9 |
|
10 struct ScriptObjectFixture : public JSAPITest { |
|
11 static const int code_size; |
|
12 static const char code[]; |
|
13 static jschar uc_code[]; |
|
14 |
|
15 ScriptObjectFixture() |
|
16 { |
|
17 for (int i = 0; i < code_size; i++) |
|
18 uc_code[i] = code[i]; |
|
19 } |
|
20 |
|
21 bool tryScript(JS::HandleObject global, JSScript *scriptArg) |
|
22 { |
|
23 JS::RootedScript script(cx, scriptArg); |
|
24 CHECK(script); |
|
25 |
|
26 JS_GC(rt); |
|
27 |
|
28 /* After a garbage collection, the script should still work. */ |
|
29 JS::RootedValue result(cx); |
|
30 CHECK(JS_ExecuteScript(cx, global, script, &result)); |
|
31 |
|
32 return true; |
|
33 } |
|
34 }; |
|
35 |
|
36 const char ScriptObjectFixture::code[] = |
|
37 "(function(a, b){return a+' '+b;}('hello', 'world'))"; |
|
38 const int ScriptObjectFixture::code_size = sizeof(ScriptObjectFixture::code) - 1; |
|
39 jschar ScriptObjectFixture::uc_code[ScriptObjectFixture::code_size]; |
|
40 |
|
41 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript) |
|
42 { |
|
43 JS::CompileOptions options(cx); |
|
44 options.setFileAndLine(__FILE__, __LINE__); |
|
45 return tryScript(global, JS_CompileScript(cx, global, code, code_size, |
|
46 options)); |
|
47 } |
|
48 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript) |
|
49 |
|
50 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript_empty) |
|
51 { |
|
52 JS::CompileOptions options(cx); |
|
53 options.setFileAndLine(__FILE__, __LINE__); |
|
54 return tryScript(global, JS_CompileScript(cx, global, "", 0, options)); |
|
55 } |
|
56 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript_empty) |
|
57 |
|
58 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScriptForPrincipals) |
|
59 { |
|
60 JS::CompileOptions options(cx); |
|
61 options.setFileAndLine(__FILE__, __LINE__); |
|
62 return tryScript(global, JS_CompileScript(cx, global, code, code_size, |
|
63 options)); |
|
64 } |
|
65 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScriptForPrincipals) |
|
66 |
|
67 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript) |
|
68 { |
|
69 JS::CompileOptions options(cx); |
|
70 options.setFileAndLine(__FILE__, __LINE__); |
|
71 return tryScript(global, JS_CompileUCScript(cx, global, uc_code, code_size, |
|
72 options)); |
|
73 } |
|
74 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript) |
|
75 |
|
76 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty) |
|
77 { |
|
78 JS::CompileOptions options(cx); |
|
79 options.setFileAndLine(__FILE__, __LINE__); |
|
80 return tryScript(global, JS_CompileUCScript(cx, global, uc_code, 0, |
|
81 options)); |
|
82 } |
|
83 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty) |
|
84 |
|
85 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipals) |
|
86 { |
|
87 JS::CompileOptions options(cx); |
|
88 options.setFileAndLine(__FILE__, __LINE__); |
|
89 return tryScript(global, JS_CompileUCScript(cx, global, uc_code, code_size, |
|
90 options)); |
|
91 } |
|
92 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipals) |
|
93 |
|
94 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile) |
|
95 { |
|
96 TempFile tempScript; |
|
97 static const char script_filename[] = "temp-bug438633_JS_CompileFile"; |
|
98 FILE *script_stream = tempScript.open(script_filename); |
|
99 CHECK(fputs(code, script_stream) != EOF); |
|
100 tempScript.close(); |
|
101 JS::CompileOptions options(cx); |
|
102 options.setFileAndLine(script_filename, 1); |
|
103 JSScript *script = JS::Compile(cx, global, options, script_filename); |
|
104 tempScript.remove(); |
|
105 return tryScript(global, script); |
|
106 } |
|
107 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile) |
|
108 |
|
109 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile_empty) |
|
110 { |
|
111 TempFile tempScript; |
|
112 static const char script_filename[] = "temp-bug438633_JS_CompileFile_empty"; |
|
113 tempScript.open(script_filename); |
|
114 tempScript.close(); |
|
115 JS::CompileOptions options(cx); |
|
116 options.setFileAndLine(script_filename, 1); |
|
117 JSScript *script = JS::Compile(cx, global, options, script_filename); |
|
118 tempScript.remove(); |
|
119 return tryScript(global, script); |
|
120 } |
|
121 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile_empty) |
|
122 |
|
123 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle) |
|
124 { |
|
125 const char *script_filename = "temporary file"; |
|
126 TempFile tempScript; |
|
127 FILE *script_stream = tempScript.open("temp-bug438633_JS_CompileFileHandle"); |
|
128 CHECK(fputs(code, script_stream) != EOF); |
|
129 CHECK(fseek(script_stream, 0, SEEK_SET) != EOF); |
|
130 JS::CompileOptions options(cx); |
|
131 options.setFileAndLine(script_filename, 1); |
|
132 return tryScript(global, JS::Compile(cx, global, options, script_stream)); |
|
133 } |
|
134 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle) |
|
135 |
|
136 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle_empty) |
|
137 { |
|
138 const char *script_filename = "empty temporary file"; |
|
139 TempFile tempScript; |
|
140 FILE *script_stream = tempScript.open("temp-bug438633_JS_CompileFileHandle_empty"); |
|
141 JS::CompileOptions options(cx); |
|
142 options.setFileAndLine(script_filename, 1); |
|
143 return tryScript(global, JS::Compile(cx, global, options, script_stream)); |
|
144 } |
|
145 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle_empty) |
|
146 |
|
147 BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandleForPrincipals) |
|
148 { |
|
149 TempFile tempScript; |
|
150 FILE *script_stream = tempScript.open("temp-bug438633_JS_CompileFileHandleForPrincipals"); |
|
151 CHECK(fputs(code, script_stream) != EOF); |
|
152 CHECK(fseek(script_stream, 0, SEEK_SET) != EOF); |
|
153 JS::CompileOptions options(cx); |
|
154 options.setFileAndLine("temporary file", 1); |
|
155 return tryScript(global, JS::Compile(cx, global, options, script_stream)); |
|
156 } |
|
157 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandleForPrincipals) |