Permalink
Browse files

can: basic module loader

  • Loading branch information...
1 parent 020cf87 commit 6710c48512d1d3a2fed78e4c2628bd488e84a09f @indutny committed Nov 3, 2012
Showing with 17 additions and 10 deletions.
  1. +1 −1 candor.io.gyp
  2. +5 −0 lib/can.can
  3. +2 −2 lib/fs.can
  4. +5 −1 lib/module.can
  5. +4 −6 src/runtime.cc
View
@@ -1,9 +1,9 @@
{
'variables': {
'library_files': [
- 'lib/can.can',
'lib/fs.can',
'lib/module.can',
+ 'lib/can.can',
]
},
'targets': [{
View
@@ -1 +1,6 @@
// Main binding
+process = global.process
+require = global.require
+
+// Load first argument
+if (process.argc > 1) require(process.argv[1])
View
@@ -4,11 +4,11 @@ fs = global._bindings.fs
buffer = global._bindings.buffer
exports.readFileSync = (filename) {
- fd = fs.open(filename, fs.flags.O_RDONLY, 438)
-
stat = fs.stat(filename)
if (stat == nil) return
+ fd = fs.open(filename, fs.flags.O_RDONLY, 438)
+
size = stat.size
buffs = []
buff_len = 0
View
@@ -1,5 +1,9 @@
// Module
+compile = global.compile
buffer = global._bindings.buffer
fs = global._natives.fs
-global.log(buffer.stringify(fs.readFileSync('/tmp/3.txt')))
+global.require = (filename) {
+ content = buffer.stringify(fs.readFileSync(filename))
+ return compile(filename, content)()
+}
View
@@ -93,13 +93,11 @@ Value* CCompile(uint32_t argc, Value* argv[]) {
String* filename = (argc >= 1 ? argv[0] : Nil::New())->ToString();
String* code = (argc >= 2 ? argv[1] : Nil::New())->ToString();
- char name[1024];
- size_t len = sizeof(name) - 1;
+ size_t len = filename->Length();
+ char* name = new char[len + 1];
- if (filename->Length() < len) {
- len = filename->Length();
- }
- strncpy(name, filename->Value(), len);
+ memcpy(name, filename->Value(), len);
+ name[len] = 0;
Function* res = Function::New(name, code->Value(), code->Length());

0 comments on commit 6710c48

Please sign in to comment.