n-api: add napi_define_subclass
Add the ability to define a subclass of a JS class by generating a JS
snippet that uses the extends
syntax and calls the native bindings
which are passed in as parameters to the generated function responsible
for defining the subclass.
An example of a generated JS snippet might be:
(function(parent, ctor, getSuperParams, prop0, prop1) {
'use strict';
class NativeSubclass extends parent {
constructor() {
super(...getSuperParams.apply(null, arguments));
ctor.apply(this, arguments);
}
subMethod() {
if (!(this instanceof NativeSubclass))
throw new Error('Illegal invocation');
return prop0.apply(this, arguments);
}
chainableMethod() {
if (!(this instanceof NativeSubclass))
throw new Error('Illegal invocation');
return prop1.apply(this, arguments);
}
}
return NativeSubclass;
})
where ctor
, getSuperParams
, prop0
, and prop1
are native
functions supplied to napi_define_subclass
.
Signed-off-by: @gabrielschulhof
Checklist
-
make -j4 test
(UNIX), orvcbuild test
(Windows) passes -
tests and/or benchmarks are included -
documentation is changed or added -
commit message follows commit guidelines