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] Transfer ownership of handles to JS when moveable. #21436

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 11 additions & 4 deletions src/embind/emval.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,24 @@ var LibraryEmVal = {
return symbol;
},

$Emval__deps: ['$emval_freelist', '$emval_handles', '$throwBindingError', '$init_emval'],
$Emval__deps: ['$emval_freelist', '$emval_handles', '$throwBindingError', '$init_emval', '_emval_decref'],
$Emval: {
toValue: (handle) => {
if (!handle) {
throwBindingError('Cannot use deleted val. handle = ' + handle);
}
let decref;
if (handle % 2 == 1) {
handle = handle - 1;
decref = __emval_decref;
}
#if ASSERTIONS
// handle 2 is supposed to be `undefined`.
assert(handle === 2 || emval_handles[handle] !== undefined && handle % 2 === 0, `invalid handle: ${handle}`);
assert(handle === 2 || emval_handles[handle] !== undefined, `invalid handle: ${handle}`);
#endif
return emval_handles[handle];
const value = emval_handles[handle];
decref && decref(handle);
return value;
},

toHandle: (value) => {
Expand Down Expand Up @@ -203,7 +210,7 @@ var LibraryEmVal = {
return Emval.toHandle(Module[name]);
},

_emval_get_property__deps: ['$Emval'],
_emval_get_property__deps: ['$Emval', '_emval_decref'],
_emval_get_property: (handle, key) => {
handle = Emval.toValue(handle);
key = Emval.toValue(key);
Expand Down
128 changes: 110 additions & 18 deletions system/include/emscripten/val.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class val {
internal::typeSupportsMemoryView<
typename std::iterator_traits<Iter>::value_type>()) {
val view{ typed_memory_view(std::distance(begin, end), std::to_address(begin)) };
return val(internal::_emval_new_array_from_memory_view(view.as_handle()));
return val(internal::_emval_new_array_from_memory_view(js_ref(view)));
}
// For numeric arrays, following codes are unreachable and the compiler
// will do 'dead code elimination'.
Expand All @@ -325,7 +325,7 @@ class val {
if constexpr (internal::typeSupportsMemoryView<T>()) {
// for numeric types, pass memory view and copy in JS side one-off
val view{ typed_memory_view(vec.size(), vec.data()) };
return val(internal::_emval_new_array_from_memory_view(view.as_handle()));
return val(internal::_emval_new_array_from_memory_view(js_ref(view)));
} else {
return array(vec.begin(), vec.end());
}
Expand Down Expand Up @@ -455,11 +455,23 @@ class val {
return instanceof(global("Array"));
}

bool equals(const val& v) const {
return internal::_emval_equals(as_handle(), v.as_handle());
template <typename T>
bool equals(const T& v) const& {
return internal::_emval_equals(as_handle(), js_ref(v));
}

bool operator==(const val& v) const {
template <typename T>
bool equals(const T& v) && {
return internal::_emval_equals(release_ownership_to_js(), js_ref(v));
}

template <typename T>
bool operator==(const T& v) const& {
return equals(v);
}

template <typename T>
bool operator==(const T& v) && {
return equals(v);
}

Expand Down Expand Up @@ -492,18 +504,23 @@ class val {
}

template<typename T>
val operator[](const T& key) const {
return val(internal::_emval_get_property(as_handle(), val_ref(key).as_handle()));
val operator[](const T& key) const& {
return val(internal::_emval_get_property(as_handle(), js_ref(key)));
}

template<typename T>
val operator[](const T& key) && {
return val(internal::_emval_get_property(release_ownership_to_js(), js_ref(key)));
}

template<typename K, typename V>
void set(const K& key, const V& value) {
internal::_emval_set_property(as_handle(), val_ref(key).as_handle(), val_ref(value).as_handle());
internal::_emval_set_property(as_handle(), js_ref(key), js_ref(value));
}

template<typename T>
bool delete_(const T& property) const {
return internal::_emval_delete(as_handle(), val_ref(property).as_handle());
return internal::_emval_delete(as_handle(), js_ref(property));
}

template<typename... Args>
Expand Down Expand Up @@ -535,7 +552,7 @@ class val {
}

template<typename T, typename ...Policies>
T as(Policies...) const {
T as(Policies...) const& {
using namespace internal;

typedef BindingType<T> BT;
Expand All @@ -550,45 +567,105 @@ class val {
return fromGenericWireType<T>(result);
}

template<typename T, typename ...Policies>
T as(Policies...) && {
using namespace internal;

typedef BindingType<T> BT;
typename WithPolicies<Policies...>::template ArgTypeList<T> targetType;

EM_DESTRUCTORS destructors = nullptr;
EM_GENERIC_WIRE_TYPE result = _emval_as(
release_ownership_to_js(),
targetType.getTypes()[0],
&destructors);
DestructorsRunner dr(destructors);
return fromGenericWireType<T>(result);
}

#ifdef __wasm64__
template<>
long as<long>() const {
long as<long>() const& {
using namespace internal;

typedef BindingType<long> BT;
typename WithPolicies<>::template ArgTypeList<long> targetType;

return _emval_as_int64(as_handle(), targetType.getTypes()[0]);
}

template<>
long as<long>() &&{
using namespace internal;

typedef BindingType<long> BT;
typename WithPolicies<>::template ArgTypeList<long> targetType;

// TODO
return _emval_as_int64(as_handle(), targetType.getTypes()[0]);
}

template<>
unsigned long as<unsigned long>() const {
unsigned long as<unsigned long>() const& {
using namespace internal;

typedef BindingType<unsigned long> BT;
typename WithPolicies<>::template ArgTypeList<unsigned long> targetType;

return _emval_as_uint64(as_handle(), targetType.getTypes()[0]);
}

template<>
unsigned long as<unsigned long>() && {
using namespace internal;

typedef BindingType<unsigned long> BT;
typename WithPolicies<>::template ArgTypeList<unsigned long> targetType;

// TODO
return _emval_as_uint64(as_handle(), targetType.getTypes()[0]);
}
#endif

template<>
int64_t as<int64_t>() const {
int64_t as<int64_t>() const& {
using namespace internal;

typedef BindingType<int64_t> BT;
typename WithPolicies<>::template ArgTypeList<int64_t> targetType;

return _emval_as_int64(as_handle(), targetType.getTypes()[0]);
}

template<>
int64_t as<int64_t>() && {
using namespace internal;

typedef BindingType<int64_t> BT;
typename WithPolicies<>::template ArgTypeList<int64_t> targetType;

// TODO
return _emval_as_int64(as_handle(), targetType.getTypes()[0]);
}

template<>
uint64_t as<uint64_t>() const {
uint64_t as<uint64_t>() const& {
using namespace internal;

typedef BindingType<uint64_t> BT;
typename WithPolicies<>::template ArgTypeList<uint64_t> targetType;

return _emval_as_uint64(as_handle(), targetType.getTypes()[0]);
}

template<>
uint64_t as<uint64_t>() && {
using namespace internal;

typedef BindingType<uint64_t> BT;
typename WithPolicies<>::template ArgTypeList<uint64_t> targetType;

// TODO
return _emval_as_uint64(as_handle(), targetType.getTypes()[0]);
}

Expand Down Expand Up @@ -664,13 +741,28 @@ class val {
return fromGenericWireType<Ret>(result);
}

// Invalidates this instance and returns handle for JS ownership.
// Expected to be immediately use by Emval.toValue() which will decref
// the handle.
// Takes advantage of the even-only property of handle to encode this
// ownership transfer as an odd handle.
EM_VAL release_ownership_to_js() {
EM_VAL taken = as_handle();
handle = 0;
return reinterpret_cast<EM_VAL>(reinterpret_cast<uint32_t>(taken) + 1);
}

template<typename T>
val val_ref(const T& v) const {
return val(v);
static EM_VAL js_ref(const T& v) {
return val(v).release_ownership_to_js();
}

static EM_VAL js_ref(val&& v) {
return v.release_ownership_to_js();
}

const val& val_ref(const val& v) const {
return v;
static EM_VAL js_ref(const val& v) {
return v.as_handle();
}

pthread_t thread;
Expand Down
Loading