Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
src: add can_call_into_js flag
Browse files Browse the repository at this point in the history
PR-URL: #82
Reviewed-By: Stephen Belanger <[email protected]>
  • Loading branch information
addaleax committed Sep 27, 2017
1 parent b07785f commit 93ea69d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static void DestroyIdsCb(uv_timer_t* handle) {
do {
std::vector<double> destroy_ids_list;
destroy_ids_list.swap(*env->destroy_ids_list());
if (!env->can_call_into_js()) return;
for (auto current_id : destroy_ids_list) {
// Want each callback to be cleaned up after itself, instead of cleaning
// them all up after the while() loop completes.
Expand All @@ -174,6 +175,9 @@ static void PushBackDestroyId(Environment* env, double id) {
if (env->async_hooks()->fields()[AsyncHooks::kDestroy] == 0)
return;

if (!env->can_call_into_js())
return;

if (env->destroy_ids_list()->empty())
uv_timer_start(env->destroy_ids_timer_handle(), DestroyIdsCb, 0, 0);

Expand Down
8 changes: 8 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ inline void Environment::set_fs_stats_field_array(double* fields) {
fs_stats_field_array_ = fields;
}

inline bool Environment::can_call_into_js() const {
return can_call_into_js_;
}

inline void Environment::set_can_call_into_js(bool can_call_into_js) {
can_call_into_js_ = can_call_into_js;
}

inline performance::performance_state* Environment::performance_state() {
return performance_state_;
}
Expand Down
5 changes: 5 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@ class Environment {
inline performance::performance_state* performance_state();
inline std::map<std::string, uint64_t>* performance_marks();

inline bool can_call_into_js() const;
inline void set_can_call_into_js(bool can_call_into_js);

inline void ThrowError(const char* errmsg);
inline void ThrowTypeError(const char* errmsg);
inline void ThrowRangeError(const char* errmsg);
Expand Down Expand Up @@ -706,6 +709,8 @@ class Environment {
size_t makecallback_cntr_;
std::vector<double> destroy_ids_list_;

bool can_call_into_js_ = true;

performance::performance_state* performance_state_ = nullptr;
std::map<std::string, uint64_t> performance_marks_;

Expand Down
9 changes: 9 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,11 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
CHECK(!object.IsEmpty());
}

if (!env->can_call_into_js()) {
failed_ = true;
return;
}

HandleScope handle_scope(env->isolate());
// If you hit this assertion, you forgot to enter the v8::Context first.
CHECK_EQ(env->context(), env->isolate()->GetCurrentContext());
Expand Down Expand Up @@ -1433,6 +1438,7 @@ void InternalCallbackScope::Close() {

Environment::TickInfo* tick_info = env_->tick_info();

if (!env_->can_call_into_js()) return;
if (tick_info->length() == 0) {
env_->isolate()->RunMicrotasks();
}
Expand All @@ -1452,6 +1458,8 @@ void InternalCallbackScope::Close() {
CHECK_EQ(env_->current_async_id(), 0);
CHECK_EQ(env_->trigger_id(), 0);

if (!env_->can_call_into_js()) return;

if (env_->tick_callback_function()->Call(process, 0, nullptr).IsEmpty()) {
failed_ = true;
}
Expand Down Expand Up @@ -4756,6 +4764,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,

const int exit_code = EmitExit(&env);

env.set_can_call_into_js(false);
env.RunCleanup();
RunAtExit(&env);
uv_key_delete(&thread_local_env);
Expand Down
2 changes: 2 additions & 0 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,8 @@ class ContextifyScript : public BaseObject {
const bool break_on_sigint,
const FunctionCallbackInfo<Value>& args,
TryCatch* try_catch) {
if (!env->can_call_into_js())
return false;
if (!ContextifyScript::InstanceOf(env, args.Holder())) {
env->ThrowTypeError(
"Script methods can only be called on script instances.");
Expand Down

0 comments on commit 93ea69d

Please sign in to comment.