diff --git a/src/java.cpp b/src/java.cpp index fb5cfbd..990f5a9 100644 --- a/src/java.cpp +++ b/src/java.cpp @@ -22,6 +22,7 @@ #endif threadId v8ThreadId; +bool isDefaultLoopRunning = false; std::queue queue_dynamicProxyJsCallData; uv_mutex_t uvMutex_dynamicProxyJsCall; @@ -78,9 +79,9 @@ void uvAsyncCb_dynamicProxyJsCall(uv_async_t *handle) { Nan::HandleScope scope; v8ThreadId = my_getThreadId(); + isDefaultLoopRunning = false; //init as false uv_mutex_init(&uvMutex_dynamicProxyJsCall); - uv_async_init(uv_default_loop(), &uvAsync_dynamicProxyJsCall, uvAsyncCb_dynamicProxyJsCall); v8::Local t = Nan::New(New); s_ct.Reset(t); @@ -106,6 +107,7 @@ void uvAsyncCb_dynamicProxyJsCall(uv_async_t *handle) { Nan::SetPrototypeMethod(t, "getStaticFieldValue", getStaticFieldValue); Nan::SetPrototypeMethod(t, "setStaticFieldValue", setStaticFieldValue); Nan::SetPrototypeMethod(t, "instanceOf", instanceOf); + Nan::SetPrototypeMethod(t, "stop", stop); Nan::Set(target, Nan::New("Java").ToLocalChecked(), Nan::GetFunction(t).ToLocalChecked()); @@ -115,6 +117,11 @@ void uvAsyncCb_dynamicProxyJsCall(uv_async_t *handle) { NAN_METHOD(Java::New) { Nan::HandleScope scope; + if (!isDefaultLoopRunning) { + uv_async_init(uv_default_loop(), &uvAsync_dynamicProxyJsCall, uvAsyncCb_dynamicProxyJsCall); + isDefaultLoopRunning = true; + } + Java *self = new Java(); self->Wrap(info.This()); @@ -1239,6 +1246,12 @@ NAN_METHOD(Java::instanceOf) { info.GetReturnValue().Set(Nan::New(res)); } +NAN_METHOD(Java::stop) { + if (isDefaultLoopRunning) { + uv_close((uv_handle_t *)&uvAsync_dynamicProxyJsCall, NULL); + } +} + template std::string to_string(T value) { std::ostringstream os; diff --git a/src/java.h b/src/java.h index 7f03657..f3a0bc9 100644 --- a/src/java.h +++ b/src/java.h @@ -56,6 +56,7 @@ class Java : public Nan::ObjectWrap { static NAN_METHOD(getStaticFieldValue); static NAN_METHOD(setStaticFieldValue); static NAN_METHOD(instanceOf); + static NAN_METHOD(stop); static NAN_GETTER(AccessorProhibitsOverwritingGetter); static NAN_SETTER(AccessorProhibitsOverwritingSetter); v8::Local ensureJvm();