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 | */ |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #include "jsapi-tests/tests.h" |
michael@0 | 9 | |
michael@0 | 10 | static bool |
michael@0 | 11 | constructHook(JSContext *cx, unsigned argc, jsval *vp) |
michael@0 | 12 | { |
michael@0 | 13 | JS::CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 14 | |
michael@0 | 15 | // Check that arguments were passed properly from JS_New. |
michael@0 | 16 | |
michael@0 | 17 | JS::RootedObject obj(cx, JS_NewObject(cx, js::Jsvalify(&JSObject::class_), JS::NullPtr(), JS::NullPtr())); |
michael@0 | 18 | if (!obj) { |
michael@0 | 19 | JS_ReportError(cx, "test failed, could not construct object"); |
michael@0 | 20 | return false; |
michael@0 | 21 | } |
michael@0 | 22 | if (strcmp(JS_GetClass(obj)->name, "Object") != 0) { |
michael@0 | 23 | JS_ReportError(cx, "test failed, wrong class for 'this'"); |
michael@0 | 24 | return false; |
michael@0 | 25 | } |
michael@0 | 26 | if (args.length() != 3) { |
michael@0 | 27 | JS_ReportError(cx, "test failed, argc == %d", args.length()); |
michael@0 | 28 | return false; |
michael@0 | 29 | } |
michael@0 | 30 | if (!args[0].isInt32() || args[2].toInt32() != 2) { |
michael@0 | 31 | JS_ReportError(cx, "test failed, wrong value in args[2]"); |
michael@0 | 32 | return false; |
michael@0 | 33 | } |
michael@0 | 34 | if (!args.isConstructing()) { |
michael@0 | 35 | JS_ReportError(cx, "test failed, not constructing"); |
michael@0 | 36 | return false; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | // Perform a side-effect to indicate that this hook was actually called. |
michael@0 | 40 | JS::RootedValue value(cx, args[0]); |
michael@0 | 41 | JS::RootedObject callee(cx, &args.callee()); |
michael@0 | 42 | if (!JS_SetElement(cx, callee, 0, value)) |
michael@0 | 43 | return false; |
michael@0 | 44 | |
michael@0 | 45 | args.rval().setObject(*obj); |
michael@0 | 46 | |
michael@0 | 47 | // trash the argv, perversely |
michael@0 | 48 | args[0].setUndefined(); |
michael@0 | 49 | args[1].setUndefined(); |
michael@0 | 50 | args[2].setUndefined(); |
michael@0 | 51 | |
michael@0 | 52 | return true; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | BEGIN_TEST(testNewObject_1) |
michael@0 | 56 | { |
michael@0 | 57 | static const size_t N = 1000; |
michael@0 | 58 | JS::AutoValueVector argv(cx); |
michael@0 | 59 | CHECK(argv.resize(N)); |
michael@0 | 60 | |
michael@0 | 61 | JS::RootedValue v(cx); |
michael@0 | 62 | EVAL("Array", &v); |
michael@0 | 63 | JS::RootedObject Array(cx, JSVAL_TO_OBJECT(v)); |
michael@0 | 64 | |
michael@0 | 65 | // With no arguments. |
michael@0 | 66 | JS::RootedObject obj(cx, JS_New(cx, Array, JS::HandleValueArray::empty())); |
michael@0 | 67 | CHECK(obj); |
michael@0 | 68 | JS::RootedValue rt(cx, JS::ObjectValue(*obj)); |
michael@0 | 69 | CHECK(JS_IsArrayObject(cx, obj)); |
michael@0 | 70 | uint32_t len; |
michael@0 | 71 | CHECK(JS_GetArrayLength(cx, obj, &len)); |
michael@0 | 72 | CHECK_EQUAL(len, 0); |
michael@0 | 73 | |
michael@0 | 74 | // With one argument. |
michael@0 | 75 | argv[0].setInt32(4); |
michael@0 | 76 | obj = JS_New(cx, Array, JS::HandleValueArray::subarray(argv, 0, 1)); |
michael@0 | 77 | CHECK(obj); |
michael@0 | 78 | rt = OBJECT_TO_JSVAL(obj); |
michael@0 | 79 | CHECK(JS_IsArrayObject(cx, obj)); |
michael@0 | 80 | CHECK(JS_GetArrayLength(cx, obj, &len)); |
michael@0 | 81 | CHECK_EQUAL(len, 4); |
michael@0 | 82 | |
michael@0 | 83 | // With N arguments. |
michael@0 | 84 | for (size_t i = 0; i < N; i++) |
michael@0 | 85 | argv[i].setInt32(i); |
michael@0 | 86 | obj = JS_New(cx, Array, JS::HandleValueArray::subarray(argv, 0, N)); |
michael@0 | 87 | CHECK(obj); |
michael@0 | 88 | rt = OBJECT_TO_JSVAL(obj); |
michael@0 | 89 | CHECK(JS_IsArrayObject(cx, obj)); |
michael@0 | 90 | CHECK(JS_GetArrayLength(cx, obj, &len)); |
michael@0 | 91 | CHECK_EQUAL(len, N); |
michael@0 | 92 | CHECK(JS_GetElement(cx, obj, N - 1, &v)); |
michael@0 | 93 | CHECK_SAME(v, INT_TO_JSVAL(N - 1)); |
michael@0 | 94 | |
michael@0 | 95 | // With JSClass.construct. |
michael@0 | 96 | static const JSClass cls = { |
michael@0 | 97 | "testNewObject_1", |
michael@0 | 98 | 0, |
michael@0 | 99 | JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, |
michael@0 | 100 | JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, nullptr, |
michael@0 | 101 | nullptr, nullptr, constructHook |
michael@0 | 102 | }; |
michael@0 | 103 | JS::RootedObject ctor(cx, JS_NewObject(cx, &cls, JS::NullPtr(), JS::NullPtr())); |
michael@0 | 104 | CHECK(ctor); |
michael@0 | 105 | JS::RootedValue rt2(cx, OBJECT_TO_JSVAL(ctor)); |
michael@0 | 106 | obj = JS_New(cx, ctor, JS::HandleValueArray::subarray(argv, 0, 3)); |
michael@0 | 107 | CHECK(obj); |
michael@0 | 108 | CHECK(JS_GetElement(cx, ctor, 0, &v)); |
michael@0 | 109 | CHECK_SAME(v, JSVAL_ZERO); |
michael@0 | 110 | |
michael@0 | 111 | return true; |
michael@0 | 112 | } |
michael@0 | 113 | END_TEST(testNewObject_1) |