(只是简单的分析一下nodejs的代码,看看大概的东西)
node_main.cc:只是一个入口而已
int main(int argc, char *argv[]) { return node::Start(argc, argv); }
node.cc:
int Start(int argc, char *argv[]) { // This needs to run *before* V8::Initialize() // Use copy here as to not modify the original argv: Init(argc, argv_copy); V8::Initialize(); { // Create all the objects, load modules, do everything. // so your next reading stop should be node::Load()! Load(process_l); // All our arguments are loaded. We've evaluated all of the scripts. We // might even have created TCP servers. Now we enter the main eventloop. If // there are no watchers on the loop (except for the ones that were // uv_unref'd) then this function exits. As long as there are active // watchers, it blocks. uv_run(uv_default_loop(), UV_RUN_DEFAULT); EmitExit(process_l); RunAtExit(); } return 0; }
Init:方法处理跟输入参数相关的东西,另外就是得到了一个初始时间。
Load:所有的脚本都是在这个时候加载的。
uv_run(uv_default_loop(), UV_RUN_DEFAULT):这个应该是nodejs消息循环的东西,但是它直接用的libuv的东西。
void Load(Handle<Object> process_l) { // Compile, execute the src/node.js file. (Which was included as static C // string in node_natives.h. 'natve_node' is the string containing that // source code.) // The node.js file returns a function 'f' atexit(AtExit); Local<Value> f_value = ExecuteString(MainSource(), IMMUTABLE_STRING("node.js")); assert(f_value->IsFunction()); Local<Function> f = Local<Function>::Cast(f_value); // Now we call 'f' with the 'process' variable that we've built up with // all our bindings. Inside node.js we'll take care of assigning things to // their places. // We start the process this way in order to be more modular. Developers // who do not like how 'src/node.js' setups the module system but do like // Node's I/O bindings may want to replace 'f' with their own function. // Add a reference to the global object Local<Object> global = v8::Context::GetCurrent()->Global(); Local<Value> args[1] = { Local<Value>::New(process_l) }; f->Call(global, 1, args); }
从这里可以看出,代码转到node.js里面,这也难怪此开源项目的名字要叫node.js了,C部分的代码实际就起到加载和运行的作用。而真正的内容在js部分,而node.js就是js部分的根。
总结:本质上来讲node.js相当于只是把libev和v8组合在一起,然后打造了一个js的服务器环境,它自身只是提供了有限的功能。例如,v8对buffer的支持不够好,然后它自己对这块进行加强了一下。