diff --git a/process/js_childprocess.cpp b/process/js_childprocess.cpp index 6243aee..cda013c 100644 --- a/process/js_childprocess.cpp +++ b/process/js_childprocess.cpp @@ -112,7 +112,7 @@ namespace OHOS::Js_sys_module::Process { { napi_value promise = nullptr; auto waitInfo = new WaitInfo; - NAPI_CALL(env_, napi_create_promise(env_, &(waitInfo->deferred), &promise)); + napi_create_promise(env_, &(waitInfo->deferred), &promise); if (isWait_) { int32_t status; @@ -122,8 +122,8 @@ namespace OHOS::Js_sys_module::Process { } isNeedRun_ = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_create_int32(env_, exitCode_, &result)); - NAPI_CALL(env_, napi_resolve_deferred(env_, waitInfo->deferred, result)); + napi_create_int32(env_, exitCode_, &result); + napi_resolve_deferred(env_, waitInfo->deferred, result); delete waitInfo; waitInfo = nullptr; diff --git a/process/js_process.cpp b/process/js_process.cpp index 5bdcbe4..4b6a424 100644 --- a/process/js_process.cpp +++ b/process/js_process.cpp @@ -33,216 +33,219 @@ namespace OHOS::Js_sys_module::Process { constexpr int NUM_OF_DATA = 4; constexpr int MAX_PATH = 260; } -Process::Process(napi_env env_) : env(env_) {} -napi_value Process::GetUid() const -{ - napi_value result = nullptr; - auto processGetuid = static_cast(getuid()); - NAPI_CALL(env, napi_create_uint32(env, processGetuid, &result)); - return result; -} - -napi_value Process::GetGid() const -{ - napi_value result = nullptr; - auto processGetgid = static_cast(getgid()); - NAPI_CALL(env, napi_create_uint32(env, processGetgid, &result)); - return result; -} - -napi_value Process::GetEUid() const -{ - napi_value result = nullptr; - auto processGeteuid = static_cast(geteuid()); - NAPI_CALL(env, napi_create_uint32(env, processGeteuid, &result)); - return result; -} - -napi_value Process::GetEGid() const -{ - napi_value result = nullptr; - auto processGetegid = static_cast(getegid()); - NAPI_CALL(env, napi_create_uint32(env, processGetegid, &result)); - return result; -} - -napi_value Process::GetGroups() const -{ - napi_value result = nullptr; - int progroups = getgroups(0, nullptr); - if (progroups == -1) { - napi_throw_error(env, "-1", "getgroups initialize failed"); + std::map Process::m_map_process_event_; + Process::Process(napi_env env_) : env(env_) {} + napi_value Process::GetUid() const + { + napi_value result = nullptr; + auto processGetuid = static_cast(getuid()); + NAPI_CALL(env, napi_create_uint32(env, processGetuid, &result)); + return result; } - std::vector pgrous(progroups); - progroups = getgroups(progroups, pgrous.data()); - if (progroups == -1) { - napi_throw_error(env, "-1", "getgroups"); - } - pgrous.resize(progroups); - gid_t proegid = getegid(); - if (std::find(pgrous.begin(), pgrous.end(), proegid) == pgrous.end()) { - pgrous.push_back(proegid); - } - std::vector arry; - for (auto iter = pgrous.begin(); iter != pgrous.end(); iter++) { - auto recive = static_cast(*iter); - arry.push_back(recive); - } - NAPI_CALL(env, napi_create_array(env, &result)); - size_t len = arry.size(); - for (size_t i = 0; i < len; i++) { - napi_value numvalue = nullptr; - NAPI_CALL(env, napi_create_uint32(env, arry[i], &numvalue)); - NAPI_CALL(env, napi_set_element(env, result, i, numvalue)); - } - return result; -} -napi_value Process::GetPid() const -{ - napi_value result = nullptr; - auto proPid = static_cast(getpid()); - napi_create_int32(env, proPid, &result); - return result; -} + napi_value Process::GetGid() const + { + napi_value result = nullptr; + auto processGetgid = static_cast(getgid()); + NAPI_CALL(env, napi_create_uint32(env, processGetgid, &result)); + return result; + } -napi_value Process::GetPpid() const -{ - napi_value result = nullptr; - auto proPpid = static_cast(getppid()); - napi_create_int32(env, proPpid, &result); - return result; -} + napi_value Process::GetEUid() const + { + napi_value result = nullptr; + auto processGeteuid = static_cast(geteuid()); + NAPI_CALL(env, napi_create_uint32(env, processGeteuid, &result)); + return result; + } -void Process::Chdir(napi_value args) const -{ - size_t prolen = 0; - napi_get_value_string_utf8(env, args, nullptr, 0, &prolen); - char* path = nullptr; - if (prolen > 0) { - path = new char[prolen + 1]; - if (memset_s(path, prolen + 1, '\0', prolen + 1) != 0) { - napi_throw_error(env, "-1", "chdir path memset_s failed"); + napi_value Process::GetEGid() const + { + napi_value result = nullptr; + auto processGetegid = static_cast(getegid()); + NAPI_CALL(env, napi_create_uint32(env, processGetegid, &result)); + return result; + } + + napi_value Process::GetGroups() const + { + napi_value result = nullptr; + int progroups = getgroups(0, nullptr); + if (progroups == -1) { + napi_throw_error(env, "-1", "getgroups initialize failed"); } - } else { - napi_throw_error(env, "-2", "prolen is error !"); - } - napi_get_value_string_utf8(env, args, path, prolen + 1, &prolen); - int proerr = 0; - if (path != nullptr) { - proerr = uv_chdir(path); - delete []path; + std::vector pgrous(progroups); + progroups = getgroups(progroups, pgrous.data()); + if (progroups == -1) { + napi_throw_error(env, "-1", "getgroups"); + } + pgrous.resize(progroups); + gid_t proegid = getegid(); + if (std::find(pgrous.begin(), pgrous.end(), proegid) == pgrous.end()) { + pgrous.push_back(proegid); + } + std::vector arry; + for (auto iter = pgrous.begin(); iter != pgrous.end(); iter++) { + auto recive = static_cast(*iter); + arry.push_back(recive); + } + NAPI_CALL(env, napi_create_array(env, &result)); + size_t len = arry.size(); + for (size_t i = 0; i < len; i++) { + napi_value numvalue = nullptr; + NAPI_CALL(env, napi_create_uint32(env, arry[i], &numvalue)); + NAPI_CALL(env, napi_set_element(env, result, i, numvalue)); + } + return result; } - if (proerr) { - napi_throw_error(env, "-1", "chdir"); + napi_value Process::GetPid() const + { + napi_value result = nullptr; + auto proPid = static_cast(getpid()); + napi_create_int32(env, proPid, &result); + return result; } -} -napi_value Process::Kill(napi_value proid, napi_value signal) -{ - int32_t pid = 0; - int32_t sig = 0; - napi_get_value_int32(env, proid, &pid); - napi_get_value_int32(env, signal, &sig); - uv_pid_t ownPid = uv_os_getpid(); - if (sig > 64 && - (pid == 0 || pid == -1 || pid == ownPid || pid == -ownPid)) { - napi_throw_error(env, "0", "process exit"); + napi_value Process::GetPpid() const + { + napi_value result = nullptr; + auto proPpid = static_cast(getppid()); + napi_create_int32(env, proPpid, &result); + return result; } - bool flag = false; - int err = uv_kill(pid, sig); - if (!err) { - flag = true; - } - napi_value result = nullptr; - NAPI_CALL(env, napi_get_boolean(env, flag, &result)); - return result; -} -napi_value Process::Uptime() const -{ - napi_value result = nullptr; - struct sysinfo information; - time_t systimer = 0; - double runsystime = 0.0; - if (sysinfo(&information)) { - napi_throw_error(env, "-1", "Failed to get sysinfo"); - } - systimer = information.uptime; - if (systimer > 0) { - runsystime = static_cast(systimer); - NAPI_CALL(env, napi_create_double(env, runsystime, &result)); - } else { - napi_throw_error(env, "-1", "Failed to get systimer"); - } - return result; -} - -void Process::Exit(napi_value number) const -{ - int32_t result = 0; - napi_get_value_int32(env, number, &result); - exit(result); -} - -napi_value Process::Cwd() const -{ - napi_value result = nullptr; - char buf[MAX_PATH * NUM_OF_DATA] = { 0 }; - size_t length = sizeof(buf); - int err = uv_cwd(buf, &length); - if (err) { - napi_throw_error(env, "1", "uv_cwd"); - } - napi_create_string_utf8(env, buf, length, &result); - return result; -} - -void Process::Abort() const -{ - abort(); -} - -void Process::On(napi_value str, napi_value function) -{ - char *buffer = nullptr; - size_t bufferSize = 0; - napi_get_value_string_utf8(env, str, buffer, 0, &bufferSize); - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]; - } - napi_get_value_string_utf8(env, str, buffer, bufferSize + 1, &bufferSize); - if (buffer != nullptr) { - map_event_[buffer] = function; - delete []buffer; - } -} -napi_value Process::Off(napi_value str) -{ - char *buffer = nullptr; - size_t bufferSize = 0; - bool flag = true; - napi_value result = nullptr; - napi_get_value_string_utf8(env, str, buffer, 0, &bufferSize); - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]; - } - napi_get_value_string_utf8(env, str, buffer, bufferSize + 1, &bufferSize); - std::string temp = buffer; - if (buffer != nullptr) { - delete[] buffer; - } - for (auto iter = map_event_.cbegin(); iter != map_event_.cend(); ++iter) { - if (iter->first == temp) { - map_event_.erase(temp); - NAPI_CALL(env, napi_get_boolean(env, flag, &result)); - return result; + void Process::Chdir(napi_value args) const + { + size_t prolen = 0; + napi_get_value_string_utf8(env, args, nullptr, 0, &prolen); + char* path = nullptr; + if (prolen > 0) { + path = new char[prolen + 1]; + if (memset_s(path, prolen + 1, '\0', prolen + 1) != 0) { + napi_throw_error(env, "-1", "chdir path memset_s failed"); + } + } else { + napi_throw_error(env, "-2", "prolen is error !"); + } + napi_get_value_string_utf8(env, args, path, prolen + 1, &prolen); + int proerr = 0; + if (path != nullptr) { + proerr = uv_chdir(path); + delete []path; + path = nullptr; + } + if (proerr) { + napi_throw_error(env, "-1", "chdir"); } } - flag = false; - NAPI_CALL(env, napi_get_boolean(env, flag, &result)); - return result; -} + napi_value Process::Kill(napi_value proid, napi_value signal) + { + int32_t pid = 0; + int32_t sig = 0; + napi_get_value_int32(env, proid, &pid); + napi_get_value_int32(env, signal, &sig); + uv_pid_t ownPid = uv_os_getpid(); + // 64:The maximum valid signal value is 64. + if (sig > 64 && (pid == 0 || pid == -1 || pid == ownPid || pid == -ownPid)) { + napi_throw_error(env, "0", "process exit"); + } + bool flag = false; + int err = uv_kill(pid, sig); + if (!err) { + flag = true; + } + napi_value result = nullptr; + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + + napi_value Process::Uptime() const + { + napi_value result = nullptr; + struct sysinfo information = {0}; + time_t systimer = 0; + double runsystime = 0.0; + if (sysinfo(&information)) { + napi_throw_error(env, "-1", "Failed to get sysinfo"); + } + systimer = information.uptime; + if (systimer > 0) { + runsystime = static_cast(systimer); + NAPI_CALL(env, napi_create_double(env, runsystime, &result)); + } else { + napi_throw_error(env, "-1", "Failed to get systimer"); + } + return result; + } + + void Process::Exit(napi_value number) const + { + int32_t result = 0; + napi_get_value_int32(env, number, &result); + exit(result); + } + + napi_value Process::Cwd() const + { + napi_value result = nullptr; + char buf[MAX_PATH * NUM_OF_DATA] = { 0 }; + size_t length = sizeof(buf); + int err = uv_cwd(buf, &length); + if (err) { + napi_throw_error(env, "1", "uv_cwd"); + } + napi_create_string_utf8(env, buf, length, &result); + return result; + } + + void Process::Abort() const + { + abort(); + } + + void Process::On(napi_value str, napi_value function) + { + char *buffer = nullptr; + size_t bufferSize = 0; + napi_get_value_string_utf8(env, str, buffer, 0, &bufferSize); + if (bufferSize > 0) { + buffer = new char[bufferSize + 1]; + } + napi_get_value_string_utf8(env, str, buffer, bufferSize + 1, &bufferSize); + if (buffer != nullptr) { + m_map_process_event_[buffer] = function; + delete []buffer; + buffer = nullptr; + } + } + napi_value Process::Off(napi_value str) + { + char *buffer = nullptr; + size_t bufferSize = 0; + bool flag = true; + napi_value result = nullptr; + napi_get_value_string_utf8(env, str, buffer, 0, &bufferSize); + if (bufferSize > 0) { + buffer = new char[bufferSize + 1]; + } + napi_get_value_string_utf8(env, str, buffer, bufferSize + 1, &bufferSize); + std::string temp = ""; + if (buffer != nullptr) { + temp = buffer; + delete []buffer; + buffer = nullptr; + } + for (auto iter = m_map_process_event_.cbegin(); iter != m_map_process_event_.cend(); ++iter) { + if (iter->first == temp) { + m_map_process_event_.erase(temp); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + } + flag = false; + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } } \ No newline at end of file diff --git a/process/js_process.h b/process/js_process.h index a627a06..116fa9b 100644 --- a/process/js_process.h +++ b/process/js_process.h @@ -22,28 +22,28 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" namespace OHOS::Js_sys_module::Process { -class Process { -public: - explicit Process(napi_env env); - virtual~Process() {} - napi_value GetUid() const; - napi_value GetGid() const; - napi_value GetEUid() const; - napi_value GetEGid() const; - napi_value GetGroups() const; - napi_value GetPid() const; - napi_value GetPpid() const; - void Chdir(napi_value args) const; - napi_value Uptime() const; - napi_value Kill(napi_value proid, napi_value signal); - void Abort() const; - void On(napi_value str, napi_value function); - napi_value Off(napi_value str); - void Exit(napi_value number) const; - napi_value Cwd() const; -private: - napi_env env; - std::map map_event_; -}; + class Process { + public: + explicit Process(napi_env env); + virtual~Process() {} + napi_value GetUid() const; + napi_value GetGid() const; + napi_value GetEUid() const; + napi_value GetEGid() const; + napi_value GetGroups() const; + napi_value GetPid() const; + napi_value GetPpid() const; + void Chdir(napi_value args) const; + napi_value Uptime() const; + napi_value Kill(napi_value proid, napi_value signal); + void Abort() const; + void On(napi_value str, napi_value function); + napi_value Off(napi_value str); + void Exit(napi_value number) const; + napi_value Cwd() const; + private: + napi_env env; + static std::map m_map_process_event_; + }; } #endif \ No newline at end of file diff --git a/process/native_module_process.cpp b/process/native_module_process.cpp index cc49699..4c0126f 100644 --- a/process/native_module_process.cpp +++ b/process/native_module_process.cpp @@ -82,14 +82,10 @@ namespace OHOS::Js_sys_module::Process { size_t argc = 2; napi_value args[2] = { nullptr }; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, &data)); - DealType(env, args, argc); auto objectInfo = new ChildProcess(env); - objectInfo->InitOptionsInfo(args[1]); - objectInfo->Spawn(args[0]); - NAPI_CALL(env, napi_wrap( env, thisVar, objectInfo, [](napi_env env, void* data, void* hint) { @@ -99,7 +95,6 @@ namespace OHOS::Js_sys_module::Process { } }, nullptr, nullptr)); - return thisVar; } @@ -107,11 +102,9 @@ namespace OHOS::Js_sys_module::Process { { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); napi_value result = object->Wait(); - return result; } @@ -119,11 +112,9 @@ namespace OHOS::Js_sys_module::Process { { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); napi_value result = object->GetOutput(); - return result; } @@ -131,11 +122,9 @@ namespace OHOS::Js_sys_module::Process { { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); object->Close(); - napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); return result; @@ -145,12 +134,9 @@ namespace OHOS::Js_sys_module::Process { { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - napi_value result = object->GetErrorOutput(); - return result; } @@ -161,19 +147,15 @@ namespace OHOS::Js_sys_module::Process { size_t argc = 1; napi_value args = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr)); - NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments"); - napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, args, &valuetype)); if ((valuetype != napi_valuetype::napi_number) && (valuetype != napi_valuetype::napi_string)) { napi_throw_error(env, nullptr, "The parameter type is incorrect"); } - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); object->Kill(args); - napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); return result; @@ -183,11 +165,9 @@ namespace OHOS::Js_sys_module::Process { { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); napi_value result = object->GetKilled(); - return result; } @@ -195,11 +175,9 @@ namespace OHOS::Js_sys_module::Process { { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); napi_value result = object->Getpid(); - return result; } @@ -207,11 +185,9 @@ namespace OHOS::Js_sys_module::Process { { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); napi_value result = object->Getppid(); - return result; } @@ -219,11 +195,9 @@ namespace OHOS::Js_sys_module::Process { { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); napi_value result = object->GetExitCode(); - return result; } @@ -233,7 +207,6 @@ namespace OHOS::Js_sys_module::Process { size_t argc = 2; napi_value args[2] = { nullptr }; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); - const char* childProcessClassName = "ChildProcess"; napi_value childProcessClass = nullptr; static napi_property_descriptor childProcessDesc[] = { @@ -253,91 +226,49 @@ namespace OHOS::Js_sys_module::Process { &childProcessClass)); napi_value result = nullptr; NAPI_CALL(env, napi_new_instance(env, childProcessClass, argc, args, &result)); - return result; } - static napi_value ProcessConstructor(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - void* data = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data)); - auto objectInfo = new Process(env); - napi_wrap( - env, thisVar, objectInfo, - [](napi_env env, void* data, void* hint) { - auto objectInfo = (Process*)data; - if (objectInfo != nullptr) { - delete objectInfo; - } - }, - nullptr, nullptr); - return thisVar; - } - static napi_value GetUid(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - return object->GetUid(); + Process object(env); + return object.GetUid(); } static napi_value GetGid(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - Process* object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); - return object->GetGid(); + Process object(env); + return object.GetGid(); } static napi_value GetEUid(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - return object->GetEUid(); + Process object(env); + return object.GetEUid(); } static napi_value GetEGid(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - return object->GetEGid(); + Process object(env); + return object.GetEGid(); } static napi_value GetGroups(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - return object->GetGroups(); + Process object(env); + return object.GetGroups(); } static napi_value GetPid(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->GetPid(); - return result; + Process object(env); + return object.GetPid(); } static napi_value GetPpid(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->GetPpid(); - return result; + Process object(env); + return object.GetPpid(); } static napi_value Chdir(napi_env env, napi_callback_info info) @@ -351,9 +282,8 @@ namespace OHOS::Js_sys_module::Process { napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, args, &valuetype)); NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected"); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - object->Chdir(args); + Process object(env); + object.Chdir(args); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); return result; @@ -361,11 +291,8 @@ namespace OHOS::Js_sys_module::Process { static napi_value Abort(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - object->Abort(); + Process object(env); + object.Abort(); napi_value res = nullptr; NAPI_CALL(env, napi_get_undefined(env, &res)); return res; @@ -373,12 +300,8 @@ namespace OHOS::Js_sys_module::Process { static napi_value Cwd(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->Cwd(); - return result; + Process object(env); + return object.Cwd(); } static napi_value Exit(napi_env env, napi_callback_info info) @@ -389,10 +312,9 @@ namespace OHOS::Js_sys_module::Process { napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, args, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error"); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - object->Exit(args); + NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.number error"); + Process object(env); + object.Exit(args); napi_value res = nullptr; NAPI_CALL(env, napi_get_undefined(env, &res)); return res; @@ -404,7 +326,7 @@ namespace OHOS::Js_sys_module::Process { size_t argc = 2; napi_value args[2] = { nullptr }; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); - NAPI_ASSERT(env, argc >= requireArgc, "Wrong nuamber of arguments"); + NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments"); napi_valuetype valuetype0; NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); if (valuetype0 != napi_valuetype::napi_string) { @@ -415,34 +337,28 @@ namespace OHOS::Js_sys_module::Process { } napi_valuetype valuetype1; NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - object->On(args[0], args[1]); + Process object(env); + object.On(args[0], args[1]); napi_value res = nullptr; NAPI_CALL(env, napi_get_undefined(env, &res)); return res; } - napi_value Off(napi_value str); static napi_value Off(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; size_t argc = 1; napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->Off(args); + Process object(env); + napi_value result = object.Off(args); return result; } static napi_value Uptime(napi_env env, napi_callback_info info) { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Process* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - return object->Uptime(); + Process object(env); + return object.Uptime(); } static napi_value KillSig(napi_env env, napi_callback_info info) @@ -452,18 +368,16 @@ namespace OHOS::Js_sys_module::Process { napi_value thisVar = nullptr; void* data = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - Process* object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + Process object(env); napi_value result = nullptr; - result = object->Kill(argv[0], argv[1]); + result = object.Kill(argv[0], argv[1]); return result; } - static napi_value ProcessInit(napi_env env, napi_value exports) + static napi_value Init(napi_env env, napi_value exports) { - const char* processClassName = "Process"; - napi_value processClass = nullptr; - static napi_property_descriptor processDesc[] = { + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("runCmd", RunCommand), DECLARE_NAPI_GETTER("getUid", GetUid), DECLARE_NAPI_GETTER("getGid", GetGid), DECLARE_NAPI_GETTER("getEuid", GetEUid), @@ -480,26 +394,10 @@ namespace OHOS::Js_sys_module::Process { DECLARE_NAPI_FUNCTION("off", Off), DECLARE_NAPI_FUNCTION("exit", Exit), }; - NAPI_CALL(env, napi_define_class(env, processClassName, strlen(processClassName), ProcessConstructor, - nullptr, sizeof(processDesc) / sizeof(processDesc[0]), processDesc, - &processClass)); - static napi_property_descriptor desc[] = { - DECLARE_NAPI_PROPERTY("Process", processClass) - }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; } - static napi_value Init(napi_env env, napi_value exports) - { - napi_property_descriptor desc[] = { - DECLARE_NAPI_FUNCTION("runCmd", RunCommand), - }; - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - ProcessInit(env, exports); - return exports; - } - static napi_module processModule = { .nm_version = 1, .nm_flags = 0,