1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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/. */
8 #include "mozilla/Utf8.h" // mozilla::Utf8Unit
10 #include "js/CompilationAndEvaluation.h" // JS::Compile{,Utf8{File,Path}}
11 #include "js/PropertyAndElement.h" // JS_SetProperty
12 #include "js/SourceText.h" // JS::Source{Ownership,Text}
13 #include "jsapi-tests/tests.h"
15 struct ScriptObjectFixture
: public JSAPIRuntimeTest
{
16 static const int code_size
;
17 static const char code
[];
18 static char16_t uc_code
[];
20 ScriptObjectFixture() {
21 for (int i
= 0; i
< code_size
; i
++) {
26 bool tryScript(JS::HandleScript script
) {
31 /* After a garbage collection, the script should still work. */
32 JS::RootedValue
result(cx
);
33 CHECK(JS_ExecuteScript(cx
, script
, &result
));
39 const char ScriptObjectFixture::code
[] =
40 "(function(a, b){return a+' '+b;}('hello', 'world'))";
41 const int ScriptObjectFixture::code_size
=
42 sizeof(ScriptObjectFixture::code
) - 1;
43 char16_t
ScriptObjectFixture::uc_code
[ScriptObjectFixture::code_size
];
45 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_CompileScript
) {
46 JS::CompileOptions
options(cx
);
47 options
.setFileAndLine(__FILE__
, __LINE__
);
49 JS::SourceText
<mozilla::Utf8Unit
> srcBuf
;
50 CHECK(srcBuf
.init(cx
, code
, code_size
, JS::SourceOwnership::Borrowed
));
52 JS::RootedScript
script(cx
, JS::Compile(cx
, options
, srcBuf
));
55 return tryScript(script
);
57 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_CompileScript
)
59 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_CompileScript_empty
) {
60 JS::CompileOptions
options(cx
);
61 options
.setFileAndLine(__FILE__
, __LINE__
);
63 JS::SourceText
<mozilla::Utf8Unit
> srcBuf
;
64 CHECK(srcBuf
.init(cx
, "", 0, JS::SourceOwnership::Borrowed
));
66 JS::RootedScript
script(cx
, JS::Compile(cx
, options
, srcBuf
));
69 return tryScript(script
);
71 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_CompileScript_empty
)
73 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_CompileScriptForPrincipals
) {
74 JS::CompileOptions
options(cx
);
75 options
.setFileAndLine(__FILE__
, __LINE__
);
77 JS::SourceText
<mozilla::Utf8Unit
> srcBuf
;
78 CHECK(srcBuf
.init(cx
, code
, code_size
, JS::SourceOwnership::Borrowed
));
80 JS::RootedScript
script(cx
, JS::Compile(cx
, options
, srcBuf
));
82 return tryScript(script
);
84 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_CompileScriptForPrincipals
)
86 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileUCScript
) {
87 JS::CompileOptions
options(cx
);
88 options
.setFileAndLine(__FILE__
, __LINE__
);
90 JS::SourceText
<char16_t
> srcBuf
;
91 CHECK(srcBuf
.init(cx
, uc_code
, code_size
, JS::SourceOwnership::Borrowed
));
93 JS::RootedScript
script(cx
, JS::Compile(cx
, options
, srcBuf
));
96 return tryScript(script
);
98 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileUCScript
)
100 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileUCScript_empty
) {
101 JS::CompileOptions
options(cx
);
102 options
.setFileAndLine(__FILE__
, __LINE__
);
104 JS::SourceText
<char16_t
> srcBuf
;
105 CHECK(srcBuf
.init(cx
, uc_code
, 0, JS::SourceOwnership::Borrowed
));
107 JS::RootedScript
script(cx
, JS::Compile(cx
, options
, srcBuf
));
110 return tryScript(script
);
112 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileUCScript_empty
)
114 BEGIN_FIXTURE_TEST(ScriptObjectFixture
,
115 bug438633_JS_CompileUCScriptForPrincipals
) {
116 JS::CompileOptions
options(cx
);
117 options
.setFileAndLine(__FILE__
, __LINE__
);
119 JS::SourceText
<char16_t
> srcBuf
;
120 CHECK(srcBuf
.init(cx
, uc_code
, code_size
, JS::SourceOwnership::Borrowed
));
122 JS::RootedScript
script(cx
, JS::Compile(cx
, options
, srcBuf
));
125 return tryScript(script
);
127 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileUCScriptForPrincipals
)
129 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileFile
) {
131 static const char script_filename
[] = "temp-bug438633_JS_CompileFile";
132 FILE* script_stream
= tempScript
.open(script_filename
);
133 CHECK(fputs(code
, script_stream
) != EOF
);
136 JS::CompileOptions
options(cx
);
137 options
.setFileAndLine(script_filename
, 1);
139 JS::RootedScript
script(cx
,
140 JS::CompileUtf8Path(cx
, options
, script_filename
));
144 return tryScript(script
);
146 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileFile
)
148 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileFile_empty
) {
150 static const char script_filename
[] = "temp-bug438633_JS_CompileFile_empty";
151 tempScript
.open(script_filename
);
154 JS::CompileOptions
options(cx
);
155 options
.setFileAndLine(script_filename
, 1);
157 JS::RootedScript
script(cx
,
158 JS::CompileUtf8Path(cx
, options
, script_filename
));
162 return tryScript(script
);
164 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileFile_empty
)
166 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileFileHandle
) {
168 FILE* script_stream
= tempScript
.open("temp-bug438633_JS_CompileFileHandle");
169 CHECK(fputs(code
, script_stream
) != EOF
);
170 CHECK(fseek(script_stream
, 0, SEEK_SET
) != EOF
);
172 JS::CompileOptions
options(cx
);
173 options
.setFileAndLine("temporary file", 1);
175 JS::RootedScript
script(cx
, JS::CompileUtf8File(cx
, options
, script_stream
));
178 return tryScript(script
);
180 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileFileHandle
)
182 BEGIN_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileFileHandle_empty
) {
184 FILE* script_stream
=
185 tempScript
.open("temp-bug438633_JS_CompileFileHandle_empty");
187 JS::CompileOptions
options(cx
);
188 options
.setFileAndLine("empty temporary file", 1);
190 JS::RootedScript
script(cx
, JS::CompileUtf8File(cx
, options
, script_stream
));
193 return tryScript(script
);
195 END_FIXTURE_TEST(ScriptObjectFixture
, bug438633_JS_CompileFileHandle_empty
)
197 BEGIN_FIXTURE_TEST(ScriptObjectFixture
,
198 bug438633_JS_CompileFileHandleForPrincipals
) {
200 FILE* script_stream
=
201 tempScript
.open("temp-bug438633_JS_CompileFileHandleForPrincipals");
202 CHECK(fputs(code
, script_stream
) != EOF
);
203 CHECK(fseek(script_stream
, 0, SEEK_SET
) != EOF
);
205 JS::CompileOptions
options(cx
);
206 options
.setFileAndLine("temporary file", 1);
208 JS::RootedScript
script(cx
, JS::CompileUtf8File(cx
, options
, script_stream
));
211 return tryScript(script
);
213 END_FIXTURE_TEST(ScriptObjectFixture
,
214 bug438633_JS_CompileFileHandleForPrincipals
)