You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
9.4 KiB

#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"
15 years ago
#include "natives.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <assert.h>
#include <dlfcn.h> /* dlopen(), dlsym() */
#include <string>
#include <list>
#include <map>
using namespace v8;
using namespace node;
// Extracts a C string from a V8 Utf8Value.
const char*
ToCString(const v8::String::Utf8Value& value)
{
return *value ? *value : "<string conversion failed>";
}
void
ReportException(TryCatch* try_catch)
{
HandleScope handle_scope;
String::Utf8Value exception(try_catch->Exception());
const char* exception_string = ToCString(exception);
Handle<Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
fprintf(stderr, "%s\n", exception_string);
} else {
// 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_string);
// 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);
}
}
// Executes a string within the current v8 context.
Handle<Value>
ExecuteString(v8::Handle<v8::String> source,
v8::Handle<v8::Value> filename)
{
HandleScope scope;
TryCatch try_catch;
Handle<Script> script = Script::Compile(source, filename);
if (script.IsEmpty()) {
ReportException(&try_catch);
exit(1);
}
Handle<Value> result = script->Run();
if (result.IsEmpty()) {
ReportException(&try_catch);
exit(1);
}
return scope.Close(result);
}
v8::Handle<v8::Value>
node_exit (const v8::Arguments& args)
{
int r = 0;
15 years ago
if (args.Length() > 0)
r = args[0]->IntegerValue();
exit(r);
15 years ago
return Undefined();
}
typedef void (*extInit)(Handle<Object> exports);
Handle<Value>
node_dlopen (const v8::Arguments& args)
{
HandleScope scope;
if (args.Length() < 2) return Undefined();
String::Utf8Value filename(args[0]->ToString());
Local<Object> target = args[1]->ToObject();
void *handle = dlopen(*filename, RTLD_LAZY);
if (handle == NULL) {
return ThrowException(String::New("dlopen() failed."));
}
void *init_handle = dlsym(handle, "init");
if (init_handle == NULL) {
ThrowException(String::New("No 'init' symbol found in module."));
return Undefined();
}
extInit init = reinterpret_cast<extInit>(init_handle);
init(target);
return Undefined();
}
v8::Handle<v8::Value>
compile (const v8::Arguments& args)
{
15 years ago
if (args.Length() < 2)
return Undefined();
HandleScope scope;
Local<String> source = args[0]->ToString();
Local<String> filename = args[1]->ToString();
Handle<Value> result = ExecuteString(source, filename);
15 years ago
return scope.Close(result);
}
static void
OnFatalError (const char* location, const char* message)
{
#define FATAL_ERROR "\033[1;31mV8 FATAL ERROR.\033[m"
if (location)
fprintf(stderr, FATAL_ERROR " %s %s\n", location, message);
15 years ago
else
fprintf(stderr, FATAL_ERROR " %s\n", message);
exit(1);
}
void
node::FatalException (TryCatch &try_catch)
{
ReportException(&try_catch);
exit(1);
}
static ev_async eio_watcher;
15 years ago
static void
node_eio_cb (EV_P_ ev_async *watcher, int revents)
{
assert(watcher == &eio_watcher);
assert(revents == EV_ASYNC);
eio_poll();
}
static void
eio_want_poll (void)
{
15 years ago
ev_async_send(EV_DEFAULT_UC_ &eio_watcher);
}
enum encoding
node::ParseEncoding (Handle<Value> encoding_v)
{
HandleScope scope;
if (!encoding_v->IsString())
return RAW;
String::Utf8Value encoding(encoding_v->ToString());
if(strcasecmp(*encoding, "utf8") == 0) {
return UTF8;
} else if (strcasecmp(*encoding, "ascii") == 0) {
return ASCII;
} else {
return RAW;
}
}
static void
ExecuteNativeJS (const char *filename, const char *data)
{
HandleScope scope;
TryCatch try_catch;
ExecuteString(String::New(data), String::New(filename));
if (try_catch.HasCaught()) {
puts("There is an error in Node's built-in javascript");
puts("This should be reported as a bug!");
ReportException(&try_catch);
exit(1);
}
}
16 years ago
static Local<Object>
Load (int argc, char *argv[])
{
16 years ago
HandleScope scope;
16 years ago
Local<Object> global_obj = Context::GetCurrent()->Global();
Local<Object> node_obj = Object::New();
16 years ago
global_obj->Set(String::NewSymbol("node"), node_obj);
15 years ago
node_obj->Set(String::NewSymbol("version"), String::New(NODE_VERSION));
Local<Array> arguments = Array::New(argc);
for (int i = 0; i < argc; i++) {
Local<String> arg = String::New(argv[i]);
arguments->Set(Integer::New(i), arg);
}
16 years ago
global_obj->Set(String::NewSymbol("ARGV"), arguments);
16 years ago
NODE_SET_METHOD(node_obj, "compile", compile);
NODE_SET_METHOD(node_obj, "reallyExit", node_exit);
NODE_SET_METHOD(node_obj, "dlopen", node_dlopen);
16 years ago
node_obj->Set(String::NewSymbol("EventEmitter"),
EventEmitter::constructor_template->GetFunction());
16 years ago
Promise::Initialize(node_obj);
Stdio::Initialize(node_obj);
16 years ago
Timer::Initialize(node_obj);
ChildProcess::Initialize(node_obj);
DefineConstants(node_obj);
Local<Object> dns = Object::New();
node_obj->Set(String::NewSymbol("dns"), dns);
DNS::Initialize(dns);
Local<Object> fs = Object::New();
16 years ago
node_obj->Set(String::NewSymbol("fs"), fs);
File::Initialize(fs);
Local<Object> tcp = Object::New();
16 years ago
node_obj->Set(String::New("tcp"), tcp);
Server::Initialize(tcp);
Connection::Initialize(tcp);
Local<Object> http = Object::New();
16 years ago
node_obj->Set(String::New("http"), http);
HTTPServer::Initialize(http);
HTTPConnection::Initialize(http);
ExecuteNativeJS("util.js", native_util);
ExecuteNativeJS("events.js", native_events);
ExecuteNativeJS("http.js", native_http);
ExecuteNativeJS("file.js", native_file);
ExecuteNativeJS("node.js", native_node);
16 years ago
return scope.Close(node_obj);
}
16 years ago
static void
CallExitHandler (Handle<Object> node_obj)
{
HandleScope scope;
Local<Value> exit_v = node_obj->Get(String::New("exit"));
assert(exit_v->IsFunction());
Handle<Function> exit_f = Handle<Function>::Cast(exit_v);
TryCatch try_catch;
16 years ago
exit_f->Call(Context::GetCurrent()->Global(), 0, NULL);
if (try_catch.HasCaught())
node::FatalException(try_catch);
16 years ago
}
static void
PrintHelp ( )
{
printf("Usage: node [switches] script.js [arguments] \n"
" -v, --version print node's version\n"
" --libs print linker flags for modules\n"
" --cflags print pre-processor and compiler flags\n"
" --v8-options print v8 command line options\n");
}
static void
15 years ago
ParseArgs (int *argc, char **argv)
{
bool cflags = false, libs = false;
for (int i = 1; i < *argc; i++) {
const char *arg = argv[i];
if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
printf("%s\n", NODE_VERSION);
exit(0);
} else if (strcmp(arg, "--cflags") == 0) {
cflags = true;
continue;
} else if (strcmp(arg, "--libs") == 0) {
libs = true;
continue;
} else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
PrintHelp();
exit(0);
} else if (strcmp(arg, "--v8-options") == 0) {
argv[i] = (char*)"--help";
}
}
/* XXX Wow this is terrible code. */
bool should_exit = false;
if (cflags) {
should_exit = true;
printf("%s ", NODE_CFLAGS);
}
if (libs) {
should_exit = true;
printf("%s ", NODE_LIBFLAGS);
}
if (should_exit) {
printf("\n");
exit(0);
}
}
16 years ago
int
node::start (int argc, char *argv[])
16 years ago
{
evcom_ignore_sigpipe();
16 years ago
ev_default_loop(EVFLAG_AUTO); // initialize the default ev loop.
// start eio thread pool
ev_async_init(&eio_watcher, node_eio_cb);
eio_init(eio_want_poll, NULL);
ev_async_start(EV_DEFAULT_UC_ &eio_watcher);
ev_unref(EV_DEFAULT_UC);
16 years ago
ParseArgs(&argc, argv);
16 years ago
V8::SetFlagsFromCommandLine(&argc, argv, true);
V8::Initialize();
V8::SetFatalErrorHandler(OnFatalError);
if(argc < 2) {
fprintf(stderr, "No script was specified.\n");
PrintHelp();
16 years ago
return 1;
}
HandleScope handle_scope;
Local<FunctionTemplate> process_template = FunctionTemplate::New();
// The global object / "process" is an instance of EventEmitter. For
// strange reasons we must initialize EventEmitter now! it will be assign
15 years ago
// to it's namespace node.EventEmitter in Load() bellow.
EventEmitter::Initialize(process_template);
Persistent<Context> context = Context::New(NULL,
process_template->InstanceTemplate());
16 years ago
Context::Scope context_scope(context);
context->Global()->Set(String::NewSymbol("process"), context->Global());
16 years ago
Local<Object> node_obj = Load(argc, argv);
ev_loop(EV_DEFAULT_UC_ 0); // main event loop
CallExitHandler(node_obj);
context.Dispose();
16 years ago
V8::Dispose();
return 0;
}