Commit ce49c329 authored by cbruni's avatar cbruni Committed by Commit bot

[api] Add v8::Object::SetAlignedPointerInInternalFields

This new API function allows for setting several internal fields at once.
By avoiding crossing the API each time for setting an internal property we
can speed up the wrapper creation which has to set two fields for every new
object.

BUG=chromium:630217

Review-Url: https://codereview.chromium.org/2185963002
Cr-Commit-Position: refs/heads/master@{#38299}
parent 33fab42e
......@@ -2892,6 +2892,8 @@ class V8_EXPORT Object : public Value {
* leads to undefined behavior.
*/
void SetAlignedPointerInInternalField(int index, void* value);
void SetAlignedPointerInInternalFields(int argc, int indices[],
void* values[]);
// Testers for local properties.
V8_DEPRECATED("Use maybe version", bool HasOwnProperty(Local<String> key));
......@@ -8246,7 +8248,6 @@ void* Object::GetAlignedPointerFromInternalField(int index) {
return SlowGetAlignedPointerFromInternalField(index);
}
String* String::Cast(v8::Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
......
......@@ -5475,7 +5475,6 @@ void* v8::Object::SlowGetAlignedPointerFromInternalField(int index) {
i::Handle<i::JSObject>::cast(obj)->GetInternalField(index), location);
}
void v8::Object::SetAlignedPointerInInternalField(int index, void* value) {
i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
const char* location = "v8::Object::SetAlignedPointerInInternalField()";
......@@ -5485,6 +5484,24 @@ void v8::Object::SetAlignedPointerInInternalField(int index, void* value) {
DCHECK_EQ(value, GetAlignedPointerFromInternalField(index));
}
void v8::Object::SetAlignedPointerInInternalFields(int argc, int indices[],
void* values[]) {
i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
const char* location = "v8::Object::SetAlignedPointerInInternalFields()";
i::DisallowHeapAllocation no_gc;
i::JSObject* object = i::JSObject::cast(*obj);
int nof_internal_fields = object->GetInternalFieldCount();
for (int i = 0; i < argc; i++) {
int index = indices[i];
if (!Utils::ApiCheck(index < nof_internal_fields, location,
"Internal field out of bounds")) {
return;
}
void* value = values[i];
object->SetInternalField(index, EncodeAlignedAsSmi(value, location));
DCHECK_EQ(value, GetAlignedPointerFromInternalField(index));
}
}
static void* ExternalValue(i::Object* obj) {
// Obscure semantics for undefined, but somehow checked in our unit tests...
......
......@@ -2688,6 +2688,40 @@ THREADED_TEST(InternalFieldsAlignedPointers) {
CHECK_EQ(huge, Object::GetAlignedPointerFromInternalField(persistent, 0));
}
THREADED_TEST(SetAlignedPointerInInternalFields) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope scope(isolate);
Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
Local<v8::ObjectTemplate> instance_templ = templ->InstanceTemplate();
instance_templ->SetInternalFieldCount(2);
Local<v8::Object> obj = templ->GetFunction(env.local())
.ToLocalChecked()
->NewInstance(env.local())
.ToLocalChecked();
CHECK_EQ(2, obj->InternalFieldCount());
int* heap_allocated_1 = new int[100];
int* heap_allocated_2 = new int[100];
int indices[] = {0, 1};
void* values[] = {heap_allocated_1, heap_allocated_2};
obj->SetAlignedPointerInInternalFields(2, indices, values);
CcTest::heap()->CollectAllGarbage();
CHECK_EQ(heap_allocated_1, obj->GetAlignedPointerFromInternalField(0));
CHECK_EQ(heap_allocated_2, obj->GetAlignedPointerFromInternalField(1));
indices[0] = 1;
indices[1] = 0;
obj->SetAlignedPointerInInternalFields(2, indices, values);
CcTest::heap()->CollectAllGarbage();
CHECK_EQ(heap_allocated_2, obj->GetAlignedPointerFromInternalField(0));
CHECK_EQ(heap_allocated_1, obj->GetAlignedPointerFromInternalField(1));
delete[] heap_allocated_1;
delete[] heap_allocated_2;
}
static void CheckAlignedPointerInEmbedderData(LocalContext* env, int index,
void* value) {
......@@ -2729,7 +2763,6 @@ THREADED_TEST(EmbedderDataAlignedPointers) {
}
}
static void CheckEmbedderData(LocalContext* env, int index,
v8::Local<Value> data) {
(*env)->SetEmbedderData(index, data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment