/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- * vim: set ts=8 sts=2 et sw=2 tw=80:
*/ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Check that arguments were passed properly from JS_New.
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); if (!obj) {
JS_ReportErrorASCII(cx, "test failed, could not construct object"); returnfalse;
} if (strcmp(JS::GetClass(obj)->name, "Object") != 0) {
JS_ReportErrorASCII(cx, "test failed, wrong class for 'this'"); returnfalse;
} if (args.length() != 3) {
JS_ReportErrorASCII(cx, "test failed, argc == %d", args.length()); returnfalse;
} if (!args[0].isInt32() || args[2].toInt32() != 2) {
JS_ReportErrorASCII(cx, "test failed, wrong value in args[2]"); returnfalse;
} if (!args.isConstructing()) {
JS_ReportErrorASCII(cx, "test failed, not constructing"); returnfalse;
}
// Perform a side-effect to indicate that this hook was actually called.
JS::RootedValue value(cx, args[0]);
JS::RootedObject callee(cx, &args.callee()); if (!JS_SetElement(cx, callee, 0, value)) { returnfalse;
}
args.rval().setObject(*obj);
// trash the argv, perversely
args[0].setUndefined();
args[1].setUndefined();
args[2].setUndefined();
returntrue;
}
BEGIN_TEST(testNewObject_1) { staticconst size_t N = 1000;
JS::RootedValueVector argv(cx);
CHECK(argv.resize(N));
// Calling Base without `new` should fail with a TypeError.
JS::RootedValue expectedError(cx);
EVAL("TypeError", &expectedError);
JS::RootedValue actualError(cx);
EVAL( "try {\n" " Base();\n" "} catch (e) {\n" " e.constructor;\n" "}\n",
&actualError);
CHECK_SAME(actualError, expectedError);
// Check prototype chains when a JS class extends a base class that's // implemented in C++ using JS_NewObjectForConstructor.
EXEC( "class MyClass extends Base {\n" " ok() { return true; }\n" "}\n" "let myObj = new MyClass();\n");
// All reserved slots are initialized to undefined. for (uint32_t i = 0; i < JSCLASS_RESERVED_SLOTS(&Base_class); i++) {
CHECK_SAME(JS::GetReservedSlot(&result.toObject(), i),
JS::UndefinedValue());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.