Skip to content

Commit 6d5b9ea

Browse files
committed
src: add can_call_into_js flag
PR-URL: ayojs#82
1 parent 745c213 commit 6d5b9ea

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

src/async-wrap.cc

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static void DestroyIdsCb(uv_timer_t* handle) {
152152
do {
153153
std::vector<double> destroy_ids_list;
154154
destroy_ids_list.swap(*env->destroy_ids_list());
155+
if (!env->can_call_into_js()) return;
155156
for (auto current_id : destroy_ids_list) {
156157
// Want each callback to be cleaned up after itself, instead of cleaning
157158
// them all up after the while() loop completes.
@@ -174,6 +175,9 @@ static void PushBackDestroyId(Environment* env, double id) {
174175
if (env->async_hooks()->fields()[AsyncHooks::kDestroy] == 0)
175176
return;
176177

178+
if (!env->can_call_into_js())
179+
return;
180+
177181
if (env->destroy_ids_list()->empty())
178182
uv_timer_start(env->destroy_ids_timer_handle(), DestroyIdsCb, 0, 0);
179183

src/env-inl.h

+8
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,14 @@ inline void Environment::set_fs_stats_field_array(double* fields) {
504504
fs_stats_field_array_ = fields;
505505
}
506506

507+
inline bool Environment::can_call_into_js() const {
508+
return can_call_into_js_;
509+
}
510+
511+
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
512+
can_call_into_js_ = can_call_into_js;
513+
}
514+
507515
inline performance::performance_state* Environment::performance_state() {
508516
return performance_state_;
509517
}

src/env.h

+5
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ class Environment {
609609
inline performance::performance_state* performance_state();
610610
inline std::map<std::string, uint64_t>* performance_marks();
611611

612+
inline bool can_call_into_js() const;
613+
inline void set_can_call_into_js(bool can_call_into_js);
614+
612615
inline void ThrowError(const char* errmsg);
613616
inline void ThrowTypeError(const char* errmsg);
614617
inline void ThrowRangeError(const char* errmsg);
@@ -706,6 +709,8 @@ class Environment {
706709
size_t makecallback_cntr_;
707710
std::vector<double> destroy_ids_list_;
708711

712+
bool can_call_into_js_ = true;
713+
709714
performance::performance_state* performance_state_ = nullptr;
710715
std::map<std::string, uint64_t> performance_marks_;
711716

src/node.cc

+9
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,11 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
13861386
CHECK(!object.IsEmpty());
13871387
}
13881388

1389+
if (!env->can_call_into_js()) {
1390+
failed_ = true;
1391+
return;
1392+
}
1393+
13891394
HandleScope handle_scope(env->isolate());
13901395
// If you hit this assertion, you forgot to enter the v8::Context first.
13911396
CHECK_EQ(env->context(), env->isolate()->GetCurrentContext());
@@ -1433,6 +1438,7 @@ void InternalCallbackScope::Close() {
14331438

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

1441+
if (!env_->can_call_into_js()) return;
14361442
if (tick_info->length() == 0) {
14371443
env_->isolate()->RunMicrotasks();
14381444
}
@@ -1452,6 +1458,8 @@ void InternalCallbackScope::Close() {
14521458
CHECK_EQ(env_->current_async_id(), 0);
14531459
CHECK_EQ(env_->trigger_id(), 0);
14541460

1461+
if (!env_->can_call_into_js()) return;
1462+
14551463
if (env_->tick_callback_function()->Call(process, 0, nullptr).IsEmpty()) {
14561464
failed_ = true;
14571465
}
@@ -4756,6 +4764,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
47564764

47574765
const int exit_code = EmitExit(&env);
47584766

4767+
env.set_can_call_into_js(false);
47594768
env.RunCleanup();
47604769
RunAtExit(&env);
47614770
uv_key_delete(&thread_local_env);

src/node_contextify.cc

+2
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,8 @@ class ContextifyScript : public BaseObject {
986986
const bool break_on_sigint,
987987
const FunctionCallbackInfo<Value>& args,
988988
TryCatch* try_catch) {
989+
if (!env->can_call_into_js())
990+
return false;
989991
if (!ContextifyScript::InstanceOf(env, args.Holder())) {
990992
env->ThrowTypeError(
991993
"Script methods can only be called on script instances.");

0 commit comments

Comments
 (0)