fixed async this reference bugs

Signed-off-by: Sven Wang <wanghancai@huawei.com>
This commit is contained in:
Sven Wang 2021-08-03 21:53:29 +08:00
parent 1f3223745a
commit c99e56064e
3 changed files with 8 additions and 0 deletions

View File

@ -91,6 +91,7 @@ private:
struct AsyncContext {
std::shared_ptr<Context> ctx = nullptr;
napi_ref callback = nullptr;
napi_ref self = nullptr;
napi_deferred defer = nullptr;
napi_async_work work = nullptr;
};

View File

@ -39,6 +39,7 @@ AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Cont
}
context_->ctx = std::move(context);
NAPI_CALL_RETURN_VOID(env, (*context_->ctx)(env, argc, argv, self));
napi_create_reference(env, self, 1, &context_->self);
}
AsyncCall::~AsyncCall()
@ -55,6 +56,10 @@ napi_value AsyncCall::Call(napi_env env, Context::ExecAction exec)
if (context_ == nullptr) {
return nullptr;
}
if (context_->ctx == nullptr) {
ZLOGD("context_ ctx is null");
return nullptr;
}
ZLOGD("async call exec");
context_->ctx->exec_ = std::move(exec);
napi_value promise = nullptr;
@ -139,6 +144,7 @@ void AsyncCall::DeleteContext(napi_env env, AsyncContext *context)
{
if (env != nullptr) {
napi_delete_reference(env, context->callback);
napi_delete_reference(env, context->self);
napi_delete_async_work(env, context->work);
}
delete context;

View File

@ -39,6 +39,7 @@ DistributedKv::Options JSUtil::Convert2Options(napi_env env, napi_value jsOption
napi_get_value_bool(env, value, &options.backup);
}
value = nullptr;
options.autoSync = false;
napi_get_named_property(env, jsOptions, "autoSync", &value);
if (value != nullptr) {
napi_get_value_bool(env, value, &options.autoSync);