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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 44 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
45 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 45 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
46 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 46 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
47 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 47 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
48 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 48 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
49 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 49 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
50 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 50 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
51 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 51 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
52 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ | 52 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ |
53 "*/ " // 16 lines * 64 'X' =~ 1024 character comment. | 53 "*/ " // 16 lines * 64 'X' =~ 1024 character comment. |
54 #define FN_WARMUP(name, src) \ | 54 #define FN(name, src) "function " name "() { " src INLINE_SPOILER " }" |
55 "function " name "() { " src INLINE_SPOILER \ | 55 #define WARMUP(name, count) "for(i = 0; i < " count "; i++) { " name "() } " |
56 " }; for(i = 0; i < 2; i++) { " name "() } " | 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 } // anonymous namespace | 63 } // anonymous namespace |
64 | 64 |
65 | 65 |
66 // Build a simple "fast accessor" and verify that it is being called. | 66 // Build a simple "fast accessor" and verify that it is being called. |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 ExpectInt32("nonzero()", 1); | 279 ExpectInt32("nonzero()", 1); |
280 val.intval = 0; | 280 val.intval = 0; |
281 ExpectInt32("nonzero()", 0); | 281 ExpectInt32("nonzero()", 0); |
282 val.intval = 27; | 282 val.intval = 27; |
283 ExpectInt32("nonzero()", 1); | 283 ExpectInt32("nonzero()", 1); |
284 | 284 |
285 // Access val.v8val: | 285 // Access val.v8val: |
286 CompileRun(FN_WARMUP("loadval", "return obj.loadval")); | 286 CompileRun(FN_WARMUP("loadval", "return obj.loadval")); |
287 ExpectString("loadval()", "Hello"); | 287 ExpectString("loadval()", "Hello"); |
288 } | 288 } |
| 289 |
| 290 |
| 291 void ApiCallbackInt(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 292 info.GetReturnValue().Set(12345); |
| 293 } |
| 294 |
| 295 const char* kApiCallbackStringValue = |
| 296 "Hello World! Bizarro C++ world, actually."; |
| 297 void ApiCallbackString(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 298 info.GetReturnValue().Set(v8_str(kApiCallbackStringValue)); |
| 299 } |
| 300 |
| 301 |
| 302 void ApiCallbackParam(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 303 CHECK_EQ(1, info.Length()); |
| 304 CHECK(info[0]->IsNumber()); |
| 305 info.GetReturnValue().Set(info[0]); |
| 306 } |
| 307 |
| 308 |
| 309 // "Fast" accessor, callback to embedder |
| 310 TEST(FastAccessorCallback) { |
| 311 LocalContext env; |
| 312 v8::Isolate* isolate = env->GetIsolate(); |
| 313 v8::HandleScope scope(isolate); |
| 314 |
| 315 v8::Local<v8::ObjectTemplate> foo = v8::ObjectTemplate::New(isolate); |
| 316 { |
| 317 auto builder = v8::experimental::FastAccessorBuilder::New(isolate); |
| 318 builder->ReturnValue( |
| 319 builder->Call(&ApiCallbackInt, builder->IntegerConstant(999))); |
| 320 foo->SetAccessorProperty(v8_str("int"), |
| 321 v8::FunctionTemplate::NewWithFastHandler( |
| 322 isolate, NativePropertyAccessor, builder)); |
| 323 |
| 324 builder = v8::experimental::FastAccessorBuilder::New(isolate); |
| 325 builder->ReturnValue( |
| 326 builder->Call(&ApiCallbackString, builder->IntegerConstant(0))); |
| 327 foo->SetAccessorProperty(v8_str("str"), |
| 328 v8::FunctionTemplate::NewWithFastHandler( |
| 329 isolate, NativePropertyAccessor, builder)); |
| 330 |
| 331 builder = v8::experimental::FastAccessorBuilder::New(isolate); |
| 332 builder->ReturnValue( |
| 333 builder->Call(&ApiCallbackParam, builder->IntegerConstant(1000))); |
| 334 foo->SetAccessorProperty(v8_str("param"), |
| 335 v8::FunctionTemplate::NewWithFastHandler( |
| 336 isolate, NativePropertyAccessor, builder)); |
| 337 } |
| 338 |
| 339 // Create an instance. |
| 340 v8::Local<v8::Object> obj = foo->NewInstance(env.local()).ToLocalChecked(); |
| 341 CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust()); |
| 342 |
| 343 // Callbacks: |
| 344 CompileRun(FN_WARMUP("callbackint", "return obj.int")); |
| 345 ExpectInt32("callbackint()", 12345); |
| 346 |
| 347 CompileRun(FN_WARMUP("callbackstr", "return obj.str")); |
| 348 ExpectString("callbackstr()", kApiCallbackStringValue); |
| 349 |
| 350 CompileRun(FN_WARMUP("callbackparam", "return obj.param")); |
| 351 ExpectInt32("callbackparam()", 1000); |
| 352 } |
OLD | NEW |