Browse Source

Combine all compiled javascript files into src/node.js

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
d737a060c8
  1. 113
      src/events.js
  2. 34
      src/file.js
  3. 3
      src/node.cc
  4. 182
      src/node.js
  5. 37
      src/util.js
  6. 7
      wscript

113
src/events.js

@ -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

34
src/file.js

@ -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;
};

3
src/node.cc

@ -664,9 +664,6 @@ static Local<Object> Load(int argc, char *argv[]) {
// Compile, execute the src/*.js files. (Which were included a static C // Compile, execute the src/*.js files. (Which were included a static C
// strings in node_natives.h) // 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] // In node.js we actually load the file specified in ARGV[1]
// so your next reading stop should be node.js! // so your next reading stop should be node.js!
ExecuteNativeJS("node.js", native_node); ExecuteNativeJS("node.js", native_node);

182
src/node.js

@ -86,6 +86,75 @@ process.createChildProcess = function (file, args, env) {
return child; 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 // From jQuery.extend in the jQuery JavaScript Library v1.3.2
// Copyright (c) 2009 John Resig // Copyright (c) 2009 John Resig
// Dual licensed under the MIT and GPL licenses. // 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 // Signal Handlers

37
src/util.js

@ -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);
};

7
wscript

@ -292,12 +292,7 @@ def build(bld):
js2c.JS2C(source, targets) js2c.JS2C(source, targets)
native_cc = bld.new_task_gen( native_cc = bld.new_task_gen(
source = """ source='src/node.js',
src/util.js
src/events.js
src/file.js
src/node.js
""",
target="src/node_natives.h", target="src/node_natives.h",
before="cxx" before="cxx"
) )

Loading…
Cancel
Save