| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 v8::HandleScope scope; | 91 v8::HandleScope scope; |
| 92 const char* extensions[] = { "v8/print", "v8/gc" }; | 92 const char* extensions[] = { "v8/print", "v8/gc" }; |
| 93 v8::ExtensionConfiguration config(2, extensions); | 93 v8::ExtensionConfiguration config(2, extensions); |
| 94 env = v8::Context::New(&config); | 94 env = v8::Context::New(&config); |
| 95 } | 95 } |
| 96 v8::HandleScope scope; | 96 v8::HandleScope scope; |
| 97 env->Enter(); | 97 env->Enter(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 | 100 |
| 101 static Object* GetGlobalProperty(const char* name) { | 101 static MaybeObject* GetGlobalProperty(const char* name) { |
| 102 Handle<String> symbol = Factory::LookupAsciiSymbol(name); | 102 Handle<String> symbol = Factory::LookupAsciiSymbol(name); |
| 103 return Top::context()->global()->GetProperty(*symbol); | 103 return Top::context()->global()->GetProperty(*symbol); |
| 104 } | 104 } |
| 105 | 105 |
| 106 | 106 |
| 107 static void SetGlobalProperty(const char* name, Object* value) { | 107 static void SetGlobalProperty(const char* name, Object* value) { |
| 108 Handle<Object> object(value); | 108 Handle<Object> object(value); |
| 109 Handle<String> symbol = Factory::LookupAsciiSymbol(name); | 109 Handle<String> symbol = Factory::LookupAsciiSymbol(name); |
| 110 Handle<JSObject> global(Top::context()->global()); | 110 Handle<JSObject> global(Top::context()->global()); |
| 111 SetProperty(global, symbol, object, NONE); | 111 SetProperty(global, symbol, object, NONE); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 133 EmbeddedVector<char, 512> buffer; | 133 EmbeddedVector<char, 512> buffer; |
| 134 OS::SNPrintF(buffer, source, x); | 134 OS::SNPrintF(buffer, source, x); |
| 135 | 135 |
| 136 Handle<JSFunction> fun = Compile(buffer.start()); | 136 Handle<JSFunction> fun = Compile(buffer.start()); |
| 137 if (fun.is_null()) return -1; | 137 if (fun.is_null()) return -1; |
| 138 | 138 |
| 139 bool has_pending_exception; | 139 bool has_pending_exception; |
| 140 Handle<JSObject> global(Top::context()->global()); | 140 Handle<JSObject> global(Top::context()->global()); |
| 141 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 141 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 142 CHECK(!has_pending_exception); | 142 CHECK(!has_pending_exception); |
| 143 return GetGlobalProperty("result")->Number(); | 143 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 144 } | 144 } |
| 145 | 145 |
| 146 | 146 |
| 147 TEST(Inc) { | 147 TEST(Inc) { |
| 148 InitializeVM(); | 148 InitializeVM(); |
| 149 v8::HandleScope scope; | 149 v8::HandleScope scope; |
| 150 CHECK_EQ(4.0, Inc(3)); | 150 CHECK_EQ(4.0, Inc(3)); |
| 151 } | 151 } |
| 152 | 152 |
| 153 | 153 |
| 154 static double Add(int x, int y) { | 154 static double Add(int x, int y) { |
| 155 Handle<JSFunction> fun = Compile("result = x + y;"); | 155 Handle<JSFunction> fun = Compile("result = x + y;"); |
| 156 if (fun.is_null()) return -1; | 156 if (fun.is_null()) return -1; |
| 157 | 157 |
| 158 SetGlobalProperty("x", Smi::FromInt(x)); | 158 SetGlobalProperty("x", Smi::FromInt(x)); |
| 159 SetGlobalProperty("y", Smi::FromInt(y)); | 159 SetGlobalProperty("y", Smi::FromInt(y)); |
| 160 bool has_pending_exception; | 160 bool has_pending_exception; |
| 161 Handle<JSObject> global(Top::context()->global()); | 161 Handle<JSObject> global(Top::context()->global()); |
| 162 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 162 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 163 CHECK(!has_pending_exception); | 163 CHECK(!has_pending_exception); |
| 164 return GetGlobalProperty("result")->Number(); | 164 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 165 } | 165 } |
| 166 | 166 |
| 167 | 167 |
| 168 TEST(Add) { | 168 TEST(Add) { |
| 169 InitializeVM(); | 169 InitializeVM(); |
| 170 v8::HandleScope scope; | 170 v8::HandleScope scope; |
| 171 CHECK_EQ(5.0, Add(2, 3)); | 171 CHECK_EQ(5.0, Add(2, 3)); |
| 172 } | 172 } |
| 173 | 173 |
| 174 | 174 |
| 175 static double Abs(int x) { | 175 static double Abs(int x) { |
| 176 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); | 176 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;"); |
| 177 if (fun.is_null()) return -1; | 177 if (fun.is_null()) return -1; |
| 178 | 178 |
| 179 SetGlobalProperty("x", Smi::FromInt(x)); | 179 SetGlobalProperty("x", Smi::FromInt(x)); |
| 180 bool has_pending_exception; | 180 bool has_pending_exception; |
| 181 Handle<JSObject> global(Top::context()->global()); | 181 Handle<JSObject> global(Top::context()->global()); |
| 182 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 182 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 183 CHECK(!has_pending_exception); | 183 CHECK(!has_pending_exception); |
| 184 return GetGlobalProperty("result")->Number(); | 184 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 185 } | 185 } |
| 186 | 186 |
| 187 | 187 |
| 188 TEST(Abs) { | 188 TEST(Abs) { |
| 189 InitializeVM(); | 189 InitializeVM(); |
| 190 v8::HandleScope scope; | 190 v8::HandleScope scope; |
| 191 CHECK_EQ(3.0, Abs(-3)); | 191 CHECK_EQ(3.0, Abs(-3)); |
| 192 } | 192 } |
| 193 | 193 |
| 194 | 194 |
| 195 static double Sum(int n) { | 195 static double Sum(int n) { |
| 196 Handle<JSFunction> fun = | 196 Handle<JSFunction> fun = |
| 197 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); | 197 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;"); |
| 198 if (fun.is_null()) return -1; | 198 if (fun.is_null()) return -1; |
| 199 | 199 |
| 200 SetGlobalProperty("n", Smi::FromInt(n)); | 200 SetGlobalProperty("n", Smi::FromInt(n)); |
| 201 bool has_pending_exception; | 201 bool has_pending_exception; |
| 202 Handle<JSObject> global(Top::context()->global()); | 202 Handle<JSObject> global(Top::context()->global()); |
| 203 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 203 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 204 CHECK(!has_pending_exception); | 204 CHECK(!has_pending_exception); |
| 205 return GetGlobalProperty("result")->Number(); | 205 return GetGlobalProperty("result")->ToObjectChecked()->Number(); |
| 206 } | 206 } |
| 207 | 207 |
| 208 | 208 |
| 209 TEST(Sum) { | 209 TEST(Sum) { |
| 210 InitializeVM(); | 210 InitializeVM(); |
| 211 v8::HandleScope scope; | 211 v8::HandleScope scope; |
| 212 CHECK_EQ(5050.0, Sum(100)); | 212 CHECK_EQ(5050.0, Sum(100)); |
| 213 } | 213 } |
| 214 | 214 |
| 215 | 215 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 "if (new Cons0().y == 87) r+=128;\n" // 128 | 249 "if (new Cons0().y == 87) r+=128;\n" // 128 |
| 250 "function Cons2(x, y) { this.sum = x + y; }\n" | 250 "function Cons2(x, y) { this.sum = x + y; }\n" |
| 251 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 | 251 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256 |
| 252 | 252 |
| 253 Handle<JSFunction> fun = Compile(source); | 253 Handle<JSFunction> fun = Compile(source); |
| 254 CHECK(!fun.is_null()); | 254 CHECK(!fun.is_null()); |
| 255 bool has_pending_exception; | 255 bool has_pending_exception; |
| 256 Handle<JSObject> global(Top::context()->global()); | 256 Handle<JSObject> global(Top::context()->global()); |
| 257 Execution::Call(fun, global, 0, NULL, &has_pending_exception); | 257 Execution::Call(fun, global, 0, NULL, &has_pending_exception); |
| 258 CHECK(!has_pending_exception); | 258 CHECK(!has_pending_exception); |
| 259 CHECK_EQ(511.0, GetGlobalProperty("r")->Number()); | 259 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number()); |
| 260 } | 260 } |
| 261 | 261 |
| 262 | 262 |
| 263 TEST(UncaughtThrow) { | 263 TEST(UncaughtThrow) { |
| 264 InitializeVM(); | 264 InitializeVM(); |
| 265 v8::HandleScope scope; | 265 v8::HandleScope scope; |
| 266 | 266 |
| 267 const char* source = "throw 42;"; | 267 const char* source = "throw 42;"; |
| 268 Handle<JSFunction> fun = Compile(source); | 268 Handle<JSFunction> fun = Compile(source); |
| 269 CHECK(!fun.is_null()); | 269 CHECK(!fun.is_null()); |
| 270 bool has_pending_exception; | 270 bool has_pending_exception; |
| 271 Handle<JSObject> global(Top::context()->global()); | 271 Handle<JSObject> global(Top::context()->global()); |
| 272 Handle<Object> result = | 272 Handle<Object> result = |
| 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, Top::pending_exception()->Number()); | 275 CHECK_EQ(42.0, Top::pending_exception()->ToObjectChecked()->Number()); |
| 276 } | 276 } |
| 277 | 277 |
| 278 | 278 |
| 279 // Tests calling a builtin function from C/C++ code, and the builtin function | 279 // Tests calling a builtin function from C/C++ code, and the builtin function |
| 280 // performs GC. It creates a stack frame looks like following: | 280 // performs GC. It creates a stack frame looks like following: |
| 281 // | C (PerformGC) | | 281 // | C (PerformGC) | |
| 282 // | JS-to-C | | 282 // | JS-to-C | |
| 283 // | JS | | 283 // | JS | |
| 284 // | C-to-JS | | 284 // | C-to-JS | |
| 285 TEST(C2JSFrames) { | 285 TEST(C2JSFrames) { |
| 286 InitializeVM(); | 286 InitializeVM(); |
| 287 v8::HandleScope scope; | 287 v8::HandleScope scope; |
| 288 | 288 |
| 289 const char* source = "function foo(a) { gc(), print(a); }"; | 289 const char* source = "function foo(a) { gc(), print(a); }"; |
| 290 | 290 |
| 291 Handle<JSFunction> fun0 = Compile(source); | 291 Handle<JSFunction> fun0 = Compile(source); |
| 292 CHECK(!fun0.is_null()); | 292 CHECK(!fun0.is_null()); |
| 293 | 293 |
| 294 // Run the generated code to populate the global object with 'foo'. | 294 // Run the generated code to populate the global object with 'foo'. |
| 295 bool has_pending_exception; | 295 bool has_pending_exception; |
| 296 Handle<JSObject> global(Top::context()->global()); | 296 Handle<JSObject> global(Top::context()->global()); |
| 297 Execution::Call(fun0, global, 0, NULL, &has_pending_exception); | 297 Execution::Call(fun0, global, 0, NULL, &has_pending_exception); |
| 298 CHECK(!has_pending_exception); | 298 CHECK(!has_pending_exception); |
| 299 | 299 |
| 300 Handle<Object> fun1 = | 300 Object* foo_symbol = Factory::LookupAsciiSymbol("foo")->ToObjectChecked(); |
| 301 Handle<Object>( | 301 MaybeObject* fun1_object = |
| 302 Top::context()->global()->GetProperty( | 302 Top::context()->global()->GetProperty(String::cast(foo_symbol)); |
| 303 *Factory::LookupAsciiSymbol("foo"))); | 303 Handle<Object> fun1(fun1_object->ToObjectChecked()); |
| 304 CHECK(fun1->IsJSFunction()); | 304 CHECK(fun1->IsJSFunction()); |
| 305 | 305 |
| 306 Object** argv[1] = { | 306 Object** argv[1] = { |
| 307 Handle<Object>::cast(Factory::LookupAsciiSymbol("hello")).location() | 307 Handle<Object>::cast(Factory::LookupAsciiSymbol("hello")).location() |
| 308 }; | 308 }; |
| 309 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv, | 309 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv, |
| 310 &has_pending_exception); | 310 &has_pending_exception); |
| 311 CHECK(!has_pending_exception); | 311 CHECK(!has_pending_exception); |
| 312 } | 312 } |
| 313 | 313 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 341 if (i > 0) | 341 if (i > 0) |
| 342 buffer[i - 1] = '\n'; | 342 buffer[i - 1] = '\n'; |
| 343 memcpy(&buffer[i], function_f, sizeof(function_f) - 1); | 343 memcpy(&buffer[i], function_f, sizeof(function_f) - 1); |
| 344 v8::Handle<v8::String> script_body = v8::String::New(buffer.start()); | 344 v8::Handle<v8::String> script_body = v8::String::New(buffer.start()); |
| 345 v8::Script::Compile(script_body, &origin)->Run(); | 345 v8::Script::Compile(script_body, &origin)->Run(); |
| 346 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( | 346 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 347 env->Global()->Get(v8::String::New("f"))); | 347 env->Global()->Get(v8::String::New("f"))); |
| 348 CHECK_EQ(i, f->GetScriptLineNumber()); | 348 CHECK_EQ(i, f->GetScriptLineNumber()); |
| 349 } | 349 } |
| 350 } | 350 } |
| OLD | NEW |