OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
jochen (gone - plz use gerrit)
2015/09/25 09:24:11
plz use the shorter header
vogelheim
2015/09/25 12:59:24
Done.
| |
27 | |
28 | |
jochen (gone - plz use gerrit)
2015/09/25 09:24:11
can you add
// TODO(jochen): Remove this after th
vogelheim
2015/09/25 12:59:24
Done.
| |
29 #include "test/cctest/test-api.h" | |
30 | |
31 #include "include/v8.h" | |
32 | |
33 | |
34 #ifdef V8_JS_ACCESSORS | |
35 static void CppAccessor(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
36 info.GetReturnValue().Set(42); | |
37 } | |
38 | |
39 static const char* JsAccessor = | |
40 "function firstChildJS(value) { return 41; }; firstChildJS"; | |
41 | |
42 TEST(JavascriptAccessors) { | |
43 v8::Isolate* isolate = CcTest::isolate(); | |
44 v8::HandleScope scope(isolate); | |
45 LocalContext env; | |
46 | |
47 // We emulate Embedder-created DOM Node instances. Specifically: | |
48 // - 'parent': FunctionTemplate ~= DOM Node superclass | |
49 // - 'child': FunctionTemplate ~= a specific DOM node type, like a <div /> | |
50 // | |
51 // We'll install both a C++-based and a JS-based accessor on the parent, | |
52 // and expect it to be callable on the child. | |
53 | |
54 // Setup the parent template ( =~ DOM Node w/ accessors). | |
55 v8::Local<v8::FunctionTemplate> parent = v8::FunctionTemplate::New(isolate); | |
56 { | |
57 auto signature = v8::Signature::New(isolate, parent); | |
58 | |
59 // cpp accessor as "firstChild": | |
60 parent->PrototypeTemplate()->SetAccessorProperty( | |
61 v8_str("firstChild"), | |
62 v8::FunctionTemplate::New(isolate, CppAccessor, v8::Local<v8::Value>(), | |
63 signature)); | |
64 | |
65 // JS accessor as "firstChildJS": | |
66 auto js_accessor = v8::Local<v8::Function>::Cast(CompileRun(JsAccessor)); | |
67 parent->PrototypeTemplate()->SetAccessorProperty(v8_str("firstChildJS"), | |
68 js_accessor); | |
69 } | |
70 | |
71 // Setup child object ( =~ a specific DOM Node, e.g. a <div> ). | |
72 // Also, make a creation function on the global object, so we can access it | |
73 // in a test. | |
74 v8::Local<v8::FunctionTemplate> child = v8::FunctionTemplate::New(isolate); | |
75 child->Inherit(parent); | |
76 env->Global()->Set(v8_str("Node"), child->GetFunction()); | |
77 | |
78 // Setup done: Let's test it: | |
79 | |
80 // The simple case: Run it once. | |
81 ExpectInt32("var n = new Node(); n.firstChild", 42); | |
82 ExpectInt32("var n = new Node(); n.firstChildJS", 41); | |
83 | |
84 // Run them in a loop. This will likely trigger the optimizing compiler: | |
85 ExpectInt32( | |
86 "var m = new Node(); " | |
87 "var sum = 0; " | |
88 "for (var i = 0; i < 3; ++i) { " | |
89 " sum += m.firstChild; " | |
90 " sum += m.firstChildJS; " | |
91 "}; " | |
92 "sum;", | |
93 3 * (42 + 41)); | |
94 | |
95 // Obtain the accessor and call it via apply on the Node: | |
96 ExpectInt32( | |
97 "var n = new Node(); " | |
98 "var g = Object.getOwnPropertyDescriptor(" | |
99 " n.__proto__.__proto__, 'firstChild')['get']; " | |
100 "g.apply(n);", | |
101 42); | |
102 ExpectInt32( | |
103 "var n = new Node(); " | |
104 "var g = Object.getOwnPropertyDescriptor(" | |
105 " n.__proto__.__proto__, 'firstChildJS')['get']; " | |
106 "g.apply(n);", | |
107 41); | |
108 | |
109 // TODO(vogelheim): Verify compatible receiver check works. | |
110 } | |
111 #endif // V8_JS_ACCESSORS | |
OLD | NEW |