commonjs 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -7
- data/commonjs.gemspec +3 -1
- data/lib/commonjs/environment.rb +6 -20
- data/lib/commonjs/module.rb +2 -2
- data/lib/commonjs/version.rb +1 -1
- data/spec/commonjs/modules_spec.rb +35 -0
- metadata +78 -50
- data/spec/commonjs/environment_spec.rb +0 -36
data/README.md
CHANGED
@@ -3,15 +3,42 @@
|
|
3
3
|
|
4
4
|
Host CommonJS JavaScript environments in Ruby
|
5
5
|
|
6
|
-
##
|
6
|
+
## Why?
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
The internet is now awash with non-browser JavaScript code. Much of this code conforms to some
|
9
|
+
simple conventions that let you use it anywhere you have a JavaScript interpreter available. These
|
10
|
+
conventions are collectively called "commonjs"
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
We have several JavaScript interpreters available to us from Ruby. Therefore, why shouldn't we be
|
13
|
+
able to use commonjs applications and libraries?
|
14
|
+
|
15
|
+
## Using common JS from Ruby.
|
16
|
+
|
17
|
+
`CommonJS` now passes all of the Modules 1.0 unit tests
|
18
|
+
|
19
|
+
env = CommonJS::Environment.new(:path => '/path/to/lib/dir')
|
20
|
+
env.require('foo.js')
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
## Future directions
|
25
|
+
|
26
|
+
By default, all you get with a bare commonjs environment is the Modules API
|
27
|
+
|
28
|
+
The plan however, is to allow you to extend your commonjs environment to have whatever native
|
29
|
+
interfaces you want in it. So for example, if you want to allow filesystem access, as well as
|
30
|
+
access to the process information, you would say:
|
31
|
+
|
32
|
+
env.modules :filesystem, :process
|
14
33
|
|
15
34
|
## Supported runtimes
|
16
35
|
|
17
|
-
|
36
|
+
### Current
|
37
|
+
|
38
|
+
* The Ruby Racer (V8) - [https://github.com/cowboyd/therubyracer]
|
39
|
+
|
40
|
+
### Desired
|
41
|
+
|
42
|
+
* The Ruby Rhino (JRuby) - [https://github.com/cowboyd/therubyrhino]
|
43
|
+
* Johnson (TraceMonkey) - [https://github.com/jbarnette/johnson]
|
44
|
+
* Lyndon (MacRuby) - [https://github.com/defunkt/lyndon]
|
data/commonjs.gemspec
CHANGED
@@ -15,7 +15,9 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = CommonJS::VERSION
|
17
17
|
|
18
|
+
|
19
|
+
gem.add_dependency "therubyracer", "~> 0.9.9"
|
20
|
+
|
18
21
|
gem.add_development_dependency "rake"
|
19
22
|
gem.add_development_dependency "rspec"
|
20
|
-
gem.add_development_dependency "therubyracer", ">= 0.10.0beta1"
|
21
23
|
end
|
data/lib/commonjs/environment.rb
CHANGED
@@ -4,8 +4,8 @@ module CommonJS
|
|
4
4
|
|
5
5
|
attr_reader :runtime
|
6
6
|
|
7
|
-
def initialize(options = {})
|
8
|
-
@runtime =
|
7
|
+
def initialize(runtime, options = {})
|
8
|
+
@runtime = runtime
|
9
9
|
@path = Pathname(options[:path])
|
10
10
|
@modules = {}
|
11
11
|
end
|
@@ -24,30 +24,16 @@ module CommonJS
|
|
24
24
|
@modules[module_id] = Module::Native.new(impl)
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
def choose
|
30
|
-
RubyRacerRuntime.new
|
27
|
+
def new_object
|
28
|
+
@runtime['Object'].new
|
31
29
|
end
|
32
30
|
|
31
|
+
private
|
32
|
+
|
33
33
|
def find(module_id)
|
34
34
|
filepath = @path.join("#{module_id}.js")
|
35
35
|
filepath if filepath.exist?
|
36
36
|
end
|
37
37
|
|
38
|
-
class RubyRacerRuntime
|
39
|
-
def initialize
|
40
|
-
require 'v8'
|
41
|
-
@context = V8::Context.new
|
42
|
-
end
|
43
|
-
|
44
|
-
def new_object
|
45
|
-
@context['Object'].new
|
46
|
-
end
|
47
|
-
|
48
|
-
def eval(source, path)
|
49
|
-
@context.eval(source, path)
|
50
|
-
end
|
51
|
-
end
|
52
38
|
end
|
53
39
|
end
|
data/lib/commonjs/module.rb
CHANGED
@@ -7,12 +7,12 @@ module CommonJS
|
|
7
7
|
def initialize(id, env)
|
8
8
|
@id = id
|
9
9
|
@env = env
|
10
|
-
@exports = env.
|
10
|
+
@exports = env.new_object
|
11
11
|
@segments = id.split('/')
|
12
12
|
end
|
13
13
|
|
14
14
|
def require_function
|
15
|
-
@require_function ||= lambda do |
|
15
|
+
@require_function ||= lambda do |module_id|
|
16
16
|
@env.require(expand(module_id))
|
17
17
|
end
|
18
18
|
end
|
data/lib/commonjs/version.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'v8'
|
4
|
+
|
5
|
+
describe "modules 1.0" do
|
6
|
+
|
7
|
+
def self.make_before(path)
|
8
|
+
proc do
|
9
|
+
@env = CommonJS::Environment.new(V8::Context.new, :path => path)
|
10
|
+
@env.native('system', QuietSystem.new)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
tests = Pathname(__FILE__).dirname.join('../ext/commonjs/tests/modules/1.0')
|
15
|
+
tests.entries.each do |path|
|
16
|
+
next if ['.','..'].include?(path.to_s)
|
17
|
+
|
18
|
+
describe path do
|
19
|
+
before(&make_before(tests.join(path)))
|
20
|
+
|
21
|
+
it "✓" do
|
22
|
+
@env.require('program')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class QuietSystem
|
28
|
+
def stdio
|
29
|
+
self
|
30
|
+
end
|
31
|
+
def print(*args)
|
32
|
+
# puts args.join('')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,56 +1,76 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: commonjs
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Charles Lowell
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
|
18
|
+
date: 2011-11-29 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: therubyracer
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
22
|
-
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 41
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 9
|
32
|
+
- 9
|
33
|
+
version: 0.9.9
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
23
38
|
prerelease: false
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: rspec
|
27
|
-
requirement: &2152590620 !ruby/object:Gem::Requirement
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
40
|
none: false
|
29
|
-
requirements:
|
30
|
-
- -
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
33
48
|
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
34
52
|
prerelease: false
|
35
|
-
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: therubyracer
|
38
|
-
requirement: &2152590020 !ruby/object:Gem::Requirement
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
39
54
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
44
62
|
type: :development
|
45
|
-
|
46
|
-
version_requirements: *2152590020
|
63
|
+
version_requirements: *id003
|
47
64
|
description: Host CommonJS JavaScript environments in Ruby
|
48
|
-
email:
|
65
|
+
email:
|
49
66
|
- cowboyd@thefrontside.net
|
50
67
|
executables: []
|
68
|
+
|
51
69
|
extensions: []
|
70
|
+
|
52
71
|
extra_rdoc_files: []
|
53
|
-
|
72
|
+
|
73
|
+
files:
|
54
74
|
- .gitignore
|
55
75
|
- .gitmodules
|
56
76
|
- Gemfile
|
@@ -61,33 +81,41 @@ files:
|
|
61
81
|
- lib/commonjs/environment.rb
|
62
82
|
- lib/commonjs/module.rb
|
63
83
|
- lib/commonjs/version.rb
|
64
|
-
- spec/commonjs/
|
84
|
+
- spec/commonjs/modules_spec.rb
|
65
85
|
- spec/spec_helper.rb
|
66
86
|
homepage: http://github.com/cowboyd/commonjs
|
67
87
|
licenses: []
|
88
|
+
|
68
89
|
post_install_message:
|
69
90
|
rdoc_options: []
|
70
|
-
|
91
|
+
|
92
|
+
require_paths:
|
71
93
|
- lib
|
72
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
95
|
none: false
|
74
|
-
requirements:
|
75
|
-
- -
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
|
78
|
-
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
104
|
none: false
|
80
|
-
requirements:
|
81
|
-
- -
|
82
|
-
- !ruby/object:Gem::Version
|
83
|
-
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
version: "0"
|
84
112
|
requirements: []
|
113
|
+
|
85
114
|
rubyforge_project:
|
86
115
|
rubygems_version: 1.8.10
|
87
116
|
signing_key:
|
88
117
|
specification_version: 3
|
89
|
-
summary: Provide access to your Ruby and Operating System runtime via the commonjs
|
90
|
-
|
91
|
-
|
92
|
-
- spec/commonjs/environment_spec.rb
|
118
|
+
summary: Provide access to your Ruby and Operating System runtime via the commonjs API
|
119
|
+
test_files:
|
120
|
+
- spec/commonjs/modules_spec.rb
|
93
121
|
- spec/spec_helper.rb
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe CommonJS::Environment do
|
4
|
-
|
5
|
-
describe "modules 1.0" do
|
6
|
-
|
7
|
-
def self.make_before(path)
|
8
|
-
proc do
|
9
|
-
@env = CommonJS::Environment.new(:path => path)
|
10
|
-
@env.native('system', QuietSystem.new)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
tests = Pathname(__FILE__).dirname.join('../ext/commonjs/tests/modules/1.0')
|
15
|
-
tests.entries.each do |path|
|
16
|
-
next if ['.','..'].include?(path.to_s)
|
17
|
-
|
18
|
-
describe path do
|
19
|
-
before(&make_before(tests.join(path)))
|
20
|
-
|
21
|
-
it "...yup." do
|
22
|
-
@env.require('program')
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
class QuietSystem
|
29
|
-
def stdio
|
30
|
-
self
|
31
|
-
end
|
32
|
-
def print(*args)
|
33
|
-
# puts args.join('')
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|