The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / src / node.cc @ 93f7f0dc
History | View | Annotate | Download (4.93 KB)
1 |
#include "node.h" |
---|---|
2 |
|
3 |
//#include "net.h"
|
4 |
#include "file.h" |
5 |
#include "process.h" |
6 |
#include "http.h" |
7 |
#include "timers.h" |
8 |
|
9 |
#include "natives.h" |
10 |
|
11 |
#include <stdio.h> |
12 |
#include <assert.h> |
13 |
|
14 |
#include <string> |
15 |
#include <list> |
16 |
#include <map> |
17 |
|
18 |
using namespace v8; |
19 |
using namespace node; |
20 |
using namespace std; |
21 |
|
22 |
static int exit_code = 0; |
23 |
|
24 |
// Extracts a C string from a V8 Utf8Value.
|
25 |
const char* |
26 |
ToCString(const v8::String::Utf8Value& value)
|
27 |
{ |
28 |
return *value ? *value : "<string conversion failed>"; |
29 |
} |
30 |
|
31 |
void
|
32 |
ReportException(v8::TryCatch* try_catch) |
33 |
{ |
34 |
v8::HandleScope handle_scope; |
35 |
v8::String::Utf8Value exception(try_catch->Exception()); |
36 |
const char* exception_string = ToCString(exception); |
37 |
v8::Handle<v8::Message> message = try_catch->Message(); |
38 |
if (message.IsEmpty()) {
|
39 |
// V8 didn't provide any extra information about this error; just
|
40 |
// print the exception.
|
41 |
printf("%s\n", exception_string);
|
42 |
} else {
|
43 |
message->PrintCurrentStackTrace(stdout); |
44 |
|
45 |
// Print (filename):(line number): (message).
|
46 |
v8::String::Utf8Value filename(message->GetScriptResourceName()); |
47 |
const char* filename_string = ToCString(filename); |
48 |
int linenum = message->GetLineNumber();
|
49 |
printf("%s:%i: %s\n", filename_string, linenum, exception_string);
|
50 |
// Print line of source code.
|
51 |
v8::String::Utf8Value sourceline(message->GetSourceLine()); |
52 |
const char* sourceline_string = ToCString(sourceline); |
53 |
printf("%s\n", sourceline_string);
|
54 |
// Print wavy underline (GetUnderline is deprecated).
|
55 |
int start = message->GetStartColumn();
|
56 |
for (int i = 0; i < start; i++) { |
57 |
printf(" ");
|
58 |
} |
59 |
int end = message->GetEndColumn();
|
60 |
for (int i = start; i < end; i++) { |
61 |
printf("^");
|
62 |
} |
63 |
printf("\n");
|
64 |
} |
65 |
} |
66 |
|
67 |
// Executes a string within the current v8 context.
|
68 |
Handle<Value> |
69 |
ExecuteString(v8::Handle<v8::String> source, |
70 |
v8::Handle<v8::Value> filename) |
71 |
{ |
72 |
HandleScope scope; |
73 |
Handle<Script> script = Script::Compile(source, filename); |
74 |
if (script.IsEmpty()) {
|
75 |
return ThrowException(String::New("Error compiling string")); |
76 |
} |
77 |
|
78 |
Handle<Value> result = script->Run(); |
79 |
if (result.IsEmpty()) {
|
80 |
return ThrowException(String::New("Error running string")); |
81 |
} |
82 |
|
83 |
return scope.Close(result);
|
84 |
} |
85 |
|
86 |
JS_METHOD(compile) |
87 |
{ |
88 |
if (args.Length() < 2) |
89 |
return Undefined();
|
90 |
|
91 |
HandleScope scope; |
92 |
|
93 |
Local<String> source = args[0]->ToString();
|
94 |
Local<String> filename = args[1]->ToString();
|
95 |
|
96 |
Handle<Value> result = ExecuteString(source, filename); |
97 |
|
98 |
return scope.Close(result);
|
99 |
} |
100 |
|
101 |
|
102 |
static void |
103 |
OnFatalError (const char* location, const char* message) |
104 |
{ |
105 |
fprintf(stderr, "Fatal error: %s %s\n", location, message);
|
106 |
ev_unloop(node_loop(), EVUNLOOP_ALL); |
107 |
} |
108 |
|
109 |
|
110 |
void
|
111 |
node_fatal_exception (TryCatch &try_catch) |
112 |
{ |
113 |
ReportException(&try_catch); |
114 |
ev_unloop(node_loop(), EVUNLOOP_ALL); |
115 |
exit_code = 1;
|
116 |
} |
117 |
|
118 |
void node_exit (int code) |
119 |
{ |
120 |
exit_code = code; |
121 |
ev_unloop(node_loop(), EVUNLOOP_ALL); |
122 |
} |
123 |
|
124 |
|
125 |
static ev_async thread_pool_watcher;
|
126 |
|
127 |
static void |
128 |
thread_pool_cb (EV_P_ ev_async *w, int revents)
|
129 |
{ |
130 |
int r = eio_poll();
|
131 |
/* returns 0 if all requests were handled, -1 if not, or the value of EIO_FINISH if != 0 */
|
132 |
|
133 |
// XXX is this check too heavy?
|
134 |
// it require three locks in eio
|
135 |
// what's the better way?
|
136 |
if (eio_nreqs () == 0 && eio_nready() == 0 && eio_npending() == 0) |
137 |
ev_async_stop(EV_DEFAULT_ w); |
138 |
} |
139 |
|
140 |
static void |
141 |
thread_pool_want_poll (void)
|
142 |
{ |
143 |
ev_async_send(EV_DEFAULT_ &thread_pool_watcher); |
144 |
} |
145 |
|
146 |
static void |
147 |
thread_pool_done_poll (void)
|
148 |
{ |
149 |
} |
150 |
|
151 |
void
|
152 |
node_eio_warmup (void)
|
153 |
{ |
154 |
ev_async_start(EV_DEFAULT_ &thread_pool_watcher); |
155 |
} |
156 |
|
157 |
int
|
158 |
main (int argc, char *argv[]) |
159 |
{ |
160 |
// start eio thread pool
|
161 |
ev_async_init(&thread_pool_watcher, thread_pool_cb); |
162 |
eio_init(thread_pool_want_poll, NULL);
|
163 |
|
164 |
V8::SetFlagsFromCommandLine(&argc, argv, true);
|
165 |
|
166 |
if(argc < 2) { |
167 |
fprintf(stderr, "No script was specified.\n");
|
168 |
return 1; |
169 |
} |
170 |
|
171 |
string filename(argv[1]); |
172 |
|
173 |
HandleScope handle_scope; |
174 |
|
175 |
Persistent<Context> context = Context::New(NULL, ObjectTemplate::New());
|
176 |
Context::Scope context_scope(context); |
177 |
V8::SetFatalErrorHandler(OnFatalError); |
178 |
|
179 |
Local<Object> g = Context::GetCurrent()->Global(); |
180 |
|
181 |
Local<Object> node = Object::New(); |
182 |
g->Set(String::New("node"), node);
|
183 |
|
184 |
JS_SET_METHOD(node, "compile", compile);
|
185 |
|
186 |
Local<Array> arguments = Array::New(argc); |
187 |
for (int i = 0; i < argc; i++) { |
188 |
Local<String> arg = String::New(argv[i]); |
189 |
arguments->Set(Integer::New(i), arg); |
190 |
} |
191 |
g->Set(String::New("ARGV"), arguments);
|
192 |
|
193 |
// BUILT-IN MODULES
|
194 |
//NodeInit_net(g);
|
195 |
NodeInit_timers(g); |
196 |
NodeInit_process(g); |
197 |
NodeInit_file(g); |
198 |
NodeInit_http(g); |
199 |
|
200 |
// NATIVE JAVASCRIPT MODULES
|
201 |
TryCatch try_catch; |
202 |
|
203 |
ExecuteString(String::New(native_file), String::New("file.js"));
|
204 |
if (try_catch.HasCaught()) goto native_js_error; |
205 |
|
206 |
ExecuteString(String::New(native_main), String::New("main.js"));
|
207 |
if (try_catch.HasCaught()) goto native_js_error; |
208 |
|
209 |
ev_loop(node_loop(), 0);
|
210 |
|
211 |
context.Dispose(); |
212 |
|
213 |
return exit_code;
|
214 |
|
215 |
native_js_error:
|
216 |
ReportException(&try_catch); |
217 |
return 1; |
218 |
} |