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.

Statistics
| Branch: | Revision:

main_repo / src / node.cc @ 1a126ed1

History | View | Annotate | Download (5.96 KB)

1
#include "node.h"
2
#define EV_STANDALONE 1
3
#include <ev.c>
4

    
5
#include "node_tcp.h"
6
#include "node_http.h"
7
#include "node_timer.h"
8

    
9
#include <stdio.h>
10
#include <assert.h>
11

    
12
#include <string>
13
#include <list>
14
#include <map>
15

    
16
using namespace v8;
17
using namespace std;
18

    
19
static int exit_code = 0;
20

    
21
// Reads a file into a v8 string.
22
static Handle<String>
23
ReadFile (const string& name) 
24
{
25

    
26
  FILE* file = fopen(name.c_str(), "rb");
27
  if (file == NULL) return Handle<String>();
28
 
29
  fseek(file, 0, SEEK_END);
30
  int size = ftell(file);
31
  rewind(file);
32

    
33
  char chars[size+1];
34
  chars[size] = '\0';
35
  for (int i = 0; i < size;) {
36
    int read = fread(&chars[i], 1, size - i, file);
37
    if(read <= 0) {
38
      perror("read()");
39
    }
40
    i += read;
41
  }
42

    
43
  uint16_t expanded_base[size+1];
44
  expanded_base[size] = '\0';
45
  for(int i = 0; i < size; i++) 
46
    expanded_base[i] = chars[i];
47

    
48
  fclose(file);
49

    
50
  HandleScope scope;
51
  Local<String> result = String::New(expanded_base, size);
52

    
53
  return scope.Close(result);
54
}
55

    
56
static Handle<Value>
57
Log (const Arguments& args) 
58
{
59
  if (args.Length() < 1) return v8::Undefined();
60
  HandleScope scope;
61
  Handle<Value> arg = args[0];
62
  String::Utf8Value value(arg);
63

    
64
  printf("%s\n", *value);
65
  fflush(stdout);
66

    
67
  return Undefined();
68
}
69

    
70

    
71
static Handle<Value>
72
BlockingFileRead (const Arguments& args)
73
{
74
  if (args.Length() < 1) return v8::Undefined();
75
  HandleScope scope;
76

    
77
  String::Utf8Value filename(args[0]);
78
  Handle<String> output = ReadFile (*filename);
79
  return scope.Close(output);
80
}
81

    
82
static void
83
OnFatalError (const char* location, const char* message)
84
{
85
  fprintf(stderr, "Fatal error. %s %s\n", location, message);
86
  ev_unloop(node_loop(), EVUNLOOP_ALL);
87
}
88

    
89

    
90
// Extracts a C string from a V8 Utf8Value.
91
const char* ToCString(const v8::String::Utf8Value& value) {
92
  return *value ? *value : "<string conversion failed>";
93
}
94

    
95
void ReportException(v8::TryCatch* try_catch) {
96
  v8::HandleScope handle_scope;
97
  v8::String::Utf8Value exception(try_catch->Exception());
98
  const char* exception_string = ToCString(exception);
99
  v8::Handle<v8::Message> message = try_catch->Message();
100
  if (message.IsEmpty()) {
101
    // V8 didn't provide any extra information about this error; just
102
    // print the exception.
103
    printf("%s\n", exception_string);
104
  } else {
105
    message->PrintCurrentStackTrace(stdout);
106

    
107
    // Print (filename):(line number): (message).
108
    v8::String::Utf8Value filename(message->GetScriptResourceName());
109
    const char* filename_string = ToCString(filename);
110
    int linenum = message->GetLineNumber();
111
    printf("%s:%i: %s\n", filename_string, linenum, exception_string);
112
    // Print line of source code.
113
    v8::String::Utf8Value sourceline(message->GetSourceLine());
114
    const char* sourceline_string = ToCString(sourceline);
115
    printf("%s\n", sourceline_string);
116
    // Print wavy underline (GetUnderline is deprecated).
117
    int start = message->GetStartColumn();
118
    for (int i = 0; i < start; i++) {
119
      printf(" ");
120
    }
121
    int end = message->GetEndColumn();
122
    for (int i = start; i < end; i++) {
123
      printf("^");
124
    }
125
    printf("\n");
126
  }
127
}
128

    
129
void
130
node_fatal_exception (TryCatch &try_catch)
131
{
132
  ReportException(&try_catch);
133
  ev_unloop(node_loop(), EVUNLOOP_ALL);
134
  exit_code = 1;
135
}
136

    
137

    
138
// Executes a string within the current v8 context.
139
bool ExecuteString(v8::Handle<v8::String> source,
140
                   v8::Handle<v8::Value> name,
141
                   bool print_result,
142
                   bool report_exceptions) {
143
  v8::HandleScope handle_scope;
144
  v8::TryCatch try_catch;
145
  v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
146
  if (script.IsEmpty()) {
147
    // Print errors that happened during compilation.
148
    if (report_exceptions)
149
      ReportException(&try_catch);
150
    return false;
151
  } else {
152
    v8::Handle<v8::Value> result = script->Run();
153
    if (result.IsEmpty()) {
154
      // Print errors that happened during execution.
155
      if (report_exceptions)
156
        ReportException(&try_catch);
157
      return false;
158
    } else {
159
      if (print_result && !result->IsUndefined()) {
160
        // If all went well and the result wasn't undefined then print
161
        // the returned value.
162
        v8::String::Utf8Value str(result);
163
        const char* cstr = ToCString(str);
164
        printf("%s\n", cstr);
165
      }
166
      return true;
167
    }
168
  }
169
}
170

    
171
// The callback that is invoked by v8 whenever the JavaScript 'load'
172
// function is called.  Loads, compiles and executes its argument
173
// JavaScript file.
174
v8::Handle<v8::Value> Load(const v8::Arguments& args) {
175
  for (int i = 0; i < args.Length(); i++) {
176
    v8::HandleScope handle_scope;
177
    v8::String::Utf8Value file(args[i]);
178
    if (*file == NULL) {
179
      return v8::ThrowException(v8::String::New("Error loading file"));
180
    }
181
    v8::Handle<v8::String> source = ReadFile(*file);
182
    if (source.IsEmpty()) {
183
      return v8::ThrowException(v8::String::New("Error loading file"));
184
    }
185
    if (!ExecuteString(source, v8::String::New(*file), false, true)) {
186
      return v8::ThrowException(v8::String::New("Error executing file"));
187
    }
188
  }
189
  return v8::Undefined();
190
}
191

    
192
int
193
main (int argc, char *argv[]) 
194
{
195
  V8::SetFlagsFromCommandLine(&argc, argv, true);
196

    
197
  if(argc != 2)  {
198
    fprintf(stderr, "No script was specified.\n");
199
    return 1;
200
  }
201
  string filename(argv[1]);
202

    
203
  HandleScope handle_scope;
204

    
205
  Persistent<Context> context = Context::New(NULL, ObjectTemplate::New());
206
  Context::Scope context_scope(context);
207

    
208
  Local<Object> g = Context::GetCurrent()->Global();
209

    
210
  g->Set ( String::New("log")
211
         , FunctionTemplate::New(Log)->GetFunction()
212
         );
213

    
214
  g->Set ( String::New("load")
215
         , FunctionTemplate::New(Load)->GetFunction()
216
         );
217

    
218
  g->Set ( String::New("blockingFileRead")
219
         , FunctionTemplate::New(BlockingFileRead)->GetFunction()
220
         );
221

    
222
  Init_timer(g);
223
  Init_tcp(g);
224
  Init_http(g);
225

    
226
  V8::SetFatalErrorHandler(OnFatalError);
227

    
228
  v8::Handle<v8::String> source = ReadFile(filename);
229
  ExecuteString(source, String::New(filename.c_str()), false, true);
230

    
231
  ev_loop(node_loop(), 0);
232

    
233
  context.Dispose();
234

    
235
  return exit_code;
236
}
237