Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7193961
  • 博文数量: 510
  • 博客积分: 12019
  • 博客等级: 上将
  • 技术积分: 6836
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-01 16:46
文章分类

全部博文(510)

文章存档

2022年(2)

2021年(6)

2020年(59)

2019年(4)

2018年(10)

2017年(5)

2016年(2)

2015年(4)

2014年(4)

2013年(16)

2012年(47)

2011年(65)

2010年(46)

2009年(34)

2008年(52)

2007年(52)

2006年(80)

2005年(22)

分类: Python/Ruby

2012-08-30 17:25:57

node.js源码研究—模块组织加载

转载于


粗略研究了一下node.js源码,它有8000行C++代码,2000行javascript代码,来看看js和C++间是怎么组织连接起来,以及各个模块是怎样互相调用的。

本文使用的node.js版本是0.4.8,可以在这里看到源码。
js2c.py
node.js使用了V8附带的js2c.py工具把所有内置的js代码转换成C++里的数组,生成node_natives.h直接include到程序中,成了C++源码的一部分。这样做能提高内置js模块的编译效率。

node.js里内置的javascript包括了主程序和模块程序,通过js2c.py让每一个js文件都生成一个源码数组,存放在build/src/node_natives.h里,node_natives.h在node.js编译后才会生成(编译的脚本wscript中调用了js2c.py),可以看到大致的结构如下:


namespace node {
const char node_native[] = {47, 47, 32, 67, 112 ......}
const char console_native[] = {47, 47, 32, 67, 112 ......}
const char buffer_native[] = {47, 47, 32, 67, 112 ......}
.....
}
struct _native { const char* name; const char* source; size_t source_len;};
static const struct _native natives[] = {
{ "node", node_native, sizeof(node_native)-1 },
{ "dgram", dgram_native, sizeof(dgram_native)-1 },
{ "console", console_native, sizeof(console_native)-1 },
{ "buffer", buffer_native, sizeof(buffer_native)-1 },
....
}

这个文件被包含在里,node_javascript.cc提供了两个接口:
MainSource() 处理node_native源码返回v8::Handle类型的数据可供编译。
DefineJavaScript(target) 把其他所有模块源码变成v8::Handle类型后加载到传入的target对象上。

所有的js模块都被转换成了C数组,接下来看看它们怎么执行和互相调用。

执行js主程序/传递process
先看看node.js的底层C++传递给javascript的一个变量process,在一开始运行node.js时,程序会先配置好process

Handleprocess = SetupProcessObject(argc, argv);

然后把process作为参数去调用js主程序src/node.js返回的函数,这样process就传递到javascript里了。

//node.cc
//通过MainSource()获取已转化的src/node.js源码,并执行它
Local f_value = ExecuteString(MainSource(), IMMUTABLE_STRING("node.js"));

//执行src/node.js后获得的是一个函数,从node.js源码可以看出:
//node.js
//(function(process) {
// global = this;
// ....
//})
Local f = Local::Cast(f_value);



//创建函数执行环境,调用函数,把process传入
Localglobal = v8::Context::GetCurrent()->Global();
Local args[1] = { Local::New(process) };
f->Call(global, 1, args);

C++模块
node.js的模块除了lib/*.js里用js语言编写的以外,还有一些使用C++编写,像os/stdio/crypto/buffer等。这些模块都通过提供的NODE_MODULE方法存储在变量_module里。提供了get_builtin_module(name)接口获取这些模块。
process.binding/C++模块加载
process提供的一个获取模块的接口是binding,它的实现Binding()函数可以在找到。

Persistent binding_cache;
static Handle Binding(const Arguments& args) {
HandleScope scope;
Local module = args[0]->ToString();
String::Utf8Value module_v(module);
node_module_struct* modp;

if (binding_cache.IsEmpty()) {
binding_cache = Persistent::New(Object::New());
}
Local exports;
if (binding_cache->Has(module)) {
exports = binding_cache->Get(module)->ToObject();

} else if ((modp = get_builtin_module(*module_v)) != NULL) {
exports = Object::New();
modp->register_func(exports);
binding_cache->Set(module, exports);

} else if (!strcmp(*module_v, "constants")) {
exports = Object::New();
DefineConstants(exports);
binding_cache->Set(module, exports);

#ifdef __POSIX__
} else if (!strcmp(*module_v, "io_watcher")) {
exports = Object::New();
IOWatcher::Initialize(exports);
binding_cache->Set(module, exports);
#endif

} else if (!strcmp(*module_v, "natives")) {
exports = Object::New();
DefineJavaScript(exports);
binding_cache->Set(module, exports);



} else {
return ThrowException(Exception::Error(String::New("No such module")));
}
return scope.Close(exports);
}

从源码可以看到,调用process.binding时,先看缓存里是否已经存在此模块,不存在再调用get_builtin_module查找C++内置模块,找到的话获取后绑定在exports上,在最后返回exports。

此外还有针对其他模块的特殊处理,其中natives模块就是调用上文提到的DefineJavaScript(exports)接口获取到所有内置的js模块绑定在exports上。

现在在js上需要调用C++提供的模块只需要调用process.binding就行了,例如

var stdio = process.binding("stdio")

js模块加载
上 实现了一个NativeModule对象用于管理js模块,它通过调用process.binding("natives")把所有内置的js模块放在 NativeModule._source上,并提供require接口供调用。在require里会给代码加一层包装,把一些变量传给这个模块。

NativeModule.wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'\n});'
];

再用process提供的其中一个js编译接口process.runInThisContext执行代码。

var fn = runInThisContext(source, this.filename, true);
fn(this.exports, NativeModule.require, this, this.filename);

于是在主程序src/node.js上可以调用NativeModule.require("net")去加载net模块,在lib/*.js的各个js模块里能通过调用传进来的require()去加载其他内置js模块。
总结流程
粗略总结一下加载模块的流程:

加载C++模块(以stdio为例):
process.binding("stdio") -> get_builtin_module("stdio") -> _module -> NODE_MODULE(node_stdio, node::Stdio::Initialize)(定义)

加载js模块(以net为例)
require("net") -> NativeModule.require("net") -> process.binding("natives")["net"] -> DefineJavaScript() -> natives[] -> node_natives.h
标签:
阅读(3170) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~