Skip to content
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

[emval] Reduce C++ -> JS calls in emscripten::val lifetime management #21366

Merged
Merged
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
35 changes: 31 additions & 4 deletions system/include/emscripten/val.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ enum {
_EMVAL_UNDEFINED = 2,
_EMVAL_NULL = 4,
_EMVAL_TRUE = 6,
_EMVAL_FALSE = 8
_EMVAL_FALSE = 8,
_EMVAL_LAST_RESERVED_HANDLE = 8,
};

typedef struct _EM_DESTRUCTORS* EM_DESTRUCTORS;
Expand Down Expand Up @@ -385,11 +386,13 @@ class val {
}

val(const val& v) : val(v.as_handle()) {
internal::_emval_incref(handle);
if (uses_ref_count()) {
internal::_emval_incref(handle);
}
}

~val() {
if (handle) {
if (uses_ref_count()) {
internal::_emval_decref(as_handle());
handle = 0;
}
Expand All @@ -402,6 +405,13 @@ class val {
return handle;
}

// Takes ownership of the handle away from, and invalidates, this instance.
EM_VAL release_ownership() {
EM_VAL taken = as_handle();
handle = 0;
return taken;
}

val& operator=(val&& v) & {
val tmp(std::move(v));
this->~val();
Expand Down Expand Up @@ -630,6 +640,12 @@ class val {
: handle(handle), thread(pthread_self())
{}

// Whether this value is a uses incref/decref (true) or is a special reserved
// value (false).
bool uses_ref_count() const {
return handle > reinterpret_cast<EM_VAL>(internal::_EMVAL_LAST_RESERVED_HANDLE);
}

template<typename WrapperType>
friend val internal::wrapped_extend(const std::string& , const val& );

Expand Down Expand Up @@ -786,9 +802,20 @@ template<typename T>
struct BindingType<T, typename std::enable_if<std::is_base_of<val, T>::value &&
!std::is_const<T>::value>::type> {
typedef EM_VAL WireType;

// Marshall to JS with move semantics when we can invalidate the temporary val
// object.
static WireType toWireType(val&& v) {
return v.release_ownership();
}

// Marshal to JS with copy semantics when we cannot transfer the val objects
// reference count.
static WireType toWireType(const val& v) {
EM_VAL handle = v.as_handle();
_emval_incref(handle);
if (v.uses_ref_count()) {
_emval_incref(handle);
}
return handle;
}
static T fromWireType(WireType v) {
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 431,
"a.js": 7080,
"a.js.gz": 2999,
"a.wasm": 11428,
"a.wasm.gz": 5724,
"total": 19181,
"total_gz": 9154
"a.wasm": 11431,
"a.wasm.gz": 5727,
"total": 19184,
"total_gz": 9157
}
Loading