Skip to content

Commit e80d146

Browse files
committed
Add throw exception on callbacks.
1 parent 71982d6 commit e80d146

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

source/loaders/node_loader/source/node_loader_impl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,11 @@ napi_value node_loader_impl_napi_to_value_callback(napi_env env, napi_callback_i
10621062

10631063
napi_value result = node_loader_impl_value_to_napi(closure_cast.safe->node_impl, env, ret);
10641064

1065+
if (value_type_id(ret) == TYPE_THROWABLE)
1066+
{
1067+
napi_throw(env, result);
1068+
}
1069+
10651070
/* Set result finalizer */
10661071
node_loader_impl_finalizer(env, result, ret);
10671072

@@ -1277,6 +1282,7 @@ napi_value node_loader_impl_value_to_napi(loader_impl_node node_impl, napi_env e
12771282

12781283
node_loader_impl_exception(env, status);
12791284

1285+
/* Passing code here seems not to work, instead set it once the error value has been created */
12801286
status = napi_create_error(env, label_value, message_value, &v);
12811287

12821288
node_loader_impl_exception(env, status);

source/loaders/node_loader/source/node_loader_port.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ napi_value node_loader_port_await(napi_env env, napi_callback_info info)
227227
/* Await to the function */
228228
void *ret = metacall_await_s(name, args, argc - 1, resolve, reject, ctx);
229229

230+
/* TODO: Is this needed? */
231+
/*
232+
if (metacall_value_id(ret) == METACALL_THROWABLE)
233+
{
234+
napi_value result = node_loader_impl_value_to_napi(node_impl, env, ret);
235+
236+
napi_throw(env, result);
237+
}
238+
*/
239+
230240
/* Release current reference of the environment */
231241
// node_loader_impl_env(node_impl, NULL);
232242

source/ports/node_port/test/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ describe('metacall', () => {
103103

104104
// TODO: Need a way to test the symbol is not globally defined.
105105
//assert.strictEqual(metacall('py_memory_export'), undefined);
106+
107+
108+
const handle_except = metacall_load_from_memory_export('py', 'def py_throw_error():\n\traise TypeError("yeet");\n');
109+
assert.notStrictEqual(handle_except, undefined);
110+
try {
111+
handle_except.py_throw_error();
112+
process.exit(1);
113+
} catch (e) {
114+
assert.strictEqual(e.message, 'yeet');
115+
// TODO: For some reason, N-API fails to set the code properly, review it
116+
// assert.strictEqual(e.code, 'TypeError');
117+
}
106118
});
107119
// Cobol tests are conditional (in order to pass CI/CD)
108120
if (process.env['OPTION_BUILD_LOADERS_COB']) {

0 commit comments

Comments
 (0)