commonjs-rhino 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4f7640189ccde5551d0a7f169a3110c31277a7d
4
+ data.tar.gz: 064cf4210d30d3a30327eec3cb0fae93d46e983b
5
+ SHA512:
6
+ metadata.gz: 6e03cf89bc2ad65228b490a5030ac90924383d9417001f8a960ca90331862089b0c4424411c115d262578a1398b950cffb75aa33376aec63b5dd9cb104bc5867
7
+ data.tar.gz: efcced0cee70429a5057d0cf21f33395e7b15a5c9318278758a55ffea7b25f653986658510f49f8059144ff4aaafaf098260d03c3051a69bea8e2ac738db1e10
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ ruby '1.9.3', engine: 'jruby', engine_version: '1.7.12'
6
+
7
+ group :development, :test do
8
+ gem 'pry-nav'
9
+ gem 'rake'
10
+ gem 'jbundler'
11
+ end
12
+
13
+ group :test do
14
+ gem 'rspec'
15
+ gem 'rr'
16
+ end
@@ -0,0 +1,3 @@
1
+ == 1.0.0
2
+
3
+ * Birthday
@@ -0,0 +1,64 @@
1
+ commonjs-rhino
2
+ ==============
3
+
4
+ CommonJs support for Rhino, in Ruby (JRuby specifically). commonjs-rhino allows you to create JavaScript contexts that contain a `require` method you can use to load CommonJs modules.
5
+
6
+ ## Installation
7
+
8
+ `gem install commonjs-rhino`
9
+
10
+ Then, require it somewhere in your code:
11
+
12
+ ```ruby
13
+ require 'commonjs-rhino'
14
+ ```
15
+
16
+ Please be aware this gem requires Mozilla's Rhino JavaScript environment, which means Rhino will need to be somewhere in your Java classpath. commonjs-rhino was developed using [jbundler](https://github.com/mkristian/jbundler), which I recommend for Java dependency management. It's pretty easy to set up, here's a quick guide: Run `gem install jbundler`, then add `require 'jbundler'` in your project before any `java_import` statements are executed. In other words, your project should contain these lines in this order:
17
+
18
+ ```ruby
19
+ require 'jbundler'
20
+ require 'commonjs-rhino'
21
+ # require other jbundler-compatible gems
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Let's say you have this nice little CommonJs JavaScript module you'd like to use in Rhino. The module exists on disk at `/path/to/camertron/teapot.js`:
27
+
28
+ ```javascript
29
+ (function() {
30
+ module.exports.strVariable = 'foobarbaz';
31
+ module.exports.func = function() {
32
+ return "I'm a little teapot";
33
+ };
34
+ }).call();
35
+ ```
36
+
37
+ Create a commonjs-rhino context, point it at your modules, and require away:
38
+
39
+ ```ruby
40
+ context = CommonjsRhino.create_context(['/path/to/camertron'])
41
+ context.eval('var hello = require("camertron/teapot")')
42
+ context.eval('hello.strVariable') # => 'foobarbaz'
43
+ ```
44
+
45
+ It's that easy!
46
+
47
+ You can also evaluate files with the handy `eval_file` method:
48
+
49
+ ```ruby
50
+ context.eval_file('/path/to/file.js')
51
+ ```
52
+
53
+ ## Requirements
54
+
55
+ 1. [JRuby](http://jruby.org/)
56
+ 2. [Rhino](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino) in your classpath (see above).
57
+
58
+ ## Running Tests
59
+
60
+ `bundle exec rspec` should do the trick :)
61
+
62
+ ## Authors
63
+
64
+ * Cameron C. Dutro: http://github.com/camertron
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
4
+
5
+ require 'bundler'
6
+ require 'jbundler'
7
+ require 'rspec/core/rake_task'
8
+ require 'rubygems/package_task'
9
+
10
+ require './lib/commonjs-rhino'
11
+
12
+ Bundler::GemHelper.install_tasks
13
+
14
+ task :default => :spec
15
+
16
+ desc 'Run specs'
17
+ RSpec::Core::RakeTask.new do |t|
18
+ t.pattern = './spec/**/*_spec.rb'
19
+ end
@@ -0,0 +1,19 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+ require 'commonjs-rhino/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "commonjs-rhino"
6
+ s.version = ::CommonjsRhino::VERSION
7
+ s.authors = ["Cameron Dutro"]
8
+ s.email = ["camertron@gmail.com"]
9
+ s.homepage = "http://github.com/camertron"
10
+
11
+ s.description = s.summary = "CommonJS support for Rhino, in Ruby."
12
+ s.requirements << "jar 'org.mozilla:rhino', '1.7R4'"
13
+
14
+ s.platform = Gem::Platform::RUBY
15
+ s.has_rdoc = true
16
+
17
+ s.require_path = 'lib'
18
+ s.files = Dir["{lib,spec}/**/*", "Gemfile", "History.txt", "README.md", "Rakefile", "commonjs-rhino.gemspec"]
19
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'java'
4
+
5
+ java_import 'java.io.FileReader'
6
+ java_import 'org.mozilla.javascript.commonjs.module.ModuleScriptProvider'
7
+ java_import 'org.mozilla.javascript.commonjs.module.provider.ModuleSourceProvider'
8
+ java_import 'org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider'
9
+ java_import 'org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider'
10
+ java_import 'org.mozilla.javascript.commonjs.module.Require'
11
+ java_import 'org.mozilla.javascript.commonjs.module.RequireBuilder'
12
+
13
+ # http://stackoverflow.com/questions/11074836/resolving-modules-using-require-js-and-java-rhino
14
+ module CommonjsRhino
15
+ class Context
16
+ attr_reader :rhino_context, :shared_scope, :require_base_paths
17
+
18
+ def initialize(shared_scope, require_base_paths)
19
+ @shared_scope = shared_scope
20
+ @require_base_paths = require_base_paths
21
+ nil
22
+ end
23
+
24
+ def eval_file(file)
25
+ with_context do |context|
26
+ context.evaluateReader(
27
+ shared_scope, FileReader.new(file), nil, 1, nil
28
+ )
29
+ end
30
+ end
31
+
32
+ def eval(str)
33
+ with_context do |context|
34
+ context.evaluateString(shared_scope, str, nil, 1, nil)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def with_context
41
+ context = Java::OrgMozillaJavascript::Context.enter
42
+ yield context
43
+ ensure
44
+ context.java_send(:exit)
45
+ nil
46
+ end
47
+ end
48
+
49
+ def self.create_context(require_base_paths = [Dir.getwd])
50
+ require_base_paths = Array(require_base_paths)
51
+
52
+ source_provider = UrlModuleSourceProvider.new(
53
+ require_base_paths.map do |base_path|
54
+ Java::JavaNet::URI.new("file://#{base_path}")
55
+ end, nil
56
+ )
57
+
58
+ script_provider = SoftCachingModuleScriptProvider.new(source_provider)
59
+
60
+ builder = RequireBuilder.new
61
+ .setModuleScriptProvider(script_provider)
62
+ .setSandboxed(false)
63
+
64
+ context = Java::OrgMozillaJavascript::Context.enter
65
+ top_level_scope = context.initStandardObjects()
66
+ req = builder.createRequire(context, top_level_scope)
67
+ req.install(top_level_scope)
68
+ context.java_send(:exit)
69
+
70
+ CommonjsRhino::Context.new(top_level_scope, require_base_paths)
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ module CommonjsRhino
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe CommonjsRhino do
6
+ let(:rhino) { CommonjsRhino }
7
+ let(:privileged_uris) { [File.expand_path('../', __FILE__)] }
8
+
9
+ describe '#create_context' do
10
+ it 'returns a context object' do
11
+ rhino.create_context(privileged_uris).tap do |context|
12
+ expect(context).to respond_to(:eval)
13
+ expect(context).to respond_to(:eval_file)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe CommonjsRhino::Context do
6
+ let(:rhino) { CommonjsRhino }
7
+
8
+ # Privileged URIs are like a load path, except that the last
9
+ # part of the path must also be used when requiring the module.
10
+ let(:privileged_uris) { [File.expand_path('../fixtures', __FILE__)] }
11
+
12
+ # Notice the module here has to include a prefix of 'fixtures/'
13
+ # even though the path we added above ends with the same.
14
+ let(:test_module) { 'fixtures/test_module' }
15
+
16
+ # Any scripts you load from disk should be referenced via paths,
17
+ # not via module syntax (i.e. include file extensions, etc)
18
+ let(:test_script) { File.expand_path('../fixtures/test_script.js', __FILE__) }
19
+
20
+ before(:each) do
21
+ @context = rhino.create_context(privileged_uris)
22
+ end
23
+
24
+ describe '#eval' do
25
+ it 'evaluates javascript code' do
26
+ expect(@context.eval('1 + 2 + 3')).to eq(6)
27
+ end
28
+
29
+ it 'supports loading modules' do
30
+ @context.eval("var foo = require('#{test_module}');")
31
+ expect(@context.eval('foo.strVariable;')).to eq('foobarbaz')
32
+ expect(@context.eval('foo.func();')).to eq("I'm a little teapot")
33
+ end
34
+
35
+ it 'does not allow modules to be loaded from non-privileged uris' do
36
+ expect(
37
+ lambda { @context.eval("var foo = require('non/existent');") }
38
+ ).to raise_error(Java::OrgMozillaJavascript::JavaScriptException)
39
+ end
40
+ end
41
+
42
+ describe '#eval_file' do
43
+ it 'loads and interprets a file' do
44
+ @context.eval_file(test_script)
45
+ expect(@context.eval('hello')).to eq('world')
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,6 @@
1
+ (function() {
2
+ module.exports.strVariable = 'foobarbaz';
3
+ module.exports.func = function() {
4
+ return "I'm a little teapot";
5
+ };
6
+ }).call();
@@ -0,0 +1 @@
1
+ var hello = 'world';
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'jbundler'
4
+ require 'rspec'
5
+ require 'commonjs-rhino'
6
+ require 'pry-nav'
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :rr
10
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commonjs-rhino
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Dutro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: CommonJS support for Rhino, in Ruby.
14
+ email:
15
+ - camertron@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - History.txt
22
+ - README.md
23
+ - Rakefile
24
+ - commonjs-rhino.gemspec
25
+ - lib/commonjs-rhino.rb
26
+ - lib/commonjs-rhino/version.rb
27
+ - spec/commonjs-rhino_spec.rb
28
+ - spec/context_spec.rb
29
+ - spec/fixtures/test_module.js
30
+ - spec/fixtures/test_script.js
31
+ - spec/spec_helper.rb
32
+ homepage: http://github.com/camertron
33
+ licenses: []
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements:
50
+ - jar 'org.mozilla:rhino', '1.7R4'
51
+ rubyforge_project:
52
+ rubygems_version: 2.2.2
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: CommonJS support for Rhino, in Ruby.
56
+ test_files: []