SHARE
TWEET

Untitled

a guest May 31st, 2014 167 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <node.h>
  2. #include "myobject.h"
  3.  
  4. v8::Persistent<v8::Function> MyObject::constructor;
  5.  
  6. MyObject::MyObject()
  7. {
  8. }
  9.  
  10. MyObject::~MyObject()
  11. {
  12. }
  13.  
  14. void MyObject::Init(v8::Handle<v8::Object> exports)
  15. {
  16.     // Prepare constructor template
  17.     v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(New);
  18.     tpl->SetClassName(v8::String::NewSymbol(OBJECT_NAME));
  19.     tpl->InstanceTemplate()->SetInternalFieldCount(1);
  20.     // Prototype
  21.     tpl->PrototypeTemplate()->Set(v8::String::NewSymbol("start_lib"), v8::FunctionTemplate::New(Start_Lib)->GetFunction());
  22.     tpl->PrototypeTemplate()->Set(v8::String::NewSymbol("stop_lib"), v8::FunctionTemplate::New(Stop_Lib)->GetFunction());
  23.     constructor = v8::Persistent<v8::Function>::New(tpl->GetFunction());
  24.     exports->Set(v8::String::NewSymbol(OBJECT_NAME), constructor);
  25. }
  26.  
  27. v8::Handle<v8::Value> MyObject::New(const v8::Arguments& args) {
  28.     v8::HandleScope scope;
  29.     if (args.IsConstructCall())
  30.     {
  31.         // Invoked as constructor: `new MyObject(...)`
  32.         MyObject* obj = new MyObject();
  33.         obj->Wrap(args.This());
  34.         return scope.Close(args.This());
  35.     }
  36.     else
  37.     {
  38.         // Invoked as plain function `MyObject(...)`, turn into construct call.
  39.         const int argc = 1;
  40.         v8::Local<v8::Value> argv[argc] = { args[0] };
  41.         return scope.Close(constructor->NewInstance(argc, argv));
  42.     }
  43. }
  44.  
  45. v8::Handle<v8::Value> MyObject::Stop_Lib(const v8::Arguments& args)
  46. {
  47.     v8::HandleScope scope;
  48.     MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
  49.         obj->lib.Stop_Lib();
  50.     return scope.Close(v8::Undefined());
  51. }
  52.  
  53. v8::Handle<v8::Value> MyObject::Start_Lib(const v8::Arguments& args)
  54. {
  55.     v8::HandleScope scope;
  56.     MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
  57.         if (args.Length() < 1)
  58.         {
  59.                 v8::ThrowException(v8::Exception::TypeError(v8::String::New("Wrong number of arguments")));
  60.                 return scope.Close(v8::Undefined());
  61.         }
  62.  
  63.         if (args[0]->IsNumber())
  64.         {
  65.                 v8::ThrowException(v8::Exception::TypeError(v8::String::New("Wrong argument type")));
  66.                 return scope.Close(v8::Undefined());
  67.         }
  68.         std::string param1(*v8::String::Utf8Value(args[0]->ToString()));
  69.         obj->lib.Start_Lib(param1.c_str());
  70.     return scope.Close(v8::Undefined());
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top