Skip to content

Efficient conversion between JS arrays and c++ vectors for numeric types #5655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,6 @@ a license to everyone to use it as detailed in LICENSE.)
* Kamil Klimek <naresh@tlen.pl>
* José Carlos Pujol <josecpujol(at)gmail.com>
* Dannii Willis <curiousdannii@gmail.com>
* Ron Cohen <ron28299@gmail.com>


50 changes: 47 additions & 3 deletions system/include/emscripten/val.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,54 @@ namespace emscripten {
template<typename T>
std::vector<T> vecFromJSArray(val v) {
auto l = v["length"].as<unsigned>();

std::vector<T> rv;
for(unsigned i = 0; i < l; ++i) {
rv.push_back(v[i].as<T>());
rv.reserve(l);

if (internal::typeSupportsMemoryView<T>()) {
rv.resize(l);

const char *arrayType;

switch (sizeof(T)) {
case 1:
if (std::is_unsigned<T>::value) {
arrayType = "Uint8Array";
} else {
arrayType = "Int8Array";
}
break;
case 2:
if (std::is_unsigned<T>::value) {
arrayType = "Uint16Array";
} else {
arrayType = "Int16Array";
}
break;
case 4:
if (std::is_unsigned<T>::value) {
arrayType = "Uint32Array";
} else if (std::is_integral<T>::value) {
arrayType = "Int32Array";
} else {
arrayType = "Float32Array";
}
break;
case 8:
arrayType = "Float64Array";
break;
default:
//This should be never reached if the 'typeSupportsMemoryView' function returns true.
throw std::logic_error("Unreachable");
}

val memory = val::module_property("buffer");
val memoryView = val::global(arrayType).new_(memory, reinterpret_cast<std::uintptr_t>(rv.data()), l);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the deletion of this new_ occur?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_ creates a JS TypedArray object, which cannot be explicitly deleted, but eventually will be collected by the JS engine Garbage Collector.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, val is refcounted, and deleted once references drop to zero. I was thinking about the Embind objects that need to be manually .delete()d here.


memoryView.call<void>("set", v);
} else {
for (unsigned i = 0; i < l; ++i) {
rv.push_back(v[i].as<T>());
}
}

return rv;
Expand Down