Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef jsapi_tests_tests_h |
michael@0 | 8 | #define jsapi_tests_tests_h |
michael@0 | 9 | |
michael@0 | 10 | #include <errno.h> |
michael@0 | 11 | #include <stdio.h> |
michael@0 | 12 | #include <stdlib.h> |
michael@0 | 13 | #include <string.h> |
michael@0 | 14 | |
michael@0 | 15 | #include "jsalloc.h" |
michael@0 | 16 | #include "jscntxt.h" |
michael@0 | 17 | #include "jsgc.h" |
michael@0 | 18 | |
michael@0 | 19 | #include "js/Vector.h" |
michael@0 | 20 | |
michael@0 | 21 | /* Note: Aborts on OOM. */ |
michael@0 | 22 | class JSAPITestString { |
michael@0 | 23 | js::Vector<char, 0, js::SystemAllocPolicy> chars; |
michael@0 | 24 | public: |
michael@0 | 25 | JSAPITestString() {} |
michael@0 | 26 | JSAPITestString(const char *s) { *this += s; } |
michael@0 | 27 | JSAPITestString(const JSAPITestString &s) { *this += s; } |
michael@0 | 28 | |
michael@0 | 29 | const char *begin() const { return chars.begin(); } |
michael@0 | 30 | const char *end() const { return chars.end(); } |
michael@0 | 31 | size_t length() const { return chars.length(); } |
michael@0 | 32 | |
michael@0 | 33 | JSAPITestString & operator +=(const char *s) { |
michael@0 | 34 | if (!chars.append(s, strlen(s))) |
michael@0 | 35 | abort(); |
michael@0 | 36 | return *this; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | JSAPITestString & operator +=(const JSAPITestString &s) { |
michael@0 | 40 | if (!chars.append(s.begin(), s.length())) |
michael@0 | 41 | abort(); |
michael@0 | 42 | return *this; |
michael@0 | 43 | } |
michael@0 | 44 | }; |
michael@0 | 45 | |
michael@0 | 46 | inline JSAPITestString operator+(JSAPITestString a, const char *b) { return a += b; } |
michael@0 | 47 | inline JSAPITestString operator+(JSAPITestString a, const JSAPITestString &b) { return a += b; } |
michael@0 | 48 | |
michael@0 | 49 | class JSAPITest |
michael@0 | 50 | { |
michael@0 | 51 | public: |
michael@0 | 52 | static JSAPITest *list; |
michael@0 | 53 | JSAPITest *next; |
michael@0 | 54 | |
michael@0 | 55 | JSRuntime *rt; |
michael@0 | 56 | JSContext *cx; |
michael@0 | 57 | JS::Heap<JSObject *> global; |
michael@0 | 58 | bool knownFail; |
michael@0 | 59 | JSAPITestString msgs; |
michael@0 | 60 | JSCompartment *oldCompartment; |
michael@0 | 61 | |
michael@0 | 62 | JSAPITest() : rt(nullptr), cx(nullptr), global(nullptr), |
michael@0 | 63 | knownFail(false), oldCompartment(nullptr) { |
michael@0 | 64 | next = list; |
michael@0 | 65 | list = this; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | virtual ~JSAPITest() { uninit(); } |
michael@0 | 69 | |
michael@0 | 70 | virtual bool init(); |
michael@0 | 71 | |
michael@0 | 72 | virtual void uninit() { |
michael@0 | 73 | if (oldCompartment) { |
michael@0 | 74 | JS_LeaveCompartment(cx, oldCompartment); |
michael@0 | 75 | oldCompartment = nullptr; |
michael@0 | 76 | } |
michael@0 | 77 | global = nullptr; |
michael@0 | 78 | if (cx) { |
michael@0 | 79 | JS::RemoveObjectRoot(cx, &global); |
michael@0 | 80 | JS_LeaveCompartment(cx, nullptr); |
michael@0 | 81 | JS_EndRequest(cx); |
michael@0 | 82 | JS_DestroyContext(cx); |
michael@0 | 83 | cx = nullptr; |
michael@0 | 84 | } |
michael@0 | 85 | if (rt) { |
michael@0 | 86 | destroyRuntime(); |
michael@0 | 87 | rt = nullptr; |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | virtual const char * name() = 0; |
michael@0 | 92 | virtual bool run(JS::HandleObject global) = 0; |
michael@0 | 93 | |
michael@0 | 94 | #define EXEC(s) do { if (!exec(s, __FILE__, __LINE__)) return false; } while (false) |
michael@0 | 95 | |
michael@0 | 96 | bool exec(const char *bytes, const char *filename, int lineno); |
michael@0 | 97 | |
michael@0 | 98 | #define EVAL(s, vp) do { if (!evaluate(s, __FILE__, __LINE__, vp)) return false; } while (false) |
michael@0 | 99 | |
michael@0 | 100 | bool evaluate(const char *bytes, const char *filename, int lineno, JS::MutableHandleValue vp); |
michael@0 | 101 | |
michael@0 | 102 | JSAPITestString jsvalToSource(JS::HandleValue v) { |
michael@0 | 103 | JSString *str = JS_ValueToSource(cx, v); |
michael@0 | 104 | if (str) { |
michael@0 | 105 | JSAutoByteString bytes(cx, str); |
michael@0 | 106 | if (!!bytes) |
michael@0 | 107 | return JSAPITestString(bytes.ptr()); |
michael@0 | 108 | } |
michael@0 | 109 | JS_ClearPendingException(cx); |
michael@0 | 110 | return JSAPITestString("<<error converting value to string>>"); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | JSAPITestString toSource(long v) { |
michael@0 | 114 | char buf[40]; |
michael@0 | 115 | sprintf(buf, "%ld", v); |
michael@0 | 116 | return JSAPITestString(buf); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | JSAPITestString toSource(unsigned long v) { |
michael@0 | 120 | char buf[40]; |
michael@0 | 121 | sprintf(buf, "%lu", v); |
michael@0 | 122 | return JSAPITestString(buf); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | JSAPITestString toSource(long long v) { |
michael@0 | 126 | char buf[40]; |
michael@0 | 127 | sprintf(buf, "%lld", v); |
michael@0 | 128 | return JSAPITestString(buf); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | JSAPITestString toSource(unsigned long long v) { |
michael@0 | 132 | char buf[40]; |
michael@0 | 133 | sprintf(buf, "%llu", v); |
michael@0 | 134 | return JSAPITestString(buf); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | JSAPITestString toSource(unsigned int v) { |
michael@0 | 138 | return toSource((unsigned long)v); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | JSAPITestString toSource(int v) { |
michael@0 | 142 | return toSource((long)v); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | JSAPITestString toSource(bool v) { |
michael@0 | 146 | return JSAPITestString(v ? "true" : "false"); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | JSAPITestString toSource(JSAtom *v) { |
michael@0 | 150 | JS::RootedValue val(cx, JS::StringValue((JSString *)v)); |
michael@0 | 151 | return jsvalToSource(val); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | JSAPITestString toSource(JSVersion v) { |
michael@0 | 155 | return JSAPITestString(JS_VersionToString(v)); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | template<typename T> |
michael@0 | 159 | bool checkEqual(const T &actual, const T &expected, |
michael@0 | 160 | const char *actualExpr, const char *expectedExpr, |
michael@0 | 161 | const char *filename, int lineno) { |
michael@0 | 162 | return (actual == expected) || |
michael@0 | 163 | fail(JSAPITestString("CHECK_EQUAL failed: expected (") + |
michael@0 | 164 | expectedExpr + ") = " + toSource(expected) + |
michael@0 | 165 | ", got (" + actualExpr + ") = " + toSource(actual), filename, lineno); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | // There are many cases where the static types of 'actual' and 'expected' |
michael@0 | 169 | // are not identical, and C++ is understandably cautious about automatic |
michael@0 | 170 | // coercions. So catch those cases and forcibly coerce, then use the |
michael@0 | 171 | // identical-type specialization. This may do bad things if the types are |
michael@0 | 172 | // actually *not* compatible. |
michael@0 | 173 | template<typename T, typename U> |
michael@0 | 174 | bool checkEqual(const T &actual, const U &expected, |
michael@0 | 175 | const char *actualExpr, const char *expectedExpr, |
michael@0 | 176 | const char *filename, int lineno) { |
michael@0 | 177 | return checkEqual(U(actual), expected, actualExpr, expectedExpr, filename, lineno); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | #define CHECK_EQUAL(actual, expected) \ |
michael@0 | 181 | do { \ |
michael@0 | 182 | if (!checkEqual(actual, expected, #actual, #expected, __FILE__, __LINE__)) \ |
michael@0 | 183 | return false; \ |
michael@0 | 184 | } while (false) |
michael@0 | 185 | |
michael@0 | 186 | bool checkSame(jsval actualArg, jsval expectedArg, |
michael@0 | 187 | const char *actualExpr, const char *expectedExpr, |
michael@0 | 188 | const char *filename, int lineno) { |
michael@0 | 189 | bool same; |
michael@0 | 190 | JS::RootedValue actual(cx, actualArg), expected(cx, expectedArg); |
michael@0 | 191 | return (JS_SameValue(cx, actual, expected, &same) && same) || |
michael@0 | 192 | fail(JSAPITestString("CHECK_SAME failed: expected JS_SameValue(cx, ") + |
michael@0 | 193 | actualExpr + ", " + expectedExpr + "), got !JS_SameValue(cx, " + |
michael@0 | 194 | jsvalToSource(actual) + ", " + jsvalToSource(expected) + ")", filename, lineno); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | #define CHECK_SAME(actual, expected) \ |
michael@0 | 198 | do { \ |
michael@0 | 199 | if (!checkSame(actual, expected, #actual, #expected, __FILE__, __LINE__)) \ |
michael@0 | 200 | return false; \ |
michael@0 | 201 | } while (false) |
michael@0 | 202 | |
michael@0 | 203 | #define CHECK(expr) \ |
michael@0 | 204 | do { \ |
michael@0 | 205 | if (!(expr)) \ |
michael@0 | 206 | return fail("CHECK failed: " #expr, __FILE__, __LINE__); \ |
michael@0 | 207 | } while (false) |
michael@0 | 208 | |
michael@0 | 209 | bool fail(JSAPITestString msg = JSAPITestString(), const char *filename = "-", int lineno = 0) { |
michael@0 | 210 | if (JS_IsExceptionPending(cx)) { |
michael@0 | 211 | js::gc::AutoSuppressGC gcoff(cx); |
michael@0 | 212 | JS::RootedValue v(cx); |
michael@0 | 213 | JS_GetPendingException(cx, &v); |
michael@0 | 214 | JS_ClearPendingException(cx); |
michael@0 | 215 | JSString *s = JS::ToString(cx, v); |
michael@0 | 216 | if (s) { |
michael@0 | 217 | JSAutoByteString bytes(cx, s); |
michael@0 | 218 | if (!!bytes) |
michael@0 | 219 | msg += bytes.ptr(); |
michael@0 | 220 | } |
michael@0 | 221 | } |
michael@0 | 222 | fprintf(stderr, "%s:%d:%.*s\n", filename, lineno, (int) msg.length(), msg.begin()); |
michael@0 | 223 | msgs += msg; |
michael@0 | 224 | return false; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | JSAPITestString messages() const { return msgs; } |
michael@0 | 228 | |
michael@0 | 229 | static const JSClass * basicGlobalClass() { |
michael@0 | 230 | static const JSClass c = { |
michael@0 | 231 | "global", JSCLASS_GLOBAL_FLAGS, |
michael@0 | 232 | JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, |
michael@0 | 233 | JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, nullptr, |
michael@0 | 234 | nullptr, nullptr, nullptr, |
michael@0 | 235 | JS_GlobalObjectTraceHook |
michael@0 | 236 | }; |
michael@0 | 237 | return &c; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | protected: |
michael@0 | 241 | static bool |
michael@0 | 242 | print(JSContext *cx, unsigned argc, jsval *vp) |
michael@0 | 243 | { |
michael@0 | 244 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
michael@0 | 245 | |
michael@0 | 246 | for (unsigned i = 0; i < args.length(); i++) { |
michael@0 | 247 | JSString *str = JS::ToString(cx, args[i]); |
michael@0 | 248 | if (!str) |
michael@0 | 249 | return false; |
michael@0 | 250 | char *bytes = JS_EncodeString(cx, str); |
michael@0 | 251 | if (!bytes) |
michael@0 | 252 | return false; |
michael@0 | 253 | printf("%s%s", i ? " " : "", bytes); |
michael@0 | 254 | JS_free(cx, bytes); |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | putchar('\n'); |
michael@0 | 258 | fflush(stdout); |
michael@0 | 259 | args.rval().setUndefined(); |
michael@0 | 260 | return true; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | bool definePrint(); |
michael@0 | 264 | |
michael@0 | 265 | static void setNativeStackQuota(JSRuntime *rt) |
michael@0 | 266 | { |
michael@0 | 267 | const size_t MAX_STACK_SIZE = |
michael@0 | 268 | /* Assume we can't use more than 5e5 bytes of C stack by default. */ |
michael@0 | 269 | #if (defined(DEBUG) && defined(__SUNPRO_CC)) || defined(JS_CPU_SPARC) |
michael@0 | 270 | /* |
michael@0 | 271 | * Sun compiler uses a larger stack space for js::Interpret() with |
michael@0 | 272 | * debug. Use a bigger gMaxStackSize to make "make check" happy. |
michael@0 | 273 | */ |
michael@0 | 274 | 5000000 |
michael@0 | 275 | #else |
michael@0 | 276 | 500000 |
michael@0 | 277 | #endif |
michael@0 | 278 | ; |
michael@0 | 279 | |
michael@0 | 280 | JS_SetNativeStackQuota(rt, MAX_STACK_SIZE); |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | virtual JSRuntime * createRuntime() { |
michael@0 | 284 | JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS); |
michael@0 | 285 | if (!rt) |
michael@0 | 286 | return nullptr; |
michael@0 | 287 | setNativeStackQuota(rt); |
michael@0 | 288 | return rt; |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | virtual void destroyRuntime() { |
michael@0 | 292 | JS_ASSERT(!cx); |
michael@0 | 293 | JS_ASSERT(rt); |
michael@0 | 294 | JS_DestroyRuntime(rt); |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | static void reportError(JSContext *cx, const char *message, JSErrorReport *report) { |
michael@0 | 298 | fprintf(stderr, "%s:%u:%s\n", |
michael@0 | 299 | report->filename ? report->filename : "<no filename>", |
michael@0 | 300 | (unsigned int) report->lineno, |
michael@0 | 301 | message); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | virtual JSContext * createContext() { |
michael@0 | 305 | JSContext *cx = JS_NewContext(rt, 8192); |
michael@0 | 306 | if (!cx) |
michael@0 | 307 | return nullptr; |
michael@0 | 308 | JS::ContextOptionsRef(cx).setVarObjFix(true); |
michael@0 | 309 | JS_SetErrorReporter(cx, &reportError); |
michael@0 | 310 | return cx; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | virtual const JSClass * getGlobalClass() { |
michael@0 | 314 | return basicGlobalClass(); |
michael@0 | 315 | } |
michael@0 | 316 | |
michael@0 | 317 | virtual JSObject * createGlobal(JSPrincipals *principals = nullptr); |
michael@0 | 318 | }; |
michael@0 | 319 | |
michael@0 | 320 | #define BEGIN_TEST(testname) \ |
michael@0 | 321 | class cls_##testname : public JSAPITest { \ |
michael@0 | 322 | public: \ |
michael@0 | 323 | virtual const char * name() { return #testname; } \ |
michael@0 | 324 | virtual bool run(JS::HandleObject global) |
michael@0 | 325 | |
michael@0 | 326 | #define END_TEST(testname) \ |
michael@0 | 327 | }; \ |
michael@0 | 328 | static cls_##testname cls_##testname##_instance; |
michael@0 | 329 | |
michael@0 | 330 | /* |
michael@0 | 331 | * A "fixture" is a subclass of JSAPITest that holds common definitions for a |
michael@0 | 332 | * set of tests. Each test that wants to use the fixture should use |
michael@0 | 333 | * BEGIN_FIXTURE_TEST and END_FIXTURE_TEST, just as one would use BEGIN_TEST and |
michael@0 | 334 | * END_TEST, but include the fixture class as the first argument. The fixture |
michael@0 | 335 | * class's declarations are then in scope for the test bodies. |
michael@0 | 336 | */ |
michael@0 | 337 | |
michael@0 | 338 | #define BEGIN_FIXTURE_TEST(fixture, testname) \ |
michael@0 | 339 | class cls_##testname : public fixture { \ |
michael@0 | 340 | public: \ |
michael@0 | 341 | virtual const char * name() { return #testname; } \ |
michael@0 | 342 | virtual bool run(JS::HandleObject global) |
michael@0 | 343 | |
michael@0 | 344 | #define END_FIXTURE_TEST(fixture, testname) \ |
michael@0 | 345 | }; \ |
michael@0 | 346 | static cls_##testname cls_##testname##_instance; |
michael@0 | 347 | |
michael@0 | 348 | /* |
michael@0 | 349 | * A class for creating and managing one temporary file. |
michael@0 | 350 | * |
michael@0 | 351 | * We could use the ISO C temporary file functions here, but those try to |
michael@0 | 352 | * create files in the root directory on Windows, which fails for users |
michael@0 | 353 | * without Administrator privileges. |
michael@0 | 354 | */ |
michael@0 | 355 | class TempFile { |
michael@0 | 356 | const char *name; |
michael@0 | 357 | FILE *stream; |
michael@0 | 358 | |
michael@0 | 359 | public: |
michael@0 | 360 | TempFile() : name(), stream() { } |
michael@0 | 361 | ~TempFile() { |
michael@0 | 362 | if (stream) |
michael@0 | 363 | close(); |
michael@0 | 364 | if (name) |
michael@0 | 365 | remove(); |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | /* |
michael@0 | 369 | * Return a stream for a temporary file named |fileName|. Infallible. |
michael@0 | 370 | * Use only once per TempFile instance. If the file is not explicitly |
michael@0 | 371 | * closed and deleted via the member functions below, this object's |
michael@0 | 372 | * destructor will clean them up. |
michael@0 | 373 | */ |
michael@0 | 374 | FILE *open(const char *fileName) |
michael@0 | 375 | { |
michael@0 | 376 | stream = fopen(fileName, "wb+"); |
michael@0 | 377 | if (!stream) { |
michael@0 | 378 | fprintf(stderr, "error opening temporary file '%s': %s\n", |
michael@0 | 379 | fileName, strerror(errno)); |
michael@0 | 380 | exit(1); |
michael@0 | 381 | } |
michael@0 | 382 | name = fileName; |
michael@0 | 383 | return stream; |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | /* Close the temporary file's stream. */ |
michael@0 | 387 | void close() { |
michael@0 | 388 | if (fclose(stream) == EOF) { |
michael@0 | 389 | fprintf(stderr, "error closing temporary file '%s': %s\n", |
michael@0 | 390 | name, strerror(errno)); |
michael@0 | 391 | exit(1); |
michael@0 | 392 | } |
michael@0 | 393 | stream = nullptr; |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | /* Delete the temporary file. */ |
michael@0 | 397 | void remove() { |
michael@0 | 398 | if (::remove(name) != 0) { |
michael@0 | 399 | fprintf(stderr, "error deleting temporary file '%s': %s\n", |
michael@0 | 400 | name, strerror(errno)); |
michael@0 | 401 | exit(1); |
michael@0 | 402 | } |
michael@0 | 403 | name = nullptr; |
michael@0 | 404 | } |
michael@0 | 405 | }; |
michael@0 | 406 | |
michael@0 | 407 | // Just a wrapper around JSPrincipals that allows static construction. |
michael@0 | 408 | class TestJSPrincipals : public JSPrincipals |
michael@0 | 409 | { |
michael@0 | 410 | public: |
michael@0 | 411 | TestJSPrincipals(int rc = 0) |
michael@0 | 412 | : JSPrincipals() |
michael@0 | 413 | { |
michael@0 | 414 | refcount = rc; |
michael@0 | 415 | } |
michael@0 | 416 | }; |
michael@0 | 417 | |
michael@0 | 418 | #endif /* jsapi_tests_tests_h */ |