首先,要注册一个叫做include的函数,代码如下
Handle<Value> Include(const Arguments& args) {
for (int i = 0; i < args.Length(); i++) {
String::Utf8Value str(args[i]);
// 这里要实现一个load_file函数,读入文件并保存成字符串
std::string js_file = load_file(*str);
if(js_file.length() > 0) {
Handle<String> source = String::New(js_file.c_str());
Handle<Script> script = Script::Compile(source);
return script->Run();
}
}
return Undefined();
}
Handle<ObjectTemplate> global = ObjectTemplate::New();
global->Set(String::New("include"), FunctionTemplate::New(Include));
然后,在javascript里,就可以这样写了。用作者的话,works like a dream!
// beginning of main javascript file
include("otherlib.js");
编译自:
阅读(1303) | 评论(0) | 转发(0) |