Skip to content

Commit

Permalink
fix deleted val problem caused by move assignment to itself (emscri…
Browse files Browse the repository at this point in the history
…pten-core#20072)

The implementation of `val& operator=(val&& v)`
should not directly call `_emval_decref(handle)`.
According to the semantics of move & assignment,
the input argument(v) need to be reset first,
and then the internal handle is judged to be
a valid handle before calling `_emval_decref`.
This avoids the loss of reference problem that
occurs when moving the assignment to itself (this).
  • Loading branch information
medns authored Aug 18, 2023
1 parent 91641db commit 3c73fbb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions system/include/emscripten/val.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,12 @@ class val {
}

val& operator=(val&& v) & {
internal::_emval_decref(handle);
handle = v.handle;
auto v_handle = v.handle;
v.handle = 0;
if (handle) {
internal::_emval_decref(handle);
}
handle = v_handle;
return *this;
}

Expand Down
11 changes: 11 additions & 0 deletions test/embind/test_val.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ int main() {
ensure_not(val::global("c").isArray());
ensure_not(val::global("d").isArray());

test("val& operator=(val&& v)");
val source(val::object());
val target(val::object());
source.set("val", 1);
target = std::move(source);
ensure(target["val"].as<int>() == 1);
// move and assign to itself
target.set("val", 2);
target = std::move(target);
ensure(target["val"].as<int>() == 2);

test("bool equals(const val& v)");
EM_ASM(
a = 1;
Expand Down
1 change: 1 addition & 0 deletions test/embind/test_val.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test: bool isFalse()...
test: bool isNumber()...
test: bool isString()...
test: bool isArray()...
test: val& operator=(val&& v)...
test: bool equals(const val& v)...
test: bool operator==(const val& v)...
test: bool operator!=(const val& v)...
Expand Down

0 comments on commit 3c73fbb

Please sign in to comment.