Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsapi-tests / testOps.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  * Tests for operators and implicit type conversion.
5  */
6
7 #include "tests.h"
8
9 static JSBool
10 my_convert(JSContext* context, JSObject* obj, JSType type, jsval* rval)
11 {
12     if (type == JSTYPE_VOID || type == JSTYPE_STRING || type == JSTYPE_NUMBER || type == JSTYPE_BOOLEAN)
13         return JS_NewNumberValue(context, 123, rval);
14     return JS_FALSE;
15 }
16
17 static JSClass myClass = {
18     "MyClass",
19     0,
20     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
21     JS_EnumerateStub, JS_ResolveStub, my_convert, JS_FinalizeStub,
22     JSCLASS_NO_OPTIONAL_MEMBERS
23 };
24
25 static JSBool
26 createMyObject(JSContext* context, uintN argc, jsval *vp)
27 {
28     JS_BeginRequest(context);
29
30     //JS_GC(context); //<- if we make GC here, all is ok
31
32     JSObject* myObject = JS_NewObject(context, &myClass, NULL, NULL);
33     *vp = OBJECT_TO_JSVAL(myObject);
34
35     JS_EndRequest(context);
36
37     return JS_TRUE;
38 }
39
40 static JSFunctionSpec s_functions[] =
41 {
42     { "createMyObject", createMyObject, 0 },
43     { 0,0,0,0 }
44 };
45
46 BEGIN_TEST(testOps_bug559006)
47 {
48     CHECK(JS_DefineFunctions(cx, global, s_functions));
49
50     EXEC("function main() { while(1) return 0 + createMyObject(); }");
51
52     for (int i = 0; i < 9; i++) {
53         jsvalRoot rval(cx);
54         CHECK(JS_CallFunctionName(cx, global, "main", 0, NULL, rval.addr()));
55         CHECK_SAME(rval, INT_TO_JSVAL(123));
56     }
57     return true;
58 }
59 END_TEST(testOps_bug559006)
60