#include "node.h" #include "net.h" #include "file.h" #include "http.h" #include "timer.h" #include "natives.h" #include #include #include #include #include using namespace v8; using namespace node; using namespace std; static int exit_code = 0; ObjectWrap::~ObjectWrap ( ) { handle_->SetInternalField(0, Undefined()); handle_.Dispose(); handle_.Clear(); } ObjectWrap::ObjectWrap (Handle handle) { // TODO throw exception if it's already set HandleScope scope; handle_ = Persistent::New(handle); Handle external = External::New(this); handle_->SetInternalField(0, external); handle_.MakeWeak(this, ObjectWrap::MakeWeak); attach_count_ = 0; weak_ = false; } void ObjectWrap::Attach () { attach_count_ += 1; } void ObjectWrap::Detach () { if (attach_count_ > 0) attach_count_ -= 1; if(weak_ && attach_count_ == 0) delete this; } void* ObjectWrap::Unwrap (Handle handle) { HandleScope scope; Handle field = Handle::Cast(handle->GetInternalField(0)); return field->Value(); } void ObjectWrap::MakeWeak (Persistent _, void *data) { ObjectWrap *obj = static_cast (data); obj->weak_ = true; if (obj->attach_count_ == 0) delete obj; } // Extracts a C string from a V8 Utf8Value. const char* ToCString(const v8::String::Utf8Value& value) { return *value ? *value : ""; } void ReportException(v8::TryCatch* try_catch) { v8::HandleScope handle_scope; v8::String::Utf8Value exception(try_catch->Exception()); const char* exception_string = ToCString(exception); v8::Handle message = try_catch->Message(); if (message.IsEmpty()) { // V8 didn't provide any extra information about this error; just // print the exception. printf("%s\n", exception_string); } else { message->PrintCurrentStackTrace(stdout); // Print (filename):(line number): (message). v8::String::Utf8Value filename(message->GetScriptResourceName()); const char* filename_string = ToCString(filename); int linenum = message->GetLineNumber(); printf("%s:%i: %s\n", filename_string, linenum, exception_string); // Print line of source code. v8::String::Utf8Value sourceline(message->GetSourceLine()); const char* sourceline_string = ToCString(sourceline); printf("%s\n", sourceline_string); // Print wavy underline (GetUnderline is deprecated). int start = message->GetStartColumn(); for (int i = 0; i < start; i++) { printf(" "); } int end = message->GetEndColumn(); for (int i = start; i < end; i++) { printf("^"); } printf("\n"); } } // Executes a string within the current v8 context. Handle ExecuteString(v8::Handle source, v8::Handle filename) { HandleScope scope; TryCatch try_catch; Handle