Skip to content

Commit 0483e9a

Browse files
joyeecheungCommit Bot
authored andcommitted
[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}
1 parent 3c8e022 commit 0483e9a

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

include/v8.h

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

3680+
/**
3681+
* Creates a JavaScript array out of a Local<Value> array in C++
3682+
* with a known length.
3683+
*/
3684+
static Local<Array> New(Isolate* isolate, Local<Value>* elements,
3685+
size_t length);
36803686
V8_INLINE static Array* Cast(Value* obj);
36813687
private:
36823688
Array();

src/api.cc

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

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

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

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)