Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7208838
  • 博文数量: 510
  • 博客积分: 12019
  • 博客等级: 上将
  • 技术积分: 6836
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-01 16:46
文章分类

全部博文(510)

文章存档

2022年(2)

2021年(6)

2020年(59)

2019年(4)

2018年(10)

2017年(5)

2016年(2)

2015年(4)

2014年(4)

2013年(16)

2012年(47)

2011年(65)

2010年(46)

2009年(34)

2008年(52)

2007年(52)

2006年(80)

2005年(22)

分类: Python/Ruby

2012-09-07 11:11:00

参考
目的通过c++扩展新模块。

例子代码  wget
          解压后在./node-v0.8.8/test/addons/hello-world

一、编写模块:

点击(此处)折叠或打开

  1. #include <node.h>
  2. #include <v8.h>

  3. using namespace v8;

  4. Handle<Value> Method(const Arguments& args) {
  5.   HandleScope scope;
  6.   return scope.Close(String::New("world"));
  7. }

  8. void init(Handle<Object> target) {
  9.   NODE_SET_METHOD(target, "hello", Method);
  10. }

  11. NODE_MODULE(binding, init);
二、编写 binding.gyp

三、编译过程
采用node-gyp组织项目。
1、安装 node-gyp
npm install -g node-gyp

2、编译过程
a、进入目录   cd   ./node-v0.8.8/test/addons/hello-world   (node-v0.8.8源码自带列子) b、node-gyp configure
c、node-gyp build   (默认Debug)
       注:node-gyp build -r(编译release)
四、使用库

点击(此处)折叠或打开

  1. var assert = require('assert');
  2. var binding = require('./build/Release/binding');
  3. assert.equal('world', binding.hello());
  4. console.log('binding.hello() =', binding.hello());
  5. ~


五、配置文件 参考

{
  'targets': [
    {
      'target_name': 'raffle',
      'sources': [ 'raffle.cc','keyvalue.pb.cc' ],
      'include_dirs': ['/usr/local/include/google/protobuf/'],
      'libraries':['-lprotobuf','-lpthread']
  
    }
  ]
}

六、复杂些的列子

点击(此处)折叠或打开

  1. //protoc --cpp_out=. ./keyvalue.proto ;node-gyp clean configure ;vi ./build/raffle.target.mk -lprotobuf -lpthread ;node-gyp build -r
  2. #include <node.h>

  3. #include <node_buffer.h>
  4. #include <v8.h>


  5. using namespace node;
  6. using namespace v8;
  7. using namespace std;

  8. std::string ObjectToString(Local<Value> value) {
  9.   String::Utf8Value utf8_value(value);
  10.   return string(*utf8_value);
  11. }



  12. Handle<Value> testF(const Arguments& args) {
  13.     HandleScope scope;
  14.   if(args.Length() < 6) {
  15.       ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
  16.        return scope.Close(Undefined());
  17.   }
  18.   if ( !args[0]->IsArray()     || !args[1]->IsArray()
  19.            || !args[2]->IsNumber()     ){
  20.                
  21.       ThrowException(Exception::TypeError(String::New("Wrong arguments")));
  22.       return scope.Close(Undefined());     
  23.   }    
  24.     
  25.     vector<int> tiletypeV;
  26.     Local<Object> ArryTiletype = args[0]->ToObject();
  27.     int len = ArryTiletype->Get(v8::String::New("length"))->ToObject()->Uint32Value();
  28.     for(int i = 0; i < len; i++){
  29.      Local<Value> element = ArryTiletype->Get(i);
  30.          tiletypeV.push_back(element->Int32Value());
  31.     }
  32.     

  33.     
  34.     int num = args[2]->ToNumber()->Int32Value();
  35.     
  36.     
  37.     vector<int> playerhandsV;
  38.     playerhandsV.push_back(1)
  39.     playerhandsV.push_back(2);

  40.     Local<Object> ArrayPlayerhands = args[1]->ToObject();
  41.     len = playerhandsV.size();    
  42.     for( int i = 0; i < len; i++ ){
  43.         ArrayPlayerhands->Set(i, v8::Integer::New(playerhandsV[i]));
  44.     }


  45.     return scope.Close(Undefined());
  46.   
  47.  }
  48.  
  49. void init(Handle<Object> target) {
  50.     NODE_SET_METHOD(target, "test", testF);
  51. }

  52. NODE_MODULE(MJ2AI, init);




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