Skip to content

Commit

Permalink
process, refactor: support property attribute in process.env.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jun 9, 2023
1 parent 492c441 commit e5cfac7
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions fibjs/src/process/process_env.cpp
Expand Up @@ -34,11 +34,13 @@ inline void on_env_update(Isolate* isolate, exlib::string key, exlib::string val
static void SetEnv(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
{
Isolate* isolate = Isolate::current(info);
exlib::string key = isolate->toString(property);

if (!isolate->m_env.IsEmpty()) {
exlib::string key = isolate->toString(property);
if (value->IsUndefined()) {
uv_os_unsetenv(key.c_str());
on_env_update(isolate, key, "");
} else {
exlib::string val = isolate->toString(value);

uv_os_setenv(key.c_str(), val.c_str());
on_env_update(isolate, key, val);
}
Expand All @@ -47,7 +49,6 @@ static void SetEnv(v8::Local<v8::Name> property, v8::Local<v8::Value> value, con
static void DelEnv(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Boolean>& info)
{
Isolate* isolate = Isolate::current(info);

exlib::string key = isolate->toString(property);

uv_os_unsetenv(key.c_str());
Expand Down Expand Up @@ -83,14 +84,19 @@ static void GetEnv(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<
info.GetReturnValue().Set(isolate->NewString(buf, sz));
}

void QueryEnv(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Integer>& info)
{
info.GetReturnValue().Set(v8::None);
}

result_t process_base::get_env(v8::Local<v8::Object>& retVal)
{
Isolate* isolate = Isolate::current();
v8::Local<v8::Context> context = isolate->context();

if (isolate->m_env.IsEmpty()) {
v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate->m_isolate);
templ->InstanceTemplate()->SetHandler(v8::NamedPropertyHandlerConfiguration(GetEnv, SetEnv, nullptr, DelEnv, EnumEnv));
templ->InstanceTemplate()->SetHandler(v8::NamedPropertyHandlerConfiguration(GetEnv, SetEnv, QueryEnv, DelEnv, EnumEnv));
v8::Local<v8::Object> o = templ->GetFunction(context).ToLocalChecked()->NewInstance(context).ToLocalChecked();
isolate->m_env.Reset(isolate->m_isolate, o);
retVal = o;
Expand Down

0 comments on commit e5cfac7

Please sign in to comment.