Skip to content

Commit

Permalink
Create a node namespace
Browse files Browse the repository at this point in the history
Part of general reorganization.
  • Loading branch information
ry committed Apr 28, 2009
1 parent 90fc8d3 commit cf1c580
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 50 deletions.
17 changes: 9 additions & 8 deletions src/file.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "node.h"
#include "file.h"
#include <string.h>

#include <sys/types.h>
Expand Down Expand Up @@ -75,7 +76,7 @@ CallTopCallback (Handle<Object> handle, const int argc, Handle<Value> argv[])
TryCatch try_catch;
callback->Call(handle, argc, argv);
if(try_catch.HasCaught()) {
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);
return;
}
}
Expand Down Expand Up @@ -106,7 +107,7 @@ FileSystem::Rename (const Arguments& args)
String::Utf8Value path(args[0]->ToString());
String::Utf8Value new_path(args[1]->ToString());

node_eio_warmup();
node::eio_warmup();
eio_req *req = eio_rename(*path, *new_path, EIO_PRI_DEFAULT, AfterRename, NULL);

return Undefined();
Expand All @@ -133,7 +134,7 @@ FileSystem::Stat (const Arguments& args)

String::Utf8Value path(args[0]->ToString());

node_eio_warmup();
node::eio_warmup();
eio_req *req = eio_stat(*path, EIO_PRI_DEFAULT, AfterStat, NULL);

return Undefined();
Expand Down Expand Up @@ -263,7 +264,7 @@ File::Close (const Arguments& args)

int fd = file->GetFD();

node_eio_warmup();
node::eio_warmup();
eio_req *req = eio_close (fd, EIO_PRI_DEFAULT, File::AfterClose, file);

return Undefined();
Expand Down Expand Up @@ -324,7 +325,7 @@ File::Open (const Arguments& args)
}

// TODO how should the mode be set?
node_eio_warmup();
node::eio_warmup();
eio_req *req = eio_open (*path, flags, 0666, EIO_PRI_DEFAULT, File::AfterOpen, file);

return Undefined();
Expand Down Expand Up @@ -392,7 +393,7 @@ File::Write (const Arguments& args)

int fd = file->GetFD();

node_eio_warmup();
node::eio_warmup();
eio_req *req = eio_write(fd, buf, length, pos, EIO_PRI_DEFAULT, File::AfterWrite, file);

return Undefined();
Expand Down Expand Up @@ -433,7 +434,7 @@ File::Read (const Arguments& args)
int fd = file->GetFD();

// NOTE: NULL pointer tells eio to allocate it itself
node_eio_warmup();
node::eio_warmup();
eio_req *req = eio_read(fd, NULL, length, pos, EIO_PRI_DEFAULT, File::AfterRead, file);
assert(req);

Expand Down Expand Up @@ -485,7 +486,7 @@ File::New(const Arguments& args)
}

void
NodeInit_file (Handle<Object> target)
node::Init_file (Handle<Object> target)
{
if (!fs.IsEmpty())
return;
Expand Down
7 changes: 5 additions & 2 deletions src/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <v8.h>

void NodeInit_file (v8::Handle<v8::Object> target);
namespace node {

#endif
void Init_file (v8::Handle<v8::Object> target);

} // namespace node
#endif // node_file_h
6 changes: 3 additions & 3 deletions src/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ on_headers_complete (ebb_request *req)
Handle<Value> r = request->connection.js_onrequest->Call(Context::GetCurrent()->Global(), argc, argv);

if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);
}

static void
Expand Down Expand Up @@ -373,7 +373,7 @@ HttpRequest::MakeBodyCallback (const char *base, size_t length)
Handle<Value> result = onbody->Call(js_object, argc, argv);

if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);
}

Local<Object>
Expand Down Expand Up @@ -651,7 +651,7 @@ newHTTPHttpServer (const Arguments& args)
}

void
NodeInit_http (Handle<Object> target)
node::Init_http (Handle<Object> target)
{
HandleScope scope;

Expand Down
5 changes: 4 additions & 1 deletion src/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <v8.h>

void NodeInit_http (v8::Handle<v8::Object> target);
namespace node {

void Init_http (v8::Handle<v8::Object> target);

} // namespace node
#endif
18 changes: 9 additions & 9 deletions src/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ Socket::ConnectTCP (const Arguments& args)
* In the future I will move to a system using adns or udns:
* http://lists.schmorp.de/pipermail/libev/2009q1/000632.html
*/
node_eio_warmup();
node::eio_warmup();
eio_req *req = eio_custom (Socket::Resolve, EIO_PRI_DEFAULT, Socket::AfterResolve, socket);

return Undefined();
Expand Down Expand Up @@ -365,7 +365,7 @@ Socket::AfterResolve (eio_req *req)

onconnect->Call(socket->handle_, argc, argv);
if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);

return 0;
}
Expand Down Expand Up @@ -488,7 +488,7 @@ Socket::OnConnect (oi_socket *s)
Handle<Value> r = on_connect->Call(socket->handle_, argc, argv);

if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);
}

void
Expand Down Expand Up @@ -527,7 +527,7 @@ Socket::OnRead (oi_socket *s, const void *buf, size_t count)
Handle<Value> r = onread->Call(socket->handle_, argc, argv);

if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);
}

void
Expand All @@ -548,7 +548,7 @@ Socket::OnClose (oi_socket *s)
Handle<Value> r = onclose->Call(socket->handle_, 0, NULL);

if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);

delete socket;
}
Expand All @@ -568,7 +568,7 @@ Socket::OnDrain (oi_socket *s)
Handle<Value> r = ondrain->Call(socket->handle_, 0, NULL);

if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);
}


Expand All @@ -587,7 +587,7 @@ Socket::OnError (oi_socket *s, oi_error e)
Handle<Value> r = onerror->Call(socket->handle_, 0, NULL);

if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);
}

void
Expand All @@ -605,11 +605,11 @@ Socket::OnTimeout (oi_socket *s)
Handle<Value> r = ontimeout->Call(socket->handle_, 0, NULL);

if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);
}

void
NodeInit_net (Handle<Object> target)
node::Init_net (Handle<Object> target)
{
HandleScope scope;

Expand Down
5 changes: 4 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <v8.h>

void NodeInit_net (v8::Handle<v8::Object> target);
namespace node {

void Init_net (v8::Handle<v8::Object> target);

} // namespace node
#endif
29 changes: 15 additions & 14 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ ExecuteString(v8::Handle<v8::String> source,
Handle<Script> script = Script::Compile(source, filename);
if (script.IsEmpty()) {
ReportException(&try_catch);
exit(1);
::exit(1);
}

Handle<Value> result = script->Run();
if (result.IsEmpty()) {
ReportException(&try_catch);
exit(1);
::exit(1);
}

return scope.Close(result);
Expand Down Expand Up @@ -121,19 +121,20 @@ OnFatalError (const char* location, const char* message)
else
fprintf(stderr, FATAL_ERROR " %s\n", message);

exit(1);
::exit(1);
}


void
node_fatal_exception (TryCatch &try_catch)
node::fatal_exception (TryCatch &try_catch)
{
ReportException(&try_catch);
ev_unloop(EV_DEFAULT_UC_ EVUNLOOP_ALL);
exit_code = 1;
}

void node_exit (int code)
void
node::exit (int code)
{
exit_code = code;
ev_unloop(EV_DEFAULT_UC_ EVUNLOOP_ALL);
Expand All @@ -152,19 +153,19 @@ thread_pool_cb (EV_P_ ev_async *w, int revents)
// it require three locks in eio
// what's the better way?
if (eio_nreqs () == 0 && eio_nready() == 0 && eio_npending() == 0)
ev_async_stop(EV_DEFAULT_ w);
ev_async_stop(EV_DEFAULT_UC_ w);
}

static void
thread_pool_want_poll (void)
{
ev_async_send(EV_DEFAULT_ &thread_pool_watcher);
ev_async_send(EV_DEFAULT_UC_ &thread_pool_watcher);
}

void
node_eio_warmup (void)
node::eio_warmup (void)
{
ev_async_start(EV_DEFAULT_ &thread_pool_watcher);
ev_async_start(EV_DEFAULT_UC_ &thread_pool_watcher);
}

int
Expand Down Expand Up @@ -207,11 +208,11 @@ main (int argc, char *argv[])
g->Set(String::New("ARGV"), arguments);

// BUILT-IN MODULES
NodeInit_net(g);
NodeInit_timers(g);
NodeInit_process(g);
NodeInit_file(g);
NodeInit_http(g);
node::Init_net(g);
node::Init_timers(g);
node::Init_process(g);
node::Init_file(g);
node::Init_http(g);

// NATIVE JAVASCRIPT MODULES
TryCatch try_catch;
Expand Down
12 changes: 6 additions & 6 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
#include <eio.h>
#include <v8.h>

namespace node {

#define NODE_SYMBOL(name) v8::String::NewSymbol(name)
#define NODE_METHOD(name) v8::Handle<v8::Value> name (const v8::Arguments& args)
#define NODE_SET_METHOD(obj, name, callback) \
obj->Set(NODE_SYMBOL(name), v8::FunctionTemplate::New(callback)->GetFunction())

enum encoding {UTF8, RAW};
void fatal_exception (v8::TryCatch &try_catch);
void exit (int code);
void eio_warmup (void); // call this before creating a new eio event.

void node_fatal_exception (v8::TryCatch &try_catch);
void node_exit (int code);

// call this after creating a new eio event.
void node_eio_warmup (void);

} // namespace node
#endif // node_h

2 changes: 1 addition & 1 deletion src/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ OnCallback (const Arguments& args)
}

void
NodeInit_process (Handle<Object> target)
node::Init_process (Handle<Object> target)
{
HandleScope scope;

Expand Down
5 changes: 4 additions & 1 deletion src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <v8.h>

void NodeInit_process (v8::Handle<v8::Object> target);
namespace node {

void Init_process (v8::Handle<v8::Object> target);

} // namespace node
#endif
4 changes: 2 additions & 2 deletions src/timers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents)
TryCatch try_catch;
callback->Call (Context::GetCurrent()->Global(), 0, NULL);
if(try_catch.HasCaught())
node_fatal_exception(try_catch);
node::fatal_exception(try_catch);

// use ev_is_active instead?
if(watcher->repeat == 0.)
Expand Down Expand Up @@ -132,7 +132,7 @@ Timer::clearTimeout (const Arguments& args)
}

void
NodeInit_timers (Handle<Object> target)
node::Init_timers (Handle<Object> target)
{
HandleScope scope;

Expand Down
5 changes: 4 additions & 1 deletion src/timers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <v8.h>

void NodeInit_timers (v8::Handle<v8::Object> target);
namespace node {

void Init_timers (v8::Handle<v8::Object> target);

} // namespace node
#endif // node_timers_h
2 changes: 1 addition & 1 deletion test/test-pingpong.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include("mjsunit");
var N = 1000;
var N = 100;
function onLoad() {
server = new Server(1024);
var count = 0;
Expand Down

0 comments on commit cf1c580

Please sign in to comment.