Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsapi-tests / testDebugger.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 #include "jsdbgapi.h"
7
8 static int callCount[2] = {0, 0};
9
10 static void *
11 callCountHook(JSContext *cx, JSStackFrame *fp, JSBool before, JSBool *ok, void *closure)
12 {
13     callCount[before]++;
14
15     jsval thisv;
16     JS_GetFrameThis(cx, fp, &thisv);  // assert if fp is incomplete
17
18     return cx;  // any non-null value causes the hook to be called again after
19 }
20
21 BEGIN_TEST(testDebugger_bug519719)
22 {
23     JS_SetCallHook(rt, callCountHook, NULL);
24     EXEC("function call(fn) { fn(0); }\n"
25          "function f(g) { for (var i = 0; i < 9; i++) call(g); }\n"
26          "f(Math.sin);\n"    // record loop, starting in f
27          "f(Math.cos);\n");  // side exit in f -> call
28     CHECK(callCount[0] == 20);
29     CHECK(callCount[1] == 20);
30     return true;
31 }
32 END_TEST(testDebugger_bug519719)
33
34 static void *
35 nonStrictThisHook(JSContext *cx, JSStackFrame *fp, JSBool before, JSBool *ok, void *closure)
36 {
37     if (before) {
38         bool *allWrapped = (bool *) closure;
39         jsval thisv;
40         JS_GetFrameThis(cx, fp, &thisv);
41         *allWrapped = *allWrapped && !JSVAL_IS_PRIMITIVE(thisv);
42     }
43     return NULL;
44 }
45
46 BEGIN_TEST(testDebugger_getThisNonStrict)
47 {
48     bool allWrapped = true;
49     JS_SetCallHook(rt, nonStrictThisHook, (void *) &allWrapped);
50     EXEC("function nonstrict() { }\n"
51          "Boolean.prototype.nonstrict = nonstrict;\n"
52          "String.prototype.nonstrict = nonstrict;\n"
53          "Number.prototype.nonstrict = nonstrict;\n"
54          "Object.prototype.nonstrict = nonstrict;\n"
55          "nonstrict.call(true);\n"
56          "true.nonstrict();\n"
57          "nonstrict.call('');\n"
58          "''.nonstrict();\n"
59          "nonstrict.call(42);\n"
60          "(42).nonstrict();\n"
61          // The below don't really get 'wrapped', but it's okay.
62          "nonstrict.call(undefined);\n"
63          "nonstrict.call(null);\n"
64          "nonstrict.call({});\n"
65          "({}).nonstrict();\n");
66     CHECK(allWrapped);
67     return true;
68 }
69 END_TEST(testDebugger_getThisNonStrict)
70
71 static void *
72 strictThisHook(JSContext *cx, JSStackFrame *fp, JSBool before, JSBool *ok, void *closure)
73 {
74     if (before) {
75         bool *anyWrapped = (bool *) closure;
76         jsval thisv;
77         JS_GetFrameThis(cx, fp, &thisv);
78         *anyWrapped = *anyWrapped || !JSVAL_IS_PRIMITIVE(thisv);
79     }
80     return NULL;
81 }
82
83 BEGIN_TEST(testDebugger_getThisStrict)
84 {
85     bool anyWrapped = false;
86     JS_SetCallHook(rt, strictThisHook, (void *) &anyWrapped);
87     EXEC("function strict() { 'use strict'; }\n"
88          "Boolean.prototype.strict = strict;\n"
89          "String.prototype.strict = strict;\n"
90          "Number.prototype.strict = strict;\n"
91          "strict.call(true);\n"
92          "true.strict();\n"
93          "strict.call('');\n"
94          "''.strict();\n"
95          "strict.call(42);\n"
96          "(42).strict();\n"
97          "strict.call(undefined);\n"
98          "strict.call(null);\n");
99     CHECK(!anyWrapped);
100     return true;
101 }
102 END_TEST(testDebugger_getThisStrict)