Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsapi-tests / testNewObject.cpp
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sw=4 et tw=99:
3  */
4
5 #include "tests.h"
6
7 const size_t N = 1000;
8 static jsval argv[N];
9
10 static JSBool
11 constructHook(JSContext *cx, uintN argc, jsval *vp)
12 {
13     // Check that arguments were passed properly from JS_New.
14     JSObject *callee = JSVAL_TO_OBJECT(JS_CALLEE(cx, vp));
15
16     JSObject *obj = JS_NewObjectForConstructor(cx, vp);
17     if (!obj) {
18         JS_ReportError(cx, "test failed, could not construct object");
19         return false;
20     }
21     if (strcmp(JS_GET_CLASS(cx, obj)->name, "Object") != 0) {
22         JS_ReportError(cx, "test failed, wrong class for 'this'");
23         return false;
24     }
25     if (argc != 3) {
26         JS_ReportError(cx, "test failed, argc == %d", argc);
27         return false;
28     }
29     if (!JSVAL_IS_INT(argv[2]) || JSVAL_TO_INT(argv[2]) != 2) {
30         JS_ReportError(cx, "test failed, wrong value in argv[2]");
31         return false;
32     }
33     if (!JS_IsConstructing(cx, vp)) {
34         JS_ReportError(cx, "test failed, not constructing");
35         return false;
36     }
37
38     // Perform a side-effect to indicate that this hook was actually called.
39     if (!JS_SetElement(cx, callee, 0, &argv[0]))
40         return false;
41
42     *vp = OBJECT_TO_JSVAL(obj);
43     argv[0] = argv[1] = argv[2] = JSVAL_VOID;  // trash the argv, perversely
44     return true;
45 }
46
47 BEGIN_TEST(testNewObject_1)
48 {
49     jsval v;
50     EVAL("Array", &v);
51     JSObject *Array = JSVAL_TO_OBJECT(v);
52
53     // With no arguments.
54     JSObject *obj = JS_New(cx, Array, 0, NULL);
55     CHECK(obj);
56     jsvalRoot rt(cx, OBJECT_TO_JSVAL(obj));
57     CHECK(JS_IsArrayObject(cx, obj));
58     jsuint len;
59     CHECK(JS_GetArrayLength(cx, obj, &len));
60     CHECK(len == 0);
61
62     // With one argument.
63     argv[0] = INT_TO_JSVAL(4);
64     obj = JS_New(cx, Array, 1, argv);
65     CHECK(obj);
66     rt = OBJECT_TO_JSVAL(obj);
67     CHECK(JS_IsArrayObject(cx, obj));
68     CHECK(JS_GetArrayLength(cx, obj, &len));
69     CHECK(len == 4);
70
71     // With N arguments.
72     for (size_t i = 0; i < N; i++)
73         argv[i] = INT_TO_JSVAL(i);
74     obj = JS_New(cx, Array, N, argv);
75     CHECK(obj);
76     rt = OBJECT_TO_JSVAL(obj);
77     CHECK(JS_IsArrayObject(cx, obj));
78     CHECK(JS_GetArrayLength(cx, obj, &len));
79     CHECK(len == N);
80     CHECK(JS_GetElement(cx, obj, N - 1, &v));
81     CHECK_SAME(v, INT_TO_JSVAL(N - 1));
82
83     // With JSClass.construct.
84     static JSClass cls = {
85         "testNewObject_1",
86         0,
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
90     };
91     JSObject *ctor = JS_NewObject(cx, &cls, NULL, NULL);
92     CHECK(ctor);
93     jsvalRoot rt2(cx, OBJECT_TO_JSVAL(ctor));
94     obj = JS_New(cx, ctor, 3, argv);
95     CHECK(obj);
96     CHECK(JS_GetElement(cx, ctor, 0, &v));
97     CHECK_SAME(v, JSVAL_ZERO);
98     return true;
99 }
100 END_TEST(testNewObject_1)