Skip to content

Commit bbb5759

Browse files
committed
CompileHandle Worker support.
1 parent 4849ab7 commit bbb5759

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/compilerhandle.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33

44
namespace lv{
55

6-
Napi::FunctionReference CompilerHandle::constructor;
7-
8-
Napi::Object CompilerHandle::Init(Napi::Env env, Napi::Object exports) {
6+
Napi::Function CompilerHandle::Init(Napi::Env env, Napi::Object exports) {
97
Napi::Function func = DefineClass(env, "CompilerHandle", {});
10-
11-
constructor = Napi::Persistent(func);
12-
constructor.SuppressDestruct();
13-
14-
exports.Set("CompilerHandle", func);
15-
return exports;
8+
return func;
169
}
1710

1811
CompilerHandle::CompilerHandle(const Napi::CallbackInfo& info)

src/compilerhandle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
namespace lv{
55

66
class CompilerHandle : public Napi::ObjectWrap<CompilerHandle> {
7+
78
public:
8-
static Napi::Object Init(Napi::Env env, Napi::Object exports);
9-
static Napi::FunctionReference constructor;
9+
static Napi::Function Init(Napi::Env env, Napi::Object exports);
1010

1111
public:
1212
CompilerHandle(const Napi::CallbackInfo& info);

src/compilerwrap.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525

2626
namespace lv{
2727

28+
class CompilerAddonData{
29+
public:
30+
Napi::FunctionReference CompilerHandleConstructor;
31+
};
32+
33+
2834
void convertToMLNode(const Napi::Value& v, MLNode& n){
2935
if ( v.IsArray() ){
3036
n = MLNode(MLNode::Type::Array);
@@ -322,7 +328,6 @@ Napi::Value createCompilerWrap(const Napi::CallbackInfo &info){
322328
lv::el::Compiler::Config config;
323329
config.initialize(compilerOptions);
324330
lv::el::Compiler::Ptr compiler = lv::el::Compiler::create(config);
325-
326331

327332
auto externalCompilerPtr = Napi::External<lv::el::Compiler::Ptr>::New(
328333
env,
@@ -332,8 +337,9 @@ Napi::Value createCompilerWrap(const Napi::CallbackInfo &info){
332337
}
333338
);
334339

340+
CompilerAddonData* compilerData = info.Env().GetInstanceData<CompilerAddonData>();
335341

336-
Napi::Object obj = CompilerHandle::constructor.New({ externalCompilerPtr });
342+
Napi::Object obj = compilerData->CompilerHandleConstructor.New({ externalCompilerPtr });
337343
Napi::Object result = Napi::Object::New(env);
338344
result.Set("value", obj);
339345
return result;
@@ -458,12 +464,22 @@ void runCompilerWrap(const Napi::CallbackInfo &info){
458464
}
459465

460466
Napi::Object Init(Napi::Env env, Napi::Object exports) {
461-
CompilerHandle::Init(env, exports);
467+
auto addonData = new CompilerAddonData();
462468

469+
// Export CompilerHandle
470+
Napi::Function funcCompilerHandle = CompilerHandle::Init(env, exports);
471+
addonData->CompilerHandleConstructor = Napi::Persistent(funcCompilerHandle);
472+
exports.Set("CompilerHandle", funcCompilerHandle);
473+
474+
// Export functions
463475
exports.Set("compile", Napi::Function::New(env, lv::compileWrap));
464476
exports.Set("compileModule", Napi::Function::New(env, lv::compileModuleWrap));
465477
exports.Set("createCompiler", Napi::Function::New(env, lv::createCompilerWrap));
466478
exports.Set("runCompiler", Napi::Function::New(env, lv::runCompilerWrap));
479+
480+
// Set AddonData to instance data
481+
env.SetInstanceData<CompilerAddonData>(addonData);
482+
467483
return exports;
468484
}
469485

0 commit comments

Comments
 (0)