Browse Source

cpplint.py node.cc and node.h

v0.7.4-release
Ryan 16 years ago
parent
commit
d6c9d31cb5
  1. 191
      src/node.cc
  2. 33
      src/node.h

191
src/node.cc

@ -1,16 +1,5 @@
#include "node.h" // Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#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 <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,20 +10,25 @@
#include <errno.h> #include <errno.h>
#include <dlfcn.h> /* dlopen(), dlsym() */ #include <dlfcn.h> /* dlopen(), dlsym() */
#include <string> #include <events.h>
#include <list> #include <dns.h>
#include <map> #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 <v8-debug.h> #include <v8-debug.h>
using namespace v8; using namespace v8;
using namespace node;
extern char **environ; extern char **environ;
Local<Value> namespace node {
node::Encode (const void *buf, size_t len, enum encoding encoding)
{ Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
HandleScope scope; HandleScope scope;
if (!len) return scope.Close(Null()); if (!len) return scope.Close(Null());
@ -51,12 +45,13 @@ node::Encode (const void *buf, size_t len, enum encoding encoding)
if (encoding == RAWS) { if (encoding == RAWS) {
const unsigned char *cbuf = static_cast<const unsigned char*>(buf); const unsigned char *cbuf = static_cast<const unsigned char*>(buf);
uint16_t twobytebuf[len]; uint16_t * twobytebuf = new uint16_t[len];
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
// XXX is the following line platform independent? // XXX is the following line platform independent?
twobytebuf[i] = cbuf[i]; twobytebuf[i] = cbuf[i];
} }
Local<String> chunk = String::New(twobytebuf, len); Local<String> chunk = String::New(twobytebuf, len);
delete twobytebuf;
return scope.Close(chunk); return scope.Close(chunk);
} }
@ -66,9 +61,7 @@ node::Encode (const void *buf, size_t len, enum encoding encoding)
} }
// Returns -1 if the handle was not valid for decoding // Returns -1 if the handle was not valid for decoding
ssize_t ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
node::DecodeBytes (v8::Handle<v8::Value> val, enum encoding encoding)
{
HandleScope scope; HandleScope scope;
if (val->IsArray()) { if (val->IsArray()) {
@ -79,11 +72,11 @@ node::DecodeBytes (v8::Handle<v8::Value> val, enum encoding encoding)
if (!val->IsString()) return -1; if (!val->IsString()) return -1;
Handle<String> string = Handle<String>::Cast(val); Handle<String> str = Handle<String>::Cast(val);
if (encoding == UTF8) return string->Utf8Length(); if (encoding == UTF8) return str->Utf8Length();
return string->Length(); return str->Length();
} }
#ifndef MIN #ifndef MIN
@ -91,9 +84,9 @@ node::DecodeBytes (v8::Handle<v8::Value> val, enum encoding encoding)
#endif #endif
// Returns number of bytes written. // Returns number of bytes written.
ssize_t ssize_t DecodeWrite(char *buf, size_t buflen,
node::DecodeWrite (char *buf, size_t buflen, v8::Handle<v8::Value> val, enum encoding encoding) v8::Handle<v8::Value> val,
{ enum encoding encoding) {
size_t i; size_t i;
HandleScope scope; HandleScope scope;
@ -115,23 +108,23 @@ node::DecodeWrite (char *buf, size_t buflen, v8::Handle<v8::Value> val, enum enc
} }
assert(val->IsString()); assert(val->IsString());
Handle<String> string = Handle<String>::Cast(val); Handle<String> str = Handle<String>::Cast(val);
if (encoding == UTF8) { if (encoding == UTF8) {
string->WriteUtf8(buf, buflen); str->WriteUtf8(buf, buflen);
return buflen; return buflen;
} }
if (encoding == ASCII) { if (encoding == ASCII) {
string->WriteAscii(buf, 0, buflen); str->WriteAscii(buf, 0, buflen);
return buflen; return buflen;
} }
// THIS IS AWFUL!!! FIXME // THIS IS AWFUL!!! FIXME
uint16_t twobytebuf[buflen]; uint16_t * twobytebuf = new uint16_t[buflen];
string->Write(twobytebuf, 0, buflen); str->Write(twobytebuf, 0, buflen);
for (size_t i = 0; i < buflen; i++) { for (size_t i = 0; i < buflen; i++) {
unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]); unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]);
@ -139,26 +132,24 @@ node::DecodeWrite (char *buf, size_t buflen, v8::Handle<v8::Value> val, enum enc
buf[i] = b[0]; buf[i] = b[0];
} }
delete twobytebuf;
return buflen; return buflen;
} }
// Extracts a C string from a V8 Utf8Value. // Extracts a C str from a V8 Utf8Value.
const char* const char* ToCString(const v8::String::Utf8Value& value) {
ToCString(const v8::String::Utf8Value& value) return *value ? *value : "<str conversion failed>";
{
return *value ? *value : "<string conversion failed>";
} }
static void static void ReportException(TryCatch *try_catch) {
ReportException (TryCatch &try_catch) Handle<Message> message = try_catch->Message();
{
Handle<Message> message = try_catch.Message();
if (message.IsEmpty()) { if (message.IsEmpty()) {
fprintf(stderr, "Error: (no message)\n"); fprintf(stderr, "Error: (no message)\n");
fflush(stderr); fflush(stderr);
return; return;
} }
Handle<Value> error = try_catch.Exception(); Handle<Value> error = try_catch->Exception();
Handle<String> stack; Handle<String> stack;
if (error->IsObject()) { if (error->IsObject()) {
Handle<Object> obj = Handle<Object>::Cast(error); Handle<Object> obj = Handle<Object>::Cast(error);
@ -198,32 +189,28 @@ ReportException (TryCatch &try_catch)
fflush(stderr); fflush(stderr);
} }
// Executes a string within the current v8 context. // Executes a str within the current v8 context.
Handle<Value> Handle<Value> ExecuteString(v8::Handle<v8::String> source,
ExecuteString(v8::Handle<v8::String> source, v8::Handle<v8::Value> filename) {
v8::Handle<v8::Value> filename)
{
HandleScope scope; HandleScope scope;
TryCatch try_catch; TryCatch try_catch;
Handle<Script> script = Script::Compile(source, filename); Handle<Script> script = Script::Compile(source, filename);
if (script.IsEmpty()) { if (script.IsEmpty()) {
ReportException(try_catch); ReportException(&try_catch);
exit(1); exit(1);
} }
Handle<Value> result = script->Run(); Handle<Value> result = script->Run();
if (result.IsEmpty()) { if (result.IsEmpty()) {
ReportException(try_catch); ReportException(&try_catch);
exit(1); exit(1);
} }
return scope.Close(result); return scope.Close(result);
} }
static Handle<Value> static Handle<Value> Cwd(const Arguments& args) {
Cwd (const Arguments& args)
{
HandleScope scope; HandleScope scope;
char output[PATH_MAX]; char output[PATH_MAX];
@ -236,9 +223,7 @@ Cwd (const Arguments& args)
return scope.Close(cwd); return scope.Close(cwd);
} }
v8::Handle<v8::Value> v8::Handle<v8::Value> Exit(const v8::Arguments& args) {
node_exit (const v8::Arguments& args)
{
int r = 0; int r = 0;
if (args.Length() > 0) if (args.Length() > 0)
r = args[0]->IntegerValue(); r = args[0]->IntegerValue();
@ -249,9 +234,7 @@ node_exit (const v8::Arguments& args)
typedef void (*extInit)(Handle<Object> exports); typedef void (*extInit)(Handle<Object> exports);
Handle<Value> Handle<Value> DLOpen(const v8::Arguments& args) {
node_dlopen (const v8::Arguments& args)
{
HandleScope scope; HandleScope scope;
if (args.Length() < 2) return Undefined(); if (args.Length() < 2) return Undefined();
@ -279,9 +262,7 @@ node_dlopen (const v8::Arguments& args)
return Undefined(); return Undefined();
} }
v8::Handle<v8::Value> v8::Handle<v8::Value> Compile(const v8::Arguments& args) {
compile (const v8::Arguments& args)
{
if (args.Length() < 2) if (args.Length() < 2)
return Undefined(); return Undefined();
@ -295,10 +276,7 @@ compile (const v8::Arguments& args)
return scope.Close(result); return scope.Close(result);
} }
static void static void OnFatalError(const char* location, const char* message) {
OnFatalError (const char* location, const char* message)
{
#define FATAL_ERROR "\033[1;31mV8 FATAL ERROR.\033[m" #define FATAL_ERROR "\033[1;31mV8 FATAL ERROR.\033[m"
if (location) if (location)
fprintf(stderr, FATAL_ERROR " %s %s\n", location, message); fprintf(stderr, FATAL_ERROR " %s %s\n", location, message);
@ -308,32 +286,24 @@ OnFatalError (const char* location, const char* message)
exit(1); exit(1);
} }
void void FatalException(TryCatch &try_catch) {
node::FatalException (TryCatch &try_catch) ReportException(&try_catch);
{
ReportException(try_catch);
exit(1); exit(1);
} }
static ev_async eio_watcher; static ev_async eio_watcher;
static void static void EIOCallback(EV_P_ ev_async *watcher, int revents) {
node_eio_cb (EV_P_ ev_async *watcher, int revents)
{
assert(watcher == &eio_watcher); assert(watcher == &eio_watcher);
assert(revents == EV_ASYNC); assert(revents == EV_ASYNC);
eio_poll(); eio_poll();
} }
static void static void EIOWantPoll(void) {
eio_want_poll (void)
{
ev_async_send(EV_DEFAULT_UC_ &eio_watcher); ev_async_send(EV_DEFAULT_UC_ &eio_watcher);
} }
enum encoding enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
node::ParseEncoding (Handle<Value> encoding_v, enum encoding _default)
{
HandleScope scope; HandleScope scope;
if (!encoding_v->IsString()) if (!encoding_v->IsString())
@ -354,23 +324,19 @@ node::ParseEncoding (Handle<Value> encoding_v, enum encoding _default)
} }
} }
static void static void ExecuteNativeJS(const char *filename, const char *data) {
ExecuteNativeJS (const char *filename, const char *data)
{
HandleScope scope; HandleScope scope;
TryCatch try_catch; TryCatch try_catch;
ExecuteString(String::New(data), String::New(filename)); ExecuteString(String::New(data), String::New(filename));
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {
puts("There is an error in Node's built-in javascript"); puts("There is an error in Node's built-in javascript");
puts("This should be reported as a bug!"); puts("This should be reported as a bug!");
ReportException(try_catch); ReportException(&try_catch);
exit(1); exit(1);
} }
} }
static Local<Object> static Local<Object> Load(int argc, char *argv[]) {
Load (int argc, char *argv[])
{
HandleScope scope; HandleScope scope;
Local<Object> global_obj = Context::GetCurrent()->Global(); Local<Object> global_obj = Context::GetCurrent()->Global();
@ -400,10 +366,10 @@ Load (int argc, char *argv[])
} }
global_obj->Set(String::NewSymbol("ENV"), env); global_obj->Set(String::NewSymbol("ENV"), env);
NODE_SET_METHOD(node_obj, "compile", compile); NODE_SET_METHOD(node_obj, "compile", Compile);
NODE_SET_METHOD(node_obj, "reallyExit", node_exit); NODE_SET_METHOD(node_obj, "reallyExit", Exit);
NODE_SET_METHOD(node_obj, "cwd", Cwd); NODE_SET_METHOD(node_obj, "cwd", Cwd);
NODE_SET_METHOD(node_obj, "dlopen", node_dlopen); NODE_SET_METHOD(node_obj, "dlopen", DLOpen);
node_obj->Set(String::NewSymbol("EventEmitter"), node_obj->Set(String::NewSymbol("EventEmitter"),
EventEmitter::constructor_template->GetFunction()); EventEmitter::constructor_template->GetFunction());
@ -442,9 +408,7 @@ Load (int argc, char *argv[])
return scope.Close(node_obj); return scope.Close(node_obj);
} }
static void static void CallExitHandler(Handle<Object> node_obj) {
CallExitHandler (Handle<Object> node_obj)
{
HandleScope scope; HandleScope scope;
Local<Value> exit_v = node_obj->Get(String::New("exit")); Local<Value> exit_v = node_obj->Get(String::New("exit"));
assert(exit_v->IsFunction()); assert(exit_v->IsFunction());
@ -455,10 +419,8 @@ CallExitHandler (Handle<Object> node_obj)
node::FatalException(try_catch); node::FatalException(try_catch);
} }
static void static void PrintHelp() {
PrintHelp ( ) printf("Usage: node [options] [--] script.js [arguments] \n"
{
printf("Usage: node [switches] script.js [arguments] \n"
" -v, --version print node's version\n" " -v, --version print node's version\n"
" --cflags print pre-processor and compiler flags\n" " --cflags print pre-processor and compiler flags\n"
" --v8-options print v8 command line options\n\n" " --v8-options print v8 command line options\n\n"
@ -466,9 +428,7 @@ PrintHelp ( )
" or with 'man node'\n"); " or with 'man node'\n");
} }
static void static void ParseArgs(int *argc, char **argv) {
ParseArgs (int *argc, char **argv)
{
for (int i = 1; i < *argc; i++) { for (int i = 1; i < *argc; i++) {
const char *arg = argv[i]; const char *arg = argv[i];
if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) { if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
@ -481,32 +441,32 @@ ParseArgs (int *argc, char **argv)
PrintHelp(); PrintHelp();
exit(0); exit(0);
} else if (strcmp(arg, "--v8-options") == 0) { } else if (strcmp(arg, "--v8-options") == 0) {
argv[i] = (char*)"--help"; argv[i] = reinterpret_cast<const char*>("--help");
} }
} }
} }
int } // namespace node
main(int argc, char *argv[])
{ int main(int argc, char *argv[]) {
ParseArgs(&argc, argv); node::ParseArgs(&argc, argv);
V8::SetFlagsFromCommandLine(&argc, argv, true); V8::SetFlagsFromCommandLine(&argc, argv, true);
evcom_ignore_sigpipe(); evcom_ignore_sigpipe();
ev_default_loop(EVFLAG_AUTO); // initialize the default ev loop. ev_default_loop(EVFLAG_AUTO); // initialize the default ev loop.
// start eio thread pool // start eio thread pool
ev_async_init(&eio_watcher, node_eio_cb); ev_async_init(&node::eio_watcher, node::EIOCallback);
eio_init(eio_want_poll, NULL); eio_init(node::EIOWantPoll, NULL);
ev_async_start(EV_DEFAULT_UC_ &eio_watcher); ev_async_start(EV_DEFAULT_UC_ &node::eio_watcher);
ev_unref(EV_DEFAULT_UC); ev_unref(EV_DEFAULT_UC);
V8::Initialize(); V8::Initialize();
V8::SetFatalErrorHandler(OnFatalError); V8::SetFatalErrorHandler(node::OnFatalError);
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "No script was specified.\n"); fprintf(stderr, "No script was specified.\n");
PrintHelp(); node::PrintHelp();
return 1; return 1;
} }
@ -517,7 +477,7 @@ main(int argc, char *argv[])
// The global object / "process" is an instance of EventEmitter. For // The global object / "process" is an instance of EventEmitter. For
// strange reasons we must initialize EventEmitter now! it will be assign // strange reasons we must initialize EventEmitter now! it will be assign
// to it's namespace node.EventEmitter in Load() bellow. // to it's namespace node.EventEmitter in Load() bellow.
EventEmitter::Initialize(process_template); node::EventEmitter::Initialize(process_template);
Persistent<Context> context = Context::New(NULL, Persistent<Context> context = Context::New(NULL,
process_template->InstanceTemplate()); process_template->InstanceTemplate());
@ -527,14 +487,15 @@ main(int argc, char *argv[])
context->Global()->Set(String::NewSymbol("process"), context->Global()); context->Global()->Set(String::NewSymbol("process"), context->Global());
Local<Object> node_obj = Load(argc, argv); Local<Object> node_obj = node::Load(argc, argv);
ev_loop(EV_DEFAULT_UC_ 0); // main event loop ev_loop(EV_DEFAULT_UC_ 0); // main event loop
CallExitHandler(node_obj); node::CallExitHandler(node_obj);
context.Dispose(); context.Dispose();
V8::Dispose(); V8::Dispose();
return 0; return 0;
} }

33
src/node.h

@ -1,19 +1,20 @@
#ifndef node_h // Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#define node_h #ifndef SRC_NODE_H_
#define SRC_NODE_H_
#include <ev.h> #include <ev.h>
#include <eio.h> #include <eio.h>
#include <v8.h> #include <v8.h>
#include <evcom.h> #include <evcom.h>
#include "object_wrap.h" #include <object_wrap.h>
#include "node_version.h" #include <node_version.h>
namespace node { namespace node {
/* Converts a unixtime to V8 Date */ /* Converts a unixtime to V8 Date */
#define NODE_UNIXTIME_V8(t) v8::Date::New(1000*static_cast<double>(t)) #define NODE_UNIXTIME_V8(t) v8::Date::New(1000*static_cast<double>(t))
#define NODE_V8_UNIXTIME(v) ((double)((v)->IntegerValue())/1000.0); #define NODE_V8_UNIXTIME(v) (static_cast<double>((v)->IntegerValue())/1000.0);
#define NODE_DEFINE_CONSTANT(target, constant) \ #define NODE_DEFINE_CONSTANT(target, constant) \
(target)->Set(v8::String::NewSymbol(#constant), \ (target)->Set(v8::String::NewSymbol(#constant), \
@ -27,26 +28,30 @@ namespace node {
do { \ do { \
v8::Local<v8::Signature> __callback##_SIG = v8::Signature::New(templ); \ v8::Local<v8::Signature> __callback##_SIG = v8::Signature::New(templ); \
v8::Local<v8::FunctionTemplate> __callback##_TEM = \ v8::Local<v8::FunctionTemplate> __callback##_TEM = \
FunctionTemplate::New(callback, v8::Handle<v8::Value>() , __callback##_SIG ); \ FunctionTemplate::New(callback, v8::Handle<v8::Value>(), \
__callback##_SIG); \
templ->PrototypeTemplate()->Set(v8::String::NewSymbol(name), \ templ->PrototypeTemplate()->Set(v8::String::NewSymbol(name), \
__callback##_TEM); \ __callback##_TEM); \
} while (0) } while (0)
enum encoding {ASCII, UTF8, RAW, RAWS}; enum encoding {ASCII, UTF8, RAW, RAWS};
enum encoding ParseEncoding (v8::Handle<v8::Value> encoding_v, enum encoding _default = RAW); enum encoding ParseEncoding(v8::Handle<v8::Value> encoding_v,
enum encoding _default = RAW);
void FatalException(v8::TryCatch &try_catch); void FatalException(v8::TryCatch &try_catch);
v8::Local<v8::Value> v8::Local<v8::Value> Encode(const void *buf, size_t len,
Encode (const void *buf, size_t len, enum encoding encoding = RAW); enum encoding encoding = RAW);
// Returns -1 if the handle was not valid for decoding // Returns -1 if the handle was not valid for decoding
ssize_t ssize_t DecodeBytes(v8::Handle<v8::Value>,
DecodeBytes (v8::Handle<v8::Value>, enum encoding encoding = RAW); enum encoding encoding = RAW);
// returns bytes written. // returns bytes written.
ssize_t ssize_t DecodeWrite(char *buf,
DecodeWrite (char *buf, size_t buflen, v8::Handle<v8::Value>, enum encoding encoding = RAW); size_t buflen,
v8::Handle<v8::Value>,
enum encoding encoding = RAW);
} // namespace node } // namespace node
#endif // node_h #endif // SRC_NODE_H_

Loading…
Cancel
Save