Skip to content

Commit 6f12141

Browse files
authored
Make emscripten::val default constructible (#17384)
The default ctor of `val` was deleted and it could not be default constructed. This limited some usages such as create an array of it but without an explicit initialization list. (More: https://en.wikipedia.org/wiki/Default_constructor) The default constructed val is same as val::undefined().
1 parent 541a3f9 commit 6f12141

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

system/include/emscripten/val.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class val {
399399
argv);
400400
}
401401

402-
val() = delete;
402+
val() : handle(EM_VAL(internal::_EMVAL_UNDEFINED)) {}
403403

404404
explicit val(const char* v)
405405
: handle(internal::_emval_new_cstring(v))

tests/embind/test_val.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ int main() {
141141
val::global().set("a", val("1"));
142142
ensure_js("a == '1'");
143143

144+
test("val()");
145+
val a;
146+
val::global().set("a", a);
147+
ensure_js("a === undefined");
148+
a = val(1);
149+
val::global().set("a", a);
150+
ensure_js("a == 1");
151+
val ar[2];
152+
ar[0] = val(1);
153+
ar[1] = val(2);
154+
val::global().set("a", val::array(ar, ar + 2));
155+
ensure_js("a instanceof Array");
156+
ensure_js_not("a instanceof Number");
157+
ensure_js("a[0] == 1");
158+
ensure_js("a[1] == 2");
159+
144160
test("bool isNull()");
145161
EM_ASM(
146162
a = null;

tests/embind/test_val.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test: val null()...
77
test: val global(const char* name = 0)...
88
test: template<typename T> explicit val(T&& value)...
99
test: val(const char* v)...
10+
test: val()...
1011
test: bool isNull()...
1112
test: bool isUndefined()...
1213
test: bool isTrue()...

0 commit comments

Comments
 (0)