Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: test/cctest/test-api-accessors.cc

Issue 1367953002: Allow JavaScript accessors on API objects. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: review feedback Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/cctest/cctest.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
7
8 #include "test/cctest/cctest.h"
9
10 #include "include/v8.h"
11
12
13 #ifdef V8_JS_ACCESSORS
14 static void CppAccessor(const v8::FunctionCallbackInfo<v8::Value>& info) {
15 info.GetReturnValue().Set(42);
16 }
17
18 static const char* JsAccessor =
19 "function firstChildJS(value) { return 41; }; firstChildJS";
20
21 TEST(JavascriptAccessors) {
22 v8::Isolate* isolate = CcTest::isolate();
23 v8::HandleScope scope(isolate);
24 LocalContext env;
25
26 // We emulate Embedder-created DOM Node instances. Specifically:
27 // - 'parent': FunctionTemplate ~= DOM Node superclass
28 // - 'child': FunctionTemplate ~= a specific DOM node type, like a <div />
29 //
30 // 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.
32
33 // Setup the parent template ( =~ DOM Node w/ accessors).
34 v8::Local<v8::FunctionTemplate> parent = v8::FunctionTemplate::New(isolate);
35 {
36 auto signature = v8::Signature::New(isolate, parent);
37
38 // cpp accessor as "firstChild":
39 parent->PrototypeTemplate()->SetAccessorProperty(
40 v8_str("firstChild"),
41 v8::FunctionTemplate::New(isolate, CppAccessor, v8::Local<v8::Value>(),
42 signature));
43
44 // JS accessor as "firstChildJS":
45 auto js_accessor = v8::Local<v8::Function>::Cast(CompileRun(JsAccessor));
46 parent->PrototypeTemplate()->SetAccessorProperty(v8_str("firstChildJS"),
47 js_accessor);
48 }
49
50 // 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
52 // in a test.
53 v8::Local<v8::FunctionTemplate> child = v8::FunctionTemplate::New(isolate);
54 child->Inherit(parent);
55 CHECK(env->Global()
56 ->Set(env.local(), v8_str("Node"),
57 child->GetFunction(env.local()).ToLocalChecked())
58 .IsJust());
59
60 // Setup done: Let's test it:
61
62 // The simple case: Run it once.
63 ExpectInt32("var n = new Node(); n.firstChild", 42);
64 ExpectInt32("var n = new Node(); n.firstChildJS", 41);
65
66 // Run them in a loop. This will likely trigger the optimizing compiler:
67 ExpectInt32(
68 "var m = new Node(); "
69 "var sum = 0; "
70 "for (var i = 0; i < 3; ++i) { "
71 " sum += m.firstChild; "
72 " sum += m.firstChildJS; "
73 "}; "
74 "sum;",
75 3 * (42 + 41));
76
77 // Obtain the accessor and call it via apply on the Node:
78 ExpectInt32(
79 "var n = new Node(); "
80 "var g = Object.getOwnPropertyDescriptor("
81 " n.__proto__.__proto__, 'firstChild')['get']; "
82 "g.apply(n);",
83 42);
84 ExpectInt32(
85 "var n = new Node(); "
86 "var g = Object.getOwnPropertyDescriptor("
87 " n.__proto__.__proto__, 'firstChildJS')['get']; "
88 "g.apply(n);",
89 41);
90
91 // TODO(vogelheim): Verify compatible receiver check works.
92 }
93 #endif // V8_JS_ACCESSORS
OLDNEW
« no previous file with comments | « test/cctest/cctest.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698