2009-03-04 00:31:37 +01:00
|
|
|
#include "node.h"
|
2009-03-03 01:56:15 +01:00
|
|
|
|
|
|
|
#include "node_tcp.h"
|
|
|
|
#include "node_http.h"
|
2009-03-03 19:17:33 +01:00
|
|
|
#include "node_timer.h"
|
2009-03-03 01:56:15 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2009-03-04 00:31:37 +01:00
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
using namespace std;
|
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
static int exit_code = 0;
|
2009-03-03 01:56:15 +01:00
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
void
|
|
|
|
node_fatal_exception (TryCatch &try_catch)
|
|
|
|
{
|
|
|
|
HandleScope handle_scope;
|
|
|
|
|
|
|
|
Local<Message> message = try_catch.Message();
|
|
|
|
String::Utf8Value error(try_catch.Exception());
|
|
|
|
|
|
|
|
fprintf( stderr
|
|
|
|
, "Uncaught Exception. line %d '%s'\n\n"
|
|
|
|
, try_catch.Message()->GetLineNumber()
|
|
|
|
, *error
|
|
|
|
);
|
|
|
|
|
|
|
|
ev_unloop(node_loop(), EVUNLOOP_ALL);
|
|
|
|
exit_code = 1;
|
|
|
|
}
|
2009-03-03 01:56:15 +01:00
|
|
|
|
|
|
|
// Reads a file into a v8 string.
|
2009-03-04 00:31:37 +01:00
|
|
|
static Handle<String>
|
|
|
|
ReadFile (const string& name)
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
|
|
|
FILE* file = fopen(name.c_str(), "rb");
|
|
|
|
if (file == NULL) return Handle<String>();
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
int size = ftell(file);
|
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
char* chars = new char[size + 1];
|
|
|
|
chars[size] = '\0';
|
|
|
|
for (int i = 0; i < size;) {
|
|
|
|
int read = fread(&chars[i], 1, size - i, file);
|
|
|
|
i += read;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
Handle<String> result = String::New(chars, size);
|
|
|
|
delete[] chars;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
static void
|
|
|
|
ParseOptions (int argc, char* argv[], map<string, string>& options, string* file)
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
string arg = argv[i];
|
|
|
|
int index = arg.find('=', 0);
|
|
|
|
if (index == string::npos) {
|
|
|
|
*file = arg;
|
|
|
|
} else {
|
|
|
|
string key = arg.substr(0, index);
|
|
|
|
string value = arg.substr(index+1);
|
|
|
|
options[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
static bool
|
|
|
|
compile (Handle<String> script)
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
|
|
|
HandleScope handle_scope;
|
|
|
|
|
|
|
|
// We're just about to compile the script; set up an error handler to
|
|
|
|
// catch any exceptions the script might throw.
|
|
|
|
TryCatch try_catch;
|
|
|
|
|
|
|
|
// Compile the script and check for errors.
|
|
|
|
Handle<Script> compiled_script = Script::Compile(script);
|
|
|
|
if (compiled_script.IsEmpty()) {
|
|
|
|
|
|
|
|
Handle<Message> message = try_catch.Message();
|
|
|
|
|
|
|
|
String::Utf8Value error(try_catch.Exception());
|
|
|
|
|
|
|
|
printf("error: %s line %d\n", *error, message->GetLineNumber());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the script!
|
|
|
|
Handle<Value> result = compiled_script->Run();
|
|
|
|
if (result.IsEmpty()) {
|
|
|
|
// The TryCatch above is still in effect and will have caught the error.
|
|
|
|
String::Utf8Value error(try_catch.Exception());
|
|
|
|
printf("error: %s\n", *error);
|
|
|
|
// Running the script failed; bail out.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
static Handle<Value>
|
|
|
|
LogCallback (const Arguments& args)
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
|
|
|
if (args.Length() < 1) return v8::Undefined();
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<Value> arg = args[0];
|
|
|
|
String::Utf8Value value(arg);
|
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
printf("%s\n", *value);
|
2009-03-03 01:56:15 +01:00
|
|
|
fflush(stdout);
|
|
|
|
|
2009-03-04 11:29:39 +01:00
|
|
|
return Undefined();
|
2009-03-03 01:56:15 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
|
2009-03-05 18:17:36 +01:00
|
|
|
static Handle<Value>
|
|
|
|
BlockingFileReadCallback (const Arguments& args)
|
|
|
|
{
|
|
|
|
if (args.Length() < 1) return v8::Undefined();
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
String::Utf8Value filename(args[0]);
|
|
|
|
|
2009-03-07 17:08:35 +01:00
|
|
|
Handle<String> output = ReadFile (*filename);
|
|
|
|
return scope.Close(output);
|
2009-03-05 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
static void
|
|
|
|
OnFatalError (const char* location, const char* message)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Fatal error. %s %s\n", location, message);
|
|
|
|
ev_unloop(node_loop(), EVUNLOOP_ALL);
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:17:33 +01:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2009-03-03 01:56:15 +01:00
|
|
|
{
|
2009-03-04 10:22:37 +01:00
|
|
|
V8::SetFlagsFromCommandLine(&argc, argv, true);
|
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
map<string, string> options;
|
|
|
|
string file;
|
|
|
|
ParseOptions(argc, argv, options, &file);
|
|
|
|
if (file.empty()) {
|
|
|
|
fprintf(stderr, "No script was specified.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<String> source = ReadFile(file);
|
|
|
|
if (source.IsEmpty()) {
|
|
|
|
fprintf(stderr, "Error reading '%s'.\n", file.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
Persistent<Context> context = Context::New(NULL, ObjectTemplate::New());
|
2009-03-03 01:56:15 +01:00
|
|
|
Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
Local<Object> g = Context::GetCurrent()->Global();
|
|
|
|
|
2009-03-06 19:49:52 +01:00
|
|
|
g->Set ( String::New("log")
|
|
|
|
, FunctionTemplate::New(LogCallback)->GetFunction()
|
|
|
|
);
|
|
|
|
|
|
|
|
g->Set ( String::New("blockingFileRead")
|
|
|
|
, FunctionTemplate::New(BlockingFileReadCallback)->GetFunction()
|
|
|
|
);
|
2009-03-05 18:17:36 +01:00
|
|
|
|
2009-03-04 10:08:33 +01:00
|
|
|
Init_timer(g);
|
2009-03-05 13:41:10 +01:00
|
|
|
Init_tcp(g);
|
2009-03-04 10:08:33 +01:00
|
|
|
Init_http(g);
|
2009-03-03 19:17:33 +01:00
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
V8::SetFatalErrorHandler(OnFatalError);
|
2009-03-03 19:17:33 +01:00
|
|
|
|
2009-03-03 01:56:15 +01:00
|
|
|
// Compile and run the script
|
|
|
|
if (!compile(source))
|
2009-03-03 19:17:33 +01:00
|
|
|
return 1;
|
2009-03-03 01:56:15 +01:00
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
ev_loop(node_loop(), 0);
|
2009-03-03 01:56:15 +01:00
|
|
|
|
|
|
|
context.Dispose();
|
|
|
|
|
2009-03-04 00:31:37 +01:00
|
|
|
return exit_code;
|
2009-03-03 01:56:15 +01:00
|
|
|
}
|
2009-03-05 13:41:10 +01:00
|
|
|
|