OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // TODO(jochen): Remove this after the setting is turned on globally. | |
6 #define V8_IMMINENT_DEPRECATION_WARNINGS | |
jochen (gone - plz use gerrit)
2015/11/27 08:33:45
yay :P
| |
7 | |
8 #include <stdlib.h> | |
9 | |
10 #include "include/v8.h" | |
11 | |
12 #include "src/api.h" | |
13 #include "test/cctest/cctest.h" | |
14 | |
15 namespace { | |
16 | |
17 static void NativePropertyAccessor( | |
18 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
19 info.GetReturnValue().Set(v8_num(123)); | |
20 } | |
21 | |
22 } // anonymous namespace | |
23 | |
24 | |
25 // Build a simple "fast accessor" and verify that it is being called. | |
26 TEST(FastAccessor) { | |
27 LocalContext env; | |
28 v8::Isolate* isolate = env->GetIsolate(); | |
29 v8::HandleScope scope(isolate); | |
30 | |
31 v8::Local<v8::FunctionTemplate> foo = v8::FunctionTemplate::New(isolate); | |
32 | |
33 // Native accessor, bar, returns 123. | |
34 foo->PrototypeTemplate()->SetAccessorProperty( | |
35 v8_str("bar"), | |
36 v8::FunctionTemplate::New(isolate, NativePropertyAccessor)); | |
37 | |
38 // Fast accessor, barf, returns 124. | |
39 auto fab = v8::experimental::FastAccessorBuilder::New(isolate); | |
40 fab->ReturnValue(fab->IntegerConstant(124)); | |
41 foo->PrototypeTemplate()->SetAccessorProperty( | |
42 v8_str("barf"), v8::FunctionTemplate::NewWithFastHandler( | |
43 isolate, NativePropertyAccessor, fab)); | |
44 | |
45 // Install foo on the global object. | |
46 CHECK(env->Global() | |
47 ->Set(env.local(), v8_str("foo"), | |
48 foo->GetFunction(env.local()).ToLocalChecked()) | |
49 .FromJust()); | |
50 | |
51 ExpectInt32("f = new foo(); f.bar", 123); | |
52 ExpectInt32("f = new foo(); f.barf", 123); | |
53 ExpectInt32("f = new foo(); f.barf", 123); | |
54 ExpectInt32("f = new foo(); f.barf", 123); | |
55 ExpectInt32("f = new foo(); f.barf", 124); | |
Michael Starzinger
2015/11/27 09:15:53
This test seems to assume that it takes exactly fo
epertoso
2015/11/27 10:04:56
+mvstanton.
mvstanton
2015/11/27 10:21:17
Good point. The code has the problem that it works
Michael Starzinger
2015/11/27 14:32:54
SGTM. I am fine with wrapping it in a function and
vogelheim
2015/11/27 16:24:50
Changed this (& below) to wrap the accessor, and t
| |
56 } | |
57 | |
58 | |
59 void AddInternalFieldAccessor(v8::Isolate* isolate, | |
60 v8::Local<v8::Template> templ, const char* name, | |
61 int field_no) { | |
62 auto builder = v8::experimental::FastAccessorBuilder::New(isolate); | |
63 builder->ReturnValue( | |
64 builder->GetInternalField(builder->GetCallTarget(), field_no)); | |
65 templ->SetAccessorProperty(v8_str(name), | |
66 v8::FunctionTemplate::NewWithFastHandler( | |
67 isolate, NativePropertyAccessor, builder)); | |
68 } | |
69 | |
70 | |
71 // "Fast" accessor that accesses an internal field. | |
72 TEST(FastAccessorWithInternalField) { | |
73 LocalContext env; | |
74 v8::Isolate* isolate = env->GetIsolate(); | |
75 v8::HandleScope scope(isolate); | |
76 | |
77 v8::Local<v8::ObjectTemplate> foo = v8::ObjectTemplate::New(isolate); | |
78 foo->SetInternalFieldCount(3); | |
79 AddInternalFieldAccessor(isolate, foo, "field0", 0); | |
80 AddInternalFieldAccessor(isolate, foo, "field1", 1); | |
81 AddInternalFieldAccessor(isolate, foo, "field2", 2); | |
82 | |
83 // Create an instance w/ 3 internal fields, put in a string, a Smi, nothing. | |
84 v8::Local<v8::Object> obj = foo->NewInstance(env.local()).ToLocalChecked(); | |
85 obj->SetInternalField(0, v8_str("Hi there!")); | |
86 obj->SetInternalField(1, v8::Integer::New(isolate, 4321)); | |
87 CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust()); | |
88 | |
89 // Ensure we go through accessors' fast paths, without checking anything. | |
90 CompileRun("obj.field0"); | |
91 CompileRun("obj.field0"); | |
92 CompileRun("obj.field0"); | |
93 CompileRun("obj.field1"); | |
94 CompileRun("obj.field1"); | |
95 CompileRun("obj.field1"); | |
96 CompileRun("obj.field2"); | |
97 CompileRun("obj.field2"); | |
98 CompileRun("obj.field2"); | |
99 | |
100 // Access fields. | |
101 ExpectString("obj.field0", "Hi there!"); | |
102 ExpectInt32("obj.field1", 4321); | |
103 ExpectUndefined("obj.field2"); | |
104 } | |
OLD | NEW |