| 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: |
| 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 "mozilla/Utf8.h" // mozilla::Utf8Unit |
| 9 |
|
| 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" |
| 14 |
|
| 15 |
struct ScriptObjectFixture : public JSAPIRuntimeTest { |
| 16 |
static const int code_size; |
| 17 |
static const char code[]; |
| 18 |
static char16_t uc_code[]; |
| 19 |
|
| 20 |
ScriptObjectFixture() { |
| 21 |
for (int i = 0; i < code_size; i++) { |
| 22 |
uc_code[i] = code[i]; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
bool tryScript(JS::HandleScript script) { |
| 27 |
CHECK(script); |
| 28 |
|
| 29 |
JS_GC(cx); |
| 30 |
|
| 31 |
/* After a garbage collection, the script should still work. */ |
| 32 |
JS::RootedValue result(cx); |
| 33 |
CHECK(JS_ExecuteScript(cx, script, &result)); |
| 34 |
|
| 35 |
return true; |
| 36 |
} |
| 37 |
}; |
| 38 |
|
| 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]; |
| 44 |
|
| 45 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript) { |
| 46 |
JS::CompileOptions options(cx); |
| 47 |
options.setFileAndLine(__FILE__, __LINE__); |
| 48 |
|
| 49 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 50 |
CHECK(srcBuf.init(cx, code, code_size, JS::SourceOwnership::Borrowed)); |
| 51 |
|
| 52 |
JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf)); |
| 53 |
CHECK(script); |
| 54 |
|
| 55 |
return tryScript(script); |
| 56 |
} |
| 57 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript) |
| 58 |
|
| 59 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript_empty) { |
| 60 |
JS::CompileOptions options(cx); |
| 61 |
options.setFileAndLine(__FILE__, __LINE__); |
| 62 |
|
| 63 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 64 |
CHECK(srcBuf.init(cx, "", 0, JS::SourceOwnership::Borrowed)); |
| 65 |
|
| 66 |
JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf)); |
| 67 |
CHECK(script); |
| 68 |
|
| 69 |
return tryScript(script); |
| 70 |
} |
| 71 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript_empty) |
| 72 |
|
| 73 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScriptForPrincipals) { |
| 74 |
JS::CompileOptions options(cx); |
| 75 |
options.setFileAndLine(__FILE__, __LINE__); |
| 76 |
|
| 77 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 78 |
CHECK(srcBuf.init(cx, code, code_size, JS::SourceOwnership::Borrowed)); |
| 79 |
|
| 80 |
JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf)); |
| 81 |
|
| 82 |
return tryScript(script); |
| 83 |
} |
| 84 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScriptForPrincipals) |
| 85 |
|
| 86 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript) { |
| 87 |
JS::CompileOptions options(cx); |
| 88 |
options.setFileAndLine(__FILE__, __LINE__); |
| 89 |
|
| 90 |
JS::SourceText<char16_t> srcBuf; |
| 91 |
CHECK(srcBuf.init(cx, uc_code, code_size, JS::SourceOwnership::Borrowed)); |
| 92 |
|
| 93 |
JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf)); |
| 94 |
CHECK(script); |
| 95 |
|
| 96 |
return tryScript(script); |
| 97 |
} |
| 98 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript) |
| 99 |
|
| 100 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty) { |
| 101 |
JS::CompileOptions options(cx); |
| 102 |
options.setFileAndLine(__FILE__, __LINE__); |
| 103 |
|
| 104 |
JS::SourceText<char16_t> srcBuf; |
| 105 |
CHECK(srcBuf.init(cx, uc_code, 0, JS::SourceOwnership::Borrowed)); |
| 106 |
|
| 107 |
JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf)); |
| 108 |
CHECK(script); |
| 109 |
|
| 110 |
return tryScript(script); |
| 111 |
} |
| 112 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty) |
| 113 |
|
| 114 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, |
| 115 |
bug438633_JS_CompileUCScriptForPrincipals) { |
| 116 |
JS::CompileOptions options(cx); |
| 117 |
options.setFileAndLine(__FILE__, __LINE__); |
| 118 |
|
| 119 |
JS::SourceText<char16_t> srcBuf; |
| 120 |
CHECK(srcBuf.init(cx, uc_code, code_size, JS::SourceOwnership::Borrowed)); |
| 121 |
|
| 122 |
JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf)); |
| 123 |
CHECK(script); |
| 124 |
|
| 125 |
return tryScript(script); |
| 126 |
} |
| 127 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipals) |
| 128 |
|
| 129 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile) { |
| 130 |
TempFile tempScript; |
| 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); |
| 134 |
tempScript.close(); |
| 135 |
|
| 136 |
JS::CompileOptions options(cx); |
| 137 |
options.setFileAndLine(script_filename, 1); |
| 138 |
|
| 139 |
JS::RootedScript script(cx, |
| 140 |
JS::CompileUtf8Path(cx, options, script_filename)); |
| 141 |
CHECK(script); |
| 142 |
|
| 143 |
tempScript.remove(); |
| 144 |
return tryScript(script); |
| 145 |
} |
| 146 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile) |
| 147 |
|
| 148 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile_empty) { |
| 149 |
TempFile tempScript; |
| 150 |
static const char script_filename[] = "temp-bug438633_JS_CompileFile_empty"; |
| 151 |
tempScript.open(script_filename); |
| 152 |
tempScript.close(); |
| 153 |
|
| 154 |
JS::CompileOptions options(cx); |
| 155 |
options.setFileAndLine(script_filename, 1); |
| 156 |
|
| 157 |
JS::RootedScript script(cx, |
| 158 |
JS::CompileUtf8Path(cx, options, script_filename)); |
| 159 |
CHECK(script); |
| 160 |
|
| 161 |
tempScript.remove(); |
| 162 |
return tryScript(script); |
| 163 |
} |
| 164 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile_empty) |
| 165 |
|
| 166 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle) { |
| 167 |
TempFile tempScript; |
| 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); |
| 171 |
|
| 172 |
JS::CompileOptions options(cx); |
| 173 |
options.setFileAndLine("temporary file", 1); |
| 174 |
|
| 175 |
JS::RootedScript script(cx, JS::CompileUtf8File(cx, options, script_stream)); |
| 176 |
CHECK(script); |
| 177 |
|
| 178 |
return tryScript(script); |
| 179 |
} |
| 180 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle) |
| 181 |
|
| 182 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle_empty) { |
| 183 |
TempFile tempScript; |
| 184 |
FILE* script_stream = |
| 185 |
tempScript.open("temp-bug438633_JS_CompileFileHandle_empty"); |
| 186 |
|
| 187 |
JS::CompileOptions options(cx); |
| 188 |
options.setFileAndLine("empty temporary file", 1); |
| 189 |
|
| 190 |
JS::RootedScript script(cx, JS::CompileUtf8File(cx, options, script_stream)); |
| 191 |
CHECK(script); |
| 192 |
|
| 193 |
return tryScript(script); |
| 194 |
} |
| 195 |
END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle_empty) |
| 196 |
|
| 197 |
BEGIN_FIXTURE_TEST(ScriptObjectFixture, |
| 198 |
bug438633_JS_CompileFileHandleForPrincipals) { |
| 199 |
TempFile tempScript; |
| 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); |
| 204 |
|
| 205 |
JS::CompileOptions options(cx); |
| 206 |
options.setFileAndLine("temporary file", 1); |
| 207 |
|
| 208 |
JS::RootedScript script(cx, JS::CompileUtf8File(cx, options, script_stream)); |
| 209 |
CHECK(script); |
| 210 |
|
| 211 |
return tryScript(script); |
| 212 |
} |
| 213 |
END_FIXTURE_TEST(ScriptObjectFixture, |
| 214 |
bug438633_JS_CompileFileHandleForPrincipals) |