-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathV8Class.cpp
38 lines (28 loc) · 1.16 KB
/
V8Class.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "V8Helpers.h"
#include "V8Class.h"
v8::Local<v8::Value> V8Class::New(v8::Local<v8::Context> ctx, std::vector<v8::Local<v8::Value>>& args)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::FunctionTemplate> _tpl = tplMap.at(isolate).Get(isolate);
v8::Local<v8::Function> func;
if(!_tpl->GetFunction(ctx).ToLocal(&func))
{
V8Helpers::Throw(isolate, "V8Class::New Invalid constructor called");
return v8::Null(isolate);
}
v8::MaybeLocal<v8::Value> maybeObj = func->CallAsConstructor(ctx, args.size(), args.data());
if(maybeObj.IsEmpty()) return v8::Null(isolate);
return maybeObj.ToLocalChecked();
}
v8::Local<v8::Value> V8Class::CreateInstance(v8::Isolate* isolate, v8::Local<v8::Context> ctx, std::vector<v8::Local<v8::Value>> args)
{
v8::Local<v8::Function> constructor = JSValue(isolate, ctx);
v8::Local<v8::Value> obj;
V8Helpers::TryCatch([&] {
v8::MaybeLocal<v8::Value> maybeObj = constructor->CallAsConstructor(ctx, args.size(), args.data());
if(maybeObj.IsEmpty()) return false;
obj = maybeObj.ToLocalChecked();
return true;
});
return obj;
}