Skip to content

Commit

Permalink
fixup: use inlined typed array
Browse files Browse the repository at this point in the history
Signed-off-by: Daeyeon Jeong <[email protected]>
  • Loading branch information
daeyeon committed Nov 4, 2022
1 parent 13ef5a2 commit 204ca1e
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 161 deletions.
25 changes: 11 additions & 14 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ const {
validateInteger,
} = require('internal/validators');
const {
exiting_aliased_Uint32Array,
exit_info_private_symbol,
getHiddenValue,
} = internalBinding('util');
Expand All @@ -91,31 +90,28 @@ setupGlobalProxy();
setupBuffer();

process.domain = null;

// process._exiting and process.exitCode
{
const exitingAliasedUint32Array =
getHiddenValue(process, exiting_aliased_Uint32Array);
// Must match `Environment::ExitInfoFields` in `src/env.h`.
const kExiting = 0;
const kExitCode = 1;
const kHasExitCode = 2;
const fields = getHiddenValue(process, exit_info_private_symbol);

ObjectDefineProperty(process, '_exiting', {
__proto__: null,
get() {
return exitingAliasedUint32Array[0] === 1;
return fields[kExiting] === 1;
},
set(value) {
exitingAliasedUint32Array[0] = value ? 1 : 0;
fields[kExiting] = value ? 1 : 0;
},
enumerable: true,
configurable: true,
});
}
process._exiting = false;

{
// Must match `Environment::ExitInfo::Fields` in `src/env.h`.
const kExitCode = 0;
const kHasExitCode = 1;
const fields = getHiddenValue(process, exit_info_private_symbol);

let exitCode;

ObjectDefineProperty(process, 'exitCode', {
__proto__: null,
get() {
Expand All @@ -140,6 +136,7 @@ process._exiting = false;
configurable: false,
});
}
process._exiting = false;

// process.config is serialized config.gypi
const nativeModule = internalBinding('builtins');
Expand Down
2 changes: 1 addition & 1 deletion src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Maybe<bool> EmitProcessBeforeExit(Environment* env) {
HandleScope handle_scope(isolate);
Context::Scope context_scope(env->context());

Local<Integer> exit_code = v8::Integer::New(
Local<Integer> exit_code = Integer::New(
isolate,
env->maybe_exit_code(static_cast<int32_t>(ExitCode::kNoFailure)));

Expand Down
30 changes: 7 additions & 23 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,6 @@ inline bool TickInfo::has_rejection_to_warn() const {
return fields_[kHasRejectionToWarn] == 1;
}

inline const AliasedInt32Array& ExitInfo::fields() {
return fields_;
}

inline bool ExitInfo::has_exit_code() const {
return fields_[kHasExitCode] == 1;
}

inline int32_t ExitInfo::exit_code() const {
return fields_[kExitCode];
}

inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
if (UNLIKELY(!isolate->InContext())) return nullptr;
v8::HandleScope handle_scope(isolate);
Expand Down Expand Up @@ -339,14 +327,6 @@ inline TickInfo* Environment::tick_info() {
return &tick_info_;
}

inline ExitInfo* Environment::exit_info() {
return &exit_info_;
}

inline int32_t Environment::maybe_exit_code(const int32_t default_code) const {
return exit_info_.has_exit_code() ? exit_info_.exit_code() : default_code;
}

inline uint64_t Environment::timer_base() const {
return timer_base_;
}
Expand Down Expand Up @@ -384,11 +364,15 @@ inline bool Environment::force_context_aware() const {
}

inline void Environment::set_exiting(bool value) {
exiting_[0] = value ? 1 : 0;
exit_info_[kExiting] = value ? 1 : 0;
}

inline int32_t Environment::maybe_exit_code(const int32_t default_code) const {
return exit_info_[kHasExitCode] != 0 ? exit_info_[kExitCode] : default_code;
}

inline AliasedUint32Array& Environment::exiting() {
return exiting_;
inline AliasedInt32Array& Environment::exit_info() {
return exit_info_;
}

inline void Environment::set_abort_on_uncaught_exception(bool value) {
Expand Down
35 changes: 4 additions & 31 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,11 @@ Environment::Environment(IsolateData* isolate_data,
async_hooks_(isolate, MAYBE_FIELD_PTR(env_info, async_hooks)),
immediate_info_(isolate, MAYBE_FIELD_PTR(env_info, immediate_info)),
tick_info_(isolate, MAYBE_FIELD_PTR(env_info, tick_info)),
exit_info_(isolate, MAYBE_FIELD_PTR(env_info, exit_info)),
timer_base_(uv_now(isolate_data->event_loop())),
exec_argv_(exec_args),
argv_(args),
exec_path_(GetExecPath(args)),
exiting_(isolate_, 1, MAYBE_FIELD_PTR(env_info, exiting)),
exit_info_(isolate_, 3, MAYBE_FIELD_PTR(env_info, exit_info)),
should_abort_on_uncaught_toggle_(
isolate_,
1,
Expand Down Expand Up @@ -1508,29 +1507,6 @@ void AsyncHooks::FailWithCorruptedAsyncStack(double expected_async_id) {
ABORT_NO_BACKTRACE();
}

ExitInfo::SerializeInfo ExitInfo::Serialize(Local<Context> context,
SnapshotCreator* creator) {
return {fields_.Serialize(context, creator)};
}

void ExitInfo::Deserialize(Local<Context> context) {
fields_.Deserialize(context);
}

std::ostream& operator<<(std::ostream& output,
const ExitInfo::SerializeInfo& i) {
output << "{ " << i.fields << " }";
return output;
}

void ExitInfo::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("fields", fields_);
}

ExitInfo::ExitInfo(Isolate* isolate, const SerializeInfo* info)
: fields_(
isolate, kFieldsCount, info == nullptr ? nullptr : &(info->fields)) {}

void Environment::Exit(ExitCode exit_code) {
if (options()->trace_exit) {
HandleScope handle_scope(isolate());
Expand Down Expand Up @@ -1619,9 +1595,8 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
info.async_hooks = async_hooks_.Serialize(ctx, creator);
info.immediate_info = immediate_info_.Serialize(ctx, creator);
info.tick_info = tick_info_.Serialize(ctx, creator);
info.exit_info = exit_info_.Serialize(ctx, creator);
info.performance_state = performance_state_->Serialize(ctx, creator);
info.exiting = exiting_.Serialize(ctx, creator);
info.exit_info = exit_info_.Serialize(ctx, creator);
info.stream_base_state = stream_base_state_.Serialize(ctx, creator);
info.should_abort_on_uncaught_toggle =
should_abort_on_uncaught_toggle_.Serialize(ctx, creator);
Expand Down Expand Up @@ -1662,9 +1637,8 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
async_hooks_.Deserialize(ctx);
immediate_info_.Deserialize(ctx);
tick_info_.Deserialize(ctx);
exit_info_.Deserialize(ctx);
performance_state_->Deserialize(ctx);
exiting_.Deserialize(ctx);
exit_info_.Deserialize(ctx);
stream_base_state_.Deserialize(ctx);
should_abort_on_uncaught_toggle_.Deserialize(ctx);

Expand Down Expand Up @@ -1851,15 +1825,14 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("builtins_without_cache", builtins_without_cache);
tracker->TrackField("destroy_async_id_list", destroy_async_id_list_);
tracker->TrackField("exec_argv", exec_argv_);
tracker->TrackField("exiting", exiting_);
tracker->TrackField("exit_info", exit_info_);
tracker->TrackField("should_abort_on_uncaught_toggle",
should_abort_on_uncaught_toggle_);
tracker->TrackField("stream_base_state", stream_base_state_);
tracker->TrackField("cleanup_queue", cleanup_queue_);
tracker->TrackField("async_hooks", async_hooks_);
tracker->TrackField("immediate_info", immediate_info_);
tracker->TrackField("tick_info", tick_info_);
tracker->TrackField("exit_info", exit_info_);
tracker->TrackField("principal_realm", principal_realm_);

// FIXME(joyeecheung): track other fields in Environment.
Expand Down
54 changes: 10 additions & 44 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,40 +459,8 @@ class TickInfo : public MemoryRetainer {
AliasedUint8Array fields_;
};

class ExitInfo : public MemoryRetainer {
public:
inline const AliasedInt32Array& fields();
inline bool has_exit_code() const;
inline int32_t exit_code() const;

SET_MEMORY_INFO_NAME(ExitInfo)
SET_SELF_SIZE(ExitInfo)
void MemoryInfo(MemoryTracker* tracker) const override;

ExitInfo(const ExitInfo&) = delete;
ExitInfo& operator=(const ExitInfo&) = delete;
ExitInfo(ExitInfo&&) = delete;
ExitInfo& operator=(ExitInfo&&) = delete;
~ExitInfo() = default;

struct SerializeInfo {
AliasedBufferIndex fields;
};
SerializeInfo Serialize(v8::Local<v8::Context> context,
v8::SnapshotCreator* creator);
void Deserialize(v8::Local<v8::Context> context);

enum Fields { kExitCode = 0, kHasExitCode, kFieldsCount };

private:
friend class Environment; // So we can call the constructor.
explicit ExitInfo(v8::Isolate* isolate, const SerializeInfo* info);

AliasedInt32Array fields_;
};

class TrackingTraceStateObserver :
public v8::TracingController::TraceStateObserver {
class TrackingTraceStateObserver
: public v8::TracingController::TraceStateObserver {
public:
explicit TrackingTraceStateObserver(Environment* env) : env_(env) {}

Expand Down Expand Up @@ -546,8 +514,7 @@ struct EnvSerializeInfo {
TickInfo::SerializeInfo tick_info;
ImmediateInfo::SerializeInfo immediate_info;
performance::PerformanceState::SerializeInfo performance_state;
ExitInfo::SerializeInfo exit_info;
AliasedBufferIndex exiting;
AliasedBufferIndex exit_info;
AliasedBufferIndex stream_base_state;
AliasedBufferIndex should_abort_on_uncaught_toggle;

Expand Down Expand Up @@ -759,8 +726,6 @@ class Environment : public MemoryRetainer {
inline AsyncHooks* async_hooks();
inline ImmediateInfo* immediate_info();
inline TickInfo* tick_info();
inline ExitInfo* exit_info();
inline int32_t maybe_exit_code(const int32_t default_code) const;
inline uint64_t timer_base() const;
inline std::shared_ptr<KVStore> env_vars();
inline void set_env_vars(std::shared_ptr<KVStore> env_vars);
Expand All @@ -776,10 +741,12 @@ class Environment : public MemoryRetainer {
inline void set_force_context_aware(bool value);
inline bool force_context_aware() const;

// This is a pseudo-boolean that keeps track of whether the process is
// exiting.
// This contains fields that are a pseudo-boolean that keeps track of whether
// the process is exiting, an integer representing the process exit code, and
// a pseudo-boolean to indicate whether the exit code is undefined.
inline AliasedInt32Array& exit_info();
inline void set_exiting(bool value);
inline AliasedUint32Array& exiting();
inline int32_t maybe_exit_code(const int32_t default_code) const;

// This stores whether the --abort-on-uncaught-exception flag was passed
// to Node.
Expand Down Expand Up @@ -1089,7 +1056,6 @@ class Environment : public MemoryRetainer {
AsyncHooks async_hooks_;
ImmediateInfo immediate_info_;
TickInfo tick_info_;
ExitInfo exit_info_;
const uint64_t timer_base_;
std::shared_ptr<KVStore> env_vars_;
bool printed_error_ = false;
Expand Down Expand Up @@ -1137,8 +1103,8 @@ class Environment : public MemoryRetainer {
uint32_t script_id_counter_ = 0;
uint32_t function_id_counter_ = 0;

// TODO(daeyeon): merge into `exit_info_`
AliasedUint32Array exiting_;
enum ExitInfoFields { kExiting = 0, kExitCode, kHasExitCode, kFieldsCount };
AliasedInt32Array exit_info_;

AliasedUint32Array should_abort_on_uncaught_toggle_;
int should_not_abort_scope_counter_ = 0;
Expand Down
1 change: 0 additions & 1 deletion src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
V(napi_type_tag, "node:napi:type_tag") \
V(napi_wrapper, "node:napi:wrapper") \
V(untransferable_object_private_symbol, "node:untransferableObject") \
V(exiting_aliased_Uint32Array, "node:exiting_aliased_Uint32Array") \
V(exit_info_private_symbol, "node:exit_info_private_symbol")

// Symbols are per-isolate primitives but Environment proxies them
Expand Down
2 changes: 0 additions & 2 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,6 @@ std::ostream& operator<<(std::ostream& output,
std::ostream& operator<<(std::ostream& output,
const AsyncHooks::SerializeInfo& d);
std::ostream& operator<<(std::ostream& output, const SnapshotMetadata& d);
std::ostream& operator<<(std::ostream& output,
const ExitInfo::SerializeInfo& d);

namespace performance {
std::ostream& operator<<(std::ostream& output,
Expand Down
11 changes: 1 addition & 10 deletions src/node_process_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,11 @@ MaybeLocal<Object> CreateProcessObject(Realm* realm) {
return MaybeLocal<Object>();
}

// process[exiting_aliased_Uint32Array]
if (process
->SetPrivate(context,
realm->env()->exiting_aliased_Uint32Array(),
realm->env()->exiting().GetJSArray())
.IsNothing()) {
return {};
}

// process[exit_info_private_symbol]
if (process
->SetPrivate(context,
realm->env()->exit_info_private_symbol(),
realm->env()->exit_info()->fields().GetJSArray())
realm->env()->exit_info().GetJSArray())
.IsNothing()) {
return {};
}
Expand Down
Loading

0 comments on commit 204ca1e

Please sign in to comment.