#include "node.h" #include "events.h" #include "dns.h" #include "net.h" #include "file.h" #include "http.h" #include "timer.h" #include "child_process.h" #include "constants.h" #include "node_stdio.h" #include "natives.h" #include #include #include #include /* PATH_MAX */ #include #include #include #include /* dlopen(), dlsym() */ #include #include #include #include using namespace v8; using namespace node; Local node::Encode (const void *buf, size_t len, enum encoding encoding) { HandleScope scope; if (!len) return scope.Close(Null()); if (encoding == RAW) { // raw encoding Local array = Array::New(len); for (size_t i = 0; i < len; i++) { unsigned char val = static_cast(buf)[i]; array->Set(Integer::New(i), Integer::New(val)); } return scope.Close(array); } if (encoding == RAWS) { const unsigned char *cbuf = static_cast(buf); uint16_t twobytebuf[len]; for (size_t i = 0; i < len; i++) { // XXX is the following line platform independent? twobytebuf[i] = cbuf[i]; } Local chunk = String::New(twobytebuf, len); return scope.Close(chunk); } // utf8 or ascii encoding Local chunk = String::New((const char*)buf, len); return scope.Close(chunk); } // Returns -1 if the handle was not valid for decoding ssize_t node::DecodeBytes (v8::Handle val, enum encoding encoding) { HandleScope scope; if (val->IsArray()) { if (encoding != RAW) return -1; Handle array = Handle::Cast(val); return array->Length(); } if (!val->IsString()) return -1; Handle string = Handle::Cast(val); if (encoding == UTF8) return string->Utf8Length(); return string->Length(); } #ifndef MIN # define MIN(a,b) ((a) < (b) ? (a) : (b)) #endif // Returns number of bytes written. ssize_t node::DecodeWrite (char *buf, size_t buflen, v8::Handle val, enum encoding encoding) { size_t i; HandleScope scope; // XXX // A lot of improvement can be made here. See: // http://code.google.com/p/v8/issues/detail?id=270 // http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c // http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611 if (val->IsArray()) { if (encoding != RAW) return -1; Handle array = Handle::Cast(val); size_t array_len = array->Length(); for (i = 0; i < MIN(buflen, array_len); i++) { Local int_value = array->Get(Integer::New(i)); buf[i] = int_value->IntegerValue(); } return i; } assert(val->IsString()); Handle string = Handle::Cast(val); if (encoding == UTF8) { string->WriteUtf8(buf, buflen); return buflen; } if (encoding == ASCII) { string->WriteAscii(buf, 0, buflen); return buflen; } // THIS IS AWFUL!!! FIXME uint16_t twobytebuf[buflen]; string->Write(twobytebuf, 0, buflen); for (size_t i = 0; i < buflen; i++) { unsigned char *b = reinterpret_cast(&twobytebuf[i]); assert(b[1] == 0); buf[i] = b[0]; } return buflen; } // Extracts a C string from a V8 Utf8Value. const char* ToCString(const v8::String::Utf8Value& value) { return *value ? *value : ""; } static void ReportException (TryCatch &try_catch) { Handle message = try_catch.Message(); if (message.IsEmpty()) { fprintf(stderr, "Error: (no message)\n"); return; } Handle error = try_catch.Exception(); Handle stack; if (error->IsObject()) { Handle obj = Handle::Cast(error); Handle raw_stack = obj->Get(String::New("stack")); if (raw_stack->IsString()) stack = Handle::Cast(raw_stack); } if (stack.IsEmpty()) { String::Utf8Value exception(error); // Print (filename):(line number): (message). String::Utf8Value filename(message->GetScriptResourceName()); const char* filename_string = ToCString(filename); int linenum = message->GetLineNumber(); fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, *exception); // Print line of source code. String::Utf8Value sourceline(message->GetSourceLine()); const char* sourceline_string = ToCString(sourceline); fprintf(stderr, "%s\n", sourceline_string); // Print wavy underline (GetUnderline is deprecated). int start = message->GetStartColumn(); for (int i = 0; i < start; i++) { fprintf(stderr, " "); } int end = message->GetEndColumn(); for (int i = start; i < end; i++) { fprintf(stderr, "^"); } fprintf(stderr, "\n"); message->PrintCurrentStackTrace(stderr); } else { String::Utf8Value trace(stack); fprintf(stderr, "%s\n", *trace); } } // Executes a string within the current v8 context. Handle ExecuteString(v8::Handle source, v8::Handle filename) { HandleScope scope; TryCatch try_catch; Handle