5959#include < string.h>
6060#include < sys/types.h>
6161
62+ #include < algorithm>
6263#include < string>
6364#include < vector>
6465
@@ -123,6 +124,7 @@ using v8::PromiseRejectMessage;
123124using v8::PropertyCallbackInfo;
124125using v8::ScriptOrigin;
125126using v8::SealHandleScope;
127+ using v8::StackTrace;
126128using v8::String;
127129using v8::TryCatch;
128130using v8::Uint32Array;
@@ -1659,35 +1661,6 @@ static void ReportException(Environment* env, const TryCatch& try_catch) {
16591661}
16601662
16611663
1662- // Executes a str within the current v8 context.
1663- static Local<Value> ExecuteString (Environment* env,
1664- Local<String> source,
1665- Local<String> filename) {
1666- EscapableHandleScope scope (env->isolate ());
1667- TryCatch try_catch (env->isolate ());
1668-
1669- // try_catch must be nonverbose to disable FatalException() handler,
1670- // we will handle exceptions ourself.
1671- try_catch.SetVerbose (false );
1672-
1673- ScriptOrigin origin (filename);
1674- MaybeLocal<v8::Script> script =
1675- v8::Script::Compile (env->context (), source, &origin);
1676- if (script.IsEmpty ()) {
1677- ReportException (env, try_catch);
1678- exit (3 );
1679- }
1680-
1681- Local<Value> result = script.ToLocalChecked ()->Run ();
1682- if (result.IsEmpty ()) {
1683- ReportException (env, try_catch);
1684- exit (4 );
1685- }
1686-
1687- return scope.Escape (result);
1688- }
1689-
1690-
16911664static void GetActiveRequests (const FunctionCallbackInfo<Value>& args) {
16921665 Environment* env = Environment::GetCurrent (args);
16931666
@@ -2543,6 +2516,21 @@ void ClearFatalExceptionHandlers(Environment* env) {
25432516}
25442517
25452518
2519+ int GetCallerScriptId (v8::Isolate* isolate) {
2520+ auto options = StackTrace::kScriptId ;
2521+ auto stack_trace = StackTrace::CurrentStackTrace (isolate, 1 , options);
2522+ if (stack_trace->GetFrameCount () > 0 )
2523+ return stack_trace->GetFrame (0 )->GetScriptId ();
2524+ return Message::kNoScriptIdInfo ;
2525+ }
2526+
2527+
2528+ inline bool IsInternalScriptId (Environment* env, int script_id) {
2529+ auto ids = env->internal_script_ids ();
2530+ return ids->end () != std::find (ids->begin (), ids->end (), script_id);
2531+ }
2532+
2533+
25462534static void Binding (const FunctionCallbackInfo<Value>& args) {
25472535 Environment* env = Environment::GetCurrent (args);
25482536
@@ -2580,16 +2568,17 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
25802568 exports = Object::New (env->isolate ());
25812569 DefineConstants (env->isolate (), exports);
25822570 cache->Set (module , exports);
2583- } else if (!strcmp (*module_v, " $natives" ) || !strcmp (*module_v, " natives" )) {
2584- if (!strcmp (*module_v, " natives" )) {
2571+ } else if (!strcmp (*module_v, " natives" )) {
2572+ auto caller_script_id = GetCallerScriptId (env->isolate ());
2573+ if (!IsInternalScriptId (env, caller_script_id)) {
25852574 // graceful-fs < 4 evals process.binding('natives').fs, which prohibits
25862575 // using internal modules in that module. Encourage people to upgrade.
25872576 auto pid = getpid ();
25882577 fprintf (stderr,
25892578 " (node:%d) process.binding('natives') is deprecated.\n " , pid);
25902579 fprintf (stderr,
25912580 " (node:%d) If you use graceful-fs < 4, please update.\n " , pid);
2592- auto stack_trace = v8:: StackTrace::CurrentStackTrace (env->isolate (), 12 );
2581+ auto stack_trace = StackTrace::CurrentStackTrace (env->isolate (), 12 );
25932582 for (int i = 0 , n = stack_trace->GetFrameCount (); i < n; i += 1 ) {
25942583 Local<v8::StackFrame> frame = stack_trace->GetFrame (i);
25952584 node::Utf8Value name (env->isolate (), frame->GetScriptName ());
@@ -2600,7 +2589,6 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
26002589 }
26012590 exports = Object::New (env->isolate ());
26022591 DefineJavaScript (env, exports);
2603- cache->Set (module , exports);
26042592 } else {
26052593 char errmsg[1024 ];
26062594 snprintf (errmsg,
@@ -3394,14 +3382,32 @@ void LoadEnvironment(Environment* env) {
33943382 // 'internal_bootstrap_node_native' is the string containing that source code.
33953383 Local<String> script_name = FIXED_ONE_BYTE_STRING (env->isolate (),
33963384 " bootstrap_node.js" );
3397- Local<Value> f_value = ExecuteString (env, MainSource (env), script_name);
3385+ ScriptOrigin origin (script_name);
3386+ Local<v8::Script> script;
3387+ auto maybe_script =
3388+ v8::Script::Compile (env->context (), MainSource (env), &origin);
3389+ if (!maybe_script.ToLocal (&script)) {
3390+ ReportException (env, try_catch);
3391+ exit (3 );
3392+ }
3393+
3394+ auto internal_script_ids = env->internal_script_ids ();
3395+ CHECK (internal_script_ids->empty ());
3396+ internal_script_ids->push_back (script->GetUnboundScript ()->GetId ());
3397+
3398+ Local<Value> result = script->Run ();
3399+ if (result.IsEmpty ()) {
3400+ ReportException (env, try_catch);
3401+ exit (4 );
3402+ }
3403+
33983404 if (try_catch.HasCaught ()) {
33993405 ReportException (env, try_catch);
34003406 exit (10 );
34013407 }
34023408 // The bootstrap_node.js file returns a function 'f'
3403- CHECK (f_value ->IsFunction ());
3404- Local<Function> f = Local<Function>::Cast (f_value );
3409+ CHECK (result ->IsFunction ());
3410+ Local<Function> f = Local<Function>::Cast (result );
34053411
34063412 // Now we call 'f' with the 'process' variable that we've built up with
34073413 // all our bindings. Inside bootstrap_node.js we'll take care of
0 commit comments