mirror of
https://github.com/openharmony/js_sys_module.git
synced 2026-07-19 23:13:39 -04:00
Signed-off-by: lifansheng <lifansheng1@huawei.com>
On branch master Your branch is up to date with 'origin/master'.
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <grp.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
@@ -22,6 +23,7 @@
|
||||
#include "js_process.h"
|
||||
#include "securec.h"
|
||||
#include "utils/log.h"
|
||||
|
||||
namespace OHOS::Js_sys_module::Process {
|
||||
static napi_value DealType(napi_env env, napi_value args[], size_t argsSize)
|
||||
{
|
||||
@@ -39,7 +41,8 @@ namespace OHOS::Js_sys_module::Process {
|
||||
if (args[1] == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
for (size_t i = 0; i < argsSize; i++) {
|
||||
size_t size = keyStr.size();
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
napi_valuetype propertyType = napi_undefined;
|
||||
napi_value property = nullptr;
|
||||
NAPI_CALL(env, napi_get_named_property(env, args[1], keyStr[i].c_str(), &property));
|
||||
@@ -56,13 +59,13 @@ namespace OHOS::Js_sys_module::Process {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: // 1:KillSignal
|
||||
case 1:
|
||||
NAPI_CALL(env, napi_typeof(env, property, &propertyType));
|
||||
NAPI_ASSERT(env, propertyType == napi_string || propertyType == napi_number
|
||||
|| propertyType == napi_undefined,
|
||||
"Wrong KillSignal argument typr. String or number expected");
|
||||
break;
|
||||
case 2: // 2:maxBuffer
|
||||
case 2:
|
||||
NAPI_CALL(env, napi_typeof(env, property, &propertyType));
|
||||
NAPI_ASSERT(env, propertyType == napi_number || propertyType == napi_undefined,
|
||||
"Wrong maxBuffer argument typr. Number expected");
|
||||
@@ -77,23 +80,28 @@ namespace OHOS::Js_sys_module::Process {
|
||||
static napi_value ChildProcessConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
void* data = nullptr;
|
||||
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) {
|
||||
auto obj = reinterpret_cast<ChildProcess*>(data);
|
||||
if (obj != nullptr) {
|
||||
delete obj;
|
||||
[](napi_env env, void* data, void* hint) {
|
||||
auto objectInfo = (ChildProcess*)data;
|
||||
if (objectInfo != nullptr) {
|
||||
delete objectInfo;
|
||||
}
|
||||
},
|
||||
nullptr, nullptr));
|
||||
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
@@ -101,9 +109,11 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
napi_value result = object->Wait();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -111,9 +121,11 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
napi_value result = object->GetOutput();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -121,9 +133,11 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
object->Close();
|
||||
|
||||
napi_value result = nullptr;
|
||||
NAPI_CALL(env, napi_get_undefined(env, &result));
|
||||
return result;
|
||||
@@ -133,9 +147,12 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
|
||||
napi_value result = object->GetErrorOutput();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -146,15 +163,19 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
object->Kill(args);
|
||||
|
||||
napi_value result = nullptr;
|
||||
NAPI_CALL(env, napi_get_undefined(env, &result));
|
||||
return result;
|
||||
@@ -164,9 +185,11 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
napi_value result = object->GetKilled();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -174,9 +197,11 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
napi_value result = object->Getpid();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -184,9 +209,11 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
napi_value result = object->Getppid();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -194,9 +221,11 @@ 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;
|
||||
|
||||
ChildProcess* object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
|
||||
napi_value result = object->GetExitCode();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -206,7 +235,8 @@ 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";
|
||||
|
||||
const char* childProcessClassName = "ChildProcess";
|
||||
napi_value childProcessClass = nullptr;
|
||||
static napi_property_descriptor childProcessDesc[] = {
|
||||
DECLARE_NAPI_FUNCTION("close", Close),
|
||||
@@ -219,12 +249,15 @@ namespace OHOS::Js_sys_module::Process {
|
||||
DECLARE_NAPI_GETTER("ppid", Getppid),
|
||||
DECLARE_NAPI_GETTER("exitCode", GetExitCode),
|
||||
};
|
||||
|
||||
NAPI_CALL(env, napi_define_class(env, childProcessClassName, strlen(childProcessClassName),
|
||||
ChildProcessConstructor, nullptr,
|
||||
sizeof(childProcessDesc) / sizeof(childProcessDesc[0]), childProcessDesc,
|
||||
&childProcessClass));
|
||||
|
||||
napi_value result = nullptr;
|
||||
NAPI_CALL(env, napi_new_instance(env, childProcessClass, argc, args, &result));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -321,6 +354,8 @@ namespace OHOS::Js_sys_module::Process {
|
||||
static napi_value On(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
bool flag = true;
|
||||
napi_value result = nullptr;
|
||||
size_t requireArgc = 2;
|
||||
size_t argc = 2;
|
||||
napi_value args[2] = { nullptr };
|
||||
@@ -329,8 +364,7 @@ namespace OHOS::Js_sys_module::Process {
|
||||
napi_valuetype valuetype0;
|
||||
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
|
||||
if (valuetype0 != napi_valuetype::napi_string) {
|
||||
bool flag = false;
|
||||
napi_value result = nullptr;
|
||||
flag = false;
|
||||
NAPI_CALL(env, napi_get_boolean(env, flag, &result));
|
||||
return result;
|
||||
}
|
||||
@@ -338,9 +372,8 @@ namespace OHOS::Js_sys_module::Process {
|
||||
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
|
||||
Process object(env);
|
||||
object.On(args[0], args[1]);
|
||||
napi_value res = nullptr;
|
||||
NAPI_CALL(env, napi_get_undefined(env, &res));
|
||||
return res;
|
||||
NAPI_CALL(env, napi_get_boolean(env, flag, &result));
|
||||
return result;
|
||||
}
|
||||
|
||||
static napi_value Off(napi_env env, napi_callback_info info)
|
||||
@@ -365,7 +398,7 @@ namespace OHOS::Js_sys_module::Process {
|
||||
size_t argc = 2;
|
||||
napi_value argv[2] = {0};
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
void* data = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
|
||||
Process object(env);
|
||||
napi_value result = nullptr;
|
||||
@@ -475,6 +508,8 @@ namespace OHOS::Js_sys_module::Process {
|
||||
|
||||
static napi_value Init(napi_env env, napi_value exports)
|
||||
{
|
||||
Process object(env);
|
||||
object.SetRejectionCallback();
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("runCmd", RunCommand),
|
||||
DECLARE_NAPI_GETTER("uid", GetUid),
|
||||
@@ -492,7 +527,7 @@ namespace OHOS::Js_sys_module::Process {
|
||||
DECLARE_NAPI_FUNCTION("on", On),
|
||||
DECLARE_NAPI_FUNCTION("off", Off),
|
||||
DECLARE_NAPI_FUNCTION("exit", Exit),
|
||||
DECLARE_NAPI_FUNCTION("getTid", GetTid),
|
||||
DECLARE_NAPI_GETTER("tid", GetTid),
|
||||
DECLARE_NAPI_FUNCTION("getStartRealtime", GetStartRealtime),
|
||||
DECLARE_NAPI_FUNCTION("getAvailableCores", GetAvailableCores),
|
||||
DECLARE_NAPI_FUNCTION("getPastCputime", GetPastCputime),
|
||||
@@ -505,6 +540,19 @@ namespace OHOS::Js_sys_module::Process {
|
||||
DECLARE_NAPI_FUNCTION("getEnvironmentVar", GetEnvironmentVar),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
|
||||
napi_value obj = nullptr;
|
||||
NAPI_CALL(env, napi_create_object(env, &obj));
|
||||
|
||||
NAPI_CALL(env, napi_wrap(
|
||||
env, obj, reinterpret_cast<void*>(Process::ClearReference),
|
||||
[](napi_env env, void* data, void* hint) {
|
||||
ClearRefCallback clearReference = reinterpret_cast<ClearRefCallback>(data);
|
||||
clearReference(env);
|
||||
},
|
||||
nullptr, nullptr));
|
||||
NAPI_CALL(env, napi_set_named_property(env, exports, "obj", obj));
|
||||
|
||||
return exports;
|
||||
}
|
||||
|
||||
@@ -522,4 +570,4 @@ namespace OHOS::Js_sys_module::Process {
|
||||
{
|
||||
napi_module_register(&processModule);
|
||||
}
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user