Index: test/cctest/test-api-fast-accessor-builder.cc |
diff --git a/test/cctest/test-api-fast-accessor-builder.cc b/test/cctest/test-api-fast-accessor-builder.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c9f65f55e615f1a49956b671fe37367a634d8b11 |
--- /dev/null |
+++ b/test/cctest/test-api-fast-accessor-builder.cc |
@@ -0,0 +1,104 @@ |
+// Copyright 2015 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// TODO(jochen): Remove this after the setting is turned on globally. |
+#define V8_IMMINENT_DEPRECATION_WARNINGS |
jochen (gone - plz use gerrit)
2015/11/27 08:33:45
yay :P
|
+ |
+#include <stdlib.h> |
+ |
+#include "include/v8.h" |
+ |
+#include "src/api.h" |
+#include "test/cctest/cctest.h" |
+ |
+namespace { |
+ |
+static void NativePropertyAccessor( |
+ const v8::FunctionCallbackInfo<v8::Value>& info) { |
+ info.GetReturnValue().Set(v8_num(123)); |
+} |
+ |
+} // anonymous namespace |
+ |
+ |
+// Build a simple "fast accessor" and verify that it is being called. |
+TEST(FastAccessor) { |
+ LocalContext env; |
+ v8::Isolate* isolate = env->GetIsolate(); |
+ v8::HandleScope scope(isolate); |
+ |
+ v8::Local<v8::FunctionTemplate> foo = v8::FunctionTemplate::New(isolate); |
+ |
+ // Native accessor, bar, returns 123. |
+ foo->PrototypeTemplate()->SetAccessorProperty( |
+ v8_str("bar"), |
+ v8::FunctionTemplate::New(isolate, NativePropertyAccessor)); |
+ |
+ // Fast accessor, barf, returns 124. |
+ auto fab = v8::experimental::FastAccessorBuilder::New(isolate); |
+ fab->ReturnValue(fab->IntegerConstant(124)); |
+ foo->PrototypeTemplate()->SetAccessorProperty( |
+ v8_str("barf"), v8::FunctionTemplate::NewWithFastHandler( |
+ isolate, NativePropertyAccessor, fab)); |
+ |
+ // Install foo on the global object. |
+ CHECK(env->Global() |
+ ->Set(env.local(), v8_str("foo"), |
+ foo->GetFunction(env.local()).ToLocalChecked()) |
+ .FromJust()); |
+ |
+ ExpectInt32("f = new foo(); f.bar", 123); |
+ ExpectInt32("f = new foo(); f.barf", 123); |
+ ExpectInt32("f = new foo(); f.barf", 123); |
+ ExpectInt32("f = new foo(); f.barf", 123); |
+ 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
|
+} |
+ |
+ |
+void AddInternalFieldAccessor(v8::Isolate* isolate, |
+ v8::Local<v8::Template> templ, const char* name, |
+ int field_no) { |
+ auto builder = v8::experimental::FastAccessorBuilder::New(isolate); |
+ builder->ReturnValue( |
+ builder->GetInternalField(builder->GetCallTarget(), field_no)); |
+ templ->SetAccessorProperty(v8_str(name), |
+ v8::FunctionTemplate::NewWithFastHandler( |
+ isolate, NativePropertyAccessor, builder)); |
+} |
+ |
+ |
+// "Fast" accessor that accesses an internal field. |
+TEST(FastAccessorWithInternalField) { |
+ LocalContext env; |
+ v8::Isolate* isolate = env->GetIsolate(); |
+ v8::HandleScope scope(isolate); |
+ |
+ v8::Local<v8::ObjectTemplate> foo = v8::ObjectTemplate::New(isolate); |
+ foo->SetInternalFieldCount(3); |
+ AddInternalFieldAccessor(isolate, foo, "field0", 0); |
+ AddInternalFieldAccessor(isolate, foo, "field1", 1); |
+ AddInternalFieldAccessor(isolate, foo, "field2", 2); |
+ |
+ // Create an instance w/ 3 internal fields, put in a string, a Smi, nothing. |
+ v8::Local<v8::Object> obj = foo->NewInstance(env.local()).ToLocalChecked(); |
+ obj->SetInternalField(0, v8_str("Hi there!")); |
+ obj->SetInternalField(1, v8::Integer::New(isolate, 4321)); |
+ CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust()); |
+ |
+ // Ensure we go through accessors' fast paths, without checking anything. |
+ CompileRun("obj.field0"); |
+ CompileRun("obj.field0"); |
+ CompileRun("obj.field0"); |
+ CompileRun("obj.field1"); |
+ CompileRun("obj.field1"); |
+ CompileRun("obj.field1"); |
+ CompileRun("obj.field2"); |
+ CompileRun("obj.field2"); |
+ CompileRun("obj.field2"); |
+ |
+ // Access fields. |
+ ExpectString("obj.field0", "Hi there!"); |
+ ExpectInt32("obj.field1", 4321); |
+ ExpectUndefined("obj.field2"); |
+} |