};
+function readAll (fd, pos, content, encoding, callback) {
+ process.fs.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) {
+ if (err) {
+ if (callback) callback(err);
+ } else if (chunk) {
+ content += chunk;
+ pos += bytesRead;
+ readAll(fd, pos, content, encoding, callback);
+ } else {
+ process.fs.close(fd, function (err) {
+ if (callback) callback(err, content);
+ });
+ }
+ });
+}
+
+exports.readFile = function (path, encoding_, callback) {
+ var encoding = typeof(encoding_) == 'string' ? encoding_ : 'utf8';
+ var callback_ = arguments[arguments.length - 1];
+ var callback = (typeof(callback_) == 'function' ? callback_ : null);
+ process.fs.open(path, process.O_RDONLY, 0666, function (err, fd) {
+ if (err) {
+ if (callback) callback(err);
+ } else {
+ readAll(fd, 0, "", encoding, callback);
+ }
+ });
+};
+
+exports.readFileSync = function (path, encoding) {
+ encoding = encoding || "utf8"; // default to utf8
+
+ var fd = process.fs.open(path, process.O_RDONLY, 0666);
+ var content = '';
+ var pos = 0;
+ var r;
+
+ while ((r = process.fs.read(fd, 4*1024, pos, encoding)) && r[0]) {
+ content += r[0];
+ pos += r[1]
+ }
+
+ process.fs.close(fd);
+
+ return content;
+};
-exports.readFile = process.fs.readFile;
-exports.readFileSync = process.fs.readFileSync;
// Used by fs.open and friends
function stringToFlags(flag) {
};
+function requireNative (id) {
+ if (internalModuleCache[id]) return internalModuleCache[id].exports;
+ if (!process.natives[id]) throw new Error('No such native module ' + id);
+ var m = new Module(id);
+ internalModuleCache[id] = m;
+ var e = m._compile(process.natives[id], id);
+ if (e) throw e;
+ return m.exports;
+}
+
+
process.createChildProcess = function (file, args, env) {
var child = new process.ChildProcess();
args = args || [];
}
-
-
-function readAll (fd, pos, content, encoding, callback) {
- process.fs.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) {
- if (err) {
- if (callback) callback(err);
- } else if (chunk) {
- content += chunk;
- pos += bytesRead;
- readAll(fd, pos, content, encoding, callback);
- } else {
- process.fs.close(fd, function (err) {
- if (callback) callback(err, content);
- });
- }
- });
-}
-
-process.fs.readFile = function (path, encoding_, callback) {
- var encoding = typeof(encoding_) == 'string' ? encoding_ : 'utf8';
- var callback_ = arguments[arguments.length - 1];
- var callback = (typeof(callback_) == 'function' ? callback_ : null);
- process.fs.open(path, process.O_RDONLY, 0666, function (err, fd) {
- if (err) {
- if (callback) callback(err);
- } else {
- readAll(fd, 0, "", encoding, callback);
- }
- });
-};
-
-process.fs.readFileSync = function (path, encoding) {
- encoding = encoding || "utf8"; // default to utf8
-
- debug('readFileSync open');
-
- var fd = process.fs.open(path, process.O_RDONLY, 0666);
- var content = '';
- var pos = 0;
- var r;
-
- while ((r = process.fs.read(fd, 4*1024, pos, encoding)) && r[0]) {
- debug('readFileSync read ' + r[1]);
- content += r[0];
- pos += r[1]
- }
-
- debug('readFileSync close');
-
- process.fs.close(fd);
-
- debug('readFileSync done');
-
- return content;
-};
-
var pathModule = createInternalModule("path", function (exports) {
exports.join = function () {
return exports.normalize(Array.prototype.join.call(arguments, "/"));
};
exports.exists = function (path, callback) {
- process.fs.stat(path, function (err, stats) {
+ requireNative('fs').stat(path, function (err, stats) {
if (callback) callback(err ? false : true);
});
};
}
});
} else {
- process.fs.readFile(id, callback);
+ requireNative('fs').readFile(id, callback);
}
}
Module.prototype._loadScriptSync = function (filename) {
- var content = process.fs.readFileSync(filename);
+ var content = requireNative('fs').readFileSync(filename);
// remove shebang
content = content.replace(/^\#\!.*/, '');