Skip to content

Commit

Permalink
deps: cherry-pick 0483e9a from upstream V8
Browse files Browse the repository at this point in the history
Original commit message:

    [api] Allow embedder to construct an Array from Local<Value>*

    Currently to obtain a v8::Array out of a C array or a std::vector,
    one needs to loop through the elements and call array->Set() multiple
    times, and these calls go into v8::Object::Set() which can be slow.
    This patch adds a new Array::New overload that converts a
    Local<Value>* with known size into a Local<Array>.

    Change-Id: I0a768f0e18eec51e78d58be455482ec6425ca188
    Reviewed-on: https://chromium-review.googlesource.com/c/1317049
    Reviewed-by: Yang Guo <yangguo@chromium.org>
    Reviewed-by: Adam Klein <adamk@chromium.org>
    Commit-Queue: Joyee Cheung <joyee@igalia.com>
    Cr-Commit-Position: refs/heads/master@{#57261}

Refs: v8/v8@0483e9a

PR-URL: nodejs#24125
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yang Guo <yangguo@chromium.org>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information
joyeecheung authored and targos committed Dec 4, 2018
1 parent 9da61bd commit 78b1437
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.1',
'v8_embedder_string': '-node.2',

##### V8 defaults for Node.js #####

Expand Down
6 changes: 6 additions & 0 deletions deps/v8/include/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -3680,6 +3680,12 @@ class V8_EXPORT Array : public Object {
*/
static Local<Array> New(Isolate* isolate, int length = 0);

/**
* Creates a JavaScript array out of a Local<Value> array in C++
* with a known length.
*/
static Local<Array> New(Isolate* isolate, Local<Value>* elements,
size_t length);
V8_INLINE static Array* Cast(Value* obj);
private:
Array();
Expand Down
17 changes: 17 additions & 0 deletions deps/v8/src/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6911,6 +6911,23 @@ Local<v8::Array> v8::Array::New(Isolate* isolate, int length) {
return Utils::ToLocal(obj);
}

Local<v8::Array> v8::Array::New(Isolate* isolate, Local<Value>* elements,
size_t length) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::Factory* factory = i_isolate->factory();
LOG_API(i_isolate, Array, New);
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
int len = static_cast<int>(length);

i::Handle<i::FixedArray> result = factory->NewFixedArray(len);
for (int i = 0; i < len; i++) {
i::Handle<i::Object> element = Utils::OpenHandle(*elements[i]);
result->set(i, *element);
}

return Utils::ToLocal(
factory->NewJSArrayWithElements(result, i::PACKED_ELEMENTS, len));
}

uint32_t v8::Array::Length() const {
i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
Expand Down
16 changes: 16 additions & 0 deletions deps/v8/test/cctest/test-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5225,6 +5225,22 @@ THREADED_TEST(Array) {
CHECK_EQ(27u, array->Length());
array = v8::Array::New(context->GetIsolate(), -27);
CHECK_EQ(0u, array->Length());

std::vector<Local<Value>> vector = {v8_num(1), v8_num(2), v8_num(3)};
array = v8::Array::New(context->GetIsolate(), vector.data(), vector.size());
CHECK_EQ(vector.size(), array->Length());
CHECK_EQ(1, arr->Get(context.local(), 0)
.ToLocalChecked()
->Int32Value(context.local())
.FromJust());
CHECK_EQ(2, arr->Get(context.local(), 1)
.ToLocalChecked()
->Int32Value(context.local())
.FromJust());
CHECK_EQ(3, arr->Get(context.local(), 2)
.ToLocalChecked()
->Int32Value(context.local())
.FromJust());
}


Expand Down

0 comments on commit 78b1437

Please sign in to comment.