diff --git a/deps/candor b/deps/candor index b0e0ef5..32e4a1e 160000 --- a/deps/candor +++ b/deps/candor @@ -1 +1 @@ -Subproject commit b0e0ef5ee532f8d53c86333701f0fdff60cadd28 +Subproject commit 32e4a1e2b4534f30d1592cd30866247b3340f723 diff --git a/deps/uv b/deps/uv index 914185d..0ddf9d6 160000 --- a/deps/uv +++ b/deps/uv @@ -1 +1 @@ -Subproject commit 914185d6fc2ffd899c9c1ce4b52e1bf6d75ce6aa +Subproject commit 0ddf9d6b98c32254b577d3d8516f7ba674222557 diff --git a/lib/fs.can b/lib/fs.can index 1c73161..04435ac 100644 --- a/lib/fs.can +++ b/lib/fs.can @@ -20,8 +20,7 @@ exports.readFile = (filename, callback) { onOpen(fd) { if (size == 0) { fs.close(fd, () {}) - - callback(nil, buffer.concat(buffs, total)) + return callback(nil, buffer.concat(buffs, total)) } buff = buffer.new(size) @@ -47,7 +46,7 @@ exports.readFile = (filename, callback) { fs.open(filename, fs.flags.O_RDONLY, 438, (err, fd) { if (err) return callback(err) - onOpen(fd, stat) + onOpen(fd) }) }) } diff --git a/lib/module.can b/lib/module.can index 4371203..9a007c9 100644 --- a/lib/module.can +++ b/lib/module.can @@ -3,12 +3,21 @@ compile = global.compile buffer = global._bindings.buffer fs = global._natives.fs +cache = clone global._natives global.require = (filename) { - content = buffer.stringify(fs.readFileSync(filename)) - content = "return (require) {\n" + content + "\n}" + if (cache[filename]) return cache[filename] - return compile( + content = buffer.stringify(fs.readFileSync(filename) || + fs.readFileSync(filename + '.can')) + content = "return (require, module, exports) {\n" + content + "\n}" + + module = {} + module.exports = cache[filename] = {} + + compile( filename, content - )()(global.require) + )()(global.require, module, module.exports) + + return module.exports } diff --git a/src/bindings/buffer.h b/src/bindings/buffer.h index 5c3e4a8..99e1d5e 100644 --- a/src/bindings/buffer.h +++ b/src/bindings/buffer.h @@ -9,8 +9,8 @@ namespace can { class Buffer : public candor::CWrapper { public: - Buffer(ssize_t size) : candor::CWrapper(&magic), size_(size) { - data_ = new char[size]; + Buffer(ssize_t size) : candor::CWrapper(&magic), size_(size > 0 ? size : 0) { + data_ = new char[size_]; } ~Buffer() {