Skip to content

Commit

Permalink
child_process, refactor: process error in spawnSync.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Apr 11, 2023
1 parent 3a0588b commit 04dec0d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
3 changes: 3 additions & 0 deletions fibjs/include/ifs/child_process.h
Expand Up @@ -69,6 +69,7 @@ class child_process_base : public object_base {
retVal->Set(context, isolate->NewString("stdout"), GetReturnValue(isolate->m_isolate, stdout)).Check();
retVal->Set(context, isolate->NewString("stderr"), GetReturnValue(isolate->m_isolate, stderr)).Check();
retVal->Set(context, isolate->NewString("status"), GetReturnValue(isolate->m_isolate, status)).Check();
retVal->Set(context, isolate->NewString("error"), GetReturnValue(isolate->m_isolate, error)).Check();
}

virtual void fillArguments(Isolate* isolate, std::vector<v8::Local<v8::Value>>& args)
Expand All @@ -78,6 +79,7 @@ class child_process_base : public object_base {
args.push_back(GetReturnValue(isolate->m_isolate, stdout));
args.push_back(GetReturnValue(isolate->m_isolate, stderr));
args.push_back(GetReturnValue(isolate->m_isolate, status));
args.push_back(GetReturnValue(isolate->m_isolate, error));
}

public:
Expand All @@ -86,6 +88,7 @@ class child_process_base : public object_base {
Variant stdout;
Variant stderr;
int32_t status;
Variant error;
};

public:
Expand Down
50 changes: 28 additions & 22 deletions fibjs/src/process/child_process.cpp
Expand Up @@ -59,16 +59,9 @@ result_t child_process_base::execFile(exlib::string command, v8::Local<v8::Array
m_cnt.inc();
m_buferr = new MemoryStream();
}
}

void start()
{
if (m_cnt == 0) {
m_retVal = new ExecFileType();
m_ac->post(0);
delete this;
return;
}
m_cnt.inc();
cp->join(m_status, this);

if (m_stdout)
m_stdout->copyTo(m_bufout, -1, m_szout, this);
Expand Down Expand Up @@ -136,6 +129,8 @@ result_t child_process_base::execFile(exlib::string command, v8::Local<v8::Array
obj_ptr<Stream_base> m_stderr;
obj_ptr<MemoryStream> m_buferr;
int64_t m_szerr;

int32_t m_status;
};

if (ac->isSync()) {
Expand Down Expand Up @@ -163,7 +158,7 @@ result_t child_process_base::execFile(exlib::string command, v8::Local<v8::Array
return CHECK_ERROR(CALL_E_NOSYNC);
}

(new ReadStdout(retVal, ac))->start();
new ReadStdout(retVal, ac);
return CALL_E_PENDDING;
}

Expand Down Expand Up @@ -223,16 +218,9 @@ result_t child_process_base::spawnSync(exlib::string command, v8::Local<v8::Arra
m_cnt.inc();
m_buferr = new MemoryStream();
}
}

void start()
{
if (m_cnt == 0) {
m_retVal = new SpawnSyncType();
m_ac->post(0);
delete this;
return;
}
m_cnt.inc();
cp->join(m_status, this);

if (m_stdout)
m_stdout->copyTo(m_bufout, -1, m_szout, this);
Expand Down Expand Up @@ -309,6 +297,8 @@ result_t child_process_base::spawnSync(exlib::string command, v8::Local<v8::Arra
obj_ptr<Stream_base> m_stderr;
obj_ptr<MemoryStream> m_buferr;
int64_t m_szerr;

int32_t m_status;
};

if (ac->isSync()) {
Expand All @@ -326,8 +316,24 @@ result_t child_process_base::spawnSync(exlib::string command, v8::Local<v8::Arra
GetConfigValue(isolate->m_isolate, opts, "encoding", codec);

result_t hr = spawn(command, args, opts, cp);
if (hr < 0)
return hr;
if (hr < 0) {
retVal = new SpawnSyncType();

retVal->pid = 0;
retVal->status = 0;

retVal->stdout.setNull();
retVal->stderr.setNull();

retVal->output = new NArray();
retVal->output->append(retVal->stdout);
retVal->output->append(retVal->stderr);

retVal->error = v8::Exception::Error(
isolate->NewString(getResultMessage(hr)));

return 0;
}

ac->m_ctxo = cp;
ac->m_ctx.resize(1);
Expand All @@ -336,7 +342,7 @@ result_t child_process_base::spawnSync(exlib::string command, v8::Local<v8::Arra
return CHECK_ERROR(CALL_E_NOSYNC);
}

(new ReadStdout(retVal, ac))->start();
new ReadStdout(retVal, ac);
return CALL_E_PENDDING;
}

Expand Down
4 changes: 2 additions & 2 deletions idl/zh-cn/child_process.idl
Expand Up @@ -164,7 +164,7 @@ module child_process
@param options 指定创建参数
@return 返回子进程运行结果
*/
static (Integer pid, NArray output, Variant stdout, Variant stderr, Integer status) spawnSync(String command, Array args, Object options = {}) async;
static (Integer pid, NArray output, Variant stdout, Variant stderr, Integer status, Variant error) spawnSync(String command, Array args, Object options = {}) async;

/*! @brief 用给定的命令发布一个子进程
options 支持的内容如下:
Expand All @@ -184,7 +184,7 @@ module child_process
@param options 指定创建参数
@return 返回子进程运行结果
*/
static (Integer pid, NArray output, Variant stdout, Variant stderr, Integer status) spawnSync(String command, Object options = {}) async;
static (Integer pid, NArray output, Variant stdout, Variant stderr, Integer status, Variant error) spawnSync(String command, Object options = {}) async;

/*! @brief 在子进程中执行一个模块
options 支持的内容如下:
Expand Down
8 changes: 4 additions & 4 deletions npm/types/dts/module/child_process.d.ts
Expand Up @@ -185,9 +185,9 @@ declare module 'child_process' {
* @return 返回子进程运行结果
*
*/
function spawnSync(command: string, args: any[], options?: FIBJS.GeneralObject): [pid: number, output: [object Object], stdout: any, stderr: any, status: number];
function spawnSync(command: string, args: any[], options?: FIBJS.GeneralObject): [pid: number, output: [object Object], stdout: any, stderr: any, status: number, error: any];

function spawnSync(command: string, args: any[], options?: FIBJS.GeneralObject, callback?: (err: Error | undefined | null, retVal: [pid: number, output: [object Object], stdout: any, stderr: any, status: number])=>any): void;
function spawnSync(command: string, args: any[], options?: FIBJS.GeneralObject, callback?: (err: Error | undefined | null, retVal: [pid: number, output: [object Object], stdout: any, stderr: any, status: number, error: any])=>any): void;

/**
* @description 用给定的命令发布一个子进程
Expand All @@ -209,9 +209,9 @@ declare module 'child_process' {
* @return 返回子进程运行结果
*
*/
function spawnSync(command: string, options?: FIBJS.GeneralObject): [pid: number, output: [object Object], stdout: any, stderr: any, status: number];
function spawnSync(command: string, options?: FIBJS.GeneralObject): [pid: number, output: [object Object], stdout: any, stderr: any, status: number, error: any];

function spawnSync(command: string, options?: FIBJS.GeneralObject, callback?: (err: Error | undefined | null, retVal: [pid: number, output: [object Object], stdout: any, stderr: any, status: number])=>any): void;
function spawnSync(command: string, options?: FIBJS.GeneralObject, callback?: (err: Error | undefined | null, retVal: [pid: number, output: [object Object], stdout: any, stderr: any, status: number, error: any])=>any): void;

/**
* @description 在子进程中执行一个模块
Expand Down
33 changes: 31 additions & 2 deletions test/child_process_test.js
Expand Up @@ -413,7 +413,36 @@ describe("child_process", () => {
assert.equal(result.stdout, "[\"/Users/lion/works/fibjs/bin/Darwin_arm64_release/fibjs\",\"/Users/lion/works/fibjs/test/process/exec2.js\",\"arg1\",\"arg2\"]\n");
assert.equal(result.stdout, result.output[0]);
assert.equal(result.stderr, result.output[1]);
assert.equal(result.status, 2);
assert.equal(result.error, undefined);
});

it("inherit in spawnSync", () => {
var result = child_process.spawnSync(cmd, [
path.join(__dirname, "process", "exec2.js"),
"arg1",
"arg2"
], {
stdio: "inherit"
});

assert.notEqual(result.pid, 0);
assert.equal(result.stdout, null);
assert.equal(result.stdout, null);
assert.equal(result.stderr, result.output[1]);
assert.equal(result.status, 2);
assert.equal(result.error, undefined);
});

it("error when spawnSync", () => {
var result = child_process.spawnSync("not_exists_exec_file");

assert.equal(result.pid, 0);
assert.equal(result.stdout, null);
assert.equal(result.stdout, null);
assert.equal(result.stderr, result.output[1]);
assert.equal(result.status, 0);
assert.notEqual(result.error, undefined);
});

it("argv 1", () => {
Expand All @@ -439,13 +468,13 @@ describe("child_process", () => {
});

it("inherit in execFile", () => {
assert.isUndefined(child_process.execFile(cmd, [
assert.equal(child_process.execFile(cmd, [
path.join(__dirname, "process", "exec2.js"),
"参数1",
"参数2"
], {
stdio: "inherit"
}).stdout);
}).stdout, null);
});

it("execArgv", () => {
Expand Down

0 comments on commit 04dec0d

Please sign in to comment.