diff --git a/.travis.yml b/.travis.yml index 2535233..3c0c43c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,8 @@ rvm: - 1.9.3 - 1.9.2 - 1.8.7 - - jruby-1.6.7 + - jruby-18mode + - jruby-19mode notifications: recipients: - cowboyd@thefrontside.net diff --git a/Gemfile b/Gemfile index b8868f8..b235c7f 100644 --- a/Gemfile +++ b/Gemfile @@ -4,4 +4,4 @@ source 'http://rubygems.org' gemspec gem "therubyracer", :platforms => :ruby -gem "therubyrhino", "~> 1.73.3", :platforms => :jruby \ No newline at end of file +gem "therubyrhino", ">= 1.73.3", :platforms => :jruby \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..982fae1 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,23 @@ +(The MIT License: http://www.opensource.org/licenses/MIT) + +Copyright (c) 2011,2012 Charles Lowell + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 02e14fa..3616b0b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ - # CommonJS [![Build Status](https://secure.travis-ci.org/cowboyd/commonjs.rb.png)](http://travis-ci.org/cowboyd/commonjs.rb) Host CommonJS JavaScript environments in Ruby @@ -14,12 +13,12 @@ able to use commonjs applications and libraries? ## Using common JS from Ruby. -`CommonJS` now passes all of the Modules 1.0 unit tests - - env = CommonJS::Environment.new(:path => '/path/to/lib/dir') - env.require('foo.js') - +`CommonJS` now passes all of the Modules 1.0 unit tests. +```ruby +env = CommonJS::Environment.new(:path => '/path/to/lib/dir') +env.require('foo.js') +``` ## Future directions @@ -29,16 +28,18 @@ The plan however, is to allow you to extend your commonjs environment to have wh interfaces you want in it. So for example, if you want to allow filesystem access, as well as access to the process information, you would say: - env.modules :filesystem, :process +```ruby +env.modules :filesystem, :process +``` ## Supported runtimes ### Current * The Ruby Racer (V8) - [https://github.com/cowboyd/therubyracer] +* The Ruby Rhino (JRuby) - [https://github.com/cowboyd/therubyrhino] ### Desired -* The Ruby Rhino (JRuby) - [https://github.com/cowboyd/therubyrhino] * Johnson (TraceMonkey) - [https://github.com/jbarnette/johnson] * Lyndon (MacRuby) - [https://github.com/defunkt/lyndon] diff --git a/commonjs.gemspec b/commonjs.gemspec index f2617a8..494a476 100644 --- a/commonjs.gemspec +++ b/commonjs.gemspec @@ -7,6 +7,7 @@ Gem::Specification.new do |gem| gem.description = "Host CommonJS JavaScript environments in Ruby" gem.summary = "Provide access to your Ruby and Operating System runtime via the commonjs API" gem.homepage = "http://github.com/cowboyd/commonjs.rb" + gem.license = "MIT" gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } gem.files = `git ls-files`.split("\n") diff --git a/lib/commonjs/environment.rb b/lib/commonjs/environment.rb index b7423b5..aff7e97 100644 --- a/lib/commonjs/environment.rb +++ b/lib/commonjs/environment.rb @@ -13,7 +13,8 @@ def initialize(runtime, options = {}) def require(module_id) unless mod = @modules[module_id] filepath = find(module_id) or fail LoadError, "no such module '#{module_id}'" - load = @runtime.eval("(function(module, require, exports) {#{File.read(filepath)}})", filepath.expand_path.to_s) + load_js = "( function(module, require, exports) {\n#{File.read(filepath)}\n} )" + load = @runtime.eval(load_js, filepath.expand_path.to_s) @modules[module_id] = mod = Module.new(module_id, self) load.call(mod, mod.require_function, mod.exports) end @@ -31,9 +32,12 @@ def new_object private def find(module_id) - if loadpath = @paths.find { |path| path.join("#{module_id}.js").exist? } - loadpath.join("#{module_id}.js") + # Add `.js` extension if neccessary. + target = if File.extname(module_id) == '.js' then module_id else "#{module_id}.js" end + if loadpath = @paths.find { |path| path.join(target).exist? } + loadpath.join(target) end end + end end diff --git a/lib/commonjs/version.rb b/lib/commonjs/version.rb index 8e70498..eebcced 100644 --- a/lib/commonjs/version.rb +++ b/lib/commonjs/version.rb @@ -1,3 +1,3 @@ module CommonJS - VERSION = "0.2.6" + VERSION = "0.2.7" end diff --git a/spec/commonjs/libjs3/foo.js b/spec/commonjs/libjs3/foo.js new file mode 100644 index 0000000..9b0973c --- /dev/null +++ b/spec/commonjs/libjs3/foo.js @@ -0,0 +1,3 @@ +// some comment +exports.foo = 'foo'; +// another comment \ No newline at end of file diff --git a/spec/commonjs/modules_spec.rb b/spec/commonjs/modules_spec.rb index ec3324d..85468ef 100644 --- a/spec/commonjs/modules_spec.rb +++ b/spec/commonjs/modules_spec.rb @@ -32,3 +32,15 @@ def print end end end + +describe "modules" do + describe "with js comments" do + before do + @env = env_with_path_value File.expand_path('../libjs3', __FILE__) + end + + it "finds modules in that path" do + @env.require('foo').foo.should == 'foo' + end + end +end diff --git a/spec/commonjs/path_spec.rb b/spec/commonjs/path_spec.rb index f02a24c..a5c46b7 100644 --- a/spec/commonjs/path_spec.rb +++ b/spec/commonjs/path_spec.rb @@ -11,7 +11,7 @@ end it "fails when a module is not in the path" do - expect {@env.require('not_here')}.should raise_error LoadError + lambda { @env.require('not_here') }.should raise_error LoadError end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9adb2f0..d73afc1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,10 +6,14 @@ def env_with_path_value(path) CommonJS::Environment.new new_runtime, :path => path end -def new_runtime - require 'v8' - V8::Context.new -rescue LoadError +if defined?(JRUBY_VERSION) require 'rhino' - Rhino::Context.new -end + def new_runtime + Rhino::Context.new + end +else + require 'v8' + def new_runtime + V8::Context.new + end +end \ No newline at end of file