Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit052e095

Browse files
joyeecheungrichardlau
authored andcommitted
vm: use internal versions of compileFunction and Script
Instead of using the public versions of the vm APIs internally,use the internal versions so that we can skip unnecessaryargument validation.The public versions would need special care to the generationof host-defined options to hit the isolate compilation cachewhen imporModuleDynamically isn't used, while internally it'salmost always used, so this allows us to handle the host-definedoptions separately.PR-URL:#50137Backport-PR-URL:#51004Refs:#35375Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>Reviewed-By: Chengzhong Wu <legendecas@gmail.com>Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent9f7899e commit052e095

File tree

7 files changed

+245
-173
lines changed

7 files changed

+245
-173
lines changed

‎lib/internal/modules/cjs/loader.js

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const {
5353
SafeMap,
5454
SafeWeakMap,
5555
String,
56+
Symbol,
5657
StringPrototypeCharAt,
5758
StringPrototypeCharCodeAt,
5859
StringPrototypeEndsWith,
@@ -85,7 +86,12 @@ const {
8586
setOwnProperty,
8687
getLazy,
8788
}=require('internal/util');
88-
const{ internalCompileFunction}=require('internal/vm');
89+
const{
90+
internalCompileFunction,
91+
makeContextifyScript,
92+
runScriptInThisContext,
93+
}=require('internal/vm');
94+
8995
constassert=require('internal/assert');
9096
constfs=require('fs');
9197
constpath=require('path');
@@ -1236,7 +1242,6 @@ Module.prototype.require = function(id) {
12361242
letresolvedArgv;
12371243
lethasPausedEntry=false;
12381244
/**@type {import('vm').Script} */
1239-
letScript;
12401245

12411246
/**
12421247
* Wraps the given content in a script and runs it in a new context.
@@ -1245,46 +1250,49 @@ let Script;
12451250
*@param {Module} cjsModuleInstance The CommonJS loader instance
12461251
*/
12471252
functionwrapSafe(filename,content,cjsModuleInstance){
1253+
consthostDefinedOptionId=Symbol(`cjs:${filename}`);
1254+
asyncfunctionimportModuleDynamically(specifier,_,importAttributes){
1255+
constcascadedLoader=getCascadedLoader();
1256+
returncascadedLoader.import(specifier,normalizeReferrerURL(filename),
1257+
importAttributes);
1258+
}
12481259
if(patched){
1249-
constwrapper=Module.wrap(content);
1250-
if(Script===undefined){
1251-
({ Script}=require('vm'));
1252-
}
1253-
constscript=newScript(wrapper,{
1254-
filename,
1255-
lineOffset:0,
1256-
importModuleDynamically:async(specifier,_,importAttributes)=>{
1257-
constcascadedLoader=getCascadedLoader();
1258-
returncascadedLoader.import(specifier,normalizeReferrerURL(filename),
1259-
importAttributes);
1260-
},
1261-
});
1260+
constwrapped=Module.wrap(content);
1261+
constscript=makeContextifyScript(
1262+
wrapped,// code
1263+
filename,// filename
1264+
0,// lineOffset
1265+
0,// columnOffset
1266+
undefined,// cachedData
1267+
false,// produceCachedData
1268+
undefined,// parsingContext
1269+
hostDefinedOptionId,// hostDefinedOptionId
1270+
importModuleDynamically,// importModuleDynamically
1271+
);
12621272

12631273
// Cache the source map for the module if present.
12641274
if(script.sourceMapURL){
12651275
maybeCacheSourceMap(filename,content,this,false,undefined,script.sourceMapURL);
12661276
}
12671277

1268-
returnscript.runInThisContext({
1269-
displayErrors:true,
1270-
});
1278+
returnrunScriptInThisContext(script,true,false);
12711279
}
12721280

1281+
constparams=['exports','require','module','__filename','__dirname'];
12731282
try{
1274-
constresult=internalCompileFunction(content,[
1275-
'exports',
1276-
'require',
1277-
'module',
1278-
'__filename',
1279-
'__dirname',
1280-
],{
1281-
filename,
1282-
importModuleDynamically(specifier,_,importAttributes){
1283-
constcascadedLoader=getCascadedLoader();
1284-
returncascadedLoader.import(specifier,normalizeReferrerURL(filename),
1285-
importAttributes);
1286-
},
1287-
});
1283+
constresult=internalCompileFunction(
1284+
content,// code,
1285+
filename,// filename
1286+
0,// lineOffset
1287+
0,// columnOffset,
1288+
undefined,// cachedData
1289+
false,// produceCachedData
1290+
undefined,// parsingContext
1291+
undefined,// contextExtensions
1292+
params,// params
1293+
hostDefinedOptionId,// hostDefinedOptionId
1294+
importModuleDynamically,// importModuleDynamically
1295+
);
12881296

12891297
// Cache the source map for the module if present.
12901298
if(result.sourceMapURL){

‎lib/internal/process/execution.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const{
4+
Symbol,
45
RegExpPrototypeExec,
56
globalThis,
67
}=primordials;
@@ -24,7 +25,9 @@ const {
2425
emitAfter,
2526
popAsyncContext,
2627
}=require('internal/async_hooks');
27-
28+
const{
29+
makeContextifyScript, runScriptInThisContext,
30+
}=require('internal/vm');
2831
// shouldAbortOnUncaughtToggle is a typed array for faster
2932
// communication with JS.
3033
const{ shouldAbortOnUncaughtToggle}=internalBinding('util');
@@ -52,8 +55,7 @@ function evalModule(source, print) {
5255

5356
functionevalScript(name,body,breakFirstLine,print,shouldLoadESM=false){
5457
constCJSModule=require('internal/modules/cjs/loader').Module;
55-
const{ kVmBreakFirstLineSymbol}=require('internal/util');
56-
const{ pathToFileURL}=require('url');
58+
const{ pathToFileURL}=require('internal/url');
5759

5860
constcwd=tryGetCwd();
5961
constorigModule=globalThis.module;// Set e.g. when called from the REPL.
@@ -78,16 +80,25 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
7880
`;
7981
globalThis.__filename=name;
8082
RegExpPrototypeExec(/^/,'');// Necessary to reset RegExp statics before user code runs.
81-
constresult=module._compile(script,`${name}-wrapper`)(()=>
82-
require('vm').runInThisContext(body,{
83-
filename:name,
84-
displayErrors:true,
85-
[kVmBreakFirstLineSymbol]:!!breakFirstLine,
86-
importModuleDynamically(specifier,_,importAttributes){
87-
constloader=asyncESM.esmLoader;
88-
returnloader.import(specifier,baseUrl,importAttributes);
89-
},
90-
}));
83+
constresult=module._compile(script,`${name}-wrapper`)(()=>{
84+
consthostDefinedOptionId=Symbol(name);
85+
asyncfunctionimportModuleDynamically(specifier,_,importAttributes){
86+
constloader=asyncESM.esmLoader;
87+
returnloader.import(specifier,baseUrl,importAttributes);
88+
}
89+
constscript=makeContextifyScript(
90+
body,// code
91+
name,// filename,
92+
0,// lineOffset
93+
0,// columnOffset,
94+
undefined,// cachedData
95+
false,// produceCachedData
96+
undefined,// parsingContext
97+
hostDefinedOptionId,// hostDefinedOptionId
98+
importModuleDynamically,// importModuleDynamically
99+
);
100+
returnrunScriptInThisContext(script,true,!!breakFirstLine);
101+
});
91102
if(print){
92103
const{ log}=require('internal/console/global');
93104
log(result);

‎lib/internal/vm.js

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
'use strict';
22

33
const{
4-
ArrayPrototypeForEach,
4+
ReflectApply,
55
Symbol,
66
}=primordials;
77

88
const{
9+
ContextifyScript,
910
compileFunction,
1011
isContext:_isContext,
1112
}=internalBinding('contextify');
13+
const{
14+
runInContext,
15+
}=ContextifyScript.prototype;
1216
const{
1317
default_host_defined_options,
1418
}=internalBinding('symbols');
1519
const{
16-
validateArray,
17-
validateBoolean,
18-
validateBuffer,
1920
validateFunction,
2021
validateObject,
21-
validateString,
22-
validateStringArray,
23-
validateInt32,
2422
}=require('internal/validators');
25-
const{
26-
ERR_INVALID_ARG_TYPE,
27-
}=require('internal/errors').codes;
2823

2924
functionisContext(object){
3025
validateObject(object,'object',{__proto__:null,allowArray:true});
@@ -48,49 +43,20 @@ function getHostDefinedOptionId(importModuleDynamically, filename) {
4843
returnSymbol(filename);
4944
}
5045

51-
functioninternalCompileFunction(code,params,options){
52-
validateString(code,'code');
53-
if(params!==undefined){
54-
validateStringArray(params,'params');
55-
}
56-
const{
57-
filename='',
58-
columnOffset=0,
59-
lineOffset=0,
60-
cachedData=undefined,
61-
produceCachedData=false,
62-
parsingContext=undefined,
63-
contextExtensions=[],
64-
importModuleDynamically,
65-
}=options;
66-
67-
validateString(filename,'options.filename');
68-
validateInt32(columnOffset,'options.columnOffset');
69-
validateInt32(lineOffset,'options.lineOffset');
70-
if(cachedData!==undefined)
71-
validateBuffer(cachedData,'options.cachedData');
72-
validateBoolean(produceCachedData,'options.produceCachedData');
73-
if(parsingContext!==undefined){
74-
if(
75-
typeofparsingContext!=='object'||
76-
parsingContext===null||
77-
!isContext(parsingContext)
78-
){
79-
thrownewERR_INVALID_ARG_TYPE(
80-
'options.parsingContext',
81-
'Context',
82-
parsingContext,
83-
);
84-
}
85-
}
86-
validateArray(contextExtensions,'options.contextExtensions');
87-
ArrayPrototypeForEach(contextExtensions,(extension,i)=>{
88-
constname=`options.contextExtensions[${i}]`;
89-
validateObject(extension,name,{__proto__:null,nullable:true});
46+
functionregisterImportModuleDynamically(referrer,importModuleDynamically){
47+
const{ importModuleDynamicallyWrap}=require('internal/vm/module');
48+
const{ registerModule}=require('internal/modules/esm/utils');
49+
registerModule(referrer,{
50+
__proto__:null,
51+
importModuleDynamically:
52+
importModuleDynamicallyWrap(importModuleDynamically),
9053
});
54+
}
9155

92-
consthostDefinedOptionId=
93-
getHostDefinedOptionId(importModuleDynamically,filename);
56+
functioninternalCompileFunction(
57+
code,filename,lineOffset,columnOffset,
58+
cachedData,produceCachedData,parsingContext,contextExtensions,
59+
params,hostDefinedOptionId,importModuleDynamically){
9460
constresult=compileFunction(
9561
code,
9662
filename,
@@ -117,23 +83,65 @@ function internalCompileFunction(code, params, options) {
11783
}
11884

11985
if(importModuleDynamically!==undefined){
120-
validateFunction(importModuleDynamically,
121-
'options.importModuleDynamically');
122-
const{ importModuleDynamicallyWrap}=require('internal/vm/module');
123-
constwrapped=importModuleDynamicallyWrap(importModuleDynamically);
124-
constfunc=result.function;
125-
const{ registerModule}=require('internal/modules/esm/utils');
126-
registerModule(func,{
127-
__proto__:null,
128-
importModuleDynamically:wrapped,
129-
});
86+
registerImportModuleDynamically(result.function,importModuleDynamically);
13087
}
13188

13289
returnresult;
13390
}
13491

92+
functionmakeContextifyScript(code,
93+
filename,
94+
lineOffset,
95+
columnOffset,
96+
cachedData,
97+
produceCachedData,
98+
parsingContext,
99+
hostDefinedOptionId,
100+
importModuleDynamically){
101+
letscript;
102+
// Calling `ReThrow()` on a native TryCatch does not generate a new
103+
// abort-on-uncaught-exception check. A dummy try/catch in JS land
104+
// protects against that.
105+
try{// eslint-disable-line no-useless-catch
106+
script=newContextifyScript(code,
107+
filename,
108+
lineOffset,
109+
columnOffset,
110+
cachedData,
111+
produceCachedData,
112+
parsingContext,
113+
hostDefinedOptionId);
114+
}catch(e){
115+
throwe;/* node-do-not-add-exception-line */
116+
}
117+
118+
if(importModuleDynamically!==undefined){
119+
registerImportModuleDynamically(script,importModuleDynamically);
120+
}
121+
returnscript;
122+
}
123+
124+
// Internal version of vm.Script.prototype.runInThisContext() which skips
125+
// argument validation.
126+
functionrunScriptInThisContext(script,displayErrors,breakOnFirstLine){
127+
returnReflectApply(
128+
runInContext,
129+
script,
130+
[
131+
null,// sandbox - use current context
132+
-1,// timeout
133+
displayErrors,// displayErrors
134+
false,// breakOnSigint
135+
breakOnFirstLine,// breakOnFirstLine
136+
],
137+
);
138+
}
139+
135140
module.exports={
136141
getHostDefinedOptionId,
137142
internalCompileFunction,
138143
isContext,
144+
makeContextifyScript,
145+
registerImportModuleDynamically,
146+
runScriptInThisContext,
139147
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp