feat(napi): throw error on non constructable class

This commit is contained in:
LongYinan 2021-12-08 17:59:30 +08:00
parent 16fa7a159e
commit 7470407306
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
2 changed files with 16 additions and 1 deletions

View file

@ -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()
}

View file

@ -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) => {