| 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 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "jsapi-tests/tests.h" |
| 8 |
|
| 9 |
#include "mozilla/Utf8.h" // mozilla::Utf8Unit |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
|
| 13 |
#include "js/ArrayBuffer.h" |
| 14 |
#include "js/CompilationAndEvaluation.h" // JS::Evaluate |
| 15 |
#include "js/GlobalObject.h" // JS_NewGlobalObject |
| 16 |
#include "js/Initialization.h" |
| 17 |
#include "js/Prefs.h" |
| 18 |
#include "js/PropertyAndElement.h" // JS_DefineFunction |
| 19 |
#include "js/RootingAPI.h" |
| 20 |
#include "js/SourceText.h" // JS::Source{Ownership,Text} |
| 21 |
|
| 22 |
JSAPIRuntimeTest* JSAPIRuntimeTest::list; |
| 23 |
JSAPIFrontendTest* JSAPIFrontendTest::list; |
| 24 |
|
| 25 |
bool JSAPIRuntimeTest::init(JSContext* maybeReusableContext) { |
| 26 |
if (maybeReusableContext && reuseGlobal) { |
| 27 |
cx = maybeReusableContext; |
| 28 |
global.init(cx, JS::CurrentGlobalOrNull(cx)); |
| 29 |
return init(); |
| 30 |
} |
| 31 |
|
| 32 |
MaybeFreeContext(maybeReusableContext); |
| 33 |
|
| 34 |
cx = createContext(); |
| 35 |
if (!cx) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
js::UseInternalJobQueues(cx); |
| 40 |
|
| 41 |
if (!JS::InitSelfHostedCode(cx)) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
global.init(cx); |
| 45 |
createGlobal(); |
| 46 |
if (!global) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
JS::EnterRealm(cx, global); |
| 50 |
return init(); |
| 51 |
} |
| 52 |
|
| 53 |
JSContext* JSAPIRuntimeTest::maybeForgetContext() { |
| 54 |
if (!reuseGlobal) { |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
|
| 58 |
JSContext* reusableCx = cx; |
| 59 |
global.reset(); |
| 60 |
cx = nullptr; |
| 61 |
return reusableCx; |
| 62 |
} |
| 63 |
|
| 64 |
/* static */ |
| 65 |
void JSAPIRuntimeTest::MaybeFreeContext(JSContext* maybeCx) { |
| 66 |
if (maybeCx) { |
| 67 |
JS::LeaveRealm(maybeCx, nullptr); |
| 68 |
JS_DestroyContext(maybeCx); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void JSAPIRuntimeTest::uninit() { |
| 73 |
global.reset(); |
| 74 |
MaybeFreeContext(cx); |
| 75 |
cx = nullptr; |
| 76 |
msgs.clear(); |
| 77 |
} |
| 78 |
|
| 79 |
bool JSAPIRuntimeTest::exec(const char* utf8, const char* filename, |
| 80 |
int lineno) { |
| 81 |
JS::CompileOptions opts(cx); |
| 82 |
opts.setFileAndLine(filename, lineno); |
| 83 |
|
| 84 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 85 |
JS::RootedValue v(cx); |
| 86 |
return (srcBuf.init(cx, utf8, strlen(utf8), JS::SourceOwnership::Borrowed) && |
| 87 |
JS::Evaluate(cx, opts, srcBuf, &v)) || |
| 88 |
fail(JSAPITestString(utf8), filename, lineno); |
| 89 |
} |
| 90 |
|
| 91 |
bool JSAPIRuntimeTest::execDontReport(const char* utf8, const char* filename, |
| 92 |
int lineno) { |
| 93 |
JS::CompileOptions opts(cx); |
| 94 |
opts.setFileAndLine(filename, lineno); |
| 95 |
|
| 96 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 97 |
JS::RootedValue v(cx); |
| 98 |
return srcBuf.init(cx, utf8, strlen(utf8), JS::SourceOwnership::Borrowed) && |
| 99 |
JS::Evaluate(cx, opts, srcBuf, &v); |
| 100 |
} |
| 101 |
|
| 102 |
bool JSAPIRuntimeTest::evaluate(const char* utf8, const char* filename, |
| 103 |
int lineno, JS::MutableHandleValue vp) { |
| 104 |
JS::CompileOptions opts(cx); |
| 105 |
opts.setFileAndLine(filename, lineno); |
| 106 |
|
| 107 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 108 |
return (srcBuf.init(cx, utf8, strlen(utf8), JS::SourceOwnership::Borrowed) && |
| 109 |
JS::Evaluate(cx, opts, srcBuf, vp)) || |
| 110 |
fail(JSAPITestString(utf8), filename, lineno); |
| 111 |
} |
| 112 |
|
| 113 |
bool JSAPIRuntimeTest::definePrint() { |
| 114 |
return JS_DefineFunction(cx, global, "print", (JSNative)print, 0, 0); |
| 115 |
} |
| 116 |
|
| 117 |
JSObject* JSAPIRuntimeTest::createGlobal(JSPrincipals* principals) { |
| 118 |
/* Create the global object. */ |
| 119 |
JS::RootedObject newGlobal(cx); |
| 120 |
JS::RealmOptions options; |
| 121 |
options.creationOptions().setSharedMemoryAndAtomicsEnabled(true); |
| 122 |
newGlobal = JS_NewGlobalObject(cx, getGlobalClass(), principals, |
| 123 |
JS::FireOnNewGlobalHook, options); |
| 124 |
if (!newGlobal) { |
| 125 |
return nullptr; |
| 126 |
} |
| 127 |
|
| 128 |
global = newGlobal; |
| 129 |
return newGlobal; |
| 130 |
} |
| 131 |
|
| 132 |
struct CommandOptions { |
| 133 |
bool list = false; |
| 134 |
bool frontendOnly = false; |
| 135 |
bool help = false; |
| 136 |
const char* filter = nullptr; |
| 137 |
}; |
| 138 |
|
| 139 |
void parseArgs(int argc, char* argv[], CommandOptions& options) { |
| 140 |
for (int i = 1; i < argc; i++) { |
| 141 |
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { |
| 142 |
options.help = true; |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
if (strcmp(argv[i], "--list") == 0) { |
| 147 |
options.list = true; |
| 148 |
continue; |
| 149 |
} |
| 150 |
|
| 151 |
if (strcmp(argv[i], "--frontend-only") == 0) { |
| 152 |
options.frontendOnly = true; |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
if (!options.filter) { |
| 157 |
options.filter = argv[i]; |
| 158 |
continue; |
| 159 |
} |
| 160 |
|
| 161 |
printf("error: Unrecognized option: %s\n", argv[i]); |
| 162 |
options.help = true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
template <typename TestT> |
| 167 |
void PrintTests(TestT* list) { |
| 168 |
for (TestT* test = list; test; test = test->next) { |
| 169 |
printf("%s\n", test->name()); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
template <typename TestT, typename InitF, typename RunF, typename BeforeUninitF> |
| 174 |
void RunTests(int& total, int& failures, CommandOptions& options, TestT* list, |
| 175 |
InitF init, RunF run, BeforeUninitF beforeUninit) { |
| 176 |
for (TestT* test = list; test; test = test->next) { |
| 177 |
const char* name = test->name(); |
| 178 |
if (options.filter && strstr(name, options.filter) == nullptr) { |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
total += 1; |
| 183 |
|
| 184 |
printf("%s\n", name); |
| 185 |
|
| 186 |
// Make sure the test name is printed before we enter the test that can |
| 187 |
// crash on failure. |
| 188 |
fflush(stdout); |
| 189 |
|
| 190 |
if (!init(test)) { |
| 191 |
printf("TEST-UNEXPECTED-FAIL | %s | Failed to initialize.\n", name); |
| 192 |
failures++; |
| 193 |
test->uninit(); |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
if (run(test)) { |
| 198 |
printf("TEST-PASS | %s | ok\n", name); |
| 199 |
} else { |
| 200 |
JSAPITestString messages = test->messages(); |
| 201 |
printf("%s | %s | %.*s\n", |
| 202 |
(test->knownFail ? "TEST-KNOWN-FAIL" : "TEST-UNEXPECTED-FAIL"), |
| 203 |
name, (int)messages.length(), messages.begin()); |
| 204 |
if (!test->knownFail) { |
| 205 |
failures++; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
beforeUninit(test); |
| 210 |
|
| 211 |
test->uninit(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
int main(int argc, char* argv[]) { |
| 216 |
int total = 0; |
| 217 |
int failures = 0; |
| 218 |
CommandOptions options; |
| 219 |
parseArgs(argc, argv, options); |
| 220 |
|
| 221 |
if (options.help) { |
| 222 |
printf("Usage: jsapi-tests [OPTIONS] [FILTER]\n"); |
| 223 |
printf("\n"); |
| 224 |
printf("Options:\n"); |
| 225 |
printf(" -h, --help Display this message\n"); |
| 226 |
printf(" --list List all tests\n"); |
| 227 |
printf( |
| 228 |
" --frontend-only Run tests for frontend-only APIs, with " |
| 229 |
"light-weight entry point\n"); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
// Override prefs for jsapi-tests. |
| 234 |
JS::Prefs::setAtStartup_experimental_weakrefs_expose_cleanupSome(true); |
| 235 |
#ifdef NIGHTLY_BUILD |
| 236 |
JS::Prefs::setAtStartup_experimental_symbols_as_weakmap_keys(true); |
| 237 |
#endif |
| 238 |
|
| 239 |
if (!options.frontendOnly) { |
| 240 |
if (!JS_Init()) { |
| 241 |
printf("TEST-UNEXPECTED-FAIL | jsapi-tests | JS_Init() failed.\n"); |
| 242 |
return 1; |
| 243 |
} |
| 244 |
} else { |
| 245 |
if (!JS_FrontendOnlyInit()) { |
| 246 |
printf("TEST-UNEXPECTED-FAIL | jsapi-tests | JS_Init() failed.\n"); |
| 247 |
return 1; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if (options.list) { |
| 252 |
PrintTests(JSAPIRuntimeTest::list); |
| 253 |
PrintTests(JSAPIFrontendTest::list); |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
// Reinitializing the global for every test is quite slow, due to having to |
| 258 |
// recompile all self-hosted builtins. Allow tests to opt-in to reusing the |
| 259 |
// global. |
| 260 |
JSContext* maybeReusedContext = nullptr; |
| 261 |
|
| 262 |
if (!options.frontendOnly) { |
| 263 |
RunTests( |
| 264 |
total, failures, options, JSAPIRuntimeTest::list, |
| 265 |
[&maybeReusedContext](JSAPIRuntimeTest* test) { |
| 266 |
return test->init(maybeReusedContext); |
| 267 |
}, |
| 268 |
[](JSAPIRuntimeTest* test) { return test->run(test->global); }, |
| 269 |
[&maybeReusedContext](JSAPIRuntimeTest* test) { |
| 270 |
// Return a non-nullptr pointer if the context & global can safely be |
| 271 |
// reused for the next test. |
| 272 |
maybeReusedContext = test->maybeForgetContext(); |
| 273 |
}); |
| 274 |
} |
| 275 |
RunTests( |
| 276 |
total, failures, options, JSAPIFrontendTest::list, |
| 277 |
[](JSAPIFrontendTest* test) { return test->init(); }, |
| 278 |
[](JSAPIFrontendTest* test) { return test->run(); }, |
| 279 |
[](JSAPIFrontendTest* test) {}); |
| 280 |
|
| 281 |
if (!options.frontendOnly) { |
| 282 |
JSAPIRuntimeTest::MaybeFreeContext(maybeReusedContext); |
| 283 |
|
| 284 |
MOZ_RELEASE_ASSERT(!JSRuntime::hasLiveRuntimes()); |
| 285 |
JS_ShutDown(); |
| 286 |
} else { |
| 287 |
JS_FrontendOnlyShutDown(); |
| 288 |
} |
| 289 |
|
| 290 |
if (failures) { |
| 291 |
printf("\n%d unexpected failure%s.\n", failures, |
| 292 |
(failures == 1 ? "" : "s")); |
| 293 |
return 1; |
| 294 |
} |
| 295 |
printf("\nPassed: ran %d tests.\n", total); |
| 296 |
return 0; |
| 297 |
} |