diff --git a/crates/napi/src/bindgen_runtime/module_register.rs b/crates/napi/src/bindgen_runtime/module_register.rs index 07e86dcf..a2cbf83e 100644 --- a/crates/napi/src/bindgen_runtime/module_register.rs +++ b/crates/napi/src/bindgen_runtime/module_register.rs @@ -249,8 +249,16 @@ unsafe extern "C" fn napi_register_module_v1( } pub(crate) unsafe extern "C" fn noop( - _env: sys::napi_env, + env: sys::napi_env, _info: sys::napi_callback_info, ) -> sys::napi_value { + if !crate::bindgen_runtime::___CALL_FROM_FACTORY.load(std::sync::atomic::Ordering::Relaxed) { + sys::napi_throw_error( + env, + ptr::null_mut(), + CStr::from_bytes_with_nul_unchecked(b"Class contains no `constructor`, can not new it!") + .as_ptr(), + ); + } ptr::null_mut() } diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index e2a30783..dfa1a192 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -132,6 +132,13 @@ test('class factory', (t) => { doge.name = '旺财' t.is(doge.name, '旺财') + + const error = t.throws(() => new ClassWithFactory()) + t.true( + error.message.startsWith( + 'Class contains no `constructor`, can not new it!', + ), + ) }) test('class constructor return Result', (t) => {