文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 35 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 388498 人次
  • RSS订阅: 文章|评论

在C++程序中嵌入V8——Google浏览器的Javascript虚拟机(2)

Admin 于 2008-09-07 08:19:03 发表C/C++

订阅: http://www.kaiyuan8.org/Feed/Article_25.aspx
引用: 点这里获取地址 (UTF-8)
在C++程序中嵌入V8——Google浏览器的Javascript虚拟机(1) < 在C++程序中嵌入V8——Google浏览器的Javascript虚拟机(2) > 阿拉伯数字转换成中文大写金额(包括小数)(C#)[1]

介绍如何在脚本中控制程序的变量与函数。

1、创建一个全局template:

  1. v8::Handle global = v8::ObjectTemplate::New(); 

全局模板对象提供一个脚本执行的环境,各个全局模板对象之间互不影响,脚本在各自的全局模板中运行。

2、自定义函数”Plus“

  1. // plus function implementation - Add two numbers 
  2. v8::Handle Plus(const v8::Arguments& args) 
  3. {  
  4.     unsigned int A = args[0]->Uint32Value(); 
  5.     unsigned int B = args[1]->Uint32Value(); 
  6.     return v8_uint32(A +  B); 
  7. }  
  8. //... 
  9. //associates "plus" on script to the Plus function 
  10. global->Set(v8::String::New("plus"), v8::FunctionTemplate::New(Plus)); 

自定义函数以const v8::Arguments& args为参数,以v8::Handle为返回值。然后通过前面定义的全局模板将“plus”与函数“Plus”关联,这样每次在脚本中调用plus函数相应执行的是程序中的”Plus“函数。

使用方法:

  1. plus(120,44); 

3、变量访问子--暴露C++程序中的变量到脚本代码中

通过变量访问子实现类似C#中的属性功能。

  1. //the x variable! 
  2. int x; 
  3.  
  4. //get the value of x variable inside javascript 
  5. static v8::Handle XGetter(v8::Local name, 
  6.    const v8::AccessorInfo& info) { 
  7.   return  v8::Number::New(x); 
  8.  
  9. //set the value of x variable inside javascript 
  10. static void XSetter(v8::Local name,v8::Local value, 
  11.     const v8::AccessorInfo& info) { 
  12.   x = value->Int32Value(); 

 注册此变量

  1. global->SetAccessor(v8::String::New("x"), XGetter, XSetter); 

 4、自定义变量”User“

  1. //the username accessible on c++ and inside the script 
  2. char username[1024]; 
  3.  
  4. //get the value of username variable inside javascript 
  5. v8::Handle userGetter(v8::Local name, 
  6.                 const v8::AccessorInfo& info) { 
  7.     return v8::String::New((char*)&username,strlen((char*)&username)); 
  8.  
  9. //set the value of username variable inside javascript 
  10. void userSetter(v8::Local name, v8::Local value, 
  11.     const v8::AccessorInfo& info) { 
  12.     v8::Local s = value->ToString(); 
  13.     s->WriteAscii((char*)&username); 

 注册此变量

  1. //create accessor for string username 
  2. global->SetAccessor(v8::String::New("user"),userGetter,userSetter); 

 5、自定义函数”Print“

  1. // The callback that is invoked by v8 whenever the JavaScript 'print' 
  2. // function is called.  Prints its arguments on stdout separated by 
  3. // spaces and ending with a newline. 
  4. v8::Handle Print(const v8::Arguments& args) { 
  5.  bool first = true
  6.  for (int i = 0; i < args.Length(); i++) 
  7.  { 
  8.    v8::HandleScope handle_scope; 
  9.    if (first) 
  10.    { 
  11.      first = false
  12.    } 
  13.    else 
  14.    { 
  15.      printf(" "); 
  16.    } 
  17.    //convert the args[i] type to normal char* string 
  18.    v8::String::AsciiValue str(args[i]); 
  19.    printf("%s", *str); 
  20.  } 
  21.  printf("\n"); 
  22.  //returning Undefined is the same as returning void... 
  23.  return v8::Undefined(); 
  24.    }  

注册此函数:

  1. //associates "print" on script to the Print function 
  2. global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); 

在脚本中使用刚才定义的变量与函数

  1. print("begin script"); 
  2. print("script executed by " + user); 
  3. if ( user == "John Doe"){ 
  4.        print("\tuser name is invalid. Changing name to Chuck Norris"); 
  5.        user = "Chuck Norris"
  6.    } 
  7. print("123 plus 27 = " + plus(123,27)); 
  8. x = plus(3456789,6543211); 
  9. print("end script"); 

我以前一直使用Lua做为嵌入脚本,v8的出现无疑又多了一个很好的选择。这里有更多的文档可以参考。

被阅1303次, 0投一票Chrome Javascript

J.L

2010-01-04 17:03:08
不错,看了。,。,。
  • 看完了要说点啥么?
  • 昵称 (不填说不了话)
  • 信箱地址 (不会被公开,但是不填也说不了话)
  • 网址 (这个不填也成)
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号