diff --git a/process/js_childprocess.cpp b/process/js_childprocess.cpp index 368af1b..a95b69b 100644 --- a/process/js_childprocess.cpp +++ b/process/js_childprocess.cpp @@ -64,9 +64,7 @@ namespace OHOS::Js_sys_module::Process { {"SIGSYS", 31} }; - ChildProcess::ChildProcess(napi_env env) : env_(env) {} - - void ChildProcess::Spawn(napi_value command) + void ChildProcess::Spawn(napi_env env, napi_value command) { int ret = pipe(stdOutFd_); if (ret < 0) { @@ -78,7 +76,7 @@ namespace OHOS::Js_sys_module::Process { HILOG_ERROR("pipe2 failed %{public}d", errno); return; } - std::string strCommnd = RequireStrValue(command); + std::string strCommnd = RequireStrValue(env, command); pid_t pid = fork(); if (!pid) { close(stdErrFd_[0]); @@ -96,11 +94,11 @@ namespace OHOS::Js_sys_module::Process { } optionsInfo_->pid = pid; ppid_ = getpid(); - CreateWorker(); + CreateWorker(env); napi_value resourceName = nullptr; - napi_create_string_utf8(env_, "TimeoutListener", strlen("TimeoutListener"), &resourceName); + napi_create_string_utf8(env, "TimeoutListener", strlen("TimeoutListener"), &resourceName); napi_create_async_work( - env_, nullptr, resourceName, TimeoutListener, + env, nullptr, resourceName, TimeoutListener, [](napi_env env, napi_status status, void* data) { OptionsInfo* optionsInfo = reinterpret_cast(data); napi_delete_async_work(env, optionsInfo->worker); @@ -108,7 +106,7 @@ namespace OHOS::Js_sys_module::Process { optionsInfo = nullptr; }, reinterpret_cast(optionsInfo_), &optionsInfo_->worker); - napi_queue_async_work(env_, optionsInfo_->worker); + napi_queue_async_work(env, optionsInfo_->worker); close(stdErrFd_[1]); close(stdOutFd_[1]); } else { @@ -116,18 +114,18 @@ namespace OHOS::Js_sys_module::Process { } } - napi_value ChildProcess::Wait() + napi_value ChildProcess::Wait(napi_env env) { napi_value promise = nullptr; auto waitInfo = new WaitInfo; - napi_create_promise(env_, &(waitInfo->deferred), &promise); + napi_create_promise(env, &(waitInfo->deferred), &promise); if (isWait_) { int32_t status; isWait_ = false; if (optionsInfo_ == nullptr) { napi_value res = nullptr; - NAPI_CALL(env_, napi_get_undefined(env_, &res)); + NAPI_CALL(env, napi_get_undefined(env, &res)); HILOG_ERROR("optionsInfo_ is nullptr"); return res; } @@ -136,109 +134,109 @@ namespace OHOS::Js_sys_module::Process { } isNeedRun_ = false; napi_value result = nullptr; - napi_create_int32(env_, static_cast(exitCode_), &result); - napi_resolve_deferred(env_, waitInfo->deferred, result); + napi_create_int32(env, static_cast(exitCode_), &result); + napi_resolve_deferred(env, waitInfo->deferred, result); delete waitInfo; waitInfo = nullptr; return promise; } - napi_value ChildProcess::GetOutput() const + napi_value ChildProcess::GetOutput(napi_env env) const { if (stdOutInfo_ == nullptr) { napi_value res = nullptr; - NAPI_CALL(env_, napi_get_undefined(env_, &res)); + NAPI_CALL(env, napi_get_undefined(env, &res)); HILOG_ERROR("stdOutInfo_ is nullptr"); return res; } - NAPI_CALL(env_, napi_create_promise(env_, &stdOutInfo_->deferred, &stdOutInfo_->promise)); + NAPI_CALL(env, napi_create_promise(env, &stdOutInfo_->deferred, &stdOutInfo_->promise)); void* data = nullptr; napi_value arrayBuffer = nullptr; size_t bufferSize = stdOutInfo_->stdData.size() + 1; - NAPI_CALL(env_, napi_create_arraybuffer(env_, bufferSize, &data, &arrayBuffer)); + NAPI_CALL(env, napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer)); if (memcpy_s(data, bufferSize, reinterpret_cast(stdOutInfo_->stdData.c_str()), stdOutInfo_->stdData.size()) != EOK) { HILOG_ERROR("getOutput memcpy_s failed"); - NAPI_CALL(env_, napi_delete_async_work(env_, stdOutInfo_->worker)); + NAPI_CALL(env, napi_delete_async_work(env, stdOutInfo_->worker)); napi_value res = nullptr; - NAPI_CALL(env_, napi_get_undefined(env_, &res)); + NAPI_CALL(env, napi_get_undefined(env, &res)); return res; } napi_value result = nullptr; - NAPI_CALL(env_, napi_create_typedarray(env_, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); - NAPI_CALL(env_, napi_resolve_deferred(env_, stdOutInfo_->deferred, result)); + NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); + NAPI_CALL(env, napi_resolve_deferred(env, stdOutInfo_->deferred, result)); return stdOutInfo_->promise; } - napi_value ChildProcess::GetErrorOutput() const + napi_value ChildProcess::GetErrorOutput(napi_env env) const { if (stdErrInfo_ == nullptr) { napi_value res = nullptr; - NAPI_CALL(env_, napi_get_undefined(env_, &res)); + NAPI_CALL(env, napi_get_undefined(env, &res)); HILOG_ERROR("stdErrInfo_ is nullptr"); return res; } - NAPI_CALL(env_, napi_create_promise(env_, &stdErrInfo_->deferred, &stdErrInfo_->promise)); + NAPI_CALL(env, napi_create_promise(env, &stdErrInfo_->deferred, &stdErrInfo_->promise)); void* data = nullptr; napi_value arrayBuffer = nullptr; size_t bufferSize = stdErrInfo_->stdData.size() + 1; - NAPI_CALL(env_, napi_create_arraybuffer(env_, bufferSize, &data, &arrayBuffer)); + NAPI_CALL(env, napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer)); if (memcpy_s(data, bufferSize, reinterpret_cast(stdErrInfo_->stdData.c_str()), stdErrInfo_->stdData.size()) != EOK) { HILOG_ERROR("getErrOutput memcpy_s failed"); - NAPI_CALL(env_, napi_delete_async_work(env_, stdErrInfo_->worker)); + NAPI_CALL(env, napi_delete_async_work(env, stdErrInfo_->worker)); napi_value res = nullptr; - NAPI_CALL(env_, napi_get_undefined(env_, &res)); + NAPI_CALL(env, napi_get_undefined(env, &res)); return res; } napi_value result = nullptr; - NAPI_CALL(env_, napi_create_typedarray(env_, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); - NAPI_CALL(env_, napi_resolve_deferred(env_, stdErrInfo_->deferred, result)); + NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); + NAPI_CALL(env, napi_resolve_deferred(env, stdErrInfo_->deferred, result)); return stdErrInfo_->promise; } - napi_value ChildProcess::GetKilled() const + napi_value ChildProcess::GetKilled(napi_env env) const { napi_value result = nullptr; - NAPI_CALL(env_, napi_get_boolean(env_, killed_, &result)); + NAPI_CALL(env, napi_get_boolean(env, killed_, &result)); return result; } - napi_value ChildProcess::Getpid() const + napi_value ChildProcess::Getpid(napi_env env) const { napi_value result = nullptr; if (optionsInfo_ == nullptr) { napi_value res = nullptr; - NAPI_CALL(env_, napi_get_undefined(env_, &res)); + NAPI_CALL(env, napi_get_undefined(env, &res)); HILOG_ERROR("optionsInfo_ is nullptr"); return res; } - NAPI_CALL(env_, napi_create_int32(env_, optionsInfo_->pid, &result)); + NAPI_CALL(env, napi_create_int32(env, optionsInfo_->pid, &result)); return result; } - napi_value ChildProcess::Getppid() const + napi_value ChildProcess::Getppid(napi_env env) const { napi_value result = nullptr; - NAPI_CALL(env_, napi_create_int32(env_, ppid_, &result)); + NAPI_CALL(env, napi_create_int32(env, ppid_, &result)); return result; } - napi_value ChildProcess::GetExitCode() const + napi_value ChildProcess::GetExitCode(napi_env env) const { napi_value result = nullptr; - NAPI_CALL(env_, napi_create_int32(env_, static_cast(exitCode_), &result)); + NAPI_CALL(env, napi_create_int32(env, static_cast(exitCode_), &result)); return result; } - void ChildProcess::CreateWorker() + void ChildProcess::CreateWorker(napi_env env) { // getstdout napi_value resourceName = nullptr; @@ -255,10 +253,10 @@ namespace OHOS::Js_sys_module::Process { } stdOutInfo_->pid = optionsInfo_->pid; stdOutInfo_->maxBuffSize = optionsInfo_->maxBuffer; - napi_create_string_utf8(env_, "ReadStdOut", NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work(env_, nullptr, resourceName, ReadStdOut, EndStdOut, + napi_create_string_utf8(env, "ReadStdOut", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work(env, nullptr, resourceName, ReadStdOut, EndStdOut, reinterpret_cast(stdOutInfo_), &stdOutInfo_->worker); - napi_queue_async_work(env_, stdOutInfo_->worker); + napi_queue_async_work(env, stdOutInfo_->worker); // getstderr stdErrInfo_ = new StdInfo(); @@ -270,10 +268,10 @@ namespace OHOS::Js_sys_module::Process { stdErrInfo_->fd = stdErrFd_[0]; stdErrInfo_->pid = optionsInfo_->pid; stdErrInfo_->maxBuffSize = optionsInfo_->maxBuffer; - napi_create_string_utf8(env_, "ReadStdErr", NAPI_AUTO_LENGTH, &resourceName); - napi_create_async_work(env_, nullptr, resourceName, ReadStdErr, EndStdErr, + napi_create_string_utf8(env, "ReadStdErr", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work(env, nullptr, resourceName, ReadStdErr, EndStdErr, reinterpret_cast(stdErrInfo_), &stdErrInfo_->worker); - napi_queue_async_work(env_, stdErrInfo_->worker); + napi_queue_async_work(env, stdErrInfo_->worker); } void ChildProcess::ReadStdOut(napi_env env, void* data) @@ -346,16 +344,16 @@ namespace OHOS::Js_sys_module::Process { stdErrInfo = nullptr; } - int ChildProcess::GetValidSignal(const napi_value signo) + int ChildProcess::GetValidSignal(napi_env env, const napi_value signo) { int32_t sig = 0; napi_valuetype valuetype = napi_undefined; - napi_typeof(env_, signo, &valuetype); + napi_typeof(env, signo, &valuetype); if (valuetype == napi_valuetype::napi_number) { - napi_get_value_int32(env_, signo, &sig); + napi_get_value_int32(env, signo, &sig); return sig; } else if (valuetype == napi_valuetype::napi_string) { - std::string buffer = RequireStrValue(signo); + std::string buffer = RequireStrValue(env, signo); auto iter = g_signalsMap.find(buffer); if (iter != g_signalsMap.end()) { sig = iter->second; @@ -368,9 +366,9 @@ namespace OHOS::Js_sys_module::Process { } } - void ChildProcess::Kill(const napi_value signo) + void ChildProcess::Kill(napi_env env, const napi_value signo) { - int signal = GetValidSignal(signo); + int signal = GetValidSignal(env, signo); std::vector signalType = {SIGINT, SIGQUIT, SIGKILL, SIGTERM}; if (optionsInfo_ == nullptr) { HILOG_ERROR("optionsInfo_ is nullptr"); @@ -422,7 +420,7 @@ namespace OHOS::Js_sys_module::Process { } } - void ChildProcess::InitOptionsInfo(napi_value options) + void ChildProcess::InitOptionsInfo(napi_env env, napi_value options) { std::vector keyStr = {"timeout", "killSignal", "maxBuffer"}; optionsInfo_ = new OptionsInfo(); @@ -434,19 +432,19 @@ namespace OHOS::Js_sys_module::Process { for (size_t i = 0; i < size; i++) { napi_status status = napi_ok; napi_value property = nullptr; - napi_get_named_property(env_, options, keyStr[i].c_str(), &property); + napi_get_named_property(env, options, keyStr[i].c_str(), &property); switch (i) { case 0: - status = napi_get_value_int32(env_, property, &optionsInfo_->timeout); + status = napi_get_value_int32(env, property, &optionsInfo_->timeout); if (status != napi_ok) { optionsInfo_->timeout = 0; } break; case 1: - optionsInfo_->killSignal = GetValidSignal(property); + optionsInfo_->killSignal = GetValidSignal(env, property); break; case 2: // 2:The parameter value - status = napi_get_value_int64(env_, property, &optionsInfo_->maxBuffer); + status = napi_get_value_int64(env, property, &optionsInfo_->maxBuffer); if (status != napi_ok) { optionsInfo_->maxBuffer = static_cast(MAXSIZE) * static_cast(MAXSIZE); } @@ -458,17 +456,17 @@ namespace OHOS::Js_sys_module::Process { optionsInfo_->isNeedRun = &isNeedRun_; } - std::string ChildProcess::RequireStrValue(const napi_value strValue) + std::string ChildProcess::RequireStrValue(napi_env env, const napi_value strValue) { size_t bufferSize = 0; - if (napi_get_value_string_utf8(env_, strValue, nullptr, 0, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, strValue, nullptr, 0, &bufferSize) != napi_ok) { HILOG_ERROR("can not get strValue size"); return nullptr; } std::string result = ""; result.reserve(bufferSize + 1); result.resize(bufferSize); - if (napi_get_value_string_utf8(env_, strValue, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, strValue, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("can not get strValue value"); return nullptr; } diff --git a/process/js_childprocess.h b/process/js_childprocess.h index 0f56654..72309db 100644 --- a/process/js_childprocess.h +++ b/process/js_childprocess.h @@ -52,10 +52,8 @@ namespace OHOS::Js_sys_module::Process { public: /** * Create child process object. - * - * @param env NAPI environment parameters. */ - explicit ChildProcess(napi_env env); + explicit ChildProcess() {} /** * Close the target process. @@ -65,59 +63,76 @@ namespace OHOS::Js_sys_module::Process { /** * Send a signal to process. * + * @param env NAPI environment parameters. * @param signal Number or string represents the signal sent. */ - void Kill(const napi_value signo); + void Kill(napi_env env, const napi_value signo); /** * Wait for the child process to finish running, and return a promise object * whose value is the exit code of the child process. + * + * @param env NAPI environment parameters. */ - napi_value Wait(); + napi_value Wait(napi_env env); /** * Get the standard output of the child process. + * + * @param env NAPI environment parameters. */ - napi_value GetOutput() const; + napi_value GetOutput(napi_env env) const; /** * Get the standard error output of the child process. + * + * @param env NAPI environment parameters. */ - napi_value GetErrorOutput() const; + napi_value GetErrorOutput(napi_env env) const; /** * Get kill status. + * + * @param env NAPI environment parameters. */ - napi_value GetKilled() const; + napi_value GetKilled(napi_env env) const; /** * Get the specific pid value. + * + * @param env NAPI environment parameters. */ - napi_value Getpid() const; + napi_value Getpid(napi_env env) const; /** * Get the parent process ID. + * + * @param env NAPI environment parameters. */ - napi_value Getppid() const; + napi_value Getppid(napi_env env) const; /** * Get exit status. + * + * @param env NAPI environment parameters. */ - napi_value GetExitCode() const; + napi_value GetExitCode(napi_env env) const; /** * Initialization option information. * + * @param env NAPI environment parameters. * @param options Option parameter. */ - void InitOptionsInfo(napi_value options); + void InitOptionsInfo(napi_env env, napi_value options); /** * Start a subprocess to execute shell commands. * + * @param env NAPI environment parameters. * @param command Command parameters. */ - void Spawn(napi_value command); + void Spawn(napi_env env, napi_value command); /** * ChildProcess destructor. @@ -130,11 +145,10 @@ namespace OHOS::Js_sys_module::Process { static void ReadStdErr(napi_env env, void* data); static void EndStdErr(napi_env env, napi_status status, void* buffer); static void TimeoutListener(napi_env env, void* data); - std::string RequireStrValue(const napi_value strValue); - int GetValidSignal(const napi_value signo); - void CreateWorker(); + std::string RequireStrValue(napi_env env, const napi_value strValue); + int GetValidSignal(napi_env env, const napi_value signo); + void CreateWorker(napi_env env); - napi_env env_ = nullptr; OptionsInfo* optionsInfo_ = nullptr; StdInfo* stdOutInfo_ = nullptr; StdInfo* stdErrInfo_ = nullptr; diff --git a/process/js_process.cpp b/process/js_process.cpp index c650369..6c4063f 100644 --- a/process/js_process.cpp +++ b/process/js_process.cpp @@ -44,50 +44,49 @@ namespace OHOS::Js_sys_module::Process { // support events thread_local std::string events = "UnHandleRejection"; - Process::Process(napi_env env) : env_(env) {} - napi_value Process::GetUid() const + napi_value Process::GetUid(napi_env env) const { napi_value result = nullptr; auto processGetuid = static_cast(getuid()); - NAPI_CALL(env_, napi_create_uint32(env_, processGetuid, &result)); + NAPI_CALL(env, napi_create_uint32(env, processGetuid, &result)); return result; } - napi_value Process::GetGid() const + napi_value Process::GetGid(napi_env env) const { napi_value result = nullptr; auto processGetgid = static_cast(getgid()); - NAPI_CALL(env_, napi_create_uint32(env_, processGetgid, &result)); + NAPI_CALL(env, napi_create_uint32(env, processGetgid, &result)); return result; } - napi_value Process::GetEUid() const + napi_value Process::GetEUid(napi_env env) const { napi_value result = nullptr; auto processGeteuid = static_cast(geteuid()); - NAPI_CALL(env_, napi_create_uint32(env_, processGeteuid, &result)); + NAPI_CALL(env, napi_create_uint32(env, processGeteuid, &result)); return result; } - napi_value Process::GetEGid() const + napi_value Process::GetEGid(napi_env env) const { napi_value result = nullptr; auto processGetegid = static_cast(getegid()); - NAPI_CALL(env_, napi_create_uint32(env_, processGetegid, &result)); + NAPI_CALL(env, napi_create_uint32(env, processGetegid, &result)); return result; } - napi_value Process::GetGroups() const + napi_value Process::GetGroups(napi_env env) const { napi_value result = nullptr; int progroups = getgroups(0, nullptr); if (progroups == -1) { - napi_throw_error(env_, "-1", "getgroups initialize failed"); + napi_throw_error(env, "-1", "getgroups initialize failed"); } std::vector pgrous(progroups); progroups = getgroups(progroups, pgrous.data()); if (progroups == -1) { - napi_throw_error(env_, "-1", "getgroups"); + napi_throw_error(env, "-1", "getgroups"); } pgrous.resize(static_cast(progroups)); gid_t proegid = getegid(); @@ -99,63 +98,63 @@ namespace OHOS::Js_sys_module::Process { auto receive = static_cast(*iter); array.push_back(receive); } - NAPI_CALL(env_, napi_create_array(env_, &result)); + NAPI_CALL(env, napi_create_array(env, &result)); size_t len = array.size(); for (size_t i = 0; i < len; i++) { napi_value numvalue = nullptr; - NAPI_CALL(env_, napi_create_uint32(env_, array[i], &numvalue)); - NAPI_CALL(env_, napi_set_element(env_, result, i, numvalue)); + NAPI_CALL(env, napi_create_uint32(env, array[i], &numvalue)); + NAPI_CALL(env, napi_set_element(env, result, i, numvalue)); } return result; } - napi_value Process::GetPid() const + napi_value Process::GetPid(napi_env env) const { napi_value result = nullptr; auto proPid = static_cast(getpid()); - napi_create_int32(env_, proPid, &result); + napi_create_int32(env, proPid, &result); return result; } - napi_value Process::GetPpid() const + napi_value Process::GetPpid(napi_env env) const { napi_value result = nullptr; auto proPpid = static_cast(getppid()); - napi_create_int32(env_, proPpid, &result); + napi_create_int32(env, proPpid, &result); return result; } - void Process::Chdir(napi_value args) const + void Process::Chdir(napi_env env, napi_value args) const { size_t prolen = 0; - if (napi_get_value_string_utf8(env_, args, nullptr, 0, &prolen) != napi_ok) { + if (napi_get_value_string_utf8(env, args, nullptr, 0, &prolen) != napi_ok) { HILOG_ERROR("can not get args size"); return; } std::string result = ""; result.reserve(prolen + 1); result.resize(prolen); - if (napi_get_value_string_utf8(env_, args, result.data(), prolen + 1, &prolen) != napi_ok) { + if (napi_get_value_string_utf8(env, args, result.data(), prolen + 1, &prolen) != napi_ok) { HILOG_ERROR("can not get args value"); return; } int proerr = 0; proerr = uv_chdir(result.c_str()); if (proerr) { - napi_throw_error(env_, "-1", "chdir"); + napi_throw_error(env, "-1", "chdir"); } } - napi_value Process::Kill(napi_value signal, napi_value proid) + napi_value Process::Kill(napi_env env, napi_value signal, napi_value proid) { int32_t pid = 0; int32_t sig = 0; - napi_get_value_int32(env_, proid, &pid); - napi_get_value_int32(env_, signal, &sig); + 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 || pid == -1 || pid == ownPid || pid == -ownPid)) { - napi_throw_error(env_, "0", "process exit"); + napi_throw_error(env, "0", "process exit"); } bool flag = false; int err = uv_kill(pid, sig); @@ -163,46 +162,46 @@ namespace OHOS::Js_sys_module::Process { flag = true; } napi_value result = nullptr; - NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; } - napi_value Process::Uptime() const + napi_value Process::Uptime(napi_env env) 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"); + 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)); + NAPI_CALL(env, napi_create_double(env, runsystime, &result)); } else { - napi_throw_error(env_, "-1", "Failed to get systimer"); + napi_throw_error(env, "-1", "Failed to get systimer"); } return result; } - void Process::Exit(napi_value number) const + void Process::Exit(napi_env env, napi_value number) const { int32_t result = 0; - napi_get_value_int32(env_, number, &result); + napi_get_value_int32(env, number, &result); exit(result); } - napi_value Process::Cwd() const + napi_value Process::Cwd(napi_env env) const { napi_value result = nullptr; char buf[260 * NUM_OF_DATA] = { 0 }; // 260:Only numbers path String size is 260. size_t length = sizeof(buf); int err = uv_cwd(buf, &length); if (err) { - napi_throw_error(env_, "1", "uv_cwd"); + napi_throw_error(env, "1", "uv_cwd"); } - napi_create_string_utf8(env_, buf, length, &result); + napi_create_string_utf8(env, buf, length, &result); return result; } @@ -211,17 +210,17 @@ namespace OHOS::Js_sys_module::Process { exit(0); } - void Process::On(napi_value str, napi_value function) + void Process::On(napi_env env, napi_value str, napi_value function) { std::string result = ""; size_t bufferSize = 0; - if (napi_get_value_string_utf8(env_, str, nullptr, NAPI_RETURN_ZERO, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, str, nullptr, NAPI_RETURN_ZERO, &bufferSize) != napi_ok) { HILOG_ERROR("can not get str size"); return; } result.reserve(bufferSize + NAPI_RETURN_ONE); result.resize(bufferSize); - if (napi_get_value_string_utf8(env_, str, result.data(), bufferSize + NAPI_RETURN_ONE, + if (napi_get_value_string_utf8(env, str, result.data(), bufferSize + NAPI_RETURN_ONE, &bufferSize) != napi_ok) { HILOG_ERROR("can not get str value"); return; @@ -231,7 +230,7 @@ namespace OHOS::Js_sys_module::Process { return; } napi_ref myCallRef = nullptr; - napi_status status = napi_create_reference(env_, function, 1, &myCallRef); + napi_status status = napi_create_reference(env, function, 1, &myCallRef); if (status != napi_ok) { HILOG_ERROR("napi_create_reference is failed"); return; @@ -246,18 +245,18 @@ namespace OHOS::Js_sys_module::Process { } } - napi_value Process::Off(napi_value str) + napi_value Process::Off(napi_env env, napi_value str) { size_t bufferSize = 0; bool flag = false; - if (napi_get_value_string_utf8(env_, str, nullptr, 0, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, str, nullptr, 0, &bufferSize) != napi_ok) { HILOG_ERROR("can not get str size"); return nullptr; } std::string result = ""; result.reserve(bufferSize + 1); result.resize(bufferSize); - if (napi_get_value_string_utf8(env_, str, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, str, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("can not get str value"); return nullptr; } @@ -265,24 +264,24 @@ namespace OHOS::Js_sys_module::Process { temp = result; auto iter = eventMap.equal_range(temp); while (iter.first != iter.second) { - NAPI_CALL(env_, napi_delete_reference(env_, iter.first->second)); + NAPI_CALL(env, napi_delete_reference(env, iter.first->second)); iter.first = eventMap.erase(iter.first); flag = true; } napi_value convertResult = nullptr; - NAPI_CALL(env_, napi_get_boolean(env_, flag, &convertResult)); + NAPI_CALL(env, napi_get_boolean(env, flag, &convertResult)); return convertResult; } - napi_value Process::GetTid() const + napi_value Process::GetTid(napi_env env) const { napi_value result = nullptr; auto proTid = static_cast(gettid()); - napi_create_int32(env_, proTid, &result); + napi_create_int32(env, proTid, &result); return result; } - napi_value Process::IsIsolatedProcess() const + napi_value Process::IsIsolatedProcess(napi_env env) const { napi_value result = nullptr; bool flag = true; @@ -290,60 +289,60 @@ namespace OHOS::Js_sys_module::Process { auto uid = prouid % PER_USER_RANGE; if ((uid >= 99000 && uid <= 99999) || // 99999:Only isolateuid numbers between 99000 and 99999. (uid >= 9000 && uid <= 98999)) { // 98999:Only appuid numbers between 9000 and 98999. - NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; } flag = false; - NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; } - napi_value Process::IsAppUid(napi_value uid) const + napi_value Process::IsAppUid(napi_env env, napi_value uid) const { int32_t number = 0; napi_value result = nullptr; bool flag = true; - napi_get_value_int32(env_, uid, &number); + napi_get_value_int32(env, uid, &number); if (number > 0) { const auto appId = number % PER_USER_RANGE; if (appId >= FIRST_APPLICATION_UID && appId <= LAST_APPLICATION_UID) { - napi_get_boolean(env_, flag, &result); + napi_get_boolean(env, flag, &result); return result; } flag = false; - NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; } else { flag = false; - NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; } } - napi_value Process::Is64Bit() const + napi_value Process::Is64Bit(napi_env env) const { napi_value result = nullptr; bool flag = true; auto size = sizeof(char*); flag = (size == NUM_OF_DATA) ? false : true; - NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; } - napi_value Process::GetEnvironmentVar(napi_value name) const + napi_value Process::GetEnvironmentVar(napi_env env, napi_value name) const { napi_value convertResult = nullptr; char buf[260 * NUM_OF_DATA] = { 0 }; // 260:Only numbers path String size is 260. size_t length = sizeof(buf); size_t bufferSize = 0; - if (napi_get_value_string_utf8(env_, name, nullptr, 0, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, name, nullptr, 0, &bufferSize) != napi_ok) { HILOG_ERROR("can not get name size"); return nullptr; } std::string result = ""; result.reserve(bufferSize + 1); result.resize(bufferSize); - if (napi_get_value_string_utf8(env_, name, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, name, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("can not get name value"); return nullptr; } @@ -351,27 +350,27 @@ namespace OHOS::Js_sys_module::Process { temp = result; auto envNum = uv_os_getenv(temp.c_str(), buf, &length); if (envNum == UV_ENOENT) { - NAPI_CALL(env_, napi_get_undefined(env_, &convertResult)); + NAPI_CALL(env, napi_get_undefined(env, &convertResult)); return convertResult; } - napi_create_string_utf8(env_, buf, strlen(buf), &convertResult); + napi_create_string_utf8(env, buf, strlen(buf), &convertResult); return convertResult; } - napi_value Process::GetUidForName(napi_value name) const + napi_value Process::GetUidForName(napi_env env, napi_value name) const { struct passwd *user = nullptr; int32_t uid = 0; napi_value convertResult = nullptr; size_t bufferSize = 0; - if (napi_get_value_string_utf8(env_, name, nullptr, 0, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, name, nullptr, 0, &bufferSize) != napi_ok) { HILOG_ERROR("can not get name size"); return nullptr; } std::string result = ""; result.reserve(bufferSize + 1); result.resize(bufferSize); - if (napi_get_value_string_utf8(env_, name, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { + if (napi_get_value_string_utf8(env, name, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("can not get name value"); return nullptr; } @@ -380,28 +379,28 @@ namespace OHOS::Js_sys_module::Process { user = getpwnam(temp.c_str()); if (user != nullptr) { uid = static_cast(user->pw_uid); - napi_create_int32(env_, uid, &convertResult); + napi_create_int32(env, uid, &convertResult); return convertResult; } - napi_create_int32(env_, (-1), &convertResult); + napi_create_int32(env, (-1), &convertResult); return convertResult; } - napi_value Process::GetThreadPriority(napi_value tid) const + napi_value Process::GetThreadPriority(napi_env env, napi_value tid) const { errno = 0; int32_t proTid = 0; napi_value result = nullptr; - napi_get_value_int32(env_, tid, &proTid); + napi_get_value_int32(env, tid, &proTid); int32_t pri = getpriority(PRIO_PROCESS, proTid); if (errno) { - napi_throw_error(env_, "-1", "Invalid tid"); + napi_throw_error(env, "-1", "Invalid tid"); } - napi_create_int32(env_, pri, &result); + napi_create_int32(env, pri, &result); return result; } - napi_value Process::GetStartRealtime() const + napi_value Process::GetStartRealtime(napi_env env) const { struct timespec timespro = {0, 0}; struct timespec timessys = {0, 0}; @@ -417,7 +416,7 @@ namespace OHOS::Js_sys_module::Process { int whenpro = ConvertTime(timespro.tv_sec, timespro.tv_nsec); int whensys = ConvertTime(timessys.tv_sec, timessys.tv_nsec); auto timedif = (whensys - whenpro); - napi_create_int32(env_, timedif, &result); + napi_create_int32(env, timedif, &result); return result; } @@ -426,7 +425,7 @@ namespace OHOS::Js_sys_module::Process { return int(tvsec * 1000) + int(tvnsec / 1000000); // 98999:Only converttime numbers is 1000 and 1000000. } - napi_value Process::GetPastCputime() const + napi_value Process::GetPastCputime(napi_env env) const { struct timespec times = {0, 0}; napi_value result = nullptr; @@ -435,17 +434,17 @@ namespace OHOS::Js_sys_module::Process { return 0; } int when = ConvertTime(times.tv_sec, times.tv_nsec); - napi_create_int32(env_, when, &result); + napi_create_int32(env, when, &result); return result; } - napi_value Process::GetSystemConfig(napi_value name) const + napi_value Process::GetSystemConfig(napi_env env, napi_value name) const { int32_t number = 0; napi_value result = nullptr; - napi_get_value_int32(env_, name, &number); + napi_get_value_int32(env, name, &number); auto configinfo = static_cast(sysconf(number)); - napi_create_int32(env_, configinfo, &result); + napi_create_int32(env, configinfo, &result); return result; } @@ -543,23 +542,23 @@ namespace OHOS::Js_sys_module::Process { return res; } - napi_value Process::SetRejectionCallback() const + napi_value Process::SetRejectionCallback(napi_env env) const { napi_value cb = nullptr; std::string callbackName = "onUnHandleRejection"; - NAPI_CALL(env_, napi_create_function(env_, callbackName.c_str(), callbackName.size(), OnUnHandleRejection, + NAPI_CALL(env, napi_create_function(env, callbackName.c_str(), callbackName.size(), OnUnHandleRejection, nullptr, &cb)); napi_ref unHandleRejectionCallbackRef = nullptr; - NAPI_CALL(env_, napi_create_reference(env_, cb, 1, &unHandleRejectionCallbackRef)); + NAPI_CALL(env, napi_create_reference(env, cb, 1, &unHandleRejectionCallbackRef)); napi_ref checkUnhandleRejectionsRef = nullptr; napi_value checkcb = nullptr; std::string cbName = "CheckUnhandleRejections"; - NAPI_CALL(env_, napi_create_function(env_, cbName.c_str(), cbName.size(), CheckUnhandleRejections, + NAPI_CALL(env, napi_create_function(env, cbName.c_str(), cbName.size(), CheckUnhandleRejections, nullptr, &checkcb)); - NAPI_CALL(env_, napi_create_reference(env_, checkcb, 1, &checkUnhandleRejectionsRef)); + NAPI_CALL(env, napi_create_reference(env, checkcb, 1, &checkUnhandleRejectionsRef)); napi_value res = nullptr; - NAPI_CALL(env_, napi_get_undefined(env_, &res)); + NAPI_CALL(env, napi_get_undefined(env, &res)); return res; } void Process::ClearReference(napi_env env) diff --git a/process/js_process.h b/process/js_process.h index 2967f13..cc05a2a 100644 --- a/process/js_process.h +++ b/process/js_process.h @@ -30,10 +30,8 @@ namespace OHOS::Js_sys_module::Process { public: /** * Create process object - * - * @param env NAPI environment parameters. */ - explicit Process(napi_env env); + explicit Process() {} /** * Process destructor. @@ -42,58 +40,76 @@ namespace OHOS::Js_sys_module::Process { /** * Get process uid. + * + * @param env The parameter is NAPI environment variables. */ - napi_value GetUid() const; + napi_value GetUid(napi_env env) const; /** * Get the user ID of the process. + * + * @param env The parameter is NAPI environment variables. */ - napi_value GetGid() const; + napi_value GetGid(napi_env env) const; /** * Get the effective user identity of the process. + * + * @param env The parameter is NAPI environment variables. */ - napi_value GetEUid() const; + napi_value GetEUid(napi_env env) const; /** * Get the effective group ID of the process. + * + * @param env The parameter is NAPI environment variables. */ - napi_value GetEGid() const; + napi_value GetEGid(napi_env env) const; /** * Get an array with supplementary group ids. + * + * @param env The parameter is NAPI environment variables.s */ - napi_value GetGroups() const; + napi_value GetGroups(napi_env env) const; /** * Get the pid of the current process. + * + * @param env The parameter is NAPI environment variables. */ - napi_value GetPid() const; + napi_value GetPid(napi_env env) const; /** * Get the pid of the parent process of the current process. + * + * @param env The parameter is NAPI environment variables. */ - napi_value GetPpid() const; + napi_value GetPpid(napi_env env) const; /** * Change the current working directory of the process. * + * @param env The parameter is NAPI environment variables. * @param args The parameter is the path. */ - void Chdir(napi_value args) const; + void Chdir(napi_env env, napi_value args) const; /** * Get the number of seconds the current system has been running. + * + * @param env The parameter is NAPI environment variables. */ - napi_value Uptime() const; + napi_value Uptime(napi_env env) const; /** * Send a signal to the specified process and end the specified process. * + * @param env The parameter is NAPI environment variables. * @param signal The parameter is the signal sent. * @param proid The parameter is the id of the process. */ - napi_value Kill(napi_value signal, napi_value proid); + napi_value Kill(napi_env env, napi_value signal, napi_value proid); /** * Causes the process to exit immediately and generate a core file. @@ -103,94 +119,114 @@ namespace OHOS::Js_sys_module::Process { /** * Store user-triggered events. * + * @param env The parameter is NAPI environment variables. * @param str The parameter is type of storage event. * @param function The parameter is callback event. */ - void On(napi_value str, napi_value function); + void On(napi_env env, napi_value str, napi_value function); /** * Delete user-stored events. * + * @param env The parameter is NAPI environment variables. * @param str The parameter is the type of delete event. */ - napi_value Off(napi_value str); + napi_value Off(napi_env env, napi_value str); /** * Terminate the program. * + * @param env The parameter is NAPI environment variables. * @param number The parameter is the exit code of the process. */ - void Exit(napi_value number) const; + void Exit(napi_env env, napi_value number) const; /** * Use this method to get the working directory of the process. + * + * @param env The parameter is NAPI environment variables. */ - napi_value Cwd() const; + napi_value Cwd(napi_env env) const; /** * Get the tid of the current process. + * + * @param env The parameter is NAPI environment variables. */ - napi_value GetTid() const; + napi_value GetTid(napi_env env) const; /** * Determines whether the process is isolated. + * + * @param env The parameter is NAPI environment variables. */ - napi_value IsIsolatedProcess() const; + napi_value IsIsolatedProcess(napi_env env) const; /** * Determine whether the uid belongs to the application. * + * @param env The parameter is NAPI environment variables. * @param uid The parameter is the uid of the application. */ - napi_value IsAppUid(napi_value uid) const; + napi_value IsAppUid(napi_env env, napi_value uid) const; /** * Determine whether the operating environment is 64-bit. + * + * @param env The parameter is NAPI environment variables. */ - napi_value Is64Bit() const; + napi_value Is64Bit(napi_env env) const; /** * Get process uid by process name. * + * @param env The parameter is NAPI environment variables. * @param name The parameter is the process name. */ - napi_value GetUidForName(napi_value name) const; + napi_value GetUidForName(napi_env env, napi_value name) const; /** * Get thread priority based on specified tid. * + * @param env The parameter is NAPI environment variables. * @param tid The parameter is the specified thread tid. */ - napi_value GetThreadPriority(napi_value tid) const; + napi_value GetThreadPriority(napi_env env, napi_value tid) const; /** * Get the real-time elapsed time from system startup to process startup. */ - napi_value GetStartRealtime() const; + napi_value GetStartRealtime(napi_env env) const; /** * Get the CPU time from the process startup to the current time. + * + * @param env The parameter is NAPI environment variables. */ - napi_value GetPastCputime() const; + napi_value GetPastCputime(napi_env env) const; /** * Get system configuration information. * + * @param env The parameter is NAPI environment variables. * @param name The parameter is the name of the specified system configuration parameter. */ - napi_value GetSystemConfig(napi_value name) const; + napi_value GetSystemConfig(napi_env env, napi_value name) const; /** * Use this method to get the value corresponding to the environment variable. * + * @param env The parameter is NAPI environment variables. * @param name The parameter is the environment variable name. */ - napi_value GetEnvironmentVar(napi_value name) const; + napi_value GetEnvironmentVar(napi_env env, napi_value name) const; /** * Set reject callback. + * + * @param env The parameter is NAPI environment variables. */ - napi_value SetRejectionCallback() const; + napi_value SetRejectionCallback(napi_env env) const; /** * Clear references to callbacks. @@ -203,7 +239,6 @@ namespace OHOS::Js_sys_module::Process { int ConvertTime(time_t tvsec, int64_t tvnsec) const; private: - napi_env env_ { nullptr }; int FIRST_APPLICATION_UID = 10000; int LAST_APPLICATION_UID = 19999; }; diff --git a/process/native_module_process.cpp b/process/native_module_process.cpp index ec0b50a..159b14a 100644 --- a/process/native_module_process.cpp +++ b/process/native_module_process.cpp @@ -85,11 +85,11 @@ namespace OHOS::Js_sys_module::Process { NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, &data)); DealType(env, args, argc); - auto objectInfo = new ChildProcess(env); + auto objectInfo = new ChildProcess(); - objectInfo->InitOptionsInfo(args[1]); + objectInfo->InitOptionsInfo(env, args[1]); - objectInfo->Spawn(args[0]); + objectInfo->Spawn(env, args[0]); NAPI_CALL(env, napi_wrap( env, thisVar, objectInfo, @@ -112,7 +112,7 @@ namespace OHOS::Js_sys_module::Process { ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - napi_value result = object->Wait(); + napi_value result = object->Wait(env); return result; } @@ -124,7 +124,7 @@ namespace OHOS::Js_sys_module::Process { ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - napi_value result = object->GetOutput(); + napi_value result = object->GetOutput(env); return result; } @@ -151,7 +151,7 @@ namespace OHOS::Js_sys_module::Process { ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - napi_value result = object->GetErrorOutput(); + napi_value result = object->GetErrorOutput(env); return result; } @@ -174,7 +174,7 @@ namespace OHOS::Js_sys_module::Process { ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - object->Kill(args); + object->Kill(env, args); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -188,7 +188,7 @@ namespace OHOS::Js_sys_module::Process { ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - napi_value result = object->GetKilled(); + napi_value result = object->GetKilled(env); return result; } @@ -200,7 +200,7 @@ namespace OHOS::Js_sys_module::Process { ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - napi_value result = object->Getpid(); + napi_value result = object->Getpid(env); return result; } @@ -212,7 +212,7 @@ namespace OHOS::Js_sys_module::Process { ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - napi_value result = object->Getppid(); + napi_value result = object->Getppid(env); return result; } @@ -224,7 +224,7 @@ namespace OHOS::Js_sys_module::Process { ChildProcess* object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); - napi_value result = object->GetExitCode(); + napi_value result = object->GetExitCode(env); return result; } @@ -263,44 +263,44 @@ namespace OHOS::Js_sys_module::Process { static napi_value GetUid(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetUid(); + Process object; + return object.GetUid(env); } static napi_value GetGid(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetGid(); + Process object; + return object.GetGid(env); } static napi_value GetEUid(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetEUid(); + Process object; + return object.GetEUid(env); } static napi_value GetEGid(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetEGid(); + Process object; + return object.GetEGid(env); } static napi_value GetGroups(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetGroups(); + Process object; + return object.GetGroups(env); } static napi_value GetPid(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetPid(); + Process object; + return object.GetPid(env); } static napi_value GetPpid(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetPpid(); + Process object; + return object.GetPpid(env); } static napi_value Chdir(napi_env env, napi_callback_info info) @@ -314,8 +314,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(env); - object.Chdir(args); + Process object; + object.Chdir(env, args); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); return result; @@ -323,7 +323,7 @@ namespace OHOS::Js_sys_module::Process { static napi_value Abort(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); + Process object; object.Abort(); napi_value res = nullptr; NAPI_CALL(env, napi_get_undefined(env, &res)); @@ -332,8 +332,8 @@ namespace OHOS::Js_sys_module::Process { static napi_value Cwd(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.Cwd(); + Process object; + return object.Cwd(env); } static napi_value Exit(napi_env env, napi_callback_info info) @@ -345,8 +345,8 @@ namespace OHOS::Js_sys_module::Process { napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, args, &valuetype)); NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.number error"); - Process object(env); - object.Exit(args); + Process object; + object.Exit(env, args); napi_value res = nullptr; NAPI_CALL(env, napi_get_undefined(env, &res)); return res; @@ -370,8 +370,8 @@ namespace OHOS::Js_sys_module::Process { } napi_valuetype valuetype1; NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); - Process object(env); - object.On(args[0], args[1]); + Process object; + object.On(env, args[0], args[1]); NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; } @@ -382,15 +382,15 @@ namespace OHOS::Js_sys_module::Process { size_t argc = 1; napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); - Process object(env); - napi_value result = object.Off(args); + Process object; + napi_value result = object.Off(env, args); return result; } static napi_value Uptime(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.Uptime(); + Process object; + return object.Uptime(env); } static napi_value KillSig(napi_env env, napi_callback_info info) @@ -400,21 +400,21 @@ 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(env); + Process object; napi_value result = nullptr; - result = object.Kill(argv[0], argv[1]); + result = object.Kill(env, argv[0], argv[1]); return result; } static napi_value GetTid(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetTid(); + Process object; + return object.GetTid(env); } static napi_value IsIsolatedProcess(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.IsIsolatedProcess(); + Process object; + return object.IsIsolatedProcess(env); } static napi_value IsAppUid(napi_env env, napi_callback_info info) @@ -426,14 +426,14 @@ namespace OHOS::Js_sys_module::Process { napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, args, &valuetype)); NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error"); - Process object(env); - return object.IsAppUid(args); + Process object; + return object.IsAppUid(env, args); } static napi_value Is64Bit(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.Is64Bit(); + Process object; + return object.Is64Bit(env); } static napi_value GetUidForName(napi_env env, napi_callback_info info) @@ -445,8 +445,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 error"); - Process object(env); - return object.GetUidForName(args); + Process object; + return object.GetUidForName(env, args); } static napi_value GetThreadPriority(napi_env env, napi_callback_info info) @@ -458,20 +458,20 @@ namespace OHOS::Js_sys_module::Process { napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, args, &valuetype)); NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error"); - Process object(env); - return object.GetThreadPriority(args); + Process object; + return object.GetThreadPriority(env, args); } static napi_value GetStartRealtime(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetStartRealtime(); + Process object; + return object.GetStartRealtime(env); } static napi_value GetPastCputime(napi_env env, [[maybe_unused]] napi_callback_info info) { - Process object(env); - return object.GetPastCputime(); + Process object; + return object.GetPastCputime(env); } static napi_value GetSystemConfig(napi_env env, napi_callback_info info) @@ -483,8 +483,8 @@ namespace OHOS::Js_sys_module::Process { napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, args, &valuetype)); NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error"); - Process object(env); - return object.GetSystemConfig(args); + Process object; + return object.GetSystemConfig(env, args); } static napi_value GetEnvironmentVar(napi_env env, napi_callback_info info) @@ -496,14 +496,14 @@ 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 error"); - Process object(env); - return object.GetEnvironmentVar(args); + Process object; + return object.GetEnvironmentVar(env, args); } static napi_value Init(napi_env env, napi_value exports) { - Process object(env); - object.SetRejectionCallback(); + Process object; + object.SetRejectionCallback(env); napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("runCmd", RunCommand), DECLARE_NAPI_GETTER("uid", GetUid), diff --git a/test/unittest/test_process.cpp b/test/unittest/test_process.cpp index 83f8405..c57ad55 100755 --- a/test/unittest/test_process.cpp +++ b/test/unittest/test_process.cpp @@ -38,11 +38,11 @@ } static OHOS::Js_sys_module::Process::ChildProcess RunCommand(napi_env env, napi_value command, napi_value options) { - OHOS::Js_sys_module::Process::ChildProcess objectInfo(env); + OHOS::Js_sys_module::Process::ChildProcess objectInfo; - objectInfo.InitOptionsInfo(options); + objectInfo.InitOptionsInfo(env, options); - objectInfo.Spawn(command); + objectInfo.Spawn(env, command); return objectInfo; } @@ -54,10 +54,10 @@ static OHOS::Js_sys_module::Process::ChildProcess RunCommand(napi_env env, napi_ HWTEST_F(NativeEngineTest, ProcessUptimeTest001, testing::ext::TestSize.Level0) { napi_env env = (napi_env)engine_; - OHOS::Js_sys_module::Process::Process process(env); - napi_value timeStart = process.Uptime(); + OHOS::Js_sys_module::Process::Process process; + napi_value timeStart = process.Uptime(env); sleep(1); - napi_value timeEnd = process.Uptime(); + napi_value timeEnd = process.Uptime(env); double start = 0; double end = 0; napi_get_value_double(env, timeStart, &start); @@ -73,7 +73,7 @@ HWTEST_F(NativeEngineTest, ProcessUptimeTest001, testing::ext::TestSize.Level0) HWTEST_F(NativeEngineTest, ProcessKillTest001, testing::ext::TestSize.Level0) { napi_env env = (napi_env)engine_; - OHOS::Js_sys_module::Process::Process process(env); + OHOS::Js_sys_module::Process::Process process; std::string command("ls; sleep 1"); napi_value temp = nullptr; @@ -81,10 +81,10 @@ HWTEST_F(NativeEngineTest, ProcessKillTest001, testing::ext::TestSize.Level0) OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); - napi_value pid = childprocess.Getpid(); + napi_value pid = childprocess.Getpid(env); napi_value signal = nullptr; napi_create_int32(env, 9, &signal); - napi_value result = process.Kill(pid, signal); + napi_value result = process.Kill(env, pid, signal); bool res = false; napi_get_value_bool(env, result, &res); ASSERT_TRUE(res); @@ -98,7 +98,7 @@ HWTEST_F(NativeEngineTest, ProcessKillTest001, testing::ext::TestSize.Level0) HWTEST_F(NativeEngineTest, ProcessKillTest002, testing::ext::TestSize.Level0) { napi_env env = (napi_env)engine_; - OHOS::Js_sys_module::Process::Process process(env); + OHOS::Js_sys_module::Process::Process process; std::string command("ls; sleep 1"); napi_value temp = nullptr; @@ -106,10 +106,10 @@ HWTEST_F(NativeEngineTest, ProcessKillTest002, testing::ext::TestSize.Level0) OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); - napi_value pid = childprocess.Getpid(); + napi_value pid = childprocess.Getpid(env); napi_value signal = nullptr; napi_create_int32(env, 999, &signal); - napi_value result = process.Kill(pid, signal); + napi_value result = process.Kill(env, pid, signal); bool res = false; napi_get_value_bool(env, result, &res); ASSERT_FALSE(res); @@ -123,7 +123,7 @@ HWTEST_F(NativeEngineTest, ProcessKillTest002, testing::ext::TestSize.Level0) HWTEST_F(NativeEngineTest, ProcessRunCmdTest001, testing::ext::TestSize.Level0) { napi_env env = (napi_env)engine_; - OHOS::Js_sys_module::Process::Process process(env); + OHOS::Js_sys_module::Process::Process process; std::string command("each abc"); napi_value temp = nullptr; @@ -131,7 +131,7 @@ HWTEST_F(NativeEngineTest, ProcessRunCmdTest001, testing::ext::TestSize.Level0) OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); - napi_value output = childprocess.GetOutput(); + napi_value output = childprocess.GetOutput(env); bool res = false; napi_is_promise(env, output, &res); ASSERT_TRUE(res); @@ -145,7 +145,7 @@ HWTEST_F(NativeEngineTest, ProcessRunCmdTest001, testing::ext::TestSize.Level0) HWTEST_F(NativeEngineTest, ProcessRunCmdTest002, testing::ext::TestSize.Level0) { napi_env env = (napi_env)engine_; - OHOS::Js_sys_module::Process::Process process(env); + OHOS::Js_sys_module::Process::Process process; std::string command("mkdir test.txt"); napi_value temp = nullptr; @@ -153,7 +153,7 @@ HWTEST_F(NativeEngineTest, ProcessRunCmdTest002, testing::ext::TestSize.Level0) OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); - napi_value errorOutput = childprocess.GetErrorOutput(); + napi_value errorOutput = childprocess.GetErrorOutput(env); bool res = false; napi_is_promise(env, errorOutput, &res); ASSERT_TRUE(res);