js/src/jsapi-tests/testNewObject.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsapi-tests/testNewObject.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + */
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#include "jsapi-tests/tests.h"
    1.12 +
    1.13 +static bool
    1.14 +constructHook(JSContext *cx, unsigned argc, jsval *vp)
    1.15 +{
    1.16 +    JS::CallArgs args = CallArgsFromVp(argc, vp);
    1.17 +
    1.18 +    // Check that arguments were passed properly from JS_New.
    1.19 +
    1.20 +    JS::RootedObject obj(cx, JS_NewObject(cx, js::Jsvalify(&JSObject::class_), JS::NullPtr(), JS::NullPtr()));
    1.21 +    if (!obj) {
    1.22 +        JS_ReportError(cx, "test failed, could not construct object");
    1.23 +        return false;
    1.24 +    }
    1.25 +    if (strcmp(JS_GetClass(obj)->name, "Object") != 0) {
    1.26 +        JS_ReportError(cx, "test failed, wrong class for 'this'");
    1.27 +        return false;
    1.28 +    }
    1.29 +    if (args.length() != 3) {
    1.30 +        JS_ReportError(cx, "test failed, argc == %d", args.length());
    1.31 +        return false;
    1.32 +    }
    1.33 +    if (!args[0].isInt32() || args[2].toInt32() != 2) {
    1.34 +        JS_ReportError(cx, "test failed, wrong value in args[2]");
    1.35 +        return false;
    1.36 +    }
    1.37 +    if (!args.isConstructing()) {
    1.38 +        JS_ReportError(cx, "test failed, not constructing");
    1.39 +        return false;
    1.40 +    }
    1.41 +
    1.42 +    // Perform a side-effect to indicate that this hook was actually called.
    1.43 +    JS::RootedValue value(cx, args[0]);
    1.44 +    JS::RootedObject callee(cx, &args.callee());
    1.45 +    if (!JS_SetElement(cx, callee, 0, value))
    1.46 +        return false;
    1.47 +
    1.48 +    args.rval().setObject(*obj);
    1.49 +
    1.50 +    // trash the argv, perversely
    1.51 +    args[0].setUndefined();
    1.52 +    args[1].setUndefined();
    1.53 +    args[2].setUndefined();
    1.54 +
    1.55 +    return true;
    1.56 +}
    1.57 +
    1.58 +BEGIN_TEST(testNewObject_1)
    1.59 +{
    1.60 +    static const size_t N = 1000;
    1.61 +    JS::AutoValueVector argv(cx);
    1.62 +    CHECK(argv.resize(N));
    1.63 +
    1.64 +    JS::RootedValue v(cx);
    1.65 +    EVAL("Array", &v);
    1.66 +    JS::RootedObject Array(cx, JSVAL_TO_OBJECT(v));
    1.67 +
    1.68 +    // With no arguments.
    1.69 +    JS::RootedObject obj(cx, JS_New(cx, Array, JS::HandleValueArray::empty()));
    1.70 +    CHECK(obj);
    1.71 +    JS::RootedValue rt(cx, JS::ObjectValue(*obj));
    1.72 +    CHECK(JS_IsArrayObject(cx, obj));
    1.73 +    uint32_t len;
    1.74 +    CHECK(JS_GetArrayLength(cx, obj, &len));
    1.75 +    CHECK_EQUAL(len, 0);
    1.76 +
    1.77 +    // With one argument.
    1.78 +    argv[0].setInt32(4);
    1.79 +    obj = JS_New(cx, Array, JS::HandleValueArray::subarray(argv, 0, 1));
    1.80 +    CHECK(obj);
    1.81 +    rt = OBJECT_TO_JSVAL(obj);
    1.82 +    CHECK(JS_IsArrayObject(cx, obj));
    1.83 +    CHECK(JS_GetArrayLength(cx, obj, &len));
    1.84 +    CHECK_EQUAL(len, 4);
    1.85 +
    1.86 +    // With N arguments.
    1.87 +    for (size_t i = 0; i < N; i++)
    1.88 +        argv[i].setInt32(i);
    1.89 +    obj = JS_New(cx, Array, JS::HandleValueArray::subarray(argv, 0, N));
    1.90 +    CHECK(obj);
    1.91 +    rt = OBJECT_TO_JSVAL(obj);
    1.92 +    CHECK(JS_IsArrayObject(cx, obj));
    1.93 +    CHECK(JS_GetArrayLength(cx, obj, &len));
    1.94 +    CHECK_EQUAL(len, N);
    1.95 +    CHECK(JS_GetElement(cx, obj, N - 1, &v));
    1.96 +    CHECK_SAME(v, INT_TO_JSVAL(N - 1));
    1.97 +
    1.98 +    // With JSClass.construct.
    1.99 +    static const JSClass cls = {
   1.100 +        "testNewObject_1",
   1.101 +        0,
   1.102 +        JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
   1.103 +        JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, nullptr,
   1.104 +        nullptr, nullptr, constructHook
   1.105 +    };
   1.106 +    JS::RootedObject ctor(cx, JS_NewObject(cx, &cls, JS::NullPtr(), JS::NullPtr()));
   1.107 +    CHECK(ctor);
   1.108 +    JS::RootedValue rt2(cx, OBJECT_TO_JSVAL(ctor));
   1.109 +    obj = JS_New(cx, ctor, JS::HandleValueArray::subarray(argv, 0, 3));
   1.110 +    CHECK(obj);
   1.111 +    CHECK(JS_GetElement(cx, ctor, 0, &v));
   1.112 +    CHECK_SAME(v, JSVAL_ZERO);
   1.113 +
   1.114 +    return true;
   1.115 +}
   1.116 +END_TEST(testNewObject_1)

mercurial