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 // TODO(jochen): Remove this after the setting is turned on globally. | 5 // TODO(jochen): Remove this after the setting is turned on globally. |
6 #define V8_IMMINENT_DEPRECATION_WARNINGS | 6 #define V8_IMMINENT_DEPRECATION_WARNINGS |
7 | 7 |
8 #include "test/cctest/cctest.h" | 8 #include "test/cctest/cctest.h" |
9 | 9 |
10 #include "include/v8.h" | 10 #include "include/v8.h" |
| 11 #include "src/compiler/pipeline.h" |
| 12 #include "src/compiler/raw-machine-assembler.h" |
11 | 13 |
12 | 14 |
13 #ifdef V8_JS_ACCESSORS | 15 namespace i = v8::internal; |
| 16 |
14 static void CppAccessor(const v8::FunctionCallbackInfo<v8::Value>& info) { | 17 static void CppAccessor(const v8::FunctionCallbackInfo<v8::Value>& info) { |
15 info.GetReturnValue().Set(42); | 18 info.GetReturnValue().Set(42); |
16 } | 19 } |
17 | 20 |
18 static const char* JsAccessor = | 21 |
19 "function firstChildJS(value) { return 41; }; firstChildJS"; | 22 v8::Local<v8::Value> RawAccessor(v8::Isolate* isolate) { |
| 23 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 24 i::Zone zone; |
| 25 i::compiler::RawMachineAssembler raw_machine_assembler( |
| 26 i_isolate, new (&zone) i::compiler::Graph(&zone), |
| 27 i::compiler::Linkage::GetJSCallDescriptor( |
| 28 &zone, false, 1, i::compiler::CallDescriptor::kNoFlags)); |
| 29 raw_machine_assembler.Return(raw_machine_assembler.NumberConstant(41)); |
| 30 i::CompilationInfo info("firstChildRaw", i_isolate, &zone); |
| 31 i::compiler::Schedule* schedule = raw_machine_assembler.Export(); |
| 32 i::Handle<i::Code> code = i::compiler::Pipeline::GenerateCodeForTesting( |
| 33 &info, raw_machine_assembler.call_descriptor(), |
| 34 raw_machine_assembler.graph(), schedule); |
| 35 return v8::Utils::ToLocal(i::Handle<i::Object>::cast(code)); |
| 36 } |
| 37 |
20 | 38 |
21 TEST(JavascriptAccessors) { | 39 TEST(JavascriptAccessors) { |
22 v8::Isolate* isolate = CcTest::isolate(); | 40 v8::Isolate* isolate = CcTest::isolate(); |
23 v8::HandleScope scope(isolate); | 41 v8::HandleScope scope(isolate); |
24 LocalContext env; | 42 LocalContext env; |
25 | 43 |
26 // We emulate Embedder-created DOM Node instances. Specifically: | 44 // We emulate Embedder-created DOM Node instances. Specifically: |
27 // - 'parent': FunctionTemplate ~= DOM Node superclass | 45 // - 'parent': FunctionTemplate ~= DOM Node superclass |
28 // - 'child': FunctionTemplate ~= a specific DOM node type, like a <div /> | 46 // - 'child': FunctionTemplate ~= a specific DOM node type, like a <div /> |
29 // | 47 // |
30 // We'll install both a C++-based and a JS-based accessor on the parent, | 48 // We'll install both a C++-based and a JS-based accessor on the parent, |
31 // and expect it to be callable on the child. | 49 // and expect it to be callable on the child. |
32 | 50 |
33 // Setup the parent template ( =~ DOM Node w/ accessors). | 51 // Setup the parent template ( =~ DOM Node w/ accessors). |
34 v8::Local<v8::FunctionTemplate> parent = v8::FunctionTemplate::New(isolate); | 52 v8::Local<v8::FunctionTemplate> parent = v8::FunctionTemplate::New(isolate); |
35 { | 53 { |
36 auto signature = v8::Signature::New(isolate, parent); | 54 auto signature = v8::Signature::New(isolate, parent); |
37 | 55 |
38 // cpp accessor as "firstChild": | 56 // cpp accessor as "firstChild": |
39 parent->PrototypeTemplate()->SetAccessorProperty( | 57 parent->PrototypeTemplate()->SetAccessorProperty( |
40 v8_str("firstChild"), | 58 v8_str("firstChild"), |
41 v8::FunctionTemplate::New(isolate, CppAccessor, v8::Local<v8::Value>(), | 59 v8::FunctionTemplate::New(isolate, CppAccessor, v8::Local<v8::Value>(), |
42 signature)); | 60 signature)); |
43 | 61 |
44 // JS accessor as "firstChildJS": | 62 // JS accessor as "firstChildRaw": |
45 auto js_accessor = v8::Local<v8::Function>::Cast(CompileRun(JsAccessor)); | 63 auto raw_accessor = RawAccessor(isolate); |
46 parent->PrototypeTemplate()->SetAccessorProperty(v8_str("firstChildJS"), | 64 parent->PrototypeTemplate()->SetAccessorProperty( |
47 js_accessor); | 65 v8_str("firstChildRaw"), v8::FunctionTemplate::NewWithFastHandler( |
| 66 isolate, CppAccessor, raw_accessor, |
| 67 v8::Local<v8::Value>(), signature)); |
48 } | 68 } |
49 | 69 |
50 // Setup child object ( =~ a specific DOM Node, e.g. a <div> ). | 70 // Setup child object ( =~ a specific DOM Node, e.g. a <div> ). |
51 // Also, make a creation function on the global object, so we can access it | 71 // Also, make a creation function on the global object, so we can access it |
52 // in a test. | 72 // in a test. |
53 v8::Local<v8::FunctionTemplate> child = v8::FunctionTemplate::New(isolate); | 73 v8::Local<v8::FunctionTemplate> child = v8::FunctionTemplate::New(isolate); |
54 child->Inherit(parent); | 74 child->Inherit(parent); |
55 CHECK(env->Global() | 75 CHECK(env->Global() |
56 ->Set(env.local(), v8_str("Node"), | 76 ->Set(env.local(), v8_str("Node"), |
57 child->GetFunction(env.local()).ToLocalChecked()) | 77 child->GetFunction(env.local()).ToLocalChecked()) |
58 .IsJust()); | 78 .IsJust()); |
59 | 79 |
60 // Setup done: Let's test it: | 80 // Setup done: Let's test it: |
61 | 81 |
62 // The simple case: Run it once. | 82 // The simple case: Run it once. |
63 ExpectInt32("var n = new Node(); n.firstChild", 42); | 83 ExpectInt32("var n = new Node(); n.firstChild", 42); |
64 ExpectInt32("var n = new Node(); n.firstChildJS", 41); | 84 ExpectInt32("var n = new Node(); n.firstChildRaw", 41); |
65 | 85 |
66 // Run them in a loop. This will likely trigger the optimizing compiler: | 86 // Run them in a loop. This will likely trigger the optimizing compiler: |
67 ExpectInt32( | 87 ExpectInt32( |
68 "var m = new Node(); " | 88 "var m = new Node(); " |
69 "var sum = 0; " | 89 "var sum = 0; " |
70 "for (var i = 0; i < 3; ++i) { " | 90 "for (var i = 0; i < 10; ++i) { " |
71 " sum += m.firstChild; " | 91 " sum += m.firstChild; " |
72 " sum += m.firstChildJS; " | 92 " sum += m.firstChildRaw; " |
73 "}; " | 93 "}; " |
74 "sum;", | 94 "sum;", |
75 3 * (42 + 41)); | 95 10 * (42 + 41)); |
76 | 96 |
77 // Obtain the accessor and call it via apply on the Node: | 97 // Obtain the accessor and call it via apply on the Node: |
78 ExpectInt32( | 98 ExpectInt32( |
79 "var n = new Node(); " | 99 "var n = new Node(); " |
80 "var g = Object.getOwnPropertyDescriptor(" | 100 "var g = Object.getOwnPropertyDescriptor(" |
81 " n.__proto__.__proto__, 'firstChild')['get']; " | 101 " n.__proto__.__proto__, 'firstChild')['get']; " |
82 "g.apply(n);", | 102 "g.apply(n);", |
83 42); | 103 42); |
84 ExpectInt32( | 104 ExpectInt32( |
85 "var n = new Node(); " | 105 "var n = new Node(); " |
86 "var g = Object.getOwnPropertyDescriptor(" | 106 "var g = Object.getOwnPropertyDescriptor(" |
87 " n.__proto__.__proto__, 'firstChildJS')['get']; " | 107 " n.__proto__.__proto__, 'firstChildRaw')['get']; " |
88 "g.apply(n);", | 108 "g.apply(n);", |
89 41); | 109 41); |
90 | 110 |
91 // TODO(vogelheim): Verify compatible receiver check works. | 111 ExpectInt32( |
| 112 "var n = new Node();" |
| 113 "var g = Object.getOwnPropertyDescriptor(" |
| 114 " n.__proto__.__proto__, 'firstChildRaw')['get'];" |
| 115 "try {" |
| 116 " var f = { firstChildRaw: '51' };" |
| 117 " g.apply(f);" |
| 118 "} catch(e) {" |
| 119 " 31415;" |
| 120 "}", |
| 121 31415); |
92 } | 122 } |
93 #endif // V8_JS_ACCESSORS | |
OLD | NEW |