Skip to content

Commit fe1d2ee

Browse files
authored
[embind] Add pointer policies for setting val object properties. (#24811)
After #24175, `val` required a pointer policy when creating a `val` with a pointer. In `val.set(key, value)`, a temporary `val` object is created, but there was no way to set the policy from user code. This patch allows the user to pass in `allow_raw_pointers()` to enable this.
1 parent b8660f6 commit fe1d2ee

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

system/include/emscripten/val.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,9 @@ class EMBIND_VISIBILITY_DEFAULT val {
478478
return val(internal::_emval_get_property(as_handle(), val_ref(key).as_handle()));
479479
}
480480

481-
template<typename K, typename V>
482-
void set(const K& key, const V& value) {
483-
internal::_emval_set_property(as_handle(), val_ref(key).as_handle(), val_ref(value).as_handle());
481+
template<typename K, typename V, typename... Policies>
482+
void set(const K& key, const V& value, Policies... policies) {
483+
internal::_emval_set_property(as_handle(), val_ref(key).as_handle(), val_ref(value, policies...).as_handle());
484484
}
485485

486486
template<typename T>
@@ -609,9 +609,9 @@ class EMBIND_VISIBILITY_DEFAULT val {
609609
return BindingType<Ret>::fromWireType(result);
610610
}
611611

612-
template<typename T>
613-
val val_ref(const T& v) const {
614-
return val(v);
612+
template<typename T, typename... Policies>
613+
val val_ref(const T& v, Policies... policies) const {
614+
return val(v, policies...);
615615
}
616616

617617
const val& val_ref(const val& v) const {

test/embind/test_val.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,13 @@ int main() {
676676
val::global().set("a", val::u16string(s));
677677
ensure_js("a == '😃 = \U0001F603 is :-D'");
678678

679+
test("val set() with policy");
680+
Dummy *dummy = new Dummy();
681+
val::global().set("a", dummy, allow_raw_pointers());
682+
ensure_js("a instanceof Module.Dummy");
683+
val::global().set("a", val::null());
684+
delete dummy;
685+
679686
printf("end\n");
680687
return 0;
681688
}

test/embind/test_val.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
start
12
test: val array()...
23
test: template<typename T> val array(const std::vector<T> vec)...
34
test: template<typename Iter> val array(Iter begin, Iter end)...
@@ -41,4 +42,5 @@ test: template<typename T> std::vector<T *> vecFromJSArray(const val& v)...
4142
test: template<typename T> std::vector<T> convertJSArrayToNumberVector(const val& v)...
4243
test: val u8string(const char* s)...
4344
test: val u16string(const char16_t* s)...
45+
test: val set() with policy...
4446
end

0 commit comments

Comments
 (0)