当前位置:网站首页>Module loader implementation of no.js
Module loader implementation of no.js
2022-07-22 08:21:00 【theanarkh】
Preface : Recently No.js A simple module loader is implemented in , This paper briefly introduces the implementation of loader .
because JS There is no concept of module loading itself , With the development of the front end , Various loading technologies have also been developed , In the early seajs,requirejs, current webpack,Node.js wait , The background of module loader is the modularization of code , Because we can't write all the code to the same file , Therefore, the module loader mainly solves the problem of loading other modules in the module , Not just the front-end language ,c Language 、python、php The same is true .No.js The reference is Node.js The implementation of the . For example, we have the following two modules .
module1.js
const func = require("module2");
func();
module2.js
module.exports = () => {
// some code
}
Let's see how to implement the function of module loading . First, let's look at the runtime execution time , How to load the first module .No.js During initialization, it will pass V8 perform No.js file .
const {
loader,
process,
} = No;
function loaderNativeModule() {
// Native JS Module list
const modules = [
{
module: 'libs/module/index.js',
name: 'module'
},
];
No.libs = {
};
// initialization
for (let i = 0; i < modules.length; i++) {
const module = {
exports: {
},
};
loader.compile(modules[i].module).call(null, loader.compile, module.exports, module);
No.libs[modules[i].name] = module.exports;
}
}
function runMain() {
No.libs.module.load(process.argv[1]);
}
loaderNativeModule();
runMain();
No.js The logic of the file is mainly two , Load native JS Module and execution user JS. First, let's look at how to load native JS modular , Module loading is through loader.compile Realized ,loader.compile yes V8 Encapsulation of functions .
void No::Loader::Compile(V8_ARGS) {
V8_ISOLATE
V8_CONTEXT
String::Utf8Value filename(isolate, args[0].As<String>());
int fd = open(*filename, 0 , O_RDONLY);
std::string content;
char buffer[4096];
while (1)
{
memset(buffer, 0, 4096);
int ret = read(fd, buffer, 4096);
if (ret == -1) {
return args.GetReturnValue().Set(newStringToLcal(isolate, "read file error"));
}
if (ret == 0) {
break;
}
content.append(buffer, ret);
}
close(fd);
ScriptCompiler::Source script_source(newStringToLcal(isolate, content.c_str()));
Local<String> params[] = {
newStringToLcal(isolate, "require"),
newStringToLcal(isolate, "exports"),
newStringToLcal(isolate, "module"),
};
MaybeLocal<Function> fun =
ScriptCompiler::CompileFunctionInContext(context, &script_source, 3, params, 0, nullptr);
if (fun.IsEmpty()) {
args.GetReturnValue().Set(Undefined(isolate));
} else {
args.GetReturnValue().Set(fun.ToLocalChecked());
}
}
Compile First read the contents of the module , And then call CompileFunctionInContext function .CompileFunctionInContext The principle of the function is as follows . Suppose the file content is 1 + 1. After executing the following code
const ret = CompileFunctionInContext("1+1", ["require", "exports", "module"])
ret become
function (require, exports, module) {
1 + 1;
}
therefore CompileFunctionInContext The function of is to encapsulate the code into a function , And you can set the formal parameter list of the function . Back to the original JS Loading process of .
for (let i = 0; i < modules.length; i++) {
const module = {
exports: {
},
};
loader.compile(modules[i].module).call(null, loader.compile, module.exports, module);
No.libs[modules[i].name] = module.exports;
}
First, through loader.compile And module content to get a function , Then pass in parameters to execute the function . Let's look at the original JS The code of the module .
class Module {
// ...
};
module.exports = Module;
Finally, a Module Function and record to the global variable No in . The native module is loaded , Then execute the user JS.
function runMain() {
No.libs.module.load(process.argv[1]);
}
Let's see. No.libs.module.load.
static load(filename, ...args) {
if (map[filename]) {
return map[filename];
}
const module = new Module(filename, ...args);
return (map[filename] = module.load());
}
Create a new one Module object , And then execute his load function .
load() {
const result = loader.compile(this.filename);
result.call(this, Module.load, this.exports, this);
return this.exports;
}
load Function final call loader.compile You get a function , Finally, pass in three parameters to execute the function , You can go through module.exports Get the export content of the module . And we see that as well , In the module require、module and exports Where did it come from , What is the content .
Postscript : Let's just briefly introduce this , For those interested, please refer to https://github.com/theanarkh/js_runtime_loader, Another branch implements a Function Version of .
边栏推荐
- R language uses LM function to build multiple linear regression model and build regression model without intercept term (the model does not contain intercept)
- Percona XtraDB Cluster安装
- 一文教你检测MOS管好坏的五大诀窍「建议收藏」
- 深度学习1感知机及实现简单反向传播网络
- 科研总结/编程常见问题
- No.js---基于V8和io_uring的JS运行时
- PMP candidates note that the examinations in these regions will be postponed
- Map与List的遍历速度比较
- 智能运维场景解析:如何通过异常检测发现业务系统状态异常
- jmter---数据库性能测试
猜你喜欢
随机推荐
No.js---基于V8和io_uring的JS运行时
Explain pytorch visualization tool visdom in detail (I)
Install kibana on win
数学建模之MATLAB画图汇总
活动报名:如何零基础快速上手开源的 Tapdata Live Data Platform?
WebSockets and server sent events
Linux(Centos)安装Mysql
架构师进阶,微服务设计与治理的 16 条常用原则
Mysql REGEXP不区分大小写解决办法
Differences among mdladdress, userbuffer and systembuffer of IRP structure
DistSQL 深度解析:打造动态化的分布式数据库
读取写入文件内容
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过
How to change the console font to console?
服务器中激活刚安装好的anaconda
计算2支股票的M天运动平均价格
企业统一社会信用代码规则
Chiitoitsu
Generating function (linear recursive relationship, generating function concept and formula derivation, violent calculation) 4000 word detailed analysis, with examples
Fast Fourier transform, Lagrange interpolation, three thousand words with examples, sister chapters, application of FFT and string matching