v8插件编译使用

来源:互联网 发布:sql修改字段绑定默认值 编辑:程序博客网 时间:2024/05/16 11:14
参考https://github.com/TooTallNate/node-gyp
目的通过c++扩展新模块。

例子代码  wget http://nodejs.org/dist/v0.8.8/node-v0.8.8.tar.gz

          解压后在./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-gyp2、编译过程a、进入目录   cd   ./node-v0.8.8/test/addons/hello-world   (node-v0.8.8源码自带列子) b、node-gyp configurec、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);

0 0
原创粉丝点击