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

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

mercurial