js/src/jsapi-tests/tests.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

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 #include "jsapi-tests/tests.h"
michael@0 8
michael@0 9 #include <stdio.h>
michael@0 10
michael@0 11 #include "js/RootingAPI.h"
michael@0 12
michael@0 13 JSAPITest *JSAPITest::list;
michael@0 14
michael@0 15 bool JSAPITest::init()
michael@0 16 {
michael@0 17 rt = createRuntime();
michael@0 18 if (!rt)
michael@0 19 return false;
michael@0 20 cx = createContext();
michael@0 21 if (!cx)
michael@0 22 return false;
michael@0 23 JS_BeginRequest(cx);
michael@0 24 JS::RootedObject global(cx, createGlobal());
michael@0 25 if (!global)
michael@0 26 return false;
michael@0 27 JS_EnterCompartment(cx, global);
michael@0 28 return true;
michael@0 29 }
michael@0 30
michael@0 31 bool JSAPITest::exec(const char *bytes, const char *filename, int lineno)
michael@0 32 {
michael@0 33 JS::RootedValue v(cx);
michael@0 34 JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet());
michael@0 35 return JS_EvaluateScript(cx, global, bytes, strlen(bytes), filename, lineno, &v) ||
michael@0 36 fail(bytes, filename, lineno);
michael@0 37 }
michael@0 38
michael@0 39 bool JSAPITest::evaluate(const char *bytes, const char *filename, int lineno,
michael@0 40 JS::MutableHandleValue vp)
michael@0 41 {
michael@0 42 JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet());
michael@0 43 return JS_EvaluateScript(cx, global, bytes, strlen(bytes), filename, lineno, vp) ||
michael@0 44 fail(bytes, filename, lineno);
michael@0 45 }
michael@0 46
michael@0 47 bool JSAPITest::definePrint()
michael@0 48 {
michael@0 49 JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet());
michael@0 50 return JS_DefineFunction(cx, global, "print", (JSNative) print, 0, 0);
michael@0 51 }
michael@0 52
michael@0 53 JSObject * JSAPITest::createGlobal(JSPrincipals *principals)
michael@0 54 {
michael@0 55 /* Create the global object. */
michael@0 56 JS::CompartmentOptions options;
michael@0 57 options.setVersion(JSVERSION_LATEST);
michael@0 58 global = JS_NewGlobalObject(cx, getGlobalClass(), principals, JS::FireOnNewGlobalHook, options);
michael@0 59 if (!global)
michael@0 60 return nullptr;
michael@0 61 JS::AddNamedObjectRoot(cx, &global, "test-global");
michael@0 62 JS::HandleObject globalHandle = JS::HandleObject::fromMarkedLocation(global.unsafeGet());
michael@0 63 JSAutoCompartment ac(cx, globalHandle);
michael@0 64
michael@0 65 /* Populate the global object with the standard globals, like Object and
michael@0 66 Array. */
michael@0 67 if (!JS_InitStandardClasses(cx, globalHandle))
michael@0 68 return nullptr;
michael@0 69 return global;
michael@0 70 }
michael@0 71
michael@0 72 int main(int argc, char *argv[])
michael@0 73 {
michael@0 74 int total = 0;
michael@0 75 int failures = 0;
michael@0 76 const char *filter = (argc == 2) ? argv[1] : nullptr;
michael@0 77
michael@0 78 if (!JS_Init()) {
michael@0 79 printf("TEST-UNEXPECTED-FAIL | jsapi-tests | JS_Init() failed.\n");
michael@0 80 return 1;
michael@0 81 }
michael@0 82
michael@0 83 for (JSAPITest *test = JSAPITest::list; test; test = test->next) {
michael@0 84 const char *name = test->name();
michael@0 85 if (filter && strstr(name, filter) == nullptr)
michael@0 86 continue;
michael@0 87
michael@0 88 total += 1;
michael@0 89
michael@0 90 printf("%s\n", name);
michael@0 91 if (!test->init()) {
michael@0 92 printf("TEST-UNEXPECTED-FAIL | %s | Failed to initialize.\n", name);
michael@0 93 failures++;
michael@0 94 continue;
michael@0 95 }
michael@0 96
michael@0 97 JS::HandleObject global = JS::HandleObject::fromMarkedLocation(test->global.unsafeGet());
michael@0 98 if (test->run(global)) {
michael@0 99 printf("TEST-PASS | %s | ok\n", name);
michael@0 100 } else {
michael@0 101 JSAPITestString messages = test->messages();
michael@0 102 printf("%s | %s | %.*s\n",
michael@0 103 (test->knownFail ? "TEST-KNOWN-FAIL" : "TEST-UNEXPECTED-FAIL"),
michael@0 104 name, (int) messages.length(), messages.begin());
michael@0 105 if (!test->knownFail)
michael@0 106 failures++;
michael@0 107 }
michael@0 108 test->uninit();
michael@0 109 }
michael@0 110
michael@0 111 JS_ShutDown();
michael@0 112
michael@0 113 if (failures) {
michael@0 114 printf("\n%d unexpected failure%s.\n", failures, (failures == 1 ? "" : "s"));
michael@0 115 return 1;
michael@0 116 }
michael@0 117 printf("\nPassed: ran %d tests.\n", total);
michael@0 118 return 0;
michael@0 119 }

mercurial