include_js 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.gitmodules +3 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/LICENSE +22 -0
- data/Rakefile +2 -0
- data/Readme.md +44 -0
- data/include_js.gemspec +26 -0
- data/lib/include_js/version.rb +3 -0
- data/lib/include_js.rb +58 -0
- data/spec/include_js_spec.rb +29 -0
- data/spec/modules_spec.rb +18 -0
- data/spec/support/test_module.js +12 -0
- data/spec/support/test_sub_module.js +3 -0
- metadata +111 -0
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
include_js (0.0.1)
|
5
|
+
therubyracer (~> 0.8.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.2)
|
11
|
+
rspec (2.6.0)
|
12
|
+
rspec-core (~> 2.6.0)
|
13
|
+
rspec-expectations (~> 2.6.0)
|
14
|
+
rspec-mocks (~> 2.6.0)
|
15
|
+
rspec-core (2.6.0)
|
16
|
+
rspec-expectations (2.6.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.6.0)
|
19
|
+
therubyracer (0.8.1)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
include_js!
|
26
|
+
rspec (~> 2.6.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2011 Andreas Haller & Thorben Schröder (kopfmaschine)
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# IncludeJS
|
2
|
+
|
3
|
+
This is an experiment to see if we can use CommonJS Modules inside Ruby.
|
4
|
+
|
5
|
+
Currently it supports the [CommonJS Modules 1.0 spec](http://www.commonjs.org/specs/modules/1.0/).
|
6
|
+
|
7
|
+
It uses [therubyracer](github.com/cowboyd/therubyracer).
|
8
|
+
|
9
|
+
## Synopsis
|
10
|
+
|
11
|
+
### Writing a CommonJS Module in JavaScript
|
12
|
+
Inside 'helpers.js':
|
13
|
+
|
14
|
+
var a = 42;
|
15
|
+
exports.foo = function() {
|
16
|
+
return 42;
|
17
|
+
}
|
18
|
+
|
19
|
+
### Loading a CommonJS Module from Ruby
|
20
|
+
helpers = IncludeJS.require('helpers') # This returns a V8::Object
|
21
|
+
helpers.foo # => 42
|
22
|
+
|
23
|
+
### Loading a CommonJS Module as a Ruby Module
|
24
|
+
class App
|
25
|
+
include IncludeJS.module('helpers')
|
26
|
+
end
|
27
|
+
App.new.foo # => 42
|
28
|
+
|
29
|
+
You can set one root path from where to load .js files
|
30
|
+
|
31
|
+
IncludeJS.root_path = 'my/app/javascripts'
|
32
|
+
|
33
|
+
Have fun.
|
34
|
+
|
35
|
+
## Running the specs
|
36
|
+
git submodule update --init
|
37
|
+
bundle install
|
38
|
+
bundle rspec spec
|
39
|
+
|
40
|
+
## Speed
|
41
|
+
Simple benchmarks are showing a noticeable gain of speed when using JS methods
|
42
|
+
instead of native Ruby ones. While the Google V8 seems to be 10x faster than
|
43
|
+
Ruby 1.8.7 it is still 4x as fast as Ruby 1.9.2
|
44
|
+
|
data/include_js.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
|
5
|
+
require 'include_js/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "include_js"
|
9
|
+
s.version = IncludeJS::VERSION
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.authors = ["Andreas Haller", "Thorben Schröder"]
|
12
|
+
s.email = ["andreashaller@gmail.com"]
|
13
|
+
s.homepage = "https://github.com/ahx/include_js"
|
14
|
+
s.summary = %q{CommonJS Modules in Ruby}
|
15
|
+
s.description = %q{Use CommonJS Modules inside Ruby via therubyracer}
|
16
|
+
|
17
|
+
s.rubyforge_project = "include_js"
|
18
|
+
|
19
|
+
s.add_dependency 'therubyracer', '~> 0.8.1'
|
20
|
+
|
21
|
+
s.add_development_dependency 'rspec', '~> 2.6.0'
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.require_paths = ["lib"]
|
26
|
+
end
|
data/lib/include_js.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'v8'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module IncludeJS
|
5
|
+
class << self
|
6
|
+
extend Forwardable
|
7
|
+
def_delegators :instance, :require, :module, :root_path, :root_path=
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.instance
|
11
|
+
@instance ||= Env.new
|
12
|
+
end
|
13
|
+
|
14
|
+
class Env
|
15
|
+
attr_accessor :root_path # FIXME Make this act like a PATH (see Modules 1.1 require.paths) and think about the API
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@root_path = File.expand_path('.')
|
19
|
+
@modules = {} # This stores all loaded modules by their absolute path (not their id)
|
20
|
+
@engine = V8::Context.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def require(module_id, globals={})
|
24
|
+
load_module(module_id, globals, nil)
|
25
|
+
end
|
26
|
+
|
27
|
+
def module(module_id)
|
28
|
+
result = Module.new
|
29
|
+
require(module_id).each do |name, method|
|
30
|
+
result.send(:define_method, name) do |*args|
|
31
|
+
method.call(*args)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
result
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def load_module(module_id, globals, caller_path)
|
40
|
+
path = absolute_path(module_id, caller_path)
|
41
|
+
return @modules[path] if @modules[path]
|
42
|
+
globals.each { |name, value| @engine[name] = value } # FIXME Remove. globals is just hax to make the tests pass
|
43
|
+
exports = @modules[path] = @engine['Object'].new
|
44
|
+
require_fn = lambda { |module_id| load_module(module_id, globals, path) }
|
45
|
+
|
46
|
+
context = @engine.eval("(function(exports, require){ #{File.read(path)}})")
|
47
|
+
context.call(exports, require_fn)
|
48
|
+
exports
|
49
|
+
end
|
50
|
+
|
51
|
+
def absolute_path(module_id, caller_path)
|
52
|
+
root = (module_id.start_with?('.') && caller_path) ? File.dirname(caller_path) : @root_path
|
53
|
+
File.expand_path("#{root}/#{module_id}.js")
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'include_js'
|
2
|
+
IncludeJS.root_path = './spec/support'
|
3
|
+
|
4
|
+
describe IncludeJS do
|
5
|
+
it "can load a CommonJS module" do
|
6
|
+
test_module = IncludeJS.require('test_module')
|
7
|
+
|
8
|
+
test_module.plus(1, 2).should be 3
|
9
|
+
test_module.minus(1, 2).should be -1
|
10
|
+
test_module.minus(2, 1).should be 1
|
11
|
+
|
12
|
+
test_module.square(4).should be 16
|
13
|
+
end
|
14
|
+
|
15
|
+
it "can include a ruby like JS modul" do
|
16
|
+
class TestClass
|
17
|
+
include IncludeJS.module('test_module')
|
18
|
+
end
|
19
|
+
|
20
|
+
test = TestClass.new
|
21
|
+
|
22
|
+
test.plus(1, 2).should be 3
|
23
|
+
test.minus(1, 2).should be -1
|
24
|
+
test.minus(2, 1).should be 1
|
25
|
+
|
26
|
+
test.square(4).should be 16
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'include_js'
|
2
|
+
|
3
|
+
describe IncludeJS do
|
4
|
+
specs = Dir.glob('./spec/support/commonjs/tests/modules/1.0/**')
|
5
|
+
raise "Could not load the CommonJS specs. Maybe you need to run 'git submodule update --init'?" if specs.empty?
|
6
|
+
specs.each { |dir|
|
7
|
+
|
8
|
+
send 'it', "passes the Modules 1.0 '#{File.basename(dir)}' test" do
|
9
|
+
outcome = []
|
10
|
+
IncludeJS.root_path = dir
|
11
|
+
env = IncludeJS.require('program', :print => lambda { |*args| outcome << args.first } )
|
12
|
+
outcome.join.should_not be_empty
|
13
|
+
outcome.join.should_not include 'FAIL'
|
14
|
+
end
|
15
|
+
|
16
|
+
}
|
17
|
+
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: include_js
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Andreas Haller
|
13
|
+
- "Thorben Schr\xC3\xB6der"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-19 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: therubyracer
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 8
|
32
|
+
- 1
|
33
|
+
version: 0.8.1
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
segments:
|
45
|
+
- 2
|
46
|
+
- 6
|
47
|
+
- 0
|
48
|
+
version: 2.6.0
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Use CommonJS Modules inside Ruby via therubyracer
|
52
|
+
email:
|
53
|
+
- andreashaller@gmail.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- .gitmodules
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- LICENSE
|
66
|
+
- Rakefile
|
67
|
+
- Readme.md
|
68
|
+
- include_js.gemspec
|
69
|
+
- lib/include_js.rb
|
70
|
+
- lib/include_js/version.rb
|
71
|
+
- spec/include_js_spec.rb
|
72
|
+
- spec/modules_spec.rb
|
73
|
+
- spec/support/test_module.js
|
74
|
+
- spec/support/test_sub_module.js
|
75
|
+
has_rdoc: true
|
76
|
+
homepage: https://github.com/ahx/include_js
|
77
|
+
licenses: []
|
78
|
+
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
requirements: []
|
101
|
+
|
102
|
+
rubyforge_project: include_js
|
103
|
+
rubygems_version: 1.3.7
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: CommonJS Modules in Ruby
|
107
|
+
test_files:
|
108
|
+
- spec/include_js_spec.rb
|
109
|
+
- spec/modules_spec.rb
|
110
|
+
- spec/support/test_module.js
|
111
|
+
- spec/support/test_sub_module.js
|