Skip to content
Permalink
Browse files

process, feat: add property `pid` to get current process id. (#483)

  • Loading branch information...
richardo2016 authored and xicilion committed Mar 29, 2019
1 parent 628fb32 commit b545b0d41bd339b0c4f67c893626a9365bc3a2ca
Showing with 35 additions and 0 deletions.
  1. +15 −0 fibjs/include/ifs/process.h
  2. +11 −0 fibjs/src/process/process.cpp
  3. +3 −0 idl/zh-cn/process.idl
  4. +6 −0 test/process_test.js
@@ -34,6 +34,7 @@ class process_base : public EventEmitter_base {
static result_t get_env(v8::Local<v8::Object>& retVal);
static result_t get_arch(exlib::string& retVal);
static result_t get_platform(exlib::string& retVal);
static result_t get_pid(int32_t& retVal);
static result_t get_stdin(obj_ptr<File_base>& retVal);
static result_t get_stdout(obj_ptr<File_base>& retVal);
static result_t get_stderr(obj_ptr<File_base>& retVal);
@@ -77,6 +78,7 @@ class process_base : public EventEmitter_base {
static void s_get_env(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_arch(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_platform(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_pid(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_stdin(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_stdout(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_stderr(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
@@ -125,6 +127,7 @@ inline ClassInfo& process_base::class_info()
{ "env", s_get_env, block_set, true },
{ "arch", s_get_arch, block_set, true },
{ "platform", s_get_platform, block_set, true },
{ "pid", s_get_pid, block_set, true },
{ "stdin", s_get_stdin, block_set, true },
{ "stdout", s_get_stdout, block_set, true },
{ "stderr", s_get_stderr, block_set, true },
@@ -237,6 +240,18 @@ inline void process_base::s_get_platform(v8::Local<v8::Name> property, const v8:
METHOD_RETURN();
}

inline void process_base::s_get_pid(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
int32_t vr;

METHOD_NAME("process.pid");
PROPERTY_ENTER();

hr = get_pid(vr);

METHOD_RETURN();
}

inline void process_base::s_get_stdin(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
obj_ptr<File_base> vr;
@@ -220,6 +220,17 @@ result_t process_base::get_platform(exlib::string& retVal)
return os_base::platform(retVal);
}

result_t process_base::get_pid(int32_t& retVal)
{
#ifdef _WIN32
retVal = GetCurrentProcessId();
#else
retVal = getpid();
#endif

return 0;
}

result_t process_base::get_stdin(obj_ptr<File_base>& retVal)
{
Isolate* isolate = Isolate::current();
@@ -66,6 +66,9 @@ module process : EventEmitter
/*! @brief 查询当前平台名称,可能的结果为 'darwin', 'freebsd', 'linux', 或 'win32' */
static readonly String platform;

/*! @brief 读取当前对象指向的进程的 id */
static readonly Integer pid;

/*! @brief 查询当前进程标准输入对象 */
static readonly File stdin;

@@ -37,6 +37,12 @@ describe('process', () => {
assert.notGreaterThan(diff[0], 2);
});

it("pid", () => {
assert.property(process, 'pid');
assert.isNumber(process.pid);
assert.ok(process.pid);
});

it("stdout", () => {
var bs = process.open(cmd, [path.join(__dirname, 'process', 'exec.js')]);

0 comments on commit b545b0d

Please sign in to comment.
You can’t perform that action at this time.