2009-07-19 72 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

相關問題