-
Notifications
You must be signed in to change notification settings - Fork 119
Open
Description
Summary
Currently, when you declare a class, you can call it as a function:
// C++
v8pp::class_<MyClass> MyClass_class(isolate);
MyClass_class.ctor<const FunctionCallbackInfo<Value>&>();// JS
const test1 = new addon.MyClass(10); // right way to do
const test2 = addon.MyClass(10); // currently working, wronglytest2 asssignation should cause an error:
Uncaught TypeError: Class constructor MyClass cannot be invoked without 'new'
attempting to "call" a class without new will result in an error.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_classes#constructing_a_class
Possible solution
You can use IsConstructCall on v8::FunctionCallbackInfo to check if the class is instanciated with new here:
Lines 225 to 228 in 5759d79
| class_info_.set_ctor([create = std::move(create)](v8::FunctionCallbackInfo<v8::Value> const& args) | |
| { | |
| return create(args); | |
| }); |
if(!args.IsConstructCall()) {
// Throw type error: Uncaught TypeError: Class constructor %s cannot be invoked without 'new'
return;
}Metadata
Metadata
Assignees
Labels
No labels