Skip to content

Commit 3e1c53f

Browse files
joyeecheungBridgeAR
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@{#57261} Refs: v8/v8@0483e9a PR-URL: #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 a1c7c19 commit 3e1c53f

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
# Reset this number to 0 on major V8 upgrades.
3232
# Increment by one for each non-official patch applied to deps/v8.
33-
'v8_embedder_string': '-node.9',
33+
'v8_embedder_string': '-node.10',
3434

3535
# Enable disassembler for `--print-code` v8 options
3636
'v8_enable_disassembler': 1,

deps/v8/include/v8.h

+6
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,12 @@ class V8_EXPORT Array : public Object {
37793779
*/
37803780
static Local<Array> New(Isolate* isolate, int length = 0);
37813781

3782+
/**
3783+
* Creates a JavaScript array out of a Local<Value> array in C++
3784+
* with a known length.
3785+
*/
3786+
static Local<Array> New(Isolate* isolate, Local<Value>* elements,
3787+
size_t length);
37823788
V8_INLINE static Array* Cast(Value* obj);
37833789
private:
37843790
Array();

deps/v8/src/api.cc

+17
Original file line numberDiff line numberDiff line change
@@ -6981,6 +6981,23 @@ Local<v8::Array> v8::Array::New(Isolate* isolate, int length) {
69816981
return Utils::ToLocal(obj);
69826982
}
69836983

6984+
Local<v8::Array> v8::Array::New(Isolate* isolate, Local<Value>* elements,
6985+
size_t length) {
6986+
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6987+
i::Factory* factory = i_isolate->factory();
6988+
LOG_API(i_isolate, Array, New);
6989+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6990+
int len = static_cast<int>(length);
6991+
6992+
i::Handle<i::FixedArray> result = factory->NewFixedArray(len);
6993+
for (int i = 0; i < len; i++) {
6994+
i::Handle<i::Object> element = Utils::OpenHandle(*elements[i]);
6995+
result->set(i, *element);
6996+
}
6997+
6998+
return Utils::ToLocal(
6999+
factory->NewJSArrayWithElements(result, i::PACKED_ELEMENTS, len));
7000+
}
69847001

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

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

+16
Original file line numberDiff line numberDiff line change
@@ -5247,6 +5247,22 @@ THREADED_TEST(Array) {
52475247
CHECK_EQ(27u, array->Length());
52485248
array = v8::Array::New(context->GetIsolate(), -27);
52495249
CHECK_EQ(0u, array->Length());
5250+
5251+
std::vector<Local<Value>> vector = {v8_num(1), v8_num(2), v8_num(3)};
5252+
array = v8::Array::New(context->GetIsolate(), vector.data(), vector.size());
5253+
CHECK_EQ(vector.size(), array->Length());
5254+
CHECK_EQ(1, arr->Get(context.local(), 0)
5255+
.ToLocalChecked()
5256+
->Int32Value(context.local())
5257+
.FromJust());
5258+
CHECK_EQ(2, arr->Get(context.local(), 1)
5259+
.ToLocalChecked()
5260+
->Int32Value(context.local())
5261+
.FromJust());
5262+
CHECK_EQ(3, arr->Get(context.local(), 2)
5263+
.ToLocalChecked()
5264+
->Int32Value(context.local())
5265+
.FromJust());
52505266
}
52515267

52525268

0 commit comments

Comments
 (0)