Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
adding Johnson.require()
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Apr 19, 2008
1 parent 1f9f150 commit 23aa72e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/prelude.js
Expand Up @@ -38,4 +38,19 @@ Johnson.Generator.create = function() {
return new Johnson.Generator(this);
}

Johnson.required = {};

Johnson.require = function(file) {
if(Johnson.required[file]) return false;
for(var directory in Ruby['$LOAD_PATH']) {
var filename = directory + "/" + Ruby.File.basename(file, ".js") + ".js";
if(Ruby.File['exists?'](filename)) {
Johnson.required[file] = true;
eval(Ruby.File.read(filename));
return true;
}
}
throw FileNotFound;
}

null; // no need to marshal a result
15 changes: 15 additions & 0 deletions test/johnson/spidermonkey/js_land_proxy_test.rb
Expand Up @@ -65,6 +65,21 @@ def setup
def test_find_constants
assert_js_equal($LOAD_PATH, "Ruby['$LOAD_PATH']")
end

def test_require_twice
assert @context.evaluate('Johnson.require("prelude")')
assert !@context.evaluate('Johnson.require("prelude")')
end

def test_catch_missing_require
assert @context.evaluate("
try {
Johnson.require('adkfjhasd');
} catch(FileNotFound) {
true;
}
")
end

def test_array_gets_returned
list = [1,2,3,4]
Expand Down

0 comments on commit 23aa72e

Please sign in to comment.