Created
June 27, 2012 05:09
-
-
Save davehorton/3001626 to your computer and use it in GitHub Desktop.
add-in module with wrapped C++ class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new-host-5:sipua dhorton$ node test.js | |
27 Jun 01:09:42 - [object SipUserAgent] | |
{} | |
sip port is: undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should return value of 5060 for port, returns undefined