Skip to content

Commit 746759e

Browse files
joyeecheungrefack
authored andcommitted
deps: cherry-pick 0483e9a from upstream V8
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 <[email protected]> Reviewed-by: Adam Klein <[email protected]> Commit-Queue: Joyee Cheung <[email protected]> Cr-Commit-Position: refs/heads/master@{nodejs#57261} Refs: v8/v8@0483e9a PR-URL: nodejs#24125 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yang Guo <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 47efb9a commit 746759e

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

common.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

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

4343
##### V8 defaults for Node.js #####
4444

deps/v8/include/v8.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,12 @@ class V8_EXPORT Array : public Object {
36803680
*/
36813681
static Local<Array> New(Isolate* isolate, int length = 0);
36823682

3683+
/**
3684+
* Creates a JavaScript array out of a Local<Value> array in C++
3685+
* with a known length.
3686+
*/
3687+
static Local<Array> New(Isolate* isolate, Local<Value>* elements,
3688+
size_t length);
36833689
V8_INLINE static Array* Cast(Value* obj);
36843690
private:
36853691
Array();

deps/v8/src/api.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6911,6 +6911,23 @@ Local<v8::Array> v8::Array::New(Isolate* isolate, int length) {
69116911
return Utils::ToLocal(obj);
69126912
}
69136913

6914+
Local<v8::Array> v8::Array::New(Isolate* isolate, Local<Value>* elements,
6915+
size_t length) {
6916+
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6917+
i::Factory* factory = i_isolate->factory();
6918+
LOG_API(i_isolate, Array, New);
6919+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6920+
int len = static_cast<int>(length);
6921+
6922+
i::Handle<i::FixedArray> result = factory->NewFixedArray(len);
6923+
for (int i = 0; i < len; i++) {
6924+
i::Handle<i::Object> element = Utils::OpenHandle(*elements[i]);
6925+
result->set(i, *element);
6926+
}
6927+
6928+
return Utils::ToLocal(
6929+
factory->NewJSArrayWithElements(result, i::PACKED_ELEMENTS, len));
6930+
}
69146931

69156932
uint32_t v8::Array::Length() const {
69166933
i::Handle<i::JSArray> obj = Utils::OpenHandle(this);

deps/v8/test/cctest/test-api.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5225,6 +5225,22 @@ THREADED_TEST(Array) {
52255225
CHECK_EQ(27u, array->Length());
52265226
array = v8::Array::New(context->GetIsolate(), -27);
52275227
CHECK_EQ(0u, array->Length());
5228+
5229+
std::vector<Local<Value>> vector = {v8_num(1), v8_num(2), v8_num(3)};
5230+
array = v8::Array::New(context->GetIsolate(), vector.data(), vector.size());
5231+
CHECK_EQ(vector.size(), array->Length());
5232+
CHECK_EQ(1, arr->Get(context.local(), 0)
5233+
.ToLocalChecked()
5234+
->Int32Value(context.local())
5235+
.FromJust());
5236+
CHECK_EQ(2, arr->Get(context.local(), 1)
5237+
.ToLocalChecked()
5238+
->Int32Value(context.local())
5239+
.FromJust());
5240+
CHECK_EQ(3, arr->Get(context.local(), 2)
5241+
.ToLocalChecked()
5242+
->Int32Value(context.local())
5243+
.FromJust());
52285244
}
52295245

52305246

0 commit comments

Comments
 (0)