From eb105536341a252c4997ce40ac336c8ecdb132ba Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 16 Jul 2009 17:19:02 +0200 Subject: [PATCH] Move node.inherit, node.path, node.cat to new file: util.js --- src/http.js | 31 ++++--------------------- src/node.cc | 1 + src/node.js | 46 ------------------------------------- src/util.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ wscript | 1 + 5 files changed, 72 insertions(+), 73 deletions(-) create mode 100644 src/util.js diff --git a/src/http.js b/src/http.js index 138e461ead..adf4ac9d1d 100644 --- a/src/http.js +++ b/src/http.js @@ -1,27 +1,4 @@ (function () { - -/** - * 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 - */ -function inherits(ctor, superCtor) { - var tempCtor = function(){}; - tempCtor.prototype = superCtor.prototype; - ctor.super_ = superCtor.prototype; - ctor.prototype = new tempCtor(); - ctor.prototype.constructor = ctor; -} - - CRLF = "\r\n"; node.http.STATUS_CODES = { 100 : 'Continue', @@ -150,7 +127,7 @@ function IncomingMessage (connection) { this.statusCode = null; this.client = this.connection; } -inherits(IncomingMessage, node.EventEmitter); +node.inherits(IncomingMessage, node.EventEmitter); IncomingMessage.prototype.setBodyEncoding = function (enc) { // TODO: Find a cleaner way of doing this. @@ -174,7 +151,7 @@ function OutgoingMessage () { this.finished = false; } -inherits(OutgoingMessage, node.EventEmitter); +node.inherits(OutgoingMessage, node.EventEmitter); OutgoingMessage.prototype.send = function (data, encoding) { data.encoding = data.constructor === String ? encoding || "ascii" : "raw"; @@ -261,7 +238,7 @@ function ServerResponse () { this.should_keep_alive = true; this.use_chunked_encoding_by_default = true; } -inherits(ServerResponse, OutgoingMessage); +node.inherits(ServerResponse, OutgoingMessage); ServerResponse.prototype.sendHeader = function (statusCode, headers) { var reason = node.http.STATUS_CODES[statusCode] || "unknown"; @@ -279,7 +256,7 @@ function ClientRequest (method, uri, header_lines) { this.sendHeaderLines(method + " " + uri + " HTTP/1.1\r\n", header_lines); } -inherits(ClientRequest, OutgoingMessage); +node.inherits(ClientRequest, OutgoingMessage); ClientRequest.prototype.finish = function (responseListener) { this.addListener("response", responseListener); diff --git a/src/node.cc b/src/node.cc index f5306619af..dd05fb9a30 100644 --- a/src/node.cc +++ b/src/node.cc @@ -261,6 +261,7 @@ Load (int argc, char *argv[]) HTTPServer::Initialize(http); HTTPConnection::Initialize(http); + ExecuteNativeJS("util.js", native_util); ExecuteNativeJS("events.js", native_events); ExecuteNativeJS("http.js", native_http); ExecuteNativeJS("file.js", native_file); diff --git a/src/node.js b/src/node.js index f672ce17eb..e9720fb1a5 100644 --- a/src/node.js +++ b/src/node.js @@ -40,52 +40,6 @@ function clearTimeout (timer) { clearInterval = clearTimeout; -// This is useful for dealing with raw encodings. -Array.prototype.encodeUtf8 = function () { - return String.fromCharCode.apply(String, this); -}; - -node.path = new function () { - this.join = function () { - var joined = ""; - for (var i = 0; i < arguments.length; i++) { - var part = arguments[i].toString(); - - /* Some logic to shorten paths */ - if (part === ".") continue; - while (/^\.\//.exec(part)) part = part.replace(/^\.\//, ""); - - if (i === 0) { - part = part.replace(/\/*$/, "/"); - } else if (i === arguments.length - 1) { - part = part.replace(/^\/*/, ""); - } else { - part = part.replace(/^\/*/, "").replace(/\/*$/, "/"); - } - joined += part; - } - return joined; - }; - - this.dirname = function (path) { - if (path.charAt(0) !== "/") path = "./" + path; - var parts = path.split("/"); - return parts.slice(0, parts.length-1).join("/"); - }; - - this.filename = function (path) { - if (path.charAt(0) !== "/") path = "./" + path; - var parts = path.split("/"); - return parts[parts.length-1]; - }; -}; - -node.cat = function(location, encoding, callback) { - var url_re = new RegExp("^http:\/\/"); - var f = url_re.exec(location) ? node.http.cat : node.fs.cat; - return f(location, encoding, callback); -}; - // Module node.Module = function (o) { diff --git a/src/util.js b/src/util.js new file mode 100644 index 0000000000..0c336920f0 --- /dev/null +++ b/src/util.js @@ -0,0 +1,66 @@ +/** + * 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 + */ +node.inherits = function (ctor, superCtor) { + var tempCtor = function(){}; + tempCtor.prototype = superCtor.prototype; + ctor.super_ = superCtor.prototype; + ctor.prototype = new tempCtor(); + ctor.prototype.constructor = ctor; +}; + +// This is useful for dealing with raw encodings. +Array.prototype.encodeUtf8 = function () { + return String.fromCharCode.apply(String, this); +}; + +node.cat = function(location, encoding, callback) { + var url_re = new RegExp("^http:\/\/"); + var f = url_re.exec(location) ? node.http.cat : node.fs.cat; + return f(location, encoding, callback); +}; + +node.path = new function () { + this.join = function () { + var joined = ""; + for (var i = 0; i < arguments.length; i++) { + var part = arguments[i].toString(); + + /* Some logic to shorten paths */ + if (part === ".") continue; + while (/^\.\//.exec(part)) part = part.replace(/^\.\//, ""); + + if (i === 0) { + part = part.replace(/\/*$/, "/"); + } else if (i === arguments.length - 1) { + part = part.replace(/^\/*/, ""); + } else { + part = part.replace(/^\/*/, "").replace(/\/*$/, "/"); + } + joined += part; + } + return joined; + }; + + this.dirname = function (path) { + if (path.charAt(0) !== "/") path = "./" + path; + var parts = path.split("/"); + return parts.slice(0, parts.length-1).join("/"); + }; + + this.filename = function (path) { + if (path.charAt(0) !== "/") path = "./" + path; + var parts = path.split("/"); + return parts[parts.length-1]; + }; +}; diff --git a/wscript b/wscript index b04b56527d..45ddf0adf4 100644 --- a/wscript +++ b/wscript @@ -142,6 +142,7 @@ def build(bld): native_cc = bld.new_task_gen( source = """ + src/util.js src/events.js src/http.js src/file.js