Skip to content

Commit 7d8a5df

Browse files
robforsBeuc
authored andcommitted
Adds 'throw' method to val. (emscripten-core#7405)
As mentioned in emscripten-core#6330. Something like this will now work: val::global("TypeError").new_("wrong type").throw_(); In the tests found in test_val.cpp it can be seen that an error can be thrown in CPP and successfully caught in JS.
1 parent fad2dc5 commit 7d8a5df

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

src/embind/emval.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@ var LibraryEmVal = {
453453
return delete object[property];
454454
},
455455

456+
_emval_throw__deps: ['$requireHandle'],
457+
_emval_throw: function(object) {
458+
object = requireHandle(object);
459+
throw object;
460+
},
461+
456462
};
457463

458464
mergeInto(LibraryManager.library, LibraryEmVal);

system/include/emscripten/val.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ namespace emscripten {
9797
bool _emval_instanceof(EM_VAL object, EM_VAL constructor);
9898
bool _emval_in(EM_VAL item, EM_VAL object);
9999
bool _emval_delete(EM_VAL object, EM_VAL property);
100+
bool _emval_throw(EM_VAL object);
100101
}
101102

102103
template<const char* address>
@@ -511,6 +512,10 @@ namespace emscripten {
511512
return internal::_emval_delete(handle, val(property).handle);
512513
}
513514

515+
void throw_() const {
516+
internal::_emval_throw(handle);
517+
}
518+
514519
private:
515520
// takes ownership, assumes handle already incref'd
516521
explicit val(internal::EM_VAL handle)

tests/embind/test_val.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ void ensure_js_not(string js_code)
5757
ensure_js(js_code);
5858
}
5959

60+
void throw_js_error(val js_error)
61+
{
62+
js_error.throw_();
63+
}
64+
65+
EMSCRIPTEN_BINDINGS(test_bindings)
66+
{
67+
emscripten::function("throw_js_error", &throw_js_error);
68+
}
69+
6070
int main()
6171
{
6272
printf("start\n");
@@ -512,6 +522,29 @@ int main()
512522
ensure_js_not("1 in a");
513523
ensure_js_not("'c' in a");
514524

525+
test("void throw_() const");
526+
EM_ASM(
527+
test_val_throw_ = function(error)
528+
{
529+
try
530+
{
531+
Module.throw_js_error(error);
532+
return false;
533+
}
534+
catch(error_thrown)
535+
{
536+
if (error_thrown != error)
537+
throw error_thrown;
538+
}
539+
return true;
540+
}
541+
);
542+
ensure_js("test_val_throw_(new Error)");
543+
ensure_js("test_val_throw_(Error)");
544+
ensure_js("test_val_throw_(2)");
545+
ensure_js("test_val_throw_('message')");
546+
ensure_js("test_val_throw_(new TypeError('message'))");
547+
515548
// this test should probably go elsewhere as it is not a member of val
516549
test("template<typename T> std::vector<T> vecFromJSArray(val v)");
517550
EM_ASM(

tests/embind/test_val.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ pass
224224
pass
225225
pass
226226
test:
227+
void throw_() const
228+
pass
229+
pass
230+
pass
231+
pass
232+
pass
233+
test:
227234
template<typename T> std::vector<T> vecFromJSArray(val v)
228235
pass
229236
pass

0 commit comments

Comments
 (0)