From b260a9108b5f5a1809b89573350ce93d55099c83 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 26 May 2009 19:48:49 +0200 Subject: [PATCH] Add errno constants. Move all constants to `node.constants` namespace. --- src/constants.cc | 449 +++++++++++++++++++++++++++++++++++++++++++++ src/constants.h | 11 ++ src/file.cc | 47 ----- src/file.js | 24 +-- src/node.cc | 5 + website/index.html | 7 +- wscript | 1 + 7 files changed, 482 insertions(+), 62 deletions(-) create mode 100644 src/constants.cc create mode 100644 src/constants.h diff --git a/src/constants.cc b/src/constants.cc new file mode 100644 index 0000000000..58e5b48446 --- /dev/null +++ b/src/constants.cc @@ -0,0 +1,449 @@ +#include "node.h" +#include "constants.h" + +#include +#include +#include + +using namespace v8; +using namespace node; + +void +node::DefineConstants (Handle target) +{ + NODE_DEFINE_CONSTANT(target, RAW); + NODE_DEFINE_CONSTANT(target, UTF8); + NODE_DEFINE_CONSTANT(target, ASCII); + + NODE_DEFINE_CONSTANT(target, STDIN_FILENO); + NODE_DEFINE_CONSTANT(target, STDOUT_FILENO); + NODE_DEFINE_CONSTANT(target, STDERR_FILENO); + + // file access modes + NODE_DEFINE_CONSTANT(target, O_RDONLY); + NODE_DEFINE_CONSTANT(target, O_WRONLY); + NODE_DEFINE_CONSTANT(target, O_RDWR); + +#ifdef O_CREAT + NODE_DEFINE_CONSTANT(target, O_CREAT); +#endif + +#ifdef O_EXCL + NODE_DEFINE_CONSTANT(target, O_EXCL); +#endif + +#ifdef O_NOCTTY + NODE_DEFINE_CONSTANT(target, O_NOCTTY); +#endif + +#ifdef O_TRUNC + NODE_DEFINE_CONSTANT(target, O_TRUNC); +#endif + +#ifdef O_APPEND + NODE_DEFINE_CONSTANT(target, O_APPEND); +#endif + +#ifdef O_CLOEXEC + NODE_DEFINE_CONSTANT(target, O_CLOEXEC); +#endif + +#ifdef O_DIRECT + NODE_DEFINE_CONSTANT(target, O_DIRECT); +#endif + +#ifdef O_DIRECTORY + NODE_DEFINE_CONSTANT(target, O_DIRECTORY); +#endif + +#ifdef O_EXCL + NODE_DEFINE_CONSTANT(target, O_EXCL); +#endif + +#ifdef O_LARGEFILE + NODE_DEFINE_CONSTANT(target, O_LARGEFILE); +#endif + +#ifdef O_NOATIME + NODE_DEFINE_CONSTANT(target, O_NOATIME); +#endif + +#ifdef O_NOFOLLOW + NODE_DEFINE_CONSTANT(target, O_NOFOLLOW); +#endif + +#ifdef O_SYNC + NODE_DEFINE_CONSTANT(target, O_SYNC); +#endif + +#ifdef S_IRWXU + NODE_DEFINE_CONSTANT(target, S_IRWXU); +#endif + + +#ifdef S_IRUSR + NODE_DEFINE_CONSTANT(target, S_IRUSR); +#endif + +#ifdef S_IWUSR + NODE_DEFINE_CONSTANT(target, S_IWUSR); +#endif + +#ifdef S_IXUSR + NODE_DEFINE_CONSTANT(target, S_IXUSR); +#endif + + +#ifdef S_IRWXG + NODE_DEFINE_CONSTANT(target, S_IRWXG); +#endif + + +#ifdef S_IRGRP + NODE_DEFINE_CONSTANT(target, S_IRGRP); +#endif + +#ifdef S_IWGRP + NODE_DEFINE_CONSTANT(target, S_IWGRP); +#endif + +#ifdef S_IXGRP + NODE_DEFINE_CONSTANT(target, S_IXGRP); +#endif + + +#ifdef S_IRWXO + NODE_DEFINE_CONSTANT(target, S_IRWXO); +#endif + + +#ifdef S_IROTH + NODE_DEFINE_CONSTANT(target, S_IROTH); +#endif + +#ifdef S_IWOTH + NODE_DEFINE_CONSTANT(target, S_IWOTH); +#endif + +#ifdef S_IXOTH + NODE_DEFINE_CONSTANT(target, S_IXOTH); +#endif + +#ifdef E2BIG + NODE_DEFINE_CONSTANT(target, E2BIG); +#endif + +#ifdef EACCES + NODE_DEFINE_CONSTANT(target, EACCES); +#endif + +#ifdef EADDRINUSE + NODE_DEFINE_CONSTANT(target, EADDRINUSE); +#endif + +#ifdef EADDRNOTAVAIL + NODE_DEFINE_CONSTANT(target, EADDRNOTAVAIL); +#endif + +#ifdef EAFNOSUPPORT + NODE_DEFINE_CONSTANT(target, EAFNOSUPPORT); +#endif + +#ifdef EAGAIN + NODE_DEFINE_CONSTANT(target, EAGAIN); +#endif + +#ifdef EALREADY + NODE_DEFINE_CONSTANT(target, EALREADY); +#endif + +#ifdef EBADF + NODE_DEFINE_CONSTANT(target, EBADF); +#endif + +#ifdef EBADMSG + NODE_DEFINE_CONSTANT(target, EBADMSG); +#endif + +#ifdef EBUSY + NODE_DEFINE_CONSTANT(target, EBUSY); +#endif + +#ifdef ECANCELED + NODE_DEFINE_CONSTANT(target, ECANCELED); +#endif + +#ifdef ECHILD + NODE_DEFINE_CONSTANT(target, ECHILD); +#endif + +#ifdef ECONNABORTED + NODE_DEFINE_CONSTANT(target, ECONNABORTED); +#endif + +#ifdef ECONNREFUSED + NODE_DEFINE_CONSTANT(target, ECONNREFUSED); +#endif + +#ifdef ECONNRESET + NODE_DEFINE_CONSTANT(target, ECONNRESET); +#endif + +#ifdef EDEADLK + NODE_DEFINE_CONSTANT(target, EDEADLK); +#endif + +#ifdef EDESTADDRREQ + NODE_DEFINE_CONSTANT(target, EDESTADDRREQ); +#endif + +#ifdef EDOM + NODE_DEFINE_CONSTANT(target, EDOM); +#endif + +#ifdef EDQUOT + NODE_DEFINE_CONSTANT(target, EDQUOT); +#endif + +#ifdef EEXIST + NODE_DEFINE_CONSTANT(target, EEXIST); +#endif + +#ifdef EFAULT + NODE_DEFINE_CONSTANT(target, EFAULT); +#endif + +#ifdef EFBIG + NODE_DEFINE_CONSTANT(target, EFBIG); +#endif + +#ifdef EHOSTUNREACH + NODE_DEFINE_CONSTANT(target, EHOSTUNREACH); +#endif + +#ifdef EIDRM + NODE_DEFINE_CONSTANT(target, EIDRM); +#endif + +#ifdef EILSEQ + NODE_DEFINE_CONSTANT(target, EILSEQ); +#endif + +#ifdef EINPROGRESS + NODE_DEFINE_CONSTANT(target, EINPROGRESS); +#endif + +#ifdef EINTR + NODE_DEFINE_CONSTANT(target, EINTR); +#endif + +#ifdef EINVAL + NODE_DEFINE_CONSTANT(target, EINVAL); +#endif + +#ifdef EIO + NODE_DEFINE_CONSTANT(target, EIO); +#endif + +#ifdef EISCONN + NODE_DEFINE_CONSTANT(target, EISCONN); +#endif + +#ifdef EISDIR + NODE_DEFINE_CONSTANT(target, EISDIR); +#endif + +#ifdef ELOOP + NODE_DEFINE_CONSTANT(target, ELOOP); +#endif + +#ifdef EMFILE + NODE_DEFINE_CONSTANT(target, EMFILE); +#endif + +#ifdef EMLINK + NODE_DEFINE_CONSTANT(target, EMLINK); +#endif + +#ifdef EMSGSIZE + NODE_DEFINE_CONSTANT(target, EMSGSIZE); +#endif + +#ifdef EMULTIHOP + NODE_DEFINE_CONSTANT(target, EMULTIHOP); +#endif + +#ifdef ENAMETOOLONG + NODE_DEFINE_CONSTANT(target, ENAMETOOLONG); +#endif + +#ifdef ENETDOWN + NODE_DEFINE_CONSTANT(target, ENETDOWN); +#endif + +#ifdef ENETRESET + NODE_DEFINE_CONSTANT(target, ENETRESET); +#endif + +#ifdef ENETUNREACH + NODE_DEFINE_CONSTANT(target, ENETUNREACH); +#endif + +#ifdef ENFILE + NODE_DEFINE_CONSTANT(target, ENFILE); +#endif + +#ifdef ENOBUFS + NODE_DEFINE_CONSTANT(target, ENOBUFS); +#endif + +#ifdef ENODATA + NODE_DEFINE_CONSTANT(target, ENODATA); +#endif + +#ifdef ENODEV + NODE_DEFINE_CONSTANT(target, ENODEV); +#endif + +#ifdef ENOENT + NODE_DEFINE_CONSTANT(target, ENOENT); +#endif + +#ifdef ENOEXEC + NODE_DEFINE_CONSTANT(target, ENOEXEC); +#endif + +#ifdef ENOLCK + NODE_DEFINE_CONSTANT(target, ENOLCK); +#endif + +#ifdef ENOLINK + NODE_DEFINE_CONSTANT(target, ENOLINK); +#endif + +#ifdef ENOMEM + NODE_DEFINE_CONSTANT(target, ENOMEM); +#endif + +#ifdef ENOMSG + NODE_DEFINE_CONSTANT(target, ENOMSG); +#endif + +#ifdef ENOPROTOOPT + NODE_DEFINE_CONSTANT(target, ENOPROTOOPT); +#endif + +#ifdef ENOSPC + NODE_DEFINE_CONSTANT(target, ENOSPC); +#endif + +#ifdef ENOSR + NODE_DEFINE_CONSTANT(target, ENOSR); +#endif + +#ifdef ENOSTR + NODE_DEFINE_CONSTANT(target, ENOSTR); +#endif + +#ifdef ENOSYS + NODE_DEFINE_CONSTANT(target, ENOSYS); +#endif + +#ifdef ENOTCONN + NODE_DEFINE_CONSTANT(target, ENOTCONN); +#endif + +#ifdef ENOTDIR + NODE_DEFINE_CONSTANT(target, ENOTDIR); +#endif + +#ifdef ENOTEMPTY + NODE_DEFINE_CONSTANT(target, ENOTEMPTY); +#endif + +#ifdef ENOTSOCK + NODE_DEFINE_CONSTANT(target, ENOTSOCK); +#endif + +#ifdef ENOTSUP + NODE_DEFINE_CONSTANT(target, ENOTSUP); +#endif + +#ifdef ENOTTY + NODE_DEFINE_CONSTANT(target, ENOTTY); +#endif + +#ifdef ENXIO + NODE_DEFINE_CONSTANT(target, ENXIO); +#endif + +#ifdef EOPNOTSUPP + NODE_DEFINE_CONSTANT(target, EOPNOTSUPP); +#endif + +#ifdef EOVERFLOW + NODE_DEFINE_CONSTANT(target, EOVERFLOW); +#endif + +#ifdef EPERM + NODE_DEFINE_CONSTANT(target, EPERM); +#endif + +#ifdef EPIPE + NODE_DEFINE_CONSTANT(target, EPIPE); +#endif + +#ifdef EPROTO + NODE_DEFINE_CONSTANT(target, EPROTO); +#endif + +#ifdef EPROTONOSUPPORT + NODE_DEFINE_CONSTANT(target, EPROTONOSUPPORT); +#endif + +#ifdef EPROTOTYPE + NODE_DEFINE_CONSTANT(target, EPROTOTYPE); +#endif + +#ifdef ERANGE + NODE_DEFINE_CONSTANT(target, ERANGE); +#endif + +#ifdef EROFS + NODE_DEFINE_CONSTANT(target, EROFS); +#endif + +#ifdef ESPIPE + NODE_DEFINE_CONSTANT(target, ESPIPE); +#endif + +#ifdef ESRCH + NODE_DEFINE_CONSTANT(target, ESRCH); +#endif + +#ifdef ESTALE + NODE_DEFINE_CONSTANT(target, ESTALE); +#endif + +#ifdef ETIME + NODE_DEFINE_CONSTANT(target, ETIME); +#endif + +#ifdef ETIMEDOUT + NODE_DEFINE_CONSTANT(target, ETIMEDOUT); +#endif + +#ifdef ETXTBSY + NODE_DEFINE_CONSTANT(target, ETXTBSY); +#endif + +#ifdef EWOULDBLOCK + NODE_DEFINE_CONSTANT(target, EWOULDBLOCK); +#endif + +#ifdef EXDEV + NODE_DEFINE_CONSTANT(target, EXDEV); +#endif + +} + diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 0000000000..f11514fe9b --- /dev/null +++ b/src/constants.h @@ -0,0 +1,11 @@ +#ifndef node_constants_h +#define node_constants_h + +#include + +namespace node { + +void DefineConstants(v8::Handle target); + +} // namespace node +#endif // node_constants_h diff --git a/src/file.cc b/src/file.cc index a3130af247..2f7dbeb338 100644 --- a/src/file.cc +++ b/src/file.cc @@ -390,51 +390,4 @@ File::Initialize (Handle target) NODE_SET_METHOD(target, "write", Write); NODE_SET_METHOD(target, "strerror", StrError); - - NODE_DEFINE_CONSTANT(target, RAW); - NODE_DEFINE_CONSTANT(target, UTF8); - - NODE_DEFINE_CONSTANT(target, STDIN_FILENO); - NODE_DEFINE_CONSTANT(target, STDOUT_FILENO); - NODE_DEFINE_CONSTANT(target, STDERR_FILENO); - // file access modes - NODE_DEFINE_CONSTANT(target, O_RDONLY); - NODE_DEFINE_CONSTANT(target, O_WRONLY); - NODE_DEFINE_CONSTANT(target, O_RDWR); - // file creation flags - NODE_DEFINE_CONSTANT(target, O_CREAT); - NODE_DEFINE_CONSTANT(target, O_EXCL); - NODE_DEFINE_CONSTANT(target, O_NOCTTY); - NODE_DEFINE_CONSTANT(target, O_TRUNC); - // file status flags - NODE_DEFINE_CONSTANT(target, O_APPEND); -// NODE_DEFINE_CONSTANT(target, O_ASYNC); - NODE_DEFINE_CONSTANT(target, O_CLOEXEC); - NODE_DEFINE_CONSTANT(target, O_DIRECT); - NODE_DEFINE_CONSTANT(target, O_DIRECTORY); - NODE_DEFINE_CONSTANT(target, O_EXCL); - NODE_DEFINE_CONSTANT(target, O_LARGEFILE); - NODE_DEFINE_CONSTANT(target, O_NOATIME); - NODE_DEFINE_CONSTANT(target, O_NOFOLLOW); -// NODE_DEFINE_CONSTANT(target, O_NONBLOCK); -// NODE_DEFINE_CONSTANT(target, O_SYNC); - NODE_DEFINE_CONSTANT(target, O_SYNC); - - NODE_DEFINE_CONSTANT(target, S_IRWXU); - - NODE_DEFINE_CONSTANT(target, S_IRUSR); - NODE_DEFINE_CONSTANT(target, S_IWUSR); - NODE_DEFINE_CONSTANT(target, S_IXUSR); - - NODE_DEFINE_CONSTANT(target, S_IRWXG); - - NODE_DEFINE_CONSTANT(target, S_IRGRP); - NODE_DEFINE_CONSTANT(target, S_IWGRP); - NODE_DEFINE_CONSTANT(target, S_IXGRP); - - NODE_DEFINE_CONSTANT(target, S_IRWXO); - - NODE_DEFINE_CONSTANT(target, S_IROTH); - NODE_DEFINE_CONSTANT(target, S_IWOTH); - NODE_DEFINE_CONSTANT(target, S_IXOTH); } diff --git a/src/file.js b/src/file.js index 12c95fa7f3..640928e428 100644 --- a/src/file.js +++ b/src/file.js @@ -11,7 +11,7 @@ node.fs.cat = function (path, encoding, callback) { callback(-1); }; - var content = (encoding == node.fs.UTF8 ? "" : []); + var content = (encoding == node.constants.UTF8 ? "" : []); var pos = 0; var chunkSize = 10*1024; @@ -42,9 +42,9 @@ node.fs.File = function (options) { options = options || {}; if (options.encoding === "utf8") { - self.encoding = node.fs.UTF8; + self.encoding = node.constants.UTF8; } else { - self.encoding = node.fs.RAW; + self.encoding = node.constants.RAW; } //node.debug("encoding: opts=" + options.encoding + " self=" + self.encoding); @@ -104,22 +104,22 @@ node.fs.File = function (options) { var flags; switch (mode) { case "r": - flags = node.fs.O_RDONLY; + flags = node.constants.O_RDONLY; break; case "r+": - flags = node.fs.O_RDWR; + flags = node.constants.O_RDWR; break; case "w": - flags = node.fs.O_CREAT | node.fs.O_TRUNC | node.fs.O_WRONLY; + flags = node.constants.O_CREAT | node.constants.O_TRUNC | node.constants.O_WRONLY; break; case "w+": - flags = node.fs.O_CREAT | node.fs.O_TRUNC | node.fs.O_RDWR; + flags = node.constants.O_CREAT | node.constants.O_TRUNC | node.constants.O_RDWR; break; case "a": - flags = node.fs.O_APPEND | node.fs.O_CREAT | node.fs.O_WRONLY; + flags = node.constants.O_APPEND | node.constants.O_CREAT | node.constants.O_WRONLY; break; case "a+": - flags = node.fs.O_APPEND | node.fs.O_CREAT | node.fs.O_RDWR; + flags = node.constants.O_APPEND | node.constants.O_CREAT | node.constants.O_RDWR; break; default: throw "Unknown mode"; @@ -173,9 +173,9 @@ node.fs.File = function (options) { }; }; -stdout = new node.fs.File({ fd: node.fs.STDOUT_FILENO }); -stderr = new node.fs.File({ fd: node.fs.STDERR_FILENO }); -stdin = new node.fs.File({ fd: node.fs.STDIN_FILENO }); +stdout = new node.fs.File({ fd: node.constants.STDOUT_FILENO }); +stderr = new node.fs.File({ fd: node.constants.STDERR_FILENO }); +stdin = new node.fs.File({ fd: node.constants.STDIN_FILENO }); puts = function (data, callback) { stdout.puts(data, callback); diff --git a/src/node.cc b/src/node.cc index b974f00cd6..5ea4e5a0c4 100644 --- a/src/node.cc +++ b/src/node.cc @@ -4,6 +4,7 @@ #include "file.h" #include "http.h" #include "timer.h" +#include "constants.h" #include "natives.h" @@ -309,6 +310,10 @@ main (int argc, char *argv[]) // BUILT-IN MODULES Timer::Initialize(node); + Local constants = Object::New(); + node->Set(String::New("constants"), constants); + DefineConstants(constants); + Local fs = Object::New(); node->Set(String::New("fs"), fs); File::Initialize(fs); diff --git a/website/index.html b/website/index.html index 704218db1f..d80d5b5021 100644 --- a/website/index.html +++ b/website/index.html @@ -300,7 +300,8 @@ node.fs.rename("/tmp/hello", "/tmp/world", function (status) {
node.fs.open(path, flags, mode, on_completion(status, fd))
open(2) -

The constants like O_CREAT are defined at node.fs.O_CREAT. +

The constants like O_CREAT are defined at + node.constants.O_CREAT.

node.fs.write(fd, data, position, on_completion(status, written))
@@ -323,8 +324,8 @@ node.fs.rename("/tmp/hello", "/tmp/world", function (status) {

position is an integer specifying where to begin reading from in the file. -

encoding is either node.fs.UTF8 or - node.fs.RAW. +

encoding is either node.constants.UTF8 or + node.constants.RAW. diff --git a/wscript b/wscript index f92b37a312..5938bdcdbf 100644 --- a/wscript +++ b/wscript @@ -147,6 +147,7 @@ def build(bld): src/net.cc src/file.cc src/timer.cc + src/constants.cc """ node.includes = """ src/