Skip to content

Commit 05948d8

Browse files
committed
lib: remove use of Debug.MakeMirror()
This paves the way for removing `vm.runInDebugContext()`. Inspection of Map and Set iterators is now done through V8 instrinsics. Fixes: #11875 PR-URL: #13295 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Robert Jefe Lindstaedt <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 0028241 commit 05948d8

File tree

6 files changed

+63
-20
lines changed

6 files changed

+63
-20
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
lib/internal/v8.js
12
lib/internal/v8_prof_polyfill.js
23
lib/punycode.js
34
test/addons/??_*

lib/internal/bootstrap_node.js

+15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
// do this good and early, since it handles errors.
2828
setupProcessFatal();
2929

30+
setupV8();
3031
setupProcessICUVersions();
3132

3233
setupGlobalVariables();
@@ -414,6 +415,20 @@
414415
};
415416
}
416417

418+
function setupV8() {
419+
// Warm up the map and set iterator preview functions. V8 compiles
420+
// functions lazily (unless --nolazy is set) so we need to do this
421+
// before we turn off --allow_natives_syntax again.
422+
const v8 = NativeModule.require('internal/v8');
423+
v8.previewMapIterator(new Map().entries(), 1);
424+
v8.previewSetIterator(new Set().entries(), 1);
425+
// Disable --allow_natives_syntax again unless it was explicitly
426+
// specified on the command line.
427+
const re = /^--allow[-_]natives[-_]syntax$/;
428+
if (!process.execArgv.some((s) => re.test(s)))
429+
process.binding('v8').setFlagsFromString('--noallow_natives_syntax');
430+
}
431+
417432
function setupProcessICUVersions() {
418433
const icu = process.binding('config').hasIntl ?
419434
process.binding('icu') : undefined;

lib/internal/v8.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
function take(it, n) {
4+
const result = [];
5+
for (const e of it) {
6+
if (--n < 0)
7+
break;
8+
result.push(e);
9+
}
10+
return result;
11+
}
12+
13+
function previewMapIterator(it, n) {
14+
return take(%MapIteratorClone(it), n);
15+
}
16+
17+
function previewSetIterator(it, n) {
18+
return take(%SetIteratorClone(it), n);
19+
}
20+
21+
module.exports = { previewMapIterator, previewSetIterator };

lib/util.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const { TextDecoder, TextEncoder } = require('internal/encoding');
2626
const { isBuffer } = require('buffer').Buffer;
2727

2828
const { errname } = process.binding('uv');
29+
const { previewMapIterator, previewSetIterator } = require('internal/v8');
2930

3031
const {
3132
getPromiseDetails,
@@ -77,7 +78,6 @@ const dateToISOString = Date.prototype.toISOString;
7778
const errorToString = Error.prototype.toString;
7879

7980
var CIRCULAR_ERROR_MESSAGE;
80-
var Debug;
8181

8282
/* eslint-disable */
8383
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
@@ -356,17 +356,6 @@ function stylizeNoColor(str, styleType) {
356356
return str;
357357
}
358358

359-
function ensureDebugIsInitialized() {
360-
if (Debug === undefined) {
361-
const runInDebugContext = require('vm').runInDebugContext;
362-
// a workaround till this entire method is removed
363-
const originalValue = process.noDeprecation;
364-
process.noDeprecation = true;
365-
Debug = runInDebugContext('Debug');
366-
process.noDeprecation = originalValue;
367-
}
368-
}
369-
370359
function formatValue(ctx, value, recurseTimes, ln) {
371360
// Primitive types cannot have properties
372361
if (typeof value !== 'object' && typeof value !== 'function') {
@@ -474,10 +463,10 @@ function formatValue(ctx, value, recurseTimes, ln) {
474463
formatter = formatTypedArray;
475464
} else if (isMapIterator(value)) {
476465
braces = ['MapIterator {', '}'];
477-
formatter = formatCollectionIterator;
466+
formatter = formatMapIterator;
478467
} else if (isSetIterator(value)) {
479468
braces = ['SetIterator {', '}'];
480-
formatter = formatCollectionIterator;
469+
formatter = formatSetIterator;
481470
} else {
482471
// Check for boxed strings with valueOf()
483472
// The .valueOf() call can fail for a multitude of reasons
@@ -782,17 +771,27 @@ function formatMap(ctx, value, recurseTimes, keys) {
782771
return output;
783772
}
784773

785-
function formatCollectionIterator(ctx, value, recurseTimes, keys) {
786-
ensureDebugIsInitialized();
787-
const mirror = Debug.MakeMirror(value, true);
788-
const vals = mirror.preview();
789-
const output = [];
774+
function formatCollectionIterator(preview, ctx, value, recurseTimes,
775+
visibleKeys, keys) {
776+
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
777+
var vals = preview(value, 100);
778+
var output = [];
790779
for (const o of vals) {
791-
output.push(formatValue(ctx, o, recurseTimes));
780+
output.push(formatValue(ctx, o, nextRecurseTimes));
792781
}
793782
return output;
794783
}
795784

785+
function formatMapIterator(ctx, value, recurseTimes, visibleKeys, keys) {
786+
return formatCollectionIterator(previewMapIterator, ctx, value, recurseTimes,
787+
visibleKeys, keys);
788+
}
789+
790+
function formatSetIterator(ctx, value, recurseTimes, visibleKeys, keys) {
791+
return formatCollectionIterator(previewSetIterator, ctx, value, recurseTimes,
792+
visibleKeys, keys);
793+
}
794+
796795
function formatPromise(ctx, value, recurseTimes, keys) {
797796
var output;
798797
const [state, result] = getPromiseDetails(value);

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
'lib/internal/http2/core.js',
133133
'lib/internal/http2/compat.js',
134134
'lib/internal/http2/util.js',
135+
'lib/internal/v8.js',
135136
'lib/internal/v8_prof_polyfill.js',
136137
'lib/internal/v8_prof_processor.js',
137138
'lib/internal/streams/lazy_transform.js',

src/node.cc

+6
Original file line numberDiff line numberDiff line change
@@ -4288,6 +4288,12 @@ void Init(int* argc,
42884288
const char no_typed_array_heap[] = "--typed_array_max_size_in_heap=0";
42894289
V8::SetFlagsFromString(no_typed_array_heap, sizeof(no_typed_array_heap) - 1);
42904290

4291+
// Needed for access to V8 intrinsics. Disabled again during bootstrapping,
4292+
// see lib/internal/bootstrap_node.js.
4293+
const char allow_natives_syntax[] = "--allow_natives_syntax";
4294+
V8::SetFlagsFromString(allow_natives_syntax,
4295+
sizeof(allow_natives_syntax) - 1);
4296+
42914297
// We should set node_is_initialized here instead of in node::Start,
42924298
// otherwise embedders using node::Init to initialize everything will not be
42934299
// able to set it and native modules will not load for them.

0 commit comments

Comments
 (0)