From d737a060c841687907604d1a0885b11b29a1a8e4 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 7 Nov 2009 14:37:22 +0100 Subject: [PATCH] Combine all compiled javascript files into src/node.js --- src/events.js | 113 ------------------------------- src/file.js | 34 ---------- src/node.cc | 3 - src/node.js | 182 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.js | 37 ---------- wscript | 7 +- 6 files changed, 183 insertions(+), 193 deletions(-) delete mode 100644 src/events.js delete mode 100644 src/file.js delete mode 100644 src/util.js diff --git a/src/events.js b/src/events.js deleted file mode 100644 index 58c3538798..0000000000 --- a/src/events.js +++ /dev/null @@ -1,113 +0,0 @@ -(function () { - -// process.EventEmitter is defined in src/events.cc -// process.EventEmitter.prototype.emit() is also defined there. -process.EventEmitter.prototype.addListener = function (type, listener) { - if (listener instanceof Function) { - if (!this._events) this._events = {}; - if (!this._events.hasOwnProperty(type)) this._events[type] = []; - // To avoid recursion in the case that type == "newListeners"! Before - // adding it to the listeners, first emit "newListeners". - this.emit("newListener", type, listener); - this._events[type].push(listener); - } - return this; -}; - -process.EventEmitter.prototype.listeners = function (type) { - if (!this._events) this._events = {}; - if (!this._events.hasOwnProperty(type)) this._events[type] = []; - return this._events[type]; -}; - -process.Promise.prototype.timeout = function(timeout) { - if (timeout === undefined) { - return this._timeoutDuration; - } - - this._timeoutDuration = timeout; - if (this._timer) { - clearTimeout(this._timer); - } - - var promiseComplete = false; - var onComplete = function() { - promiseComplete = true; - }; - - this - .addCallback(onComplete) - .addCancelback(onComplete) - .addErrback(onComplete); - - var self = this - this._timer = setTimeout(function() { - if (promiseComplete) { - return; - } - - self.emitError(new Error('timeout')); - }, this._timeoutDuration); - - return this; -}; - -process.Promise.prototype.cancel = function() { - this._events['success'] = []; - this._events['error'] = []; - - this.emitCancel(); -}; - -process.Promise.prototype.emitCancel = function() { - var args = Array.prototype.slice.call(arguments); - args.unshift('cancel'); - - this.emit.apply(this, args); -}; - -process.Promise.prototype.addCallback = function (listener) { - this.addListener("success", listener); - return this; -}; - -process.Promise.prototype.addErrback = function (listener) { - this.addListener("error", listener); - return this; -}; - -process.Promise.prototype.addCancelback = function (listener) { - this.addListener("cancel", listener); - return this; -}; - -process.Promise.prototype.wait = function () { - var ret; - var had_error = false; - this.addCallback(function () { - if (arguments.length == 1) { - ret = arguments[0]; - } else if (arguments.length > 1) { - ret = []; - for (var i = 0; i < arguments.length; i++) { - ret.push(arguments[i]); - } - } - }) - .addErrback(function (arg) { - had_error = true; - ret = arg; - }) - .block(); - - if (had_error) { - if (ret) { - throw ret; - } else { - throw new Error("Promise completed with error (No arguments given.)"); - } - } - return ret; -}; - -})(); // end anonymous namespace diff --git a/src/file.js b/src/file.js deleted file mode 100644 index 56a71ca22b..0000000000 --- a/src/file.js +++ /dev/null @@ -1,34 +0,0 @@ - -process.fs.cat = function (path, encoding) { - var promise = new process.Promise(); - - encoding = encoding || "utf8"; // default to utf8 - - process.fs.open(path, process.O_RDONLY, 0666).addCallback(function (fd) { - var content = "", pos = 0; - - function readChunk () { - process.fs.read(fd, 16*1024, pos, encoding).addCallback(function (chunk, bytes_read) { - if (chunk) { - if (chunk.constructor === String) { - content += chunk; - } else { - content = content.concat(chunk); - } - - pos += bytes_read; - readChunk(); - } else { - promise.emitSuccess(content); - process.fs.close(fd); - } - }).addErrback(function () { - promise.emitError(); - }); - } - readChunk(); - }).addErrback(function () { - promise.emitError(new Error("Could not open " + path)); - }); - return promise; -}; diff --git a/src/node.cc b/src/node.cc index ef55ef1c53..b63bbafcdb 100644 --- a/src/node.cc +++ b/src/node.cc @@ -664,9 +664,6 @@ static Local Load(int argc, char *argv[]) { // Compile, execute the src/*.js files. (Which were included a static C // strings in node_natives.h) - ExecuteNativeJS("util.js", native_util); - ExecuteNativeJS("events.js", native_events); - ExecuteNativeJS("file.js", native_file); // In node.js we actually load the file specified in ARGV[1] // so your next reading stop should be node.js! ExecuteNativeJS("node.js", native_node); diff --git a/src/node.js b/src/node.js index 5891551aec..e17725343c 100644 --- a/src/node.js +++ b/src/node.js @@ -86,6 +86,75 @@ process.createChildProcess = function (file, args, env) { return child; }; +process.fs.cat = function (path, encoding) { + var promise = new process.Promise(); + + encoding = encoding || "utf8"; // default to utf8 + + process.fs.open(path, process.O_RDONLY, 0666).addCallback(function (fd) { + var content = "", pos = 0; + + function readChunk () { + process.fs.read(fd, 16*1024, pos, encoding).addCallback(function (chunk, bytes_read) { + if (chunk) { + if (chunk.constructor === String) { + content += chunk; + } else { + content = content.concat(chunk); + } + + pos += bytes_read; + readChunk(); + } else { + promise.emitSuccess(content); + process.fs.close(fd); + } + }).addErrback(function () { + promise.emitError(); + }); + } + readChunk(); + }).addErrback(function () { + promise.emitError(new Error("Could not open " + path)); + }); + return promise; +}; + +/** + * Inherit the prototype methods from one constructor into another. + * + * The Function.prototype.inherits from lang.js rewritten as a standalone + * function (not on Function.prototype). NOTE: If this file is to be loaded + * during bootstrapping this function needs to be revritten using some native + * functions as prototype setup using normal JavaScript does not work as + * expected during bootstrapping (see mirror.js in r114903). + * + * @param {function} ctor Constructor function which needs to inherit the + * prototype + * @param {function} superCtor Constructor function to inherit prototype from + */ +process.inherits = function (ctor, superCtor) { + var tempCtor = function(){}; + tempCtor.prototype = superCtor.prototype; + ctor.super_ = superCtor.prototype; + ctor.prototype = new tempCtor(); + ctor.prototype.constructor = ctor; +}; + +process.assert = function (x, msg) { + if (!(x)) throw new Error(msg || "assertion error"); +}; + +process.cat = function(location, encoding) { + var url_re = new RegExp("^http:\/\/"); + if (url_re.exec(location)) { + throw new Error("process.cat for http urls is temporarally disabled."); + } + //var f = url_re.exec(location) ? process.http.cat : process.fs.cat; + //return f(location, encoding); + return process.fs.cat(location, encoding); +}; + // From jQuery.extend in the jQuery JavaScript Library v1.3.2 // Copyright (c) 2009 John Resig // Dual licensed under the MIT and GPL licenses. @@ -141,6 +210,119 @@ process.mixin = function() { }; +// Event + +// process.EventEmitter is defined in src/events.cc +// process.EventEmitter.prototype.emit() is also defined there. +process.EventEmitter.prototype.addListener = function (type, listener) { + if (listener instanceof Function) { + if (!this._events) this._events = {}; + if (!this._events.hasOwnProperty(type)) this._events[type] = []; + // To avoid recursion in the case that type == "newListeners"! Before + // adding it to the listeners, first emit "newListeners". + this.emit("newListener", type, listener); + this._events[type].push(listener); + } + return this; +}; + +process.EventEmitter.prototype.listeners = function (type) { + if (!this._events) this._events = {}; + if (!this._events.hasOwnProperty(type)) this._events[type] = []; + return this._events[type]; +}; + +process.Promise.prototype.timeout = function(timeout) { + if (timeout === undefined) { + return this._timeoutDuration; + } + + this._timeoutDuration = timeout; + if (this._timer) { + clearTimeout(this._timer); + } + + var promiseComplete = false; + var onComplete = function() { + promiseComplete = true; + }; + + this + .addCallback(onComplete) + .addCancelback(onComplete) + .addErrback(onComplete); + + var self = this + this._timer = setTimeout(function() { + if (promiseComplete) { + return; + } + + self.emitError(new Error('timeout')); + }, this._timeoutDuration); + + return this; +}; + +process.Promise.prototype.cancel = function() { + this._events['success'] = []; + this._events['error'] = []; + + this.emitCancel(); +}; + +process.Promise.prototype.emitCancel = function() { + var args = Array.prototype.slice.call(arguments); + args.unshift('cancel'); + + this.emit.apply(this, args); +}; + +process.Promise.prototype.addCallback = function (listener) { + this.addListener("success", listener); + return this; +}; + +process.Promise.prototype.addErrback = function (listener) { + this.addListener("error", listener); + return this; +}; + +process.Promise.prototype.addCancelback = function (listener) { + this.addListener("cancel", listener); + return this; +}; + +process.Promise.prototype.wait = function () { + var ret; + var had_error = false; + this.addCallback(function () { + if (arguments.length == 1) { + ret = arguments[0]; + } else if (arguments.length > 1) { + ret = []; + for (var i = 0; i < arguments.length; i++) { + ret.push(arguments[i]); + } + } + }) + .addErrback(function (arg) { + had_error = true; + ret = arg; + }) + .block(); + + if (had_error) { + if (ret) { + throw ret; + } else { + throw new Error("Promise completed with error (No arguments given.)"); + } + } + return ret; +}; + + // Signal Handlers diff --git a/src/util.js b/src/util.js deleted file mode 100644 index 31da60b499..0000000000 --- a/src/util.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Inherit the prototype methods from one constructor into another. - * - * The Function.prototype.inherits from lang.js rewritten as a standalone - * function (not on Function.prototype). NOTE: If this file is to be loaded - * during bootstrapping this function needs to be revritten using some native - * functions as prototype setup using normal JavaScript does not work as - * expected during bootstrapping (see mirror.js in r114903). - * - * @param {function} ctor Constructor function which needs to inherit the - * prototype - * @param {function} superCtor Constructor function to inherit prototype from - */ -process.inherits = function (ctor, superCtor) { - var tempCtor = function(){}; - tempCtor.prototype = superCtor.prototype; - ctor.super_ = superCtor.prototype; - ctor.prototype = new tempCtor(); - ctor.prototype.constructor = ctor; -}; - -process.assert = function (x, msg) { - if (!(x)) throw new Error(msg || "assertion error"); -}; - -process.cat = function(location, encoding) { - var url_re = new RegExp("^http:\/\/"); - if (url_re.exec(location)) { - throw new Error("process.cat for http urls is temporarally disabled."); - } - //var f = url_re.exec(location) ? process.http.cat : process.fs.cat; - //return f(location, encoding); - return process.fs.cat(location, encoding); -}; - - - diff --git a/wscript b/wscript index b8a4a4efd6..1087d9a002 100644 --- a/wscript +++ b/wscript @@ -292,12 +292,7 @@ def build(bld): js2c.JS2C(source, targets) native_cc = bld.new_task_gen( - source = """ - src/util.js - src/events.js - src/file.js - src/node.js - """, + source='src/node.js', target="src/node_natives.h", before="cxx" )