| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdlib.h> | 5 #include <stdlib.h> |
| 6 | 6 |
| 7 #include "include/v8.h" | 7 #include "include/v8.h" |
| 8 #include "include/v8-experimental.h" | 8 #include "include/v8-experimental.h" |
| 9 | 9 |
| 10 #include "src/api.h" | 10 #include "src/api.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 "*/ " // 16 lines * 64 'X' =~ 1024 character comment. | 53 "*/ " // 16 lines * 64 'X' =~ 1024 character comment. |
| 54 #define FN(name, src) "function " name "() { " src INLINE_SPOILER " }" | 54 #define FN(name, src) "function " name "() { " src INLINE_SPOILER " }" |
| 55 #define WARMUP(name, count) "for(i = 0; i < " count "; i++) { " name "() } " | 55 #define WARMUP(name, count) "for(i = 0; i < " count "; i++) { " name "() } " |
| 56 #define FN_WARMUP(name, src) FN(name, src) "; " WARMUP(name, "2") | 56 #define FN_WARMUP(name, src) FN(name, src) "; " WARMUP(name, "2") |
| 57 | 57 |
| 58 static void NativePropertyAccessor( | 58 static void NativePropertyAccessor( |
| 59 const v8::FunctionCallbackInfo<v8::Value>& info) { | 59 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 60 info.GetReturnValue().Set(v8_num(123)); | 60 info.GetReturnValue().Set(v8_num(123)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 const char* kWatermarkProperty = "watermark"; |
| 64 |
| 63 } // anonymous namespace | 65 } // anonymous namespace |
| 64 | 66 |
| 67 void CheckImplicitParameters(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 68 v8::Isolate* isolate = info.GetIsolate(); |
| 69 CHECK_NOT_NULL(isolate); |
| 70 |
| 71 auto context = isolate->GetCurrentContext(); |
| 72 CHECK(!context.IsEmpty()); |
| 73 |
| 74 // The context must point to the same isolate, this should be enough to |
| 75 // validate the context, mainly to prevent having a random object instead. |
| 76 CHECK_EQ(isolate, context->GetIsolate()); |
| 77 CHECK(info.Data()->IsUndefined()); |
| 78 |
| 79 CHECK(info.Holder()->Has(context, v8_str(kWatermarkProperty)).FromJust()); |
| 80 } |
| 65 | 81 |
| 66 // Build a simple "fast accessor" and verify that it is being called. | 82 // Build a simple "fast accessor" and verify that it is being called. |
| 67 TEST(FastAccessor) { | 83 TEST(FastAccessor) { |
| 68 LocalContext env; | 84 LocalContext env; |
| 69 v8::Isolate* isolate = env->GetIsolate(); | 85 v8::Isolate* isolate = env->GetIsolate(); |
| 70 v8::HandleScope scope(isolate); | 86 v8::HandleScope scope(isolate); |
| 71 | 87 |
| 72 v8::Local<v8::FunctionTemplate> foo = v8::FunctionTemplate::New(isolate); | 88 v8::Local<v8::FunctionTemplate> foo = v8::FunctionTemplate::New(isolate); |
| 73 | 89 |
| 74 // Native accessor, bar, returns 123. | 90 // Native accessor, bar, returns 123. |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 ExpectInt32("nonzero()", 0); | 309 ExpectInt32("nonzero()", 0); |
| 294 val.intval = 27; | 310 val.intval = 27; |
| 295 ExpectInt32("nonzero()", 1); | 311 ExpectInt32("nonzero()", 1); |
| 296 | 312 |
| 297 // Access val.v8val: | 313 // Access val.v8val: |
| 298 CompileRun(FN_WARMUP("loadval", "return obj.loadval")); | 314 CompileRun(FN_WARMUP("loadval", "return obj.loadval")); |
| 299 ExpectString("loadval()", "Hello"); | 315 ExpectString("loadval()", "Hello"); |
| 300 } | 316 } |
| 301 | 317 |
| 302 void ApiCallbackInt(const v8::FunctionCallbackInfo<v8::Value>& info) { | 318 void ApiCallbackInt(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 319 CheckImplicitParameters(info); |
| 303 info.GetReturnValue().Set(12345); | 320 info.GetReturnValue().Set(12345); |
| 304 } | 321 } |
| 305 | 322 |
| 306 const char* kApiCallbackStringValue = | 323 const char* kApiCallbackStringValue = |
| 307 "Hello World! Bizarro C++ world, actually."; | 324 "Hello World! Bizarro C++ world, actually."; |
| 308 void ApiCallbackString(const v8::FunctionCallbackInfo<v8::Value>& info) { | 325 void ApiCallbackString(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 326 CheckImplicitParameters(info); |
| 309 info.GetReturnValue().Set(v8_str(kApiCallbackStringValue)); | 327 info.GetReturnValue().Set(v8_str(kApiCallbackStringValue)); |
| 310 } | 328 } |
| 311 | 329 |
| 312 void ApiCallbackParam(const v8::FunctionCallbackInfo<v8::Value>& info) { | 330 void ApiCallbackParam(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 331 CheckImplicitParameters(info); |
| 313 CHECK_EQ(1, info.Length()); | 332 CHECK_EQ(1, info.Length()); |
| 314 CHECK(info[0]->IsNumber()); | 333 CHECK(info[0]->IsNumber()); |
| 315 info.GetReturnValue().Set(info[0]); | 334 info.GetReturnValue().Set(info[0]); |
| 316 } | 335 } |
| 317 | 336 |
| 318 // "Fast" accessor, callback to embedder | 337 // "Fast" accessor, callback to embedder |
| 319 TEST(FastAccessorCallback) { | 338 TEST(FastAccessorCallback) { |
| 320 // Crankshaft support for fast accessors is not implemented; crankshafted | 339 // Crankshaft support for fast accessors is not implemented; crankshafted |
| 321 // code uses the slow accessor which breaks this test's expectations. | 340 // code uses the slow accessor which breaks this test's expectations. |
| 322 v8::internal::FLAG_always_opt = false; | 341 v8::internal::FLAG_always_opt = false; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 341 isolate, NativePropertyAccessor, builder)); | 360 isolate, NativePropertyAccessor, builder)); |
| 342 | 361 |
| 343 builder = v8::experimental::FastAccessorBuilder::New(isolate); | 362 builder = v8::experimental::FastAccessorBuilder::New(isolate); |
| 344 builder->ReturnValue( | 363 builder->ReturnValue( |
| 345 builder->Call(&ApiCallbackParam, builder->IntegerConstant(1000))); | 364 builder->Call(&ApiCallbackParam, builder->IntegerConstant(1000))); |
| 346 foo->SetAccessorProperty(v8_str("param"), | 365 foo->SetAccessorProperty(v8_str("param"), |
| 347 v8::FunctionTemplate::NewWithFastHandler( | 366 v8::FunctionTemplate::NewWithFastHandler( |
| 348 isolate, NativePropertyAccessor, builder)); | 367 isolate, NativePropertyAccessor, builder)); |
| 349 } | 368 } |
| 350 | 369 |
| 370 // Add dummy property to validate the holder. |
| 371 foo->Set(isolate, kWatermarkProperty, v8::Undefined(isolate)); |
| 372 |
| 351 // Create an instance. | 373 // Create an instance. |
| 352 v8::Local<v8::Object> obj = foo->NewInstance(env.local()).ToLocalChecked(); | 374 v8::Local<v8::Object> obj = foo->NewInstance(env.local()).ToLocalChecked(); |
| 353 CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust()); | 375 CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust()); |
| 354 | 376 |
| 355 // Callbacks: | 377 // Callbacks: |
| 356 CompileRun(FN_WARMUP("callbackint", "return obj.int")); | 378 CompileRun(FN_WARMUP("callbackint", "return obj.int")); |
| 357 ExpectInt32("callbackint()", 12345); | 379 ExpectInt32("callbackint()", 12345); |
| 358 | 380 |
| 359 CompileRun(FN_WARMUP("callbackstr", "return obj.str")); | 381 CompileRun(FN_WARMUP("callbackstr", "return obj.str")); |
| 360 ExpectString("callbackstr()", kApiCallbackStringValue); | 382 ExpectString("callbackstr()", kApiCallbackStringValue); |
| 361 | 383 |
| 362 CompileRun(FN_WARMUP("callbackparam", "return obj.param")); | 384 CompileRun(FN_WARMUP("callbackparam", "return obj.param")); |
| 363 ExpectInt32("callbackparam()", 1000); | 385 ExpectInt32("callbackparam()", 1000); |
| 364 } | 386 } |
| OLD | NEW |