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

Unified Diff: test/cctest/test-api-fast-accessor-builder.cc

Issue 1474543004: Implement Fast Accessor Builder (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Remove impl_ cleanup. Drop tests for --optimize-for-size. Also rebase. Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/cctest/cctest.gyp ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..a6f1a4229be0e5db489569f6c6c87848ad779d64
--- /dev/null
+++ b/test/cctest/test-api-fast-accessor-builder.cc
@@ -0,0 +1,274 @@
+// 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.
+
+#include <stdlib.h>
+
+#include "include/v8.h"
+#include "include/v8-experimental.h"
+
+#include "src/api.h"
+#include "test/cctest/cctest.h"
+
+namespace {
+
+// These tests mean to exercise v8::FastAccessorBuilder. Since initially the
+// "native" accessor will get called, we need to 'warmup' any accessor first,
+// to make sure we're actually testing the v8::FastAccessorBuilder result.
+// To accomplish this, we will
+// - call each accesssor N times before the actual test.
+// - wrap that call in a function, so that all such calls will go
+// through a single call site.
+// - register a native accessor which is different from the build one
+// (so that our tests will always fail if we don't end up in the 'fast'
+// accessor)
+//
+// This doesn't work if the src function is inlined - as it is when
+// --always-opt is enabled - since then every inlined functino is its own
+// callsite. Hence most test will check for i::FLAG_always_opt.
+#define WARMUP(src) "for(i = 0; i < 2; i++) { " src " } "
+
+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) {
+ if (i::FLAG_always_opt || i::FLAG_optimize_for_size) return;
+
+ 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());
+
+ // Wrap f.barf + IC warmup.
+ CompileRun(
+ "function barf() { f = new foo(); return f.barf }; " WARMUP("barf()"));
+
+ ExpectInt32("f = new foo(); f.bar", 123);
+ ExpectInt32("f = new foo(); f.barf", 123); // First call in this call site.
+ ExpectInt32("barf()", 124); // Call via warmed-up callsite.
+}
+
+
+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->LoadInternalField(builder->GetReceiver(), field_no));
+ templ->SetAccessorProperty(v8_str(name),
+ v8::FunctionTemplate::NewWithFastHandler(
+ isolate, NativePropertyAccessor, builder));
+}
+
+
+// "Fast" accessor that accesses an internal field.
+TEST(FastAccessorWithInternalField) {
+ if (i::FLAG_always_opt || i::FLAG_optimize_for_size) return;
+
+ 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());
+
+ // Warmup.
+ CompileRun("function field0() { return obj.field0 }; " WARMUP("field0()"));
+ CompileRun("function field1() { return obj.field1 }; " WARMUP("field1()"));
+ CompileRun("function field2() { return obj.field2 }; " WARMUP("field2()"));
+
+ // Access fields.
+ ExpectString("field0()", "Hi there!");
+ ExpectInt32("field1()", 4321);
+ ExpectUndefined("field2()");
+}
+
+
+// "Fast" accessor with control flow via ...OrReturnNull methods.
+TEST(FastAccessorOrReturnNull) {
+ if (i::FLAG_always_opt || i::FLAG_optimize_for_size) return;
+
+ LocalContext env;
+ v8::Isolate* isolate = env->GetIsolate();
+ v8::HandleScope scope(isolate);
+
+ v8::Local<v8::ObjectTemplate> foo = v8::ObjectTemplate::New(isolate);
+ foo->SetInternalFieldCount(2);
+ {
+ // accessor "nullcheck": Return null if field 0 is non-null object; else 5.
+ auto builder = v8::experimental::FastAccessorBuilder::New(isolate);
+ auto val = builder->LoadInternalField(builder->GetReceiver(), 0);
+ builder->CheckNotZeroOrReturnNull(val);
+ builder->ReturnValue(builder->IntegerConstant(5));
+ foo->SetAccessorProperty(v8_str("nullcheck"),
+ v8::FunctionTemplate::NewWithFastHandler(
+ isolate, NativePropertyAccessor, builder));
+ }
+ {
+ // accessor "maskcheck": Return null if field 1 has 3rd bit set.
+ auto builder = v8::experimental::FastAccessorBuilder::New(isolate);
+ auto val = builder->LoadInternalField(builder->GetReceiver(), 1);
+ builder->CheckFlagSetOrReturnNull(val, 0x4);
+ builder->ReturnValue(builder->IntegerConstant(42));
+ foo->SetAccessorProperty(v8_str("maskcheck"),
+ v8::FunctionTemplate::NewWithFastHandler(
+ isolate, NativePropertyAccessor, builder));
+ }
+
+ // Create an instance.
+ v8::Local<v8::Object> obj = foo->NewInstance(env.local()).ToLocalChecked();
+ CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust());
+
+ // CheckNotZeroOrReturnNull:
+ CompileRun(
+ "function nullcheck() { return obj.nullcheck }; " WARMUP("nullcheck()"));
+ obj->SetAlignedPointerInInternalField(0, /* anything != nullptr */ isolate);
+ ExpectInt32("nullcheck()", 5);
+ obj->SetAlignedPointerInInternalField(0, nullptr);
+ ExpectNull("nullcheck()");
+
+ // CheckFlagSetOrReturnNull:
+ CompileRun(
+ "function maskcheck() { return obj.maskcheck }; " WARMUP("maskcheck()"));
+ obj->SetAlignedPointerInInternalField(1, reinterpret_cast<void*>(0xf0));
+ ExpectInt32("maskcheck()", 42);
+ obj->SetAlignedPointerInInternalField(1, reinterpret_cast<void*>(0xfe));
+ ExpectNull("maskcheck()");
+}
+
+
+// "Fast" accessor with simple control flow via explicit labels.
+TEST(FastAccessorControlFlowWithLabels) {
+ if (i::FLAG_always_opt || i::FLAG_optimize_for_size) return;
+
+ LocalContext env;
+ v8::Isolate* isolate = env->GetIsolate();
+ v8::HandleScope scope(isolate);
+
+ v8::Local<v8::ObjectTemplate> foo = v8::ObjectTemplate::New(isolate);
+ foo->SetInternalFieldCount(1);
+ {
+ // accessor isnull: 0 for nullptr, else 1.
+ auto builder = v8::experimental::FastAccessorBuilder::New(isolate);
+ auto label = builder->MakeLabel();
+ auto val = builder->LoadInternalField(builder->GetReceiver(), 0);
+ builder->CheckNotZeroOrJump(val, label);
+ builder->ReturnValue(builder->IntegerConstant(0));
+ builder->SetLabel(label);
+ builder->ReturnValue(builder->IntegerConstant(1));
+ foo->SetAccessorProperty(v8_str("isnull"),
+ v8::FunctionTemplate::NewWithFastHandler(
+ isolate, NativePropertyAccessor, builder));
+ }
+
+ // Create an instance.
+ v8::Local<v8::Object> obj = foo->NewInstance(env.local()).ToLocalChecked();
+ CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust());
+
+ // CheckNotZeroOrReturnNull:
+ CompileRun("function isnull() { return obj.isnull }; " WARMUP("isnull()"));
+ obj->SetAlignedPointerInInternalField(0, /* anything != nullptr */ isolate);
+ ExpectInt32("isnull()", 1);
+ obj->SetAlignedPointerInInternalField(0, nullptr);
+ ExpectInt32("isnull()", 0);
+}
+
+
+// "Fast" accessor, loading things.
+TEST(FastAccessorLoad) {
+ if (i::FLAG_always_opt || i::FLAG_optimize_for_size) return;
+
+ LocalContext env;
+ v8::Isolate* isolate = env->GetIsolate();
+ v8::HandleScope scope(isolate);
+
+ v8::Local<v8::ObjectTemplate> foo = v8::ObjectTemplate::New(isolate);
+ foo->SetInternalFieldCount(1);
+
+ // Internal field 0 is a pointer to a C++ data structure that we wish to load
+ // field values from.
+ struct {
+ size_t intval;
+ v8::Local<v8::String> v8val;
+ } val = {54321, v8_str("Hello")};
+
+ {
+ // accessor intisnonzero
+ int intval_offset =
+ static_cast<int>(reinterpret_cast<intptr_t>(&val.intval) -
+ reinterpret_cast<intptr_t>(&val));
+ auto builder = v8::experimental::FastAccessorBuilder::New(isolate);
+ auto label = builder->MakeLabel();
+ auto val = builder->LoadValue(
+ builder->LoadInternalField(builder->GetReceiver(), 0), intval_offset);
+ builder->CheckNotZeroOrJump(val, label);
+ builder->ReturnValue(builder->IntegerConstant(0));
+ builder->SetLabel(label);
+ builder->ReturnValue(builder->IntegerConstant(1));
+ foo->SetAccessorProperty(v8_str("nonzero"),
+ v8::FunctionTemplate::NewWithFastHandler(
+ isolate, NativePropertyAccessor, builder));
+ }
+ {
+ // accessor loadval
+ int v8val_offset = static_cast<int>(reinterpret_cast<intptr_t>(&val.v8val) -
+ reinterpret_cast<intptr_t>(&val));
+ auto builder = v8::experimental::FastAccessorBuilder::New(isolate);
+ builder->ReturnValue(builder->LoadObject(
+ builder->LoadInternalField(builder->GetReceiver(), 0), v8val_offset));
+ foo->SetAccessorProperty(v8_str("loadval"),
+ v8::FunctionTemplate::NewWithFastHandler(
+ isolate, NativePropertyAccessor, builder));
+ }
+
+ // Create an instance.
+ v8::Local<v8::Object> obj = foo->NewInstance(env.local()).ToLocalChecked();
+ obj->SetAlignedPointerInInternalField(0, &val);
+ CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust());
+
+ // Access val.intval:
+ CompileRun("function nonzero() { return obj.nonzero }; " WARMUP("nonzero()"));
+ ExpectInt32("nonzero()", 1);
+ val.intval = 0;
+ ExpectInt32("nonzero()", 0);
+ val.intval = 27;
+ ExpectInt32("nonzero()", 1);
+
+ // Access val.v8val:
+ CompileRun("function loadval() { return obj.loadval }; " WARMUP("loadval()"));
+ ExpectString("loadval()", "Hello");
+}
« no previous file with comments | « test/cctest/cctest.gyp ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698