2009-07-19 76 views

回答

22

您必须手动添加此功能,这里是我是如何做的:

Handle<Value> Include(const Arguments& args) { 
    for (int i = 0; i < args.Length(); i++) { 
     String::Utf8Value str(args[i]); 

     // load_file loads the file with this name into a string, 
     // I imagine you can write a function to do this :) 
     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文件。我把它和我的项目一起使用,就像做梦一样。

// beginning of main javascript file 
include("otherlib.js"); 
+0

非常感谢:) – 2009-08-23 07:06:38

+0

看起来像V8在此答案发布之后已更新。您现在在调用“设置”时需要Functiontemplate的 - > GetFunction()访​​问器:例如FunctionTemplate :: New(Include) - > GetFunction() – 2013-05-28 19:09:26

相关问题