1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99:
11 constructHook(JSContext *cx, uintN argc, jsval *vp)
13 // Check that arguments were passed properly from JS_New.
14 JSObject *callee = JSVAL_TO_OBJECT(JS_CALLEE(cx, vp));
16 JSObject *obj = JS_NewObjectForConstructor(cx, vp);
18 JS_ReportError(cx, "test failed, could not construct object");
21 if (strcmp(JS_GET_CLASS(cx, obj)->name, "Object") != 0) {
22 JS_ReportError(cx, "test failed, wrong class for 'this'");
26 JS_ReportError(cx, "test failed, argc == %d", argc);
29 if (!JSVAL_IS_INT(argv[2]) || JSVAL_TO_INT(argv[2]) != 2) {
30 JS_ReportError(cx, "test failed, wrong value in argv[2]");
33 if (!JS_IsConstructing(cx, vp)) {
34 JS_ReportError(cx, "test failed, not constructing");
38 // Perform a side-effect to indicate that this hook was actually called.
39 if (!JS_SetElement(cx, callee, 0, &argv[0]))
42 *vp = OBJECT_TO_JSVAL(obj);
43 argv[0] = argv[1] = argv[2] = JSVAL_VOID; // trash the argv, perversely
47 BEGIN_TEST(testNewObject_1)
51 JSObject *Array = JSVAL_TO_OBJECT(v);
54 JSObject *obj = JS_New(cx, Array, 0, NULL);
56 jsvalRoot rt(cx, OBJECT_TO_JSVAL(obj));
57 CHECK(JS_IsArrayObject(cx, obj));
59 CHECK(JS_GetArrayLength(cx, obj, &len));
63 argv[0] = INT_TO_JSVAL(4);
64 obj = JS_New(cx, Array, 1, argv);
66 rt = OBJECT_TO_JSVAL(obj);
67 CHECK(JS_IsArrayObject(cx, obj));
68 CHECK(JS_GetArrayLength(cx, obj, &len));
72 for (size_t i = 0; i < N; i++)
73 argv[i] = INT_TO_JSVAL(i);
74 obj = JS_New(cx, Array, N, argv);
76 rt = OBJECT_TO_JSVAL(obj);
77 CHECK(JS_IsArrayObject(cx, obj));
78 CHECK(JS_GetArrayLength(cx, obj, &len));
80 CHECK(JS_GetElement(cx, obj, N - 1, &v));
81 CHECK_SAME(v, INT_TO_JSVAL(N - 1));
83 // With JSClass.construct.
84 static JSClass cls = {
87 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
88 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
89 NULL, NULL, NULL, constructHook, NULL, NULL, NULL, NULL
91 JSObject *ctor = JS_NewObject(cx, &cls, NULL, NULL);
93 jsvalRoot rt2(cx, OBJECT_TO_JSVAL(ctor));
94 obj = JS_New(cx, ctor, 3, argv);
96 CHECK(JS_GetElement(cx, ctor, 0, &v));
97 CHECK_SAME(v, JSVAL_ZERO);
100 END_TEST(testNewObject_1)