Skip to content

Instantly share code, notes, and snippets.

@davehorton
Created June 27, 2012 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davehorton/3001626 to your computer and use it in GitHub Desktop.
Save davehorton/3001626 to your computer and use it in GitHub Desktop.
add-in module with wrapped C++ class
new-host-5:sipua dhorton$ node test.js
27 Jun 01:09:42 - [object SipUserAgent]
{}
sip port is: undefined
#include <node.h>
#include <v8.h>
#include "sipua.h"
using namespace v8;
namespace {
Handle<Value> GetPort(Local<String> property, const AccessorInfo& info) {
Local<Object>self= info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
int value = static_cast<SipUa*>(ptr)->getPort();
return Integer::New(value);
}
} ;
Handle<Value>CreateUserAgent(const Arguments& args){
fprintf(stderr, "in createus\n") ;
HandleScope scope;
Handle<ObjectTemplate> uaTemplate = ObjectTemplate::New() ;
uaTemplate->SetInternalFieldCount(1) ;
uaTemplate->SetAccessor(String::New("port"), GetPort );
SipUa* p = new SipUa() ;
Handle<Object> result = uaTemplate->NewInstance();
Handle<External> sua_ptr = External::New( p );
result->SetInternalField(0, sua_ptr);
return scope.Close(result);
}
void init(Handle<Object> target){
target->Set(String::NewSymbol("createUserAgent"), FunctionTemplate::New(CreateUserAgent)->GetFunction());
}
NODE_MODULE(sip, init)
#ifndef __SIP_UA_H__
#define __SIP_UA_H__
class SipUa {
public:
SipUa() : port_(5060) {}
SipUa( unsigned int port ) : port_(port) {};
~SipUa() {};
unsigned int getPort()const{ return port_; }
void setPort( unsigned int port ){ port_ = port ; }
protected:
unsigned int port_ ;
} ;
#endif
var sip = require('./build/Release/sip');
var util = require('util') ;
var uas = sip.createUserAgent() ;
util.log( uas ) ;
console.log( uas ) ;
console.log('sip port is: ' + uas.port ) ;
@davehorton
Copy link
Author

should return value of 5060 for port, returns undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment