Chinaunix首页 | 论坛 | 博客
  • 博客访问: 149349
  • 博文数量: 31
  • 博客积分: 1455
  • 博客等级: 上尉
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-09 02:20
文章存档

2012年(2)

2011年(18)

2010年(11)

分类: C/C++

2011-11-09 15:20:14

首先,要注册一个叫做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");



编译自:

阅读(1196) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~