From 7a94b6935d4c2b67396acd9038afca6f9560f8c5 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Mon, 20 Apr 2015 13:52:51 -0400 Subject: [PATCH] fix(osx): fix build on OSX OSX uses clang by default, and there were a number of changes in order to get things building there. As an added bonus, I've cleaned up a bunch of things given that clang is somewhat stricter out of the box. In particular these changes are included: - switch to using constructor functions rather than constructor templates where appropriate - a few bug fixes with signed vs unsigned integer comparison - switched event support to use an EventEmitter implementation rather than callbacks --- binding.gyp | 5 + lib/index.js | 35 ++- src/domain.cc | 525 ++++++++++++++++-------------------------- src/domain.h | 17 +- src/error.cc | 311 ++++++++++++------------- src/error.h | 6 +- src/event_impl.h | 2 + src/hypervisor.cc | 53 ++--- src/interface.cc | 20 +- src/interface.h | 2 +- src/libvirt_handle.cc | 3 +- src/libvirt_handle.h | 9 +- src/network.cc | 14 +- src/network.h | 2 +- src/network_filter.cc | 14 +- src/network_filter.h | 2 +- src/node_device.cc | 14 +- src/node_device.h | 2 +- src/node_libvirt.cc | 2 +- src/node_libvirt.h | 4 - src/secret.cc | 14 +- src/secret.h | 2 +- src/storage_pool.cc | 28 +-- src/storage_pool.h | 1 + src/storage_volume.cc | 18 +- src/storage_volume.h | 1 + src/worker.h | 4 +- 27 files changed, 495 insertions(+), 615 deletions(-) diff --git a/binding.gyp b/binding.gyp index 420349f..59ca773 100644 --- a/binding.gyp +++ b/binding.gyp @@ -33,6 +33,11 @@ '-std=c++11', ' Domain::constructor_template; +Persistent Domain::constructor; void Domain::Initialize(Handle exports) { - Local t = FunctionTemplate::New(); + Local t = NanNew(); + t->SetClassName(NanNew("Domain")); t->InstanceTemplate()->SetInternalFieldCount(1); // ACTIONS @@ -76,124 +78,130 @@ void Domain::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "lookupSnapshotByName", LookupSnapshotByName); NODE_SET_PROTOTYPE_METHOD(t, "getSnapshots", GetSnapshots); + // Events + NODE_SET_PROTOTYPE_METHOD(t, "registerEvent", RegisterEvent); + NODE_SET_PROTOTYPE_METHOD(t, "unregisterEvent", UnregisterEvent); + NanAssignPersistent(constructor_template, t); - constructor_template->SetClassName(NanNew("Domain")); + NanAssignPersistent(constructor, t->GetFunction()); exports->Set(NanNew("Domain"), t->GetFunction()); - Local object_tmpl = t->InstanceTemplate(); - //Constants initialization //virDomainState - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_NOSTATE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_RUNNING); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_BLOCKED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_PAUSED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_SHUTDOWN); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_SHUTOFF); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_CRASHED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_NOSTATE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_RUNNING); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_BLOCKED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_PAUSED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_SHUTDOWN); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_SHUTOFF); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_CRASHED); #ifdef VIR_DOMAIN_PMSUSPENDED // If its available in libvirt.h, then make it available in node - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_PMSUSPENDED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_PMSUSPENDED); #endif //virDomainDeviceModifyFlags - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_DEVICE_MODIFY_CURRENT); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_DEVICE_MODIFY_LIVE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_DEVICE_MODIFY_CONFIG); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_DEVICE_MODIFY_CURRENT); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_DEVICE_MODIFY_LIVE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_DEVICE_MODIFY_CONFIG); //virDomainMigrateFlags - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MIGRATE_LIVE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MIGRATE_PEER2PEER); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MIGRATE_TUNNELLED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MIGRATE_PERSIST_DEST); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MIGRATE_UNDEFINE_SOURCE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MIGRATE_PAUSED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MIGRATE_NON_SHARED_DISK); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MIGRATE_NON_SHARED_INC); + NODE_DEFINE_CONSTANT(exports, VIR_MIGRATE_LIVE); + NODE_DEFINE_CONSTANT(exports, VIR_MIGRATE_PEER2PEER); + NODE_DEFINE_CONSTANT(exports, VIR_MIGRATE_TUNNELLED); + NODE_DEFINE_CONSTANT(exports, VIR_MIGRATE_PERSIST_DEST); + NODE_DEFINE_CONSTANT(exports, VIR_MIGRATE_UNDEFINE_SOURCE); + NODE_DEFINE_CONSTANT(exports, VIR_MIGRATE_PAUSED); + NODE_DEFINE_CONSTANT(exports, VIR_MIGRATE_NON_SHARED_DISK); + NODE_DEFINE_CONSTANT(exports, VIR_MIGRATE_NON_SHARED_INC); //virDomainXMLFlags - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_XML_SECURE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_XML_INACTIVE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_XML_UPDATE_CPU); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_XML_SECURE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_XML_INACTIVE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_XML_UPDATE_CPU); //virDomainJobType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_JOB_NONE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_JOB_BOUNDED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_JOB_UNBOUNDED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_JOB_COMPLETED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_JOB_FAILED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_JOB_CANCELLED); - - //virDomainMemoryFlags - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MEMORY_VIRTUAL); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_MEMORY_PHYSICAL); - - //virDomainEventType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_DEFINED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_UNDEFINED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STARTED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_SUSPENDED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_RESUMED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STOPPED); - - //virDomainEventIOErrorAction - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_IO_ERROR_NONE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_IO_ERROR_PAUSE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_IO_ERROR_REPORT); - - //virDomainEventResumedDetailType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_RESUMED_UNPAUSED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_RESUMED_MIGRATED); - - //virDomainEventStartedDetailType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STARTED_BOOTED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STARTED_MIGRATED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STARTED_RESTORED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT); - - //virDomainEventStoppedDetailType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STOPPED_DESTROYED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STOPPED_CRASHED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STOPPED_MIGRATED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STOPPED_SAVED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STOPPED_FAILED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT); - - //virDomainEventSuspendedDetailType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_SUSPENDED_PAUSED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_SUSPENDED_IOERROR); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_SUSPENDED_WATCHDOG); - - //virDomainEventUndefinedDetailType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_UNDEFINED_REMOVED); - - //virDomainEventWatchdogAction - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_WATCHDOG_NONE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_WATCHDOG_PAUSE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_WATCHDOG_RESET); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_WATCHDOG_POWEROFF); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_WATCHDOG_SHUTDOWN); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_WATCHDOG_DEBUG); - - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_SEND_KEY_MAX_KEYS); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_JOB_NONE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_JOB_BOUNDED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_JOB_UNBOUNDED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_JOB_COMPLETED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_JOB_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_JOB_CANCELLED); + + // virDomainMemoryFlags + NODE_DEFINE_CONSTANT(exports, VIR_MEMORY_VIRTUAL); + NODE_DEFINE_CONSTANT(exports, VIR_MEMORY_PHYSICAL); + + // virDomainEventType + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_DEFINED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_UNDEFINED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STARTED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_SUSPENDED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_RESUMED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STOPPED); + + // virDomainEventIOErrorAction + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_IO_ERROR_NONE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_IO_ERROR_PAUSE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_IO_ERROR_REPORT); + + // virDomainEventResumedDetailType + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_RESUMED_UNPAUSED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_RESUMED_MIGRATED); + + // virDomainEventStartedDetailType + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STARTED_BOOTED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STARTED_MIGRATED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STARTED_RESTORED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT); + + // virDomainEventStoppedDetailType + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STOPPED_DESTROYED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STOPPED_CRASHED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STOPPED_MIGRATED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STOPPED_SAVED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STOPPED_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT); + + // virDomainEventSuspendedDetailType + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_SUSPENDED_PAUSED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_SUSPENDED_IOERROR); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_SUSPENDED_WATCHDOG); + + // virDomainEventUndefinedDetailType + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_UNDEFINED_REMOVED); + + // virDomainEventWatchdogAction + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_WATCHDOG_NONE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_WATCHDOG_PAUSE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_WATCHDOG_RESET); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_WATCHDOG_POWEROFF); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_WATCHDOG_SHUTDOWN); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_WATCHDOG_DEBUG); + + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_SEND_KEY_MAX_KEYS); + + + // ETC + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_LIFECYCLE); } Local Domain::NewInstance(const LibVirtHandle &handle) { - NanScope(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local object = ctor->NewInstance(); + Domain *domain = new Domain(handle.ToDomain()); - Local object = constructor_template->GetFunction()->NewInstance(); domain->Wrap(object); return NanEscapeScope(object); } Domain::~Domain() { - fprintf(stderr, "\n\nDELETING THE DOMAIN!!!\n\n"); - if (handle_ != NULL) virDomainFree(handle_); handle_ = 0; @@ -802,7 +810,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetInfo) result->Set(NanNew("vcpus"), NanNew(info_.nrVirtCpu)); result->Set(NanNew("cpuTime"), NanNew(info_.cpuTime)); - v8::Local argv[] = { NanNull(), result }; + Local argv[] = { NanNull(), result }; callback->Call(2, argv); } @@ -841,7 +849,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetBlockInfo) result->Set(NanNew("allocation"), NanNew(info_.allocation)); result->Set(NanNew("physical"), NanNew(info_.physical)); - v8::Local argv[] = { NanNull(), result }; + Local argv[] = { NanNull(), result }; callback->Call(2, argv); } @@ -883,7 +891,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetBlockStats) result->Set(NanNew("writeBytes"), NanNew(stats_.wr_bytes)); result->Set(NanNew("errors"), NanNew(stats_.errs)); - v8::Local argv[] = { NanNull(), result }; + Local argv[] = { NanNull(), result }; callback->Call(2, argv); } @@ -941,7 +949,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetSecurityLabel) result->Set(NanNew("label"), NanNew(info_.label)); result->Set(NanNew("enforcing"), NanNew(info_.enforcing)); - v8::Local argv[] = { NanNull(), result }; + Local argv[] = { NanNull(), result }; callback->Call(2, argv); } @@ -991,7 +999,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetInterfaceStats) result->Set(NanNew("rx"), rx); result->Set(NanNew("tx"), tx); - v8::Local argv[] = { NanNull(), result }; + Local argv[] = { NanNull(), result }; callback->Call(2, argv); } @@ -1035,7 +1043,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetJobInfo) result->Set(NanNew("memory"), memory); result->Set(NanNew("file"), file); - v8::Local argv[] = { NanNull(), result }; + Local argv[] = { NanNull(), result }; callback->Call(2, argv); } @@ -1093,7 +1101,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetMemoryStats) } } - v8::Local argv[] = { NanNull(), result }; + Local argv[] = { NanNull(), result }; callback->Call(2, argv); } @@ -1286,7 +1294,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetVcpus) result->Set(NanNew(i), cpu); } - v8::Local argv[] = { NanNull(), result }; + Local argv[] = { NanNull(), result }; callback->Call(2, argv); } @@ -1531,7 +1539,7 @@ NAN_METHOD(Domain::MemoryPeek) Local flags_ = Local::Cast(args[2]); unsigned int length = flags_->Length(); for (unsigned int i = 0; i < length; i++) - flags |= flags_->Get(Integer::New(i))->Int32Value(); + flags |= flags_->Get(NanNew(i))->Int32Value(); Domain *domain = ObjectWrap::Unwrap(args.This()); NanCallback *callback = new NanCallback(args[3].As()); @@ -1832,7 +1840,7 @@ NLV_WORKER_EXECUTE(Domain, GetSnapshots) NLV_WORKER_OKCALLBACK(Domain, GetSnapshots) { NanScope(); - Local snapshots = Array::New(xmls_.size()); + Local snapshots = NanNew(xmls_.size()); int i = 0; for (std::vector::iterator it = xmls_.begin() ; it != xmls_.end(); ++it) @@ -1842,6 +1850,7 @@ NLV_WORKER_OKCALLBACK(Domain, GetSnapshots) callback->Call(2, argv); } + NAN_METHOD(Domain::RegisterEvent) { NanScope(); @@ -1850,78 +1859,65 @@ NAN_METHOD(Domain::RegisterEvent) NanThrowTypeError("You must specify a object and a callback as argument"); NanReturnUndefined(); } + Local arg_obj = args[0]->ToObject(); if (!arg_obj->Has(NanNew("evtype")) || !arg_obj->Get(NanNew("evtype"))->IsInt32()) { NanThrowTypeError("You must specify an valid event type"); NanReturnUndefined(); } - if( !arg_obj->Has(NanNew("callback")) || - !arg_obj->Get(NanNew("callback"))->IsFunction()) { - NanThrowTypeError("You must specify a valid callback function"); - NanReturnUndefined(); - } - - Hypervisor *hypervisor = ObjectWrap::Unwrap(args.This()); - Domain *domain = NULL; - if (arg_obj->Has(NanNew("domain"))) { - Local domain_obj = arg_obj->Get(NanNew("domain"))->ToObject(); - if (!NanHasInstance(Domain::constructor_template, domain_obj)) { - NanThrowTypeError("You must specify a Domain object instance"); - NanReturnUndefined(); - } - domain = ObjectWrap::Unwrap(domain_obj); - } - - int evtype = arg_obj->Get(NanNew("evtype"))->Int32Value(); - Local jscallback = arg_obj->Get(NanNew("callback")); - Persistent opaque = Persistent::New(NanNew()); - opaque->Set(NanNew("hypervisor"), args.This()); - opaque->Set(NanNew("callback"), jscallback); + Domain *domain = ObjectWrap::Unwrap(args.This()); + int eventId = arg_obj->Get(NanNew("evtype"))->Int32Value(); NanCallback *callback = new NanCallback(args[1].As()); - NanAsyncQueueWorker(new RegisterEventWorker(callback, hypervisor->handle_, domain != NULL ? domain->handle_ : NULL, evtype, (void*)*opaque)); + NanAsyncQueueWorker(new RegisterEventWorker(callback, domain->handle_, domain, eventId)); NanReturnUndefined(); } NLV_WORKER_EXECUTE(Domain, RegisterEvent) { virConnectDomainEventGenericCallback callback = NULL; - - switch (evtype_) { + switch (eventId_) { case VIR_DOMAIN_EVENT_ID_LIFECYCLE: - callback = VIR_DOMAIN_EVENT_CALLBACK(domain_event_lifecycle_callback); + callback = VIR_DOMAIN_EVENT_CALLBACK(Domain::domain_event_lifecycle_callback); break; case VIR_DOMAIN_EVENT_ID_REBOOT: - callback = VIR_DOMAIN_EVENT_CALLBACK(domain_event_generic_callback); + callback = VIR_DOMAIN_EVENT_CALLBACK(Domain::domain_event_generic_callback); break; case VIR_DOMAIN_EVENT_ID_RTC_CHANGE: - callback = VIR_DOMAIN_EVENT_CALLBACK(domain_event_rtcchange_callback); + callback = VIR_DOMAIN_EVENT_CALLBACK(Domain::domain_event_rtcchange_callback); break; case VIR_DOMAIN_EVENT_ID_WATCHDOG: - callback = VIR_DOMAIN_EVENT_CALLBACK(domain_event_watchdog_callback); + callback = VIR_DOMAIN_EVENT_CALLBACK(Domain::domain_event_watchdog_callback); break; case VIR_DOMAIN_EVENT_ID_IO_ERROR: - callback = VIR_DOMAIN_EVENT_CALLBACK(domain_event_io_error_callback); + callback = VIR_DOMAIN_EVENT_CALLBACK(Domain::domain_event_io_error_callback); break; case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON: - callback = VIR_DOMAIN_EVENT_CALLBACK(domain_event_io_error_reason_callback); + callback = VIR_DOMAIN_EVENT_CALLBACK(Domain::domain_event_io_error_reason_callback); break; case VIR_DOMAIN_EVENT_ID_GRAPHICS: - callback = VIR_DOMAIN_EVENT_CALLBACK(domain_event_graphics_callback); + callback = VIR_DOMAIN_EVENT_CALLBACK(Domain::domain_event_graphics_callback); break; default: - callback = VIR_DOMAIN_EVENT_CALLBACK(domain_event_generic_callback); + callback = VIR_DOMAIN_EVENT_CALLBACK(Domain::domain_event_generic_callback); break; } - int ret = virConnectDomainEventRegisterAny(Handle().ToConnection(), domain_, evtype_, callback, opaque_, domain_event_free); - if (ret == -1) { + virDomainPtr domain = Handle().ToDomain(); + int result = virConnectDomainEventRegisterAny( + virDomainGetConnect(domain), domain, eventId_, callback, domain_, domain_event_free + ); + + if (result == -1) { SetVirError(virGetLastError()); return; } - data_ = ret; + // @todo: this should be added to a vector so we can free it if the + // object is deleted + + data_ = result; } NAN_METHOD(Domain::UnregisterEvent) @@ -1929,22 +1925,23 @@ NAN_METHOD(Domain::UnregisterEvent) NanScope(); if (args.Length() != 2 || !args[0]->IsInt32() || !args[1]->IsFunction()) { - NanThrowTypeError("You must specify a integer and a callback"); + NanThrowTypeError("You must specify a callback identifier and a callback"); NanReturnUndefined(); } - Hypervisor *hypervisor = ObjectWrap::Unwrap(args.This()); + Domain *domain = ObjectWrap::Unwrap(args.This()); NanCallback *callback = new NanCallback(args[1].As()); - NanAsyncQueueWorker(new UnregisterEventWorker(callback, hypervisor->handle_, args[0]->Int32Value())); + NanAsyncQueueWorker(new UnregisterEventWorker(callback, domain->handle_, args[0]->Int32Value())); NanReturnUndefined(); } NLV_WORKER_EXECUTE(Domain, UnregisterEvent) { - if (virConnectDomainEventDeregisterAny(Handle().ToConnection(), callbackid_) == -1) { + if (virConnectDomainEventDeregisterAny(virDomainGetConnect(Handle().ToDomain()), callbackId_) == -1) { SetVirError(virGetLastError()); return; } + data_ = true; } @@ -1971,24 +1968,24 @@ NAN_METHOD(Domain::SetSchedulerParameters) type = virDomainGetSchedulerType(domain->handle_, &nparams); if (type == NULL) { - ThrowException(Error::New(virGetLastError())); - return NanFalse(); + NanThrowError(Error::New(virGetLastError())); + NanReturnValue(NanFalse()); } free(type); params = (virSchedParameterPtr) malloc(sizeof(*params) * nparams); if (params == NULL) { - LIBVIRT_THROW_EXCEPTION("unable to allocate memory"); - return NanFalse(); + NanThrowError("unable to allocate memory"); + NanReturnValue(NanFalse()); } memset(params, 0, sizeof(*params) * nparams); ret = virDomainGetSchedulerParameters(domain->handle_, params, &nparams); if(ret == -1) { - ThrowException(Error::New(virGetLastError())); + NanThrowError(Error::New(virGetLastError())); free(params); - return NanFalse(); + NanReturnValue(NanFalse()); } for (int i = 0; i < nparams; i++) { @@ -2023,77 +2020,41 @@ NAN_METHOD(Domain::SetSchedulerParameters) ret = virDomainSetSchedulerParameters(domain->handle_, params, nparams); if (ret == -1) { - ThrowException(Error::New(virGetLastError())); + NanThrowError(Error::New(virGetLastError())); free(params); - return NanFalse(); + NanReturnValue(NanFalse()); } free(params); - return NanTrue(); + NanReturnValue(NanTrue()); +} + +void Domain::domain_event_free(void *opaque) +{ + fprintf(stderr, "NOT IMPLEMENTED!"); } int Domain::domain_event_lifecycle_callback(virConnectPtr conn, virDomainPtr dom, int event, int detail, void *opaque) { NanScope(); - Local domain_obj = Domain::NewInstance(dom); - /* - The virDomainPtr object handle passed into the callback upon delivery - of an event is only valid for the duration of execution of the callback - If the callback wishes to keep the domain object after the callback, - it shall take a reference to it, by calling virDomainRef - */ - virDomainRef(dom); - - Local argv[3]; - - Persistent obj = static_cast(opaque); - Local hyp = obj->Get(NanNew("hypervisor"))->ToObject(); - - Local callback = obj->Get(NanNew("callback")).As(); Local data = NanNew(); data->Set(NanNew("evtype"), NanNew(event)); data->Set(NanNew("detail"), NanNew(detail)); - argv[0] = hyp; - argv[1] = domain_obj; //FIXME change with domain->handle_ - argv[2] = data; - - TryCatch try_catch; - callback->Call(hyp, 3, argv); - if (try_catch.HasCaught()) { - FatalException(try_catch); - } + Handle argv[2] = { + NanNew("lifecycleEvent"), + data + }; + ObjectWrap *domain = static_cast(opaque); + NanMakeCallback(NanObjectWrapHandle(domain), "emit", 2, argv); return 0; } int Domain::domain_event_generic_callback(virConnectPtr conn, virDomainPtr dom, void *opaque) { - NanScope(); - - Local domain_obj = Domain::NewInstance(dom); - /* - The virDomainPtr object handle passed into the callback upon delivery - of an event is only valid for the duration of execution of the callback - If the callback wishes to keep the domain object after the callback, - it shall take a reference to it, by calling virDomainRef - */ - virDomainRef(dom); - - Local argv[2]; - Persistent obj = static_cast(opaque); - Local hyp = obj->Get(NanNew("hypervisor"))->ToObject(); - Local callback = obj->Get(NanNew("callback")).As(); - - argv[0] = hyp; - argv[1] = domain_obj; //FIXME change with domain->handle_ - TryCatch try_catch; - callback->Call(hyp, 2, argv); - if (try_catch.HasCaught()) { - FatalException(try_catch); - } - + fprintf(stderr, "GENERIC CALLBACK CALLED"); return 0; } @@ -2101,35 +2062,16 @@ int Domain::domain_event_rtcchange_callback(virConnectPtr conn, virDomainPtr dom long long utcoffset, void *opaque) { NanScope(); - - Local domain_obj = Domain::NewInstance(dom); - - /* - The virDomainPtr object handle passed into the callback upon delivery - of an event is only valid for the duration of execution of the callback - If the callback wishes to keep the domain object after the callback, - it shall take a reference to it, by calling virDomainRef - */ - virDomainRef(dom); - - Local argv[3]; - Persistent obj = static_cast(opaque); - Local hyp = obj->Get(NanNew("hypervisor"))->ToObject(); - - Local callback = obj->Get(NanNew("callback")).As(); Local data = NanNew(); - data->Set(NanNew("utc_offset"), NanNew(utcoffset)); - - argv[0] = hyp; - argv[1] = domain_obj; //FIXME change with domain->handle_ - argv[2] = data; + data->Set(NanNew("utcOffset"), NanNew(utcoffset)); - TryCatch try_catch; - callback->Call(hyp, 3, argv); - if (try_catch.HasCaught()) { - FatalException(try_catch); - } + Handle argv[2] = { + NanNew("rtcChange"), + data + }; + ObjectWrap *domain = static_cast(opaque); + NanMakeCallback(NanObjectWrapHandle(domain), "emit", 2, argv); return 0; } @@ -2137,35 +2079,16 @@ int Domain::domain_event_watchdog_callback(virConnectPtr conn, virDomainPtr dom, void *opaque) { NanScope(); - - Local domain_obj = Domain::NewInstance(dom); - - /* - The virDomainPtr object handle passed into the callback upon delivery - of an event is only valid for the duration of execution of the callback - If the callback wishes to keep the domain object after the callback, - it shall take a reference to it, by calling virDomainRef - */ - virDomainRef(dom); - - Local argv[3]; - Persistent obj = static_cast(opaque); - Local hyp = obj->Get(NanNew("hypervisor"))->ToObject(); - Local callback = obj->Get(NanNew("callback")).As(); - Local data = NanNew(); data->Set(NanNew("action"), NanNew(action)); - argv[0] = hyp; - argv[1] = domain_obj; //FIXME change with domain->handle_ - argv[2] = data; - - TryCatch try_catch; - callback->Call(hyp, 3, argv); - if (try_catch.HasCaught()) { - FatalException(try_catch); - } + Handle argv[2] = { + NanNew("watchdogEvent"), + data + }; + ObjectWrap *domain = static_cast(opaque); + NanMakeCallback(NanObjectWrapHandle(domain), "emit", 2, argv); return 0; } @@ -2174,37 +2097,20 @@ int Domain::domain_event_io_error_callback(virConnectPtr conn, virDomainPtr dom, int action, void *opaque) { NanScope(); - - Local domain_obj = Domain::NewInstance(dom); - - /* - The virDomainPtr object handle passed into the callback upon delivery - of an event is only valid for the duration of execution of the callback - If the callback wishes to keep the domain object after the callback, - it shall take a reference to it, by calling virDomainRef - */ - virDomainRef(dom); - - Local argv[3]; - Persistent obj = static_cast(opaque); - Local hyp = obj->Get(NanNew("hypervisor"))->ToObject(); - Local callback = obj->Get(NanNew("callback")).As(); - Local data = NanNew(); - data->Set(NanNew("src_path"), NanNew(src_path)); - data->Set(NanNew("dev_alias"), NanNew(dev_alias)); + data->Set(NanNew("sourcePath"), NanNew(src_path)); + data->Set(NanNew("devAlias"), NanNew(dev_alias)); data->Set(NanNew("action"), NanNew(action)); - argv[0] = hyp; - argv[1] = domain_obj; //FIXME change with domain->handle_ - argv[2] = data; + data->Set(NanNew("action"), NanNew(action)); - TryCatch try_catch; - callback->Call(hyp, 3, argv); - if (try_catch.HasCaught()) { - FatalException(try_catch); - } + Handle argv[2] = { + NanNew("ioError"), + data + }; + ObjectWrap *domain = static_cast(opaque); + NanMakeCallback(NanObjectWrapHandle(domain), "emit", 2, argv); return 0; } @@ -2213,35 +2119,19 @@ int Domain::domain_event_io_error_reason_callback(virConnectPtr conn, virDomainP int action, const char *reason, void *opaque) { NanScope(); - Local domain_obj = Domain::NewInstance(dom); - - /*The virDomainPtr object handle passed into the callback upon delivery - of an event is only valid for the duration of execution of the callback - If the callback wishes to keep the domain object after the callback, - it shall take a reference to it, by calling virDomainRef*/ - virDomainRef(dom); - - Local argv[3]; - Persistent obj = static_cast(opaque); - Local hyp = obj->Get(NanNew("hypervisor"))->ToObject(); - Local callback = obj->Get(NanNew("callback")).As(); - Local data = NanNew(); - data->Set(NanNew("src_path"), NanNew(src_path)); - data->Set(NanNew("dev_alias"), NanNew(dev_alias)); + data->Set(NanNew("sourcePath"), NanNew(src_path)); + data->Set(NanNew("devAlias"), NanNew(dev_alias)); data->Set(NanNew("reason"), NanNew(reason)); data->Set(NanNew("action"), NanNew(action)); - argv[0] = hyp; - argv[1] = domain_obj; //FIXME change with domain->handle_ - argv[2] = data; - - TryCatch try_catch; - callback->Call(hyp, 3, argv); - if (try_catch.HasCaught()) { - FatalException(try_catch); - } + Handle argv[2] = { + NanNew("ioErrorReason"), + data + }; + ObjectWrap *domain = static_cast(opaque); + NanMakeCallback(NanObjectWrapHandle(domain), "emit", 2, argv); return 0; } @@ -2253,21 +2143,6 @@ int Domain::domain_event_graphics_callback(virConnectPtr conn, virDomainPtr dom, void *opaque) { NanScope(); - Local domain_obj = Domain::NewInstance(dom); - - /* - The virDomainPtr object handle passed into the callback upon delivery - of an event is only valid for the duration of execution of the callback - If the callback wishes to keep the domain object after the callback, - it shall take a reference to it, by calling virDomainRef - */ - virDomainRef(dom); - - Local argv[3]; - Persistent obj = static_cast(opaque); - Local hyp = obj->Get(NanNew("hypervisor"))->ToObject(); - Local callback = obj->Get(NanNew("callback")).As(); - Local lfamily; switch (local->family) { case VIR_DOMAIN_EVENT_GRAPHICS_ADDRESS_IPV4: @@ -2314,24 +2189,14 @@ int Domain::domain_event_graphics_callback(virConnectPtr conn, virDomainPtr dom, data->Set(NanNew("phase"), NanNew(phase)); data->Set(NanNew("auth_scheme"), NanNew(auth_scheme)); - argv[0] = hyp; - argv[1] = domain_obj; //FIXME change with domain->handle_ - argv[2] = data; + Handle argv[2] = { + NanNew("graphicsEvent"), + data + }; - TryCatch try_catch; - callback->Call(hyp, 3, argv); - if (try_catch.HasCaught()) { - FatalException(try_catch); - } + ObjectWrap *domain = static_cast(opaque); + NanMakeCallback(NanObjectWrapHandle(domain), "emit", 2, argv); return 0; } -void Domain::domain_event_free(void* /* opaque */) -{ - fprintf(stderr, "NOT IMPLEMENTED!"); -} - - - - } //namespace NodeLibvirt diff --git a/src/domain.h b/src/domain.h index aa7e6b5..178328d 100644 --- a/src/domain.h +++ b/src/domain.h @@ -19,6 +19,7 @@ class Domain : public ObjectWrap private: explicit Domain(virDomainPtr handle) : handle_(handle) {} static Persistent constructor_template; + static Persistent constructor; virDomainPtr handle_; friend class Hypervisor; @@ -131,22 +132,21 @@ class Domain : public ObjectWrap class RegisterEventWorker : public PrimitiveReturnWorker { public: - RegisterEventWorker(NanCallback *callback, const LibVirtHandle &handle, virDomainPtr domain, int evtype, void *opaque) - : PrimitiveReturnWorker(callback, handle), domain_(domain), evtype_(evtype), opaque_(opaque) {} + RegisterEventWorker(NanCallback *callback, const LibVirtHandle &handle, Domain *domain, int eventId) + : PrimitiveReturnWorker(callback, handle), domain_(domain), eventId_(eventId) {} void Execute(); private: - virDomainPtr domain_; - int evtype_; - void *opaque_; + Domain *domain_; + int eventId_; }; class UnregisterEventWorker : public PrimitiveReturnWorker { public: - UnregisterEventWorker(NanCallback *callback, const LibVirtHandle &handle, int callbackid) - : PrimitiveReturnWorker(callback, handle), callbackid_(callbackid) {} + UnregisterEventWorker(NanCallback *callback, const LibVirtHandle &handle, int callbackId) + : PrimitiveReturnWorker(callback, handle), callbackId_(callbackId) {} void Execute(); private: - int callbackid_; + int callbackId_; }; // ACTION METHOD WORKERS @@ -465,7 +465,6 @@ class Domain : public ObjectWrap const char *authScheme, virDomainEventGraphicsSubjectPtr subject, void *opaque); - }; } //namespace NodeLibvirt diff --git a/src/error.cc b/src/error.cc index b9a72fc..05d0d88 100644 --- a/src/error.cc +++ b/src/error.cc @@ -3,168 +3,144 @@ namespace NodeLibvirt { -Persistent Error::constructor_template; - -static Persistent code_symbol; -static Persistent domain_symbol; -static Persistent message_symbol; -static Persistent level_symbol; -static Persistent str1_symbol; -static Persistent str2_symbol; -static Persistent str3_symbol; -static Persistent int1_symbol; -static Persistent int2_symbol; - -void Error::Initialize() +Persistent Error::constructor; +void Error::Initialize(Handle exports) { - code_symbol = NODE_PSYMBOL("code"); - domain_symbol = NODE_PSYMBOL("domain"); - message_symbol = NODE_PSYMBOL("message"); - level_symbol = NODE_PSYMBOL("level"); - str1_symbol = NODE_PSYMBOL("str1"); - str2_symbol = NODE_PSYMBOL("str2"); - str3_symbol = NODE_PSYMBOL("str3"); - int1_symbol = NODE_PSYMBOL("int1"); - int2_symbol = NODE_PSYMBOL("int2"); - - Local t = FunctionTemplate::New(); - - // t->Inherit(EventEmitter::constructor_template); + Local t = NanNew(); + t->SetClassName(NanNew("Error")); Local ot = t->InstanceTemplate(); ot->SetInternalFieldCount(1); - - ot->SetAccessor(code_symbol, Error::Getter); - ot->SetAccessor(domain_symbol, Error::Getter); - ot->SetAccessor(message_symbol, Error::Getter); - ot->SetAccessor(level_symbol, Error::Getter); - ot->SetAccessor(str1_symbol, Error::Getter); - ot->SetAccessor(str2_symbol, Error::Getter); - ot->SetAccessor(str3_symbol, Error::Getter); - ot->SetAccessor(int1_symbol, Error::Getter); - ot->SetAccessor(int2_symbol, Error::Getter); + ot->SetAccessor(NanNew("code"), Error::Getter); + ot->SetAccessor(NanNew("domain"), Error::Getter); + ot->SetAccessor(NanNew("message"), Error::Getter); + ot->SetAccessor(NanNew("level"), Error::Getter); + ot->SetAccessor(NanNew("str1"), Error::Getter); + ot->SetAccessor(NanNew("str2"), Error::Getter); + ot->SetAccessor(NanNew("str3"), Error::Getter); + ot->SetAccessor(NanNew("int1"), Error::Getter); + ot->SetAccessor(NanNew("int2"), Error::Getter); + + NanAssignPersistent(constructor, t->GetFunction()); //define constants //virErrorNumber - NODE_DEFINE_CONSTANT(ot, VIR_ERR_OK); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INTERNAL_ERROR); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_MEMORY); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_SUPPORT); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_UNKNOWN_HOST); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_CONNECT); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_CONN); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_DOMAIN); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_ARG); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_OPERATION_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_GET_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_POST_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_HTTP_ERROR); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_SEXPR_SERIAL); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_XEN); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_XEN_CALL); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_OS_TYPE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_KERNEL); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_ROOT); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_SOURCE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_TARGET); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_NAME); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_OS); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_DEVICE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_XENSTORE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_DRIVER_FULL); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_CALL_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_XML_ERROR); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_DOM_EXIST); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_OPERATION_DENIED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_OPEN_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_READ_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_PARSE_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_CONF_SYNTAX); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_WRITE_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_XML_DETAIL); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_NETWORK); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NETWORK_EXIST); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_SYSTEM_ERROR); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_RPC); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_GNUTLS_ERROR); - NODE_DEFINE_CONSTANT(ot, VIR_WAR_NO_NETWORK); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_DOMAIN); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_NETWORK); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_MAC); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_AUTH_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_STORAGE_POOL); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_STORAGE_VOL); - NODE_DEFINE_CONSTANT(ot, VIR_WAR_NO_STORAGE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_STORAGE_POOL); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_STORAGE_VOL); - NODE_DEFINE_CONSTANT(ot, VIR_WAR_NO_NODE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_NODE_DEVICE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_NODE_DEVICE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_SECURITY_MODEL); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_OPERATION_INVALID); - NODE_DEFINE_CONSTANT(ot, VIR_WAR_NO_INTERFACE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_INTERFACE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_INTERFACE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_MULTIPLE_INTERFACES); - NODE_DEFINE_CONSTANT(ot, VIR_WAR_NO_NWFILTER); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_NWFILTER); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_NWFILTER); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_BUILD_FIREWALL); - NODE_DEFINE_CONSTANT(ot, VIR_WAR_NO_SECRET); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_SECRET); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_SECRET); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_CONFIG_UNSUPPORTED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_OPERATION_TIMEOUT); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_MIGRATE_PERSIST_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_HOOK_SCRIPT_FAILED); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_INVALID_DOMAIN_SNAPSHOT); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NO_DOMAIN_SNAPSHOT); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_OK); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INTERNAL_ERROR); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_MEMORY); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_SUPPORT); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_UNKNOWN_HOST); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_CONNECT); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_CONN); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_DOMAIN); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_ARG); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_OPERATION_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_GET_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_POST_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_HTTP_ERROR); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_SEXPR_SERIAL); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_XEN); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_XEN_CALL); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_OS_TYPE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_KERNEL); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_ROOT); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_SOURCE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_TARGET); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_NAME); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_OS); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_DEVICE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_XENSTORE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_DRIVER_FULL); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_CALL_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_XML_ERROR); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_DOM_EXIST); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_OPERATION_DENIED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_OPEN_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_READ_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_PARSE_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_CONF_SYNTAX); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_WRITE_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_XML_DETAIL); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_NETWORK); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NETWORK_EXIST); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_SYSTEM_ERROR); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_RPC); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_GNUTLS_ERROR); + NODE_DEFINE_CONSTANT(exports, VIR_WAR_NO_NETWORK); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_DOMAIN); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_NETWORK); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_MAC); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_AUTH_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_STORAGE_POOL); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_STORAGE_VOL); + NODE_DEFINE_CONSTANT(exports, VIR_WAR_NO_STORAGE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_STORAGE_POOL); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_STORAGE_VOL); + NODE_DEFINE_CONSTANT(exports, VIR_WAR_NO_NODE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_NODE_DEVICE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_NODE_DEVICE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_SECURITY_MODEL); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_OPERATION_INVALID); + NODE_DEFINE_CONSTANT(exports, VIR_WAR_NO_INTERFACE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_INTERFACE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_INTERFACE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_MULTIPLE_INTERFACES); + NODE_DEFINE_CONSTANT(exports, VIR_WAR_NO_NWFILTER); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_NWFILTER); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_NWFILTER); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_BUILD_FIREWALL); + NODE_DEFINE_CONSTANT(exports, VIR_WAR_NO_SECRET); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_SECRET); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_SECRET); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_CONFIG_UNSUPPORTED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_OPERATION_TIMEOUT); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_MIGRATE_PERSIST_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_HOOK_SCRIPT_FAILED); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_INVALID_DOMAIN_SNAPSHOT); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NO_DOMAIN_SNAPSHOT); //virErrorDomain - NODE_DEFINE_CONSTANT(ot, VIR_FROM_NONE); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_XEN); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_XEND); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_XENSTORE); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_SEXPR); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_XML); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_DOM); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_RPC); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_PROXY); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_CONF); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_QEMU); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_NET); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_TEST); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_REMOTE); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_OPENVZ); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_XENXM); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_STATS_LINUX); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_LXC); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_STORAGE); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_NETWORK); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_DOMAIN); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_UML); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_NODEDEV); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_XEN_INOTIFY); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_SECURITY); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_VBOX); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_INTERFACE); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_ONE); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_ESX); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_PHYP); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_SECRET); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_CPU); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_XENAPI); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_NWFILTER); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_HOOK); - NODE_DEFINE_CONSTANT(ot, VIR_FROM_DOMAIN_SNAPSHOT); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_NONE); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_XEN); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_XEND); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_XENSTORE); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_SEXPR); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_XML); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_DOM); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_RPC); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_PROXY); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_CONF); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_QEMU); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_NET); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_TEST); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_REMOTE); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_OPENVZ); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_XENXM); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_STATS_LINUX); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_LXC); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_STORAGE); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_NETWORK); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_DOMAIN); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_UML); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_NODEDEV); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_XEN_INOTIFY); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_SECURITY); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_VBOX); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_INTERFACE); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_ONE); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_ESX); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_PHYP); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_SECRET); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_CPU); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_XENAPI); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_NWFILTER); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_HOOK); + NODE_DEFINE_CONSTANT(exports, VIR_FROM_DOMAIN_SNAPSHOT); //virErrorLevel - NODE_DEFINE_CONSTANT(ot, VIR_ERR_NONE); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_WARNING); - NODE_DEFINE_CONSTANT(ot, VIR_ERR_ERROR); - - constructor_template = Persistent::New(t); - constructor_template->SetClassName(NanNew("Error")); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_NONE); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_WARNING); + NODE_DEFINE_CONSTANT(exports, VIR_ERR_ERROR); } Error::Error(virErrorPtr error) @@ -173,18 +149,15 @@ Error::Error(virErrorPtr error) error_ = error; } -Handle Error::New(virErrorPtr error) +Local Error::New(virErrorPtr error) { - NanScope(); - - if (error == NULL) - NanReturnUndefined(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local instance = ctor->NewInstance(); Error *err = new Error(error); - Local object = err->constructor_template->GetFunction()->NewInstance(); - err->Wrap(object); - - return NanEscapeScope(object); + err->Wrap(instance); + return NanEscapeScope(instance); } NAN_GETTER(Error::Getter) @@ -194,23 +167,23 @@ NAN_GETTER(Error::Getter) Error *error = ObjectWrap::Unwrap(args.This()); virErrorPtr error_ = error->error_; - if (property == code_symbol) { + if (property == NanNew("code")) { NanReturnValue(NanNew(error_->code)); - } else if (property == domain_symbol) { + } else if (property == NanNew("domain")) { NanReturnValue(NanNew(error_->domain)); - } else if (property == message_symbol) { + } else if (property == NanNew("message")) { NanReturnValue(NanNew(error_->message)); - } else if (property == level_symbol) { + } else if (property == NanNew("level")) { NanReturnValue(NanNew(error_->level)); - } else if (property == str1_symbol) { + } else if (property == NanNew("str1")) { NanReturnValue(NanNew(error_->str1 != NULL ? error_->str1 : "")); - } else if (property == str2_symbol) { + } else if (property == NanNew("str2")) { NanReturnValue(NanNew(error_->str2 != NULL ? error_->str2 : "")); - } else if (property == str3_symbol) { + } else if (property == NanNew("str3")) { NanReturnValue(NanNew(error_->str3 != NULL ? error_->str3 : "")); - } else if (property == int1_symbol) { + } else if (property == NanNew("int1")) { NanReturnValue(NanNew(error_->int1)); - } else if (property == int2_symbol) { + } else if (property == NanNew("int2")) { NanReturnValue(NanNew(error_->int2)); } diff --git a/src/error.h b/src/error.h index 709f9eb..b000ef5 100644 --- a/src/error.h +++ b/src/error.h @@ -15,12 +15,12 @@ namespace NodeLibvirt { class Error : public ObjectWrap { public: - static void Initialize(); - static Handle New(virErrorPtr error); + static void Initialize(Handle exports); + static Local New(virErrorPtr error); private: explicit Error(virErrorPtr error); - static Persistent constructor_template; + static Persistent constructor; static NAN_GETTER(Getter); diff --git a/src/event_impl.h b/src/event_impl.h index 2475999..b8b6124 100644 --- a/src/event_impl.h +++ b/src/event_impl.h @@ -27,6 +27,7 @@ class EventImpl static int RemoveTimeout(int timer); static void HandleCallback(uv_poll_t* handle, int status, int events); + #if UV_VERSION_MAJOR < 1 static void UpdateHandlesOnce(uv_check_t* handle, int status); static void CheckCallback(uv_check_t* handle, int status); @@ -36,6 +37,7 @@ class EventImpl static void CheckCallback(uv_check_t* handle); static void TimerCallback(uv_timer_t* handle); #endif + static void TimeoutCallback(uv_handle_s* handle); static void ClosePollCallback(uv_handle_t* handle); diff --git a/src/hypervisor.cc b/src/hypervisor.cc index 0588a82..395b2cc 100644 --- a/src/hypervisor.cc +++ b/src/hypervisor.cc @@ -20,7 +20,7 @@ void Hypervisor::Initialize(Handle exports) { NanScope(); - Local t = FunctionTemplate::New(New); + Local t = NanNew(New); t->SetClassName(NanNew("Hypervisor")); t->InstanceTemplate()->SetInternalFieldCount(1); @@ -116,43 +116,38 @@ void Hypervisor::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "lookupDomainByName", Domain::LookupByName); NODE_SET_PROTOTYPE_METHOD(t, "lookupDomainByUUID", Domain::LookupByUUID); - NODE_SET_PROTOTYPE_METHOD(t, "registerDomainEvent", Domain::RegisterEvent); - NODE_SET_PROTOTYPE_METHOD(t, "unregisterDomainEvent", Domain::UnregisterEvent); - - Local object_tmpl = t->InstanceTemplate(); - //Constants initialization // virConnectCredentialType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_USERNAME); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_AUTHNAME); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_LANGUAGE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_CNONCE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_PASSPHRASE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_ECHOPROMPT); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_NOECHOPROMPT); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_REALM); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CRED_EXTERNAL); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_USERNAME); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_AUTHNAME); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_LANGUAGE); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_CNONCE); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_PASSPHRASE); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_ECHOPROMPT); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_NOECHOPROMPT); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_REALM); + NODE_DEFINE_CONSTANT(exports, VIR_CRED_EXTERNAL); // virCPUCompareResult - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CPU_COMPARE_ERROR); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CPU_COMPARE_INCOMPATIBLE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CPU_COMPARE_IDENTICAL); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_CPU_COMPARE_SUPERSET); + NODE_DEFINE_CONSTANT(exports, VIR_CPU_COMPARE_ERROR); + NODE_DEFINE_CONSTANT(exports, VIR_CPU_COMPARE_INCOMPATIBLE); + NODE_DEFINE_CONSTANT(exports, VIR_CPU_COMPARE_IDENTICAL); + NODE_DEFINE_CONSTANT(exports, VIR_CPU_COMPARE_SUPERSET); // virSecretUsageType - NODE_DEFINE_CONSTANT(object_tmpl, VIR_SECRET_USAGE_TYPE_NONE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_SECRET_USAGE_TYPE_VOLUME); + NODE_DEFINE_CONSTANT(exports, VIR_SECRET_USAGE_TYPE_NONE); + NODE_DEFINE_CONSTANT(exports, VIR_SECRET_USAGE_TYPE_VOLUME); // virDomainEventID - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_ID_LIFECYCLE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_ID_REBOOT); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_ID_RTC_CHANGE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_ID_WATCHDOG); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_ID_IO_ERROR); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_ID_GRAPHICS); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_LIFECYCLE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_REBOOT); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_RTC_CHANGE); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_WATCHDOG); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_IO_ERROR); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_GRAPHICS); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON); #ifdef VIR_ENUM_SENTINELS - NODE_DEFINE_CONSTANT(object_tmpl, VIR_DOMAIN_EVENT_ID_LAST); + NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_LAST); #endif t->SetClassName(NanNew("Hypervisor")); diff --git a/src/interface.cc b/src/interface.cc index 5b9f017..22c695f 100644 --- a/src/interface.cc +++ b/src/interface.cc @@ -5,10 +5,11 @@ namespace NodeLibvirt { -Persistent Interface::constructor_template; +Persistent Interface::constructor; void Interface::Initialize(Handle exports) { - Local t = FunctionTemplate::New(); + Local t = NanNew(); + t->SetClassName(NanNew("Interface")); t->InstanceTemplate()->SetInternalFieldCount(1); NODE_SET_PROTOTYPE_METHOD(t, "start", Start); @@ -19,22 +20,21 @@ void Interface::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "undefine", Undefine); NODE_SET_PROTOTYPE_METHOD(t, "toXml", ToXml); - Local object_tmpl = t->InstanceTemplate(); + NanAssignPersistent(constructor, t->GetFunction()); + exports->Set(NanNew("Interface"), t->GetFunction()); //Constants //virInterfaceXMLFlags - NODE_DEFINE_CONSTANT(object_tmpl, VIR_INTERFACE_XML_INACTIVE); - - NanAssignPersistent(constructor_template, t); - constructor_template->SetClassName(NanNew("Interface")); - exports->Set(NanNew("Interface"), t->GetFunction()); + NODE_DEFINE_CONSTANT(exports, VIR_INTERFACE_XML_INACTIVE); } Local Interface::NewInstance(const LibVirtHandle &handle) { - NanScope(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local object = ctor->NewInstance(); + Interface *interface = new Interface(handle.ToInterface()); - Local object = constructor_template->GetFunction()->NewInstance(); interface->Wrap(object); return NanEscapeScope(object); } diff --git a/src/interface.h b/src/interface.h index 8e9f492..614e843 100644 --- a/src/interface.h +++ b/src/interface.h @@ -19,7 +19,7 @@ class Interface : public ObjectWrap virInterfacePtr GetInterface() const; private: - static Persistent constructor_template; + static Persistent constructor; virInterfacePtr handle_; friend class Hypervisor; diff --git a/src/libvirt_handle.cc b/src/libvirt_handle.cc index c5c5786..b82e259 100644 --- a/src/libvirt_handle.cc +++ b/src/libvirt_handle.cc @@ -3,8 +3,9 @@ namespace NodeLibvirt { -struct LibVirtHandlePrivate +class LibVirtHandlePrivate { +public: LibVirtHandlePrivate() { data.connection = 0; data.interface = 0; diff --git a/src/libvirt_handle.h b/src/libvirt_handle.h index 63aabdb..789a1e3 100644 --- a/src/libvirt_handle.h +++ b/src/libvirt_handle.h @@ -2,7 +2,14 @@ #define LIBVIRT_HANDLE_H #include -#include + +#ifdef __APPLE__ +# include +using namespace std::tr1; +#else +# include +#endif + using namespace std; namespace NodeLibvirt { diff --git a/src/network.cc b/src/network.cc index 9138a95..59d4865 100644 --- a/src/network.cc +++ b/src/network.cc @@ -5,10 +5,11 @@ namespace NodeLibvirt { -Persistent Network::constructor_template; +Persistent Network::constructor; void Network::Initialize(Handle exports) { - Local t = FunctionTemplate::New(); + Local t = NanNew(); + t->SetClassName(NanNew("Network")); t->InstanceTemplate()->SetInternalFieldCount(1); NODE_SET_PROTOTYPE_METHOD(t, "start", Start); @@ -23,16 +24,17 @@ void Network::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "toXml", ToXml); NODE_SET_PROTOTYPE_METHOD(t, "getBridgeName", GetBridgeName); - NanAssignPersistent(constructor_template, t); - constructor_template->SetClassName(NanNew("Network")); + NanAssignPersistent(constructor, t->GetFunction()); exports->Set(NanNew("Network"), t->GetFunction()); } Local Network::NewInstance(const LibVirtHandle &handle) { - NanScope(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local object = ctor->NewInstance(); + Network *network = new Network(handle.ToNetwork()); - Local object = constructor_template->GetFunction()->NewInstance(); network->Wrap(object); return NanEscapeScope(object); } diff --git a/src/network.h b/src/network.h index d1d0999..4a1a167 100644 --- a/src/network.h +++ b/src/network.h @@ -18,7 +18,7 @@ class Network : public ObjectWrap private: explicit Network(virNetworkPtr handle) : handle_(handle) {} - static Persistent constructor_template; + static Persistent constructor; virNetworkPtr handle_; friend class Hypervisor; diff --git a/src/network_filter.cc b/src/network_filter.cc index 9dd0dc0..78de2e3 100644 --- a/src/network_filter.cc +++ b/src/network_filter.cc @@ -6,10 +6,11 @@ namespace NodeLibvirt { -Persistent NetworkFilter::constructor_template; +Persistent NetworkFilter::constructor; void NetworkFilter::Initialize(Handle exports) { - Local t = FunctionTemplate::New(); + Local t = NanNew(); + t->SetClassName(NanNew("NetworkFilter")); t->InstanceTemplate()->SetInternalFieldCount(1); NODE_SET_PROTOTYPE_METHOD(t, "getName", NetworkFilter::GetName); @@ -17,16 +18,17 @@ void NetworkFilter::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "undefine", NetworkFilter::Undefine); NODE_SET_PROTOTYPE_METHOD(t, "toXml", NetworkFilter::ToXml); - NanAssignPersistent(constructor_template, t); - constructor_template->SetClassName(NanNew("NetworkFilter")); + NanAssignPersistent(constructor, t->GetFunction()); exports->Set(NanNew("NetworkFilter"), t->GetFunction()); } Local NetworkFilter::NewInstance(const LibVirtHandle &handle) { - NanScope(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local object = ctor->NewInstance(); + NetworkFilter *filter = new NetworkFilter(handle.ToNetworkFilter()); - Local object = constructor_template->GetFunction()->NewInstance(); filter->Wrap(object); return NanEscapeScope(object); } diff --git a/src/network_filter.h b/src/network_filter.h index d594bed..3a8a89f 100644 --- a/src/network_filter.h +++ b/src/network_filter.h @@ -18,7 +18,7 @@ class NetworkFilter : public ObjectWrap private: explicit NetworkFilter(virNWFilterPtr handle) : handle_(handle) {} - static Persistent constructor_template; + static Persistent constructor; virNWFilterPtr handle_; friend class Hypervisor; diff --git a/src/node_device.cc b/src/node_device.cc index 3250053..f524e9f 100644 --- a/src/node_device.cc +++ b/src/node_device.cc @@ -8,10 +8,11 @@ using namespace v8; namespace NodeLibvirt { -Persistent NodeDevice::constructor_template; +Persistent NodeDevice::constructor; void NodeDevice::Initialize(Handle exports) { - Local t = FunctionTemplate::New(); + Local t = NanNew(); + t->SetClassName(NanNew("NodeDevice")); t->InstanceTemplate()->SetInternalFieldCount(1); NODE_SET_PROTOTYPE_METHOD(t, "destroy", Destroy); @@ -23,16 +24,17 @@ void NodeDevice::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "toXml", ToXml); NODE_SET_PROTOTYPE_METHOD(t, "getCapabilities", GetCapabilities); - NanAssignPersistent(constructor_template, t); - constructor_template->SetClassName(NanNew("NodeDevice")); + NanAssignPersistent(constructor, t->GetFunction()); exports->Set(NanNew("NodeDevice"), t->GetFunction()); } Local NodeDevice::NewInstance(const LibVirtHandle &handle) { - NanScope(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local object = ctor->NewInstance(); + NodeDevice *nodeDevice = new NodeDevice(handle.ToNodeDevice()); - Local object = constructor_template->GetFunction()->NewInstance(); nodeDevice->Wrap(object); return NanEscapeScope(object); } diff --git a/src/node_device.h b/src/node_device.h index 4b3620f..4037edf 100644 --- a/src/node_device.h +++ b/src/node_device.h @@ -18,7 +18,7 @@ class NodeDevice : public ObjectWrap private: explicit NodeDevice(virNodeDevicePtr handle) : handle_(handle) {} - static Persistent constructor_template; + static Persistent constructor; virNodeDevicePtr handle_; friend class Hypervisor; diff --git a/src/node_libvirt.cc b/src/node_libvirt.cc index c5e8af2..a2b30e4 100644 --- a/src/node_libvirt.cc +++ b/src/node_libvirt.cc @@ -22,7 +22,7 @@ void InitAll(Handle exports) { virInitialize(); Hypervisor::Initialize(exports); - Error::Initialize(); + Error::Initialize(exports); Domain::Initialize(exports); NodeDevice::Initialize(exports); Interface::Initialize(exports); diff --git a/src/node_libvirt.h b/src/node_libvirt.h index c0d9f9b..124e50b 100644 --- a/src/node_libvirt.h +++ b/src/node_libvirt.h @@ -12,10 +12,6 @@ using namespace node; #include "libvirt_handle.h" #define NODE_LIBVIRT_VERSION "v0.1.0" -#define LIBVIRT_THROW_EXCEPTION(err) \ - v8::Local exception = v8::Exception::Error( \ - v8::String::New(err)); \ - ThrowException(exception); #define NLV_WARN_UNFINISHED(Method) \ fprintf(stderr, "WARNING: " #Method " is still synchronous and needs to be ported\n"); diff --git a/src/secret.cc b/src/secret.cc index 1fc2997..650f4e4 100644 --- a/src/secret.cc +++ b/src/secret.cc @@ -5,10 +5,11 @@ namespace NodeLibvirt { -Persistent Secret::constructor_template; +Persistent Secret::constructor; void Secret::Initialize(Handle exports) { - Local t = FunctionTemplate::New(); + Local t = NanNew(); + t->SetClassName(NanNew("Secret")); t->InstanceTemplate()->SetInternalFieldCount(1); NODE_SET_PROTOTYPE_METHOD(t, "undefine", Undefine); @@ -19,16 +20,17 @@ void Secret::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "getUsageType", GetUsageType); NODE_SET_PROTOTYPE_METHOD(t, "toXml", ToXml); - NanAssignPersistent(constructor_template, t); - constructor_template->SetClassName(NanNew("Secret")); + NanAssignPersistent(constructor, t->GetFunction()); exports->Set(NanNew("Secret"), t->GetFunction()); } Local Secret::NewInstance(const LibVirtHandle &handle) { - NanScope(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local object = ctor->NewInstance(); + Secret *secret = new Secret(handle.ToSecret()); - Local object = constructor_template->GetFunction()->NewInstance(); secret->Wrap(object); return NanEscapeScope(object); } diff --git a/src/secret.h b/src/secret.h index a92abdd..0841a9b 100644 --- a/src/secret.h +++ b/src/secret.h @@ -18,7 +18,7 @@ class Secret : public ObjectWrap private: explicit Secret(virSecretPtr handle) : handle_(handle) {} - static Persistent constructor_template; + static Persistent constructor; virSecretPtr handle_; friend class Hypervisor; diff --git a/src/storage_pool.cc b/src/storage_pool.cc index f36e407..a5cf3e8 100644 --- a/src/storage_pool.cc +++ b/src/storage_pool.cc @@ -12,10 +12,12 @@ namespace NodeLibvirt { Persistent StoragePool::constructor_template; +Persistent StoragePool::constructor; void StoragePool::Initialize(Handle exports) { NanScope(); - Local t = FunctionTemplate::New(); + Local t = NanNew(); + t->SetClassName(NanNew("StoragePool")); t->InstanceTemplate()->SetInternalFieldCount(1); NODE_SET_PROTOTYPE_METHOD(t, "build", Build); @@ -38,29 +40,29 @@ void StoragePool::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "lookupStorageVolumeByName", StorageVolume::LookupByName); NanAssignPersistent(constructor_template, t); - constructor_template->SetClassName(NanNew("StoragePool")); + NanAssignPersistent(constructor, t->GetFunction()); exports->Set(NanNew("StoragePool"), t->GetFunction()); - Local object_tmpl = t->InstanceTemplate(); - //Constants initialization //virStoragePoolDeleteFlags - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_POOL_DELETE_NORMAL); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_POOL_DELETE_ZEROED); + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_POOL_DELETE_NORMAL); + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_POOL_DELETE_ZEROED); //virStoragePoolState - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_POOL_INACTIVE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_POOL_BUILDING); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_POOL_RUNNING); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_POOL_DEGRADED); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_POOL_INACCESSIBLE); + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_POOL_INACTIVE); + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_POOL_BUILDING); + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_POOL_RUNNING); + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_POOL_DEGRADED); + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_POOL_INACCESSIBLE); } Local StoragePool::NewInstance(const LibVirtHandle &handle) { - NanScope(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local object = ctor->NewInstance(); + StoragePool *storagePool = new StoragePool(handle.ToStoragePool()); - Local object = constructor_template->GetFunction()->NewInstance(); storagePool->Wrap(object); return NanEscapeScope(object); } diff --git a/src/storage_pool.h b/src/storage_pool.h index a08c77e..b581e97 100644 --- a/src/storage_pool.h +++ b/src/storage_pool.h @@ -19,6 +19,7 @@ class StoragePool : public ObjectWrap private: explicit StoragePool(virStoragePoolPtr handle) : handle_(handle) {} static Persistent constructor_template; + static Persistent constructor; virStoragePoolPtr handle_; friend class StorageVolume; diff --git a/src/storage_volume.cc b/src/storage_volume.cc index 4c25cf3..1c9b72b 100644 --- a/src/storage_volume.cc +++ b/src/storage_volume.cc @@ -11,10 +11,12 @@ namespace NodeLibvirt { Persistent StorageVolume::constructor_template; +Persistent StorageVolume::constructor; void StorageVolume::Initialize(Handle exports) { NanScope(); - Local t = FunctionTemplate::New(); + Local t = NanNew(); + t->SetClassName(NanNew("StorageVolume")); t->InstanceTemplate()->SetInternalFieldCount(1); NODE_SET_PROTOTYPE_METHOD(t, "getInfo", GetInfo); @@ -26,19 +28,21 @@ void StorageVolume::Initialize(Handle exports) NODE_SET_PROTOTYPE_METHOD(t, "wipe", Wipe); NanAssignPersistent(constructor_template, t); - constructor_template->SetClassName(NanNew("StorageVolume")); + NanAssignPersistent(constructor, t->GetFunction()); exports->Set(NanNew("StorageVolume"), t->GetFunction()); - Local object_tmpl = t->InstanceTemplate(); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_VOL_FILE); - NODE_DEFINE_CONSTANT(object_tmpl, VIR_STORAGE_VOL_BLOCK); + // Constants + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_VOL_FILE); + NODE_DEFINE_CONSTANT(exports, VIR_STORAGE_VOL_BLOCK); } Local StorageVolume::NewInstance(const LibVirtHandle &handle) { - NanScope(); + NanEscapableScope(); + Local ctor = NanNew(constructor); + Local object = ctor->NewInstance(); + StorageVolume *storageVolume = new StorageVolume(handle.ToStorageVolume()); - Local object = constructor_template->GetFunction()->NewInstance(); storageVolume->Wrap(object); return NanEscapeScope(object); } diff --git a/src/storage_volume.h b/src/storage_volume.h index bc69d68..caf4975 100644 --- a/src/storage_volume.h +++ b/src/storage_volume.h @@ -19,6 +19,7 @@ class StorageVolume : public ObjectWrap private: explicit StorageVolume(virStorageVolPtr handle) : handle_(handle) {} static Persistent constructor_template; + static Persistent constructor; virStorageVolPtr handle_; friend class StoragePool; diff --git a/src/worker.h b/src/worker.h index 4a10e44..4ed8d6f 100644 --- a/src/worker.h +++ b/src/worker.h @@ -1,6 +1,8 @@ #ifndef SRC_WORKER_H_ #define SRC_WORKER_H_ +#include + #include #include @@ -66,7 +68,7 @@ class ListReturnWorker : public LibVirtWorker virtual void HandleOKCallback() { NanScope(); v8::Local result = NanNew(data_.size()); - for (int i = 0; i < data_.size(); ++i) + for (unsigned int i = 0; i < data_.size(); ++i) result->Set(NanNew(i), NanNew(data_[i])); v8::Local argv[] = { NanNull(), result };