| 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 v8::ExtensionConfiguration config(2, extensions); | 94 v8::ExtensionConfiguration config(2, extensions); |
| 95 env = v8::Context::New(&config); | 95 env = v8::Context::New(&config); |
| 96 } | 96 } |
| 97 v8::HandleScope scope; | 97 v8::HandleScope scope; |
| 98 env->Enter(); | 98 env->Enter(); |
| 99 } | 99 } |
| 100 | 100 |
| 101 | 101 |
| 102 static MaybeObject* GetGlobalProperty(const char* name) { | 102 static MaybeObject* GetGlobalProperty(const char* name) { |
| 103 Handle<String> symbol = FACTORY->LookupAsciiSymbol(name); | 103 Handle<String> symbol = FACTORY->LookupAsciiSymbol(name); |
| 104 return Isolate::Current()->context()->global()->GetProperty(*symbol); | 104 return Isolate::Current()->context()->global_object()->GetProperty(*symbol); |
| 105 } | 105 } |
| 106 | 106 |
| 107 | 107 |
| 108 static void SetGlobalProperty(const char* name, Object* value) { | 108 static void SetGlobalProperty(const char* name, Object* value) { |
| 109 Handle<Object> object(value); | 109 Handle<Object> object(value); |
| 110 Handle<String> symbol = FACTORY->LookupAsciiSymbol(name); | 110 Handle<String> symbol = FACTORY->LookupAsciiSymbol(name); |
| 111 Handle<JSObject> global(Isolate::Current()->context()->global()); | 111 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 112 SetProperty(global, symbol, object, NONE, kNonStrictMode); | 112 SetProperty(global, symbol, object, NONE, kNonStrictMode); |
| 113 } | 113 } |
| 114 | 114 |
| 115 | 115 |
| 116 static Handle<JSFunction> Compile(const char* source) { | 116 static Handle<JSFunction> Compile(const char* source) { |
| 117 Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source))); | 117 Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source))); |
| 118 Handle<SharedFunctionInfo> shared_function = | 118 Handle<SharedFunctionInfo> shared_function = |
| 119 Compiler::Compile(source_code, | 119 Compiler::Compile(source_code, |
| 120 Handle<String>(), | 120 Handle<String>(), |
| 121 0, | 121 0, |
| 122 0, | 122 0, |
| 123 NULL, | 123 NULL, |
| 124 NULL, | 124 NULL, |
| 125 Handle<String>::null(), | 125 Handle<String>::null(), |
| 126 NOT_NATIVES_CODE); | 126 NOT_NATIVES_CODE); |
| 127 return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function, | 127 return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function, |
| 128 Isolate::Current()->native_context()); | 128 Isolate::Current()->native_context()); |
| 129 } | 129 } |
| 130 | 130 |
| 131 | 131 |
| 132 static double Inc(int x) { | 132 static double Inc(int x) { |
| 133 const char* source = "result = %d + 1;"; | 133 const char* source = "result = %d + 1;"; |
| 134 EmbeddedVector<char, 512> buffer; | 134 EmbeddedVector<char, 512> buffer; |
| 135 OS::SNPrintF(buffer, source, x); | 135 OS::SNPrintF(buffer, source, x); |
| 136 | 136 |
| 137 Handle<JSFunction> fun = Compile(buffer.start()); | 137 Handle<JSFunction> fun = Compile(buffer.start()); |
| 138 if (fun.is_null()) return -1; | 138 if (fun.is_null()) return -1; |
| 139 | 139 |
| 140 bool has_pending_exception; | 140 bool has_pending_exception; |
| 141 Handle<JSObject> global(Isolate::Current()->context()->global()); | 141 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 142 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 142 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 143 CHECK(!has_pending_exception); | 143 CHECK(!has_pending_exception); |
| 144 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 144 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 145 } | 145 } |
| 146 | 146 |
| 147 | 147 |
| 148 TEST(Inc) { | 148 TEST(Inc) { |
| 149 InitializeVM(); | 149 InitializeVM(); |
| 150 v8::HandleScope scope; | 150 v8::HandleScope scope; |
| 151 CHECK_EQ(4.0, Inc(3)); | 151 CHECK_EQ(4.0, Inc(3)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 | 154 |
| 155 static double Add(int x, int y) { | 155 static double Add(int x, int y) { |
| 156 Handle<JSFunction> fun = Compile("result = x + y;"); | 156 Handle<JSFunction> fun = Compile("result = x + y;"); |
| 157 if (fun.is_null()) return -1; | 157 if (fun.is_null()) return -1; |
| 158 | 158 |
| 159 SetGlobalProperty("x", Smi::FromInt(x)); | 159 SetGlobalProperty("x", Smi::FromInt(x)); |
| 160 SetGlobalProperty("y", Smi::FromInt(y)); | 160 SetGlobalProperty("y", Smi::FromInt(y)); |
| 161 bool has_pending_exception; | 161 bool has_pending_exception; |
| 162 Handle<JSObject> global(Isolate::Current()->context()->global()); | 162 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 163 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 163 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 164 CHECK(!has_pending_exception); | 164 CHECK(!has_pending_exception); |
| 165 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 165 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 166 } | 166 } |
| 167 | 167 |
| 168 | 168 |
| 169 TEST(Add) { | 169 TEST(Add) { |
| 170 InitializeVM(); | 170 InitializeVM(); |
| 171 v8::HandleScope scope; | 171 v8::HandleScope scope; |
| 172 CHECK_EQ(5.0, Add(2, 3)); | 172 CHECK_EQ(5.0, Add(2, 3)); |
| 173 } | 173 } |
| 174 | 174 |
| 175 | 175 |
| 176 static double Abs(int x) { | 176 static double Abs(int x) { |
| 177 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); | 177 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
| 178 if (fun.is_null()) return -1; | 178 if (fun.is_null()) return -1; |
| 179 | 179 |
| 180 SetGlobalProperty("x", Smi::FromInt(x)); | 180 SetGlobalProperty("x", Smi::FromInt(x)); |
| 181 bool has_pending_exception; | 181 bool has_pending_exception; |
| 182 Handle<JSObject> global(Isolate::Current()->context()->global()); | 182 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 183 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 183 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 184 CHECK(!has_pending_exception); | 184 CHECK(!has_pending_exception); |
| 185 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 185 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 | 188 |
| 189 TEST(Abs) { | 189 TEST(Abs) { |
| 190 InitializeVM(); | 190 InitializeVM(); |
| 191 v8::HandleScope scope; | 191 v8::HandleScope scope; |
| 192 CHECK_EQ(3.0, Abs(-3)); | 192 CHECK_EQ(3.0, Abs(-3)); |
| 193 } | 193 } |
| 194 | 194 |
| 195 | 195 |
| 196 static double Sum(int n) { | 196 static double Sum(int n) { |
| 197 Handle<JSFunction> fun = | 197 Handle<JSFunction> fun = |
| 198 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); | 198 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
| 199 if (fun.is_null()) return -1; | 199 if (fun.is_null()) return -1; |
| 200 | 200 |
| 201 SetGlobalProperty("n", Smi::FromInt(n)); | 201 SetGlobalProperty("n", Smi::FromInt(n)); |
| 202 bool has_pending_exception; | 202 bool has_pending_exception; |
| 203 Handle<JSObject> global(Isolate::Current()->context()->global()); | 203 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 204 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 204 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 205 CHECK(!has_pending_exception); | 205 CHECK(!has_pending_exception); |
| 206 return GetGlobalProperty("result")->ToObjectChecked()->Number(); | 206 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 207 } | 207 } |
| 208 | 208 |
| 209 | 209 |
| 210 TEST(Sum) { | 210 TEST(Sum) { |
| 211 InitializeVM(); | 211 InitializeVM(); |
| 212 v8::HandleScope scope; | 212 v8::HandleScope scope; |
| 213 CHECK_EQ(5050.0, Sum(100)); | 213 CHECK_EQ(5050.0, Sum(100)); |
| 214 } | 214 } |
| 215 | 215 |
| 216 | 216 |
| 217 TEST(Print) { | 217 TEST(Print) { |
| 218 InitializeVM(); | 218 InitializeVM(); |
| 219 v8::HandleScope scope; | 219 v8::HandleScope scope; |
| 220 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; | 220 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);"; |
| 221 Handle<JSFunction> fun = Compile(source); | 221 Handle<JSFunction> fun = Compile(source); |
| 222 if (fun.is_null()) return; | 222 if (fun.is_null()) return; |
| 223 bool has_pending_exception; | 223 bool has_pending_exception; |
| 224 Handle<JSObject> global(Isolate::Current()->context()->global()); | 224 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 225 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 225 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 226 CHECK(!has_pending_exception); | 226 CHECK(!has_pending_exception); |
| 227 } | 227 } |
| 228 | 228 |
| 229 | 229 |
| 230 // The following test method stems from my coding efforts today. It | 230 // The following test method stems from my coding efforts today. It |
| 231 // tests all the functionality I have added to the compiler today | 231 // tests all the functionality I have added to the compiler today |
| 232 TEST(Stuff) { | 232 TEST(Stuff) { |
| 233 InitializeVM(); | 233 InitializeVM(); |
| 234 v8::HandleScope scope; | 234 v8::HandleScope scope; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 247 "if (baz() == 6) r+=32;\n" // 32 | 247 "if (baz() == 6) r+=32;\n" // 32 |
| 248 "function Cons0() { this.x = 42; this.y = 87; }\n" | 248 "function Cons0() { this.x = 42; this.y = 87; }\n" |
| 249 "if (new Cons0().x == 42) r+=64;\n" // 64 | 249 "if (new Cons0().x == 42) r+=64;\n" // 64 |
| 250 "if (new Cons0().y == 87) r+=128;\n" // 128 | 250 "if (new Cons0().y == 87) r+=128;\n" // 128 |
| 251 "function Cons2(x, y) { this.sum = x + y; }\n" | 251 "function Cons2(x, y) { this.sum = x + y; }\n" |
| 252 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 | 252 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
| 253 | 253 |
| 254 Handle<JSFunction> fun = Compile(source); | 254 Handle<JSFunction> fun = Compile(source); |
| 255 CHECK(!fun.is_null()); | 255 CHECK(!fun.is_null()); |
| 256 bool has_pending_exception; | 256 bool has_pending_exception; |
| 257 Handle<JSObject> global(Isolate::Current()->context()->global()); | 257 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 258 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 258 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 259 CHECK(!has_pending_exception); | 259 CHECK(!has_pending_exception); |
| 260 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); | 260 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
| 261 } | 261 } |
| 262 | 262 |
| 263 | 263 |
| 264 TEST(UncaughtThrow) { | 264 TEST(UncaughtThrow) { |
| 265 InitializeVM(); | 265 InitializeVM(); |
| 266 v8::HandleScope scope; | 266 v8::HandleScope scope; |
| 267 | 267 |
| 268 const char* source = "throw 42;"; | 268 const char* source = "throw 42;"; |
| 269 Handle<JSFunction> fun = Compile(source); | 269 Handle<JSFunction> fun = Compile(source); |
| 270 CHECK(!fun.is_null()); | 270 CHECK(!fun.is_null()); |
| 271 bool has_pending_exception; | 271 bool has_pending_exception; |
| 272 Handle<JSObject> global(Isolate::Current()->context()->global()); | 272 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 273 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 273 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 274 CHECK(has_pending_exception); | 274 CHECK(has_pending_exception); |
| 275 CHECK_EQ(42.0, Isolate::Current()->pending_exception()-> | 275 CHECK_EQ(42.0, Isolate::Current()->pending_exception()-> |
| 276 ToObjectChecked()->Number()); | 276 ToObjectChecked()->Number()); |
| 277 } | 277 } |
| 278 | 278 |
| 279 | 279 |
| 280 // Tests calling a builtin function from C/C++ code, and the builtin function | 280 // Tests calling a builtin function from C/C++ code, and the builtin function |
| 281 // performs GC. It creates a stack frame looks like following: | 281 // performs GC. It creates a stack frame looks like following: |
| 282 // | C (PerformGC) | | 282 // | C (PerformGC) | |
| 283 // | JS-to-C | | 283 // | JS-to-C | |
| 284 // | JS | | 284 // | JS | |
| 285 // | C-to-JS | | 285 // | C-to-JS | |
| 286 TEST(C2JSFrames) { | 286 TEST(C2JSFrames) { |
| 287 InitializeVM(); | 287 InitializeVM(); |
| 288 v8::HandleScope scope; | 288 v8::HandleScope scope; |
| 289 | 289 |
| 290 const char* source = "function foo(a) { gc(), print(a); }"; | 290 const char* source = "function foo(a) { gc(), print(a); }"; |
| 291 | 291 |
| 292 Handle<JSFunction> fun0 = Compile(source); | 292 Handle<JSFunction> fun0 = Compile(source); |
| 293 CHECK(!fun0.is_null()); | 293 CHECK(!fun0.is_null()); |
| 294 | 294 |
| 295 // Run the generated code to populate the global object with 'foo'. | 295 // Run the generated code to populate the global object with 'foo'. |
| 296 bool has_pending_exception; | 296 bool has_pending_exception; |
| 297 Handle<JSObject> global(Isolate::Current()->context()->global()); | 297 Handle<JSObject> global(Isolate::Current()->context()->global_object()); |
| 298 Execution::Call(fun0, global, 0, NULL, &has_pending_exception); | 298 Execution::Call(fun0, global, 0, NULL, &has_pending_exception); |
| 299 CHECK(!has_pending_exception); | 299 CHECK(!has_pending_exception); |
| 300 | 300 |
| 301 Object* foo_symbol = FACTORY->LookupAsciiSymbol("foo")->ToObjectChecked(); | 301 Object* foo_symbol = FACTORY->LookupAsciiSymbol("foo")->ToObjectChecked(); |
| 302 MaybeObject* fun1_object = Isolate::Current()->context()->global()-> | 302 MaybeObject* fun1_object = Isolate::Current()->context()->global_object()-> |
| 303 GetProperty(String::cast(foo_symbol)); | 303 GetProperty(String::cast(foo_symbol)); |
| 304 Handle<Object> fun1(fun1_object->ToObjectChecked()); | 304 Handle<Object> fun1(fun1_object->ToObjectChecked()); |
| 305 CHECK(fun1->IsJSFunction()); | 305 CHECK(fun1->IsJSFunction()); |
| 306 | 306 |
| 307 Handle<Object> argv[] = { FACTORY->LookupAsciiSymbol("hello") }; | 307 Handle<Object> argv[] = { FACTORY->LookupAsciiSymbol("hello") }; |
| 308 Execution::Call(Handle<JSFunction>::cast(fun1), | 308 Execution::Call(Handle<JSFunction>::cast(fun1), |
| 309 global, | 309 global, |
| 310 ARRAY_SIZE(argv), | 310 ARRAY_SIZE(argv), |
| 311 argv, | 311 argv, |
| 312 &has_pending_exception); | 312 &has_pending_exception); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 CompileRun("function f() { a = 12345678 }; f();"); | 429 CompileRun("function f() { a = 12345678 }; f();"); |
| 430 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); | 430 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); |
| 431 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); | 431 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
| 432 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); | 432 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); |
| 433 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); | 433 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
| 434 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); | 434 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); |
| 435 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); | 435 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
| 436 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); | 436 CheckCodeForUnsafeLiteral(GetJSFunction(env->Global(), "f")); |
| 437 } | 437 } |
| 438 #endif | 438 #endif |
| OLD | NEW |