Permalink
Browse files

can: module cache, fix fs.readFile

  • Loading branch information...
1 parent 4b6031b commit ecb0375030847f80233580c8fb138139b3b0f89e @indutny committed Nov 4, 2012
Showing with 19 additions and 11 deletions.
  1. +1 −1 deps/candor
  2. +1 −1 deps/uv
  3. +2 −3 lib/fs.can
  4. +13 −4 lib/module.can
  5. +2 −2 src/bindings/buffer.h
View
@@ -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)
})
})
}
View
@@ -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
}
View
@@ -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() {

0 comments on commit ecb0375

Please sign in to comment.