OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 | 76 |
77 static double Inc(Isolate* isolate, int x) { | 77 static double Inc(Isolate* isolate, int x) { |
78 const char* source = "result = %d + 1;"; | 78 const char* source = "result = %d + 1;"; |
79 EmbeddedVector<char, 512> buffer; | 79 EmbeddedVector<char, 512> buffer; |
80 OS::SNPrintF(buffer, source, x); | 80 OS::SNPrintF(buffer, source, x); |
81 | 81 |
82 Handle<JSFunction> fun = Compile(buffer.start()); | 82 Handle<JSFunction> fun = Compile(buffer.start()); |
83 if (fun.is_null()) return -1; | 83 if (fun.is_null()) return -1; |
84 | 84 |
85 bool has_pending_exception; | |
86 Handle<JSObject> global(isolate->context()->global_object()); | 85 Handle<JSObject> global(isolate->context()->global_object()); |
87 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 86 Execution::Call(isolate, fun, global, 0, NULL).Check(); |
88 CHECK(!has_pending_exception); | |
89 return GetGlobalProperty("result")->Number(); | 87 return GetGlobalProperty("result")->Number(); |
90 } | 88 } |
91 | 89 |
92 | 90 |
93 TEST(Inc) { | 91 TEST(Inc) { |
94 CcTest::InitializeVM(); | 92 CcTest::InitializeVM(); |
95 v8::HandleScope scope(CcTest::isolate()); | 93 v8::HandleScope scope(CcTest::isolate()); |
96 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3)); | 94 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3)); |
97 } | 95 } |
98 | 96 |
99 | 97 |
100 static double Add(Isolate* isolate, int x, int y) { | 98 static double Add(Isolate* isolate, int x, int y) { |
101 Handle<JSFunction> fun = Compile("result = x + y;"); | 99 Handle<JSFunction> fun = Compile("result = x + y;"); |
102 if (fun.is_null()) return -1; | 100 if (fun.is_null()) return -1; |
103 | 101 |
104 SetGlobalProperty("x", Smi::FromInt(x)); | 102 SetGlobalProperty("x", Smi::FromInt(x)); |
105 SetGlobalProperty("y", Smi::FromInt(y)); | 103 SetGlobalProperty("y", Smi::FromInt(y)); |
106 bool has_pending_exception; | |
107 Handle<JSObject> global(isolate->context()->global_object()); | 104 Handle<JSObject> global(isolate->context()->global_object()); |
108 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 105 Execution::Call(isolate, fun, global, 0, NULL).Check(); |
109 CHECK(!has_pending_exception); | |
110 return GetGlobalProperty("result")->Number(); | 106 return GetGlobalProperty("result")->Number(); |
111 } | 107 } |
112 | 108 |
113 | 109 |
114 TEST(Add) { | 110 TEST(Add) { |
115 CcTest::InitializeVM(); | 111 CcTest::InitializeVM(); |
116 v8::HandleScope scope(CcTest::isolate()); | 112 v8::HandleScope scope(CcTest::isolate()); |
117 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3)); | 113 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3)); |
118 } | 114 } |
119 | 115 |
120 | 116 |
121 static double Abs(Isolate* isolate, int x) { | 117 static double Abs(Isolate* isolate, int x) { |
122 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); | 118 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
123 if (fun.is_null()) return -1; | 119 if (fun.is_null()) return -1; |
124 | 120 |
125 SetGlobalProperty("x", Smi::FromInt(x)); | 121 SetGlobalProperty("x", Smi::FromInt(x)); |
126 bool has_pending_exception; | |
127 Handle<JSObject> global(isolate->context()->global_object()); | 122 Handle<JSObject> global(isolate->context()->global_object()); |
128 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 123 Execution::Call(isolate, fun, global, 0, NULL).Check(); |
129 CHECK(!has_pending_exception); | |
130 return GetGlobalProperty("result")->Number(); | 124 return GetGlobalProperty("result")->Number(); |
131 } | 125 } |
132 | 126 |
133 | 127 |
134 TEST(Abs) { | 128 TEST(Abs) { |
135 CcTest::InitializeVM(); | 129 CcTest::InitializeVM(); |
136 v8::HandleScope scope(CcTest::isolate()); | 130 v8::HandleScope scope(CcTest::isolate()); |
137 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3)); | 131 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3)); |
138 } | 132 } |
139 | 133 |
140 | 134 |
141 static double Sum(Isolate* isolate, int n) { | 135 static double Sum(Isolate* isolate, int n) { |
142 Handle<JSFunction> fun = | 136 Handle<JSFunction> fun = |
143 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); | 137 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
144 if (fun.is_null()) return -1; | 138 if (fun.is_null()) return -1; |
145 | 139 |
146 SetGlobalProperty("n", Smi::FromInt(n)); | 140 SetGlobalProperty("n", Smi::FromInt(n)); |
147 bool has_pending_exception; | |
148 Handle<JSObject> global(isolate->context()->global_object()); | 141 Handle<JSObject> global(isolate->context()->global_object()); |
149 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 142 Execution::Call(isolate, fun, global, 0, NULL).Check(); |
150 CHECK(!has_pending_exception); | |
151 return GetGlobalProperty("result")->Number(); | 143 return GetGlobalProperty("result")->Number(); |
152 } | 144 } |
153 | 145 |
154 | 146 |
155 TEST(Sum) { | 147 TEST(Sum) { |
156 CcTest::InitializeVM(); | 148 CcTest::InitializeVM(); |
157 v8::HandleScope scope(CcTest::isolate()); | 149 v8::HandleScope scope(CcTest::isolate()); |
158 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100)); | 150 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100)); |
159 } | 151 } |
160 | 152 |
161 | 153 |
162 TEST(Print) { | 154 TEST(Print) { |
163 v8::HandleScope scope(CcTest::isolate()); | 155 v8::HandleScope scope(CcTest::isolate()); |
164 v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION); | 156 v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION); |
165 v8::Context::Scope context_scope(context); | 157 v8::Context::Scope context_scope(context); |
166 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; | 158 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; |
167 Handle<JSFunction> fun = Compile(source); | 159 Handle<JSFunction> fun = Compile(source); |
168 if (fun.is_null()) return; | 160 if (fun.is_null()) return; |
169 bool has_pending_exception; | |
170 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); | 161 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); |
171 Execution::Call( | 162 Execution::Call(CcTest::i_isolate(), fun, global, 0, NULL).Check(); |
172 CcTest::i_isolate(), fun, global, 0, NULL, &has_pending_exception); | |
173 CHECK(!has_pending_exception); | |
174 } | 163 } |
175 | 164 |
176 | 165 |
177 // The following test method stems from my coding efforts today. It | 166 // The following test method stems from my coding efforts today. It |
178 // tests all the functionality I have added to the compiler today | 167 // tests all the functionality I have added to the compiler today |
179 TEST(Stuff) { | 168 TEST(Stuff) { |
180 CcTest::InitializeVM(); | 169 CcTest::InitializeVM(); |
181 v8::HandleScope scope(CcTest::isolate()); | 170 v8::HandleScope scope(CcTest::isolate()); |
182 const char* source = | 171 const char* source = |
183 "r = 0;\n" | 172 "r = 0;\n" |
184 "a = new Object;\n" | 173 "a = new Object;\n" |
185 "if (a == a) r+=1;\n" // 1 | 174 "if (a == a) r+=1;\n" // 1 |
186 "if (a != new Object()) r+=2;\n" // 2 | 175 "if (a != new Object()) r+=2;\n" // 2 |
187 "a.x = 42;\n" | 176 "a.x = 42;\n" |
188 "if (a.x == 42) r+=4;\n" // 4 | 177 "if (a.x == 42) r+=4;\n" // 4 |
189 "function foo() { var x = 87; return x; }\n" | 178 "function foo() { var x = 87; return x; }\n" |
190 "if (foo() == 87) r+=8;\n" // 8 | 179 "if (foo() == 87) r+=8;\n" // 8 |
191 "function bar() { var x; x = 99; return x; }\n" | 180 "function bar() { var x; x = 99; return x; }\n" |
192 "if (bar() == 99) r+=16;\n" // 16 | 181 "if (bar() == 99) r+=16;\n" // 16 |
193 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n" | 182 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n" |
194 "if (baz() == 6) r+=32;\n" // 32 | 183 "if (baz() == 6) r+=32;\n" // 32 |
195 "function Cons0() { this.x = 42; this.y = 87; }\n" | 184 "function Cons0() { this.x = 42; this.y = 87; }\n" |
196 "if (new Cons0().x == 42) r+=64;\n" // 64 | 185 "if (new Cons0().x == 42) r+=64;\n" // 64 |
197 "if (new Cons0().y == 87) r+=128;\n" // 128 | 186 "if (new Cons0().y == 87) r+=128;\n" // 128 |
198 "function Cons2(x, y) { this.sum = x + y; }\n" | 187 "function Cons2(x, y) { this.sum = x + y; }\n" |
199 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 | 188 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
200 | 189 |
201 Handle<JSFunction> fun = Compile(source); | 190 Handle<JSFunction> fun = Compile(source); |
202 CHECK(!fun.is_null()); | 191 CHECK(!fun.is_null()); |
203 bool has_pending_exception; | |
204 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); | 192 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object()); |
205 Execution::Call( | 193 Execution::Call( |
206 CcTest::i_isolate(), fun, global, 0, NULL, &has_pending_exception); | 194 CcTest::i_isolate(), fun, global, 0, NULL).Check(); |
207 CHECK(!has_pending_exception); | |
208 CHECK_EQ(511.0, GetGlobalProperty("r")->Number()); | 195 CHECK_EQ(511.0, GetGlobalProperty("r")->Number()); |
209 } | 196 } |
210 | 197 |
211 | 198 |
212 TEST(UncaughtThrow) { | 199 TEST(UncaughtThrow) { |
213 CcTest::InitializeVM(); | 200 CcTest::InitializeVM(); |
214 v8::HandleScope scope(CcTest::isolate()); | 201 v8::HandleScope scope(CcTest::isolate()); |
215 | 202 |
216 const char* source = "throw 42;"; | 203 const char* source = "throw 42;"; |
217 Handle<JSFunction> fun = Compile(source); | 204 Handle<JSFunction> fun = Compile(source); |
218 CHECK(!fun.is_null()); | 205 CHECK(!fun.is_null()); |
219 bool has_pending_exception; | |
220 Isolate* isolate = fun->GetIsolate(); | 206 Isolate* isolate = fun->GetIsolate(); |
221 Handle<JSObject> global(isolate->context()->global_object()); | 207 Handle<JSObject> global(isolate->context()->global_object()); |
222 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | 208 CHECK(Execution::Call(isolate, fun, global, 0, NULL).is_null()); |
223 CHECK(has_pending_exception); | |
224 CHECK_EQ(42.0, isolate->pending_exception()->Number()); | 209 CHECK_EQ(42.0, isolate->pending_exception()->Number()); |
225 } | 210 } |
226 | 211 |
227 | 212 |
228 // Tests calling a builtin function from C/C++ code, and the builtin function | 213 // Tests calling a builtin function from C/C++ code, and the builtin function |
229 // performs GC. It creates a stack frame looks like following: | 214 // performs GC. It creates a stack frame looks like following: |
230 // | C (PerformGC) | | 215 // | C (PerformGC) | |
231 // | JS-to-C | | 216 // | JS-to-C | |
232 // | JS | | 217 // | JS | |
233 // | C-to-JS | | 218 // | C-to-JS | |
234 TEST(C2JSFrames) { | 219 TEST(C2JSFrames) { |
235 FLAG_expose_gc = true; | 220 FLAG_expose_gc = true; |
236 v8::HandleScope scope(CcTest::isolate()); | 221 v8::HandleScope scope(CcTest::isolate()); |
237 v8::Local<v8::Context> context = | 222 v8::Local<v8::Context> context = |
238 CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION); | 223 CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION); |
239 v8::Context::Scope context_scope(context); | 224 v8::Context::Scope context_scope(context); |
240 | 225 |
241 const char* source = "function foo(a) { gc(), print(a); }"; | 226 const char* source = "function foo(a) { gc(), print(a); }"; |
242 | 227 |
243 Handle<JSFunction> fun0 = Compile(source); | 228 Handle<JSFunction> fun0 = Compile(source); |
244 CHECK(!fun0.is_null()); | 229 CHECK(!fun0.is_null()); |
245 Isolate* isolate = fun0->GetIsolate(); | 230 Isolate* isolate = fun0->GetIsolate(); |
246 | 231 |
247 // Run the generated code to populate the global object with 'foo'. | 232 // Run the generated code to populate the global object with 'foo'. |
248 bool has_pending_exception; | |
249 Handle<JSObject> global(isolate->context()->global_object()); | 233 Handle<JSObject> global(isolate->context()->global_object()); |
250 Execution::Call( | 234 Execution::Call(isolate, fun0, global, 0, NULL).Check(); |
251 isolate, fun0, global, 0, NULL, &has_pending_exception); | |
252 CHECK(!has_pending_exception); | |
253 | 235 |
254 Handle<String> foo_string = isolate->factory()->InternalizeOneByteString( | 236 Handle<String> foo_string = isolate->factory()->InternalizeOneByteString( |
255 STATIC_ASCII_VECTOR("foo")); | 237 STATIC_ASCII_VECTOR("foo")); |
256 Handle<Object> fun1 = Object::GetProperty( | 238 Handle<Object> fun1 = Object::GetProperty( |
257 isolate->global_object(), foo_string); | 239 isolate->global_object(), foo_string); |
258 CHECK(fun1->IsJSFunction()); | 240 CHECK(fun1->IsJSFunction()); |
259 | 241 |
260 Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString( | 242 Handle<Object> argv[] = { isolate->factory()->InternalizeOneByteString( |
261 STATIC_ASCII_VECTOR("hello")) }; | 243 STATIC_ASCII_VECTOR("hello")) }; |
262 Execution::Call(isolate, | 244 Execution::Call(isolate, |
263 Handle<JSFunction>::cast(fun1), | 245 Handle<JSFunction>::cast(fun1), |
264 global, | 246 global, |
265 ARRAY_SIZE(argv), | 247 ARRAY_SIZE(argv), |
266 argv, | 248 argv).Check(); |
267 &has_pending_exception); | |
268 CHECK(!has_pending_exception); | |
269 } | 249 } |
270 | 250 |
271 | 251 |
272 // Regression 236. Calling InitLineEnds on a Script with undefined | 252 // Regression 236. Calling InitLineEnds on a Script with undefined |
273 // source resulted in crash. | 253 // source resulted in crash. |
274 TEST(Regression236) { | 254 TEST(Regression236) { |
275 CcTest::InitializeVM(); | 255 CcTest::InitializeVM(); |
276 Isolate* isolate = CcTest::i_isolate(); | 256 Isolate* isolate = CcTest::i_isolate(); |
277 Factory* factory = isolate->factory(); | 257 Factory* factory = isolate->factory(); |
278 v8::HandleScope scope(CcTest::isolate()); | 258 v8::HandleScope scope(CcTest::isolate()); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 CompileRun("function f() { a = 12345678 }; f();"); | 373 CompileRun("function f() { a = 12345678 }; f();"); |
394 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 374 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
395 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); | 375 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
396 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 376 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
397 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); | 377 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
398 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 378 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
399 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); | 379 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
400 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 380 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
401 } | 381 } |
402 #endif | 382 #endif |
OLD | NEW |