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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 } // anonymous namespace | 63 } // anonymous namespace |
64 | 64 |
65 void CheckImplicitParameters(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
66 v8::Isolate* isolate = info.GetIsolate(); | |
67 CHECK_NOT_NULL(isolate); | |
68 | |
69 auto context = isolate->GetCurrentContext(); | |
70 CHECK(!context.IsEmpty()); | |
71 | |
72 // The context must point to the same isolate, this should be enough to | |
73 // validate the context, mainly to prevent having a random object instead. | |
74 CHECK_EQ(isolate, context->GetIsolate()); | |
75 CHECK(info.Data()->IsUndefined()); | |
vogelheim
2016/07/19 12:46:10
Maybe we can check info.Holder()?
This should be
| |
76 } | |
65 | 77 |
66 // Build a simple "fast accessor" and verify that it is being called. | 78 // Build a simple "fast accessor" and verify that it is being called. |
67 TEST(FastAccessor) { | 79 TEST(FastAccessor) { |
68 LocalContext env; | 80 LocalContext env; |
69 v8::Isolate* isolate = env->GetIsolate(); | 81 v8::Isolate* isolate = env->GetIsolate(); |
70 v8::HandleScope scope(isolate); | 82 v8::HandleScope scope(isolate); |
71 | 83 |
72 v8::Local<v8::FunctionTemplate> foo = v8::FunctionTemplate::New(isolate); | 84 v8::Local<v8::FunctionTemplate> foo = v8::FunctionTemplate::New(isolate); |
73 | 85 |
74 // Native accessor, bar, returns 123. | 86 // Native accessor, bar, returns 123. |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 ExpectInt32("nonzero()", 0); | 305 ExpectInt32("nonzero()", 0); |
294 val.intval = 27; | 306 val.intval = 27; |
295 ExpectInt32("nonzero()", 1); | 307 ExpectInt32("nonzero()", 1); |
296 | 308 |
297 // Access val.v8val: | 309 // Access val.v8val: |
298 CompileRun(FN_WARMUP("loadval", "return obj.loadval")); | 310 CompileRun(FN_WARMUP("loadval", "return obj.loadval")); |
299 ExpectString("loadval()", "Hello"); | 311 ExpectString("loadval()", "Hello"); |
300 } | 312 } |
301 | 313 |
302 void ApiCallbackInt(const v8::FunctionCallbackInfo<v8::Value>& info) { | 314 void ApiCallbackInt(const v8::FunctionCallbackInfo<v8::Value>& info) { |
315 CheckImplicitParameters(info); | |
303 info.GetReturnValue().Set(12345); | 316 info.GetReturnValue().Set(12345); |
304 } | 317 } |
305 | 318 |
306 const char* kApiCallbackStringValue = | 319 const char* kApiCallbackStringValue = |
307 "Hello World! Bizarro C++ world, actually."; | 320 "Hello World! Bizarro C++ world, actually."; |
308 void ApiCallbackString(const v8::FunctionCallbackInfo<v8::Value>& info) { | 321 void ApiCallbackString(const v8::FunctionCallbackInfo<v8::Value>& info) { |
322 CheckImplicitParameters(info); | |
309 info.GetReturnValue().Set(v8_str(kApiCallbackStringValue)); | 323 info.GetReturnValue().Set(v8_str(kApiCallbackStringValue)); |
310 } | 324 } |
311 | 325 |
312 void ApiCallbackParam(const v8::FunctionCallbackInfo<v8::Value>& info) { | 326 void ApiCallbackParam(const v8::FunctionCallbackInfo<v8::Value>& info) { |
327 CheckImplicitParameters(info); | |
313 CHECK_EQ(1, info.Length()); | 328 CHECK_EQ(1, info.Length()); |
314 CHECK(info[0]->IsNumber()); | 329 CHECK(info[0]->IsNumber()); |
315 info.GetReturnValue().Set(info[0]); | 330 info.GetReturnValue().Set(info[0]); |
316 } | 331 } |
317 | 332 |
318 // "Fast" accessor, callback to embedder | 333 // "Fast" accessor, callback to embedder |
319 TEST(FastAccessorCallback) { | 334 TEST(FastAccessorCallback) { |
320 // Crankshaft support for fast accessors is not implemented; crankshafted | 335 // Crankshaft support for fast accessors is not implemented; crankshafted |
321 // code uses the slow accessor which breaks this test's expectations. | 336 // code uses the slow accessor which breaks this test's expectations. |
322 v8::internal::FLAG_always_opt = false; | 337 v8::internal::FLAG_always_opt = false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 // Callbacks: | 370 // Callbacks: |
356 CompileRun(FN_WARMUP("callbackint", "return obj.int")); | 371 CompileRun(FN_WARMUP("callbackint", "return obj.int")); |
357 ExpectInt32("callbackint()", 12345); | 372 ExpectInt32("callbackint()", 12345); |
358 | 373 |
359 CompileRun(FN_WARMUP("callbackstr", "return obj.str")); | 374 CompileRun(FN_WARMUP("callbackstr", "return obj.str")); |
360 ExpectString("callbackstr()", kApiCallbackStringValue); | 375 ExpectString("callbackstr()", kApiCallbackStringValue); |
361 | 376 |
362 CompileRun(FN_WARMUP("callbackparam", "return obj.param")); | 377 CompileRun(FN_WARMUP("callbackparam", "return obj.param")); |
363 ExpectInt32("callbackparam()", 1000); | 378 ExpectInt32("callbackparam()", 1000); |
364 } | 379 } |
OLD | NEW |