Skip to content

Commit 35a62c5

Browse files
committed
squash! alternative take without $natives
1 parent f51b943 commit 35a62c5

File tree

8 files changed

+75
-47
lines changed

8 files changed

+75
-47
lines changed

lib/_debugger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Client.prototype._addHandle = function(desc) {
180180
};
181181

182182

183-
const natives = process.binding('$natives');
183+
const natives = process.binding('natives');
184184

185185

186186
Client.prototype._addScript = function(desc) {

lib/internal/bootstrap_node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@
401401
this.loading = false;
402402
}
403403

404-
NativeModule._source = process.binding('$natives');
404+
NativeModule._source = process.binding('natives');
405405
NativeModule._cache = {};
406406

407407
NativeModule.require = function(id) {

lib/internal/v8_prof_processor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const scriptFiles = [
1515
var script = '';
1616

1717
scriptFiles.forEach(function(s) {
18-
script += process.binding('$natives')[s] + '\n';
18+
script += process.binding('natives')[s] + '\n';
1919
});
2020

2121
var tickArguments = [];

src/env-inl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ inline uint64_t Environment::timer_base() const {
277277
return timer_base_;
278278
}
279279

280+
inline int Environment::bootstrap_script_id() const {
281+
CHECK(!internal_script_ids_.empty());
282+
return internal_script_ids_[0];
283+
}
284+
285+
inline std::vector<int>* Environment::internal_script_ids() {
286+
return &internal_script_ids_;
287+
}
288+
280289
inline bool Environment::using_domains() const {
281290
return using_domains_;
282291
}

src/env.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "v8.h"
1717

1818
#include <stdint.h>
19+
#include <vector>
1920

2021
// Caveat emptor: we're going slightly crazy with macros here but the end
2122
// hopefully justifies the means. We have a lot of per-context properties
@@ -451,6 +452,9 @@ class Environment {
451452
inline node_ares_task_list* cares_task_list();
452453
inline IsolateData* isolate_data() const;
453454

455+
inline int bootstrap_script_id() const;
456+
inline std::vector<int>* internal_script_ids();
457+
454458
inline bool using_domains() const;
455459
inline void set_using_domains(bool value);
456460

@@ -578,6 +582,7 @@ class Environment {
578582
uint32_t* heap_space_statistics_buffer_ = nullptr;
579583

580584
char* http_parser_buffer_;
585+
std::vector<int> internal_script_ids_;
581586

582587
#define V(PropertyName, TypeName) \
583588
v8::Persistent<TypeName> PropertyName ## _;

src/node.cc

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
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;
123124
using v8::PropertyCallbackInfo;
124125
using v8::ScriptOrigin;
125126
using v8::SealHandleScope;
127+
using v8::StackTrace;
126128
using v8::String;
127129
using v8::TryCatch;
128130
using 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-
16911664
static 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+
25462534
static 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

src/node_contextify.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,20 +515,18 @@ class ContextifyScript : public BaseObject {
515515
else if (produce_cached_data)
516516
compile_options = ScriptCompiler::kProduceCodeCache;
517517

518-
MaybeLocal<UnboundScript> v8_script = ScriptCompiler::CompileUnboundScript(
519-
env->isolate(),
520-
&source,
521-
compile_options);
522-
523-
if (v8_script.IsEmpty()) {
518+
Local<UnboundScript> script;
519+
auto maybe_script =
520+
ScriptCompiler::CompileUnboundScript(env->isolate(), &source,
521+
compile_options);
522+
if (!maybe_script.ToLocal(&script)) {
524523
if (display_errors) {
525524
DecorateErrorStack(env, try_catch);
526525
}
527526
try_catch.ReThrow();
528527
return;
529528
}
530-
contextify_script->script_.Reset(env->isolate(),
531-
v8_script.ToLocalChecked());
529+
contextify_script->script_.Reset(env->isolate(), script);
532530

533531
if (compile_options == ScriptCompiler::kConsumeCodeCache) {
534532
args.This()->Set(
@@ -548,6 +546,12 @@ class ContextifyScript : public BaseObject {
548546
env->cached_data_produced_string(),
549547
Boolean::New(env->isolate(), cached_data_produced));
550548
}
549+
550+
auto caller_script_id = GetCallerScriptId(env->isolate());
551+
if (caller_script_id == env->bootstrap_script_id()) {
552+
// Called by NativeModule.require().
553+
env->internal_script_ids()->push_back(script->GetId());
554+
}
551555
}
552556

553557

src/node_internals.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ void SetupProcessObject(Environment* env,
151151
int exec_argc,
152152
const char* const* exec_argv);
153153

154+
// Returns the script id of the top-most JS frame or v8::Message::kNoScriptId
155+
// if no such frame exists or if the frame cannot be mapped to a script.
156+
int GetCallerScriptId(v8::Isolate* isolate);
157+
154158
enum Endianness {
155159
kLittleEndian, // _Not_ LITTLE_ENDIAN, clashes with endian.h.
156160
kBigEndian

0 commit comments

Comments
 (0)