Skip to content

Commit

Permalink
can: module cache, fix fs.readFile
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Nov 4, 2012
1 parent 4b6031b commit ecb0375
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion deps/candor
2 changes: 1 addition & 1 deletion deps/uv
5 changes: 2 additions & 3 deletions lib/fs.can
Expand Up @@ -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)
Expand All @@ -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)
})
})
}
Expand Down
17 changes: 13 additions & 4 deletions lib/module.can
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions src/bindings/buffer.h
Expand Up @@ -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() {
Expand Down

0 comments on commit ecb0375

Please sign in to comment.