commonjs 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ [submodule "spec/ext/commonjs"]
2
+ path = spec/ext/commonjs
3
+ url = https://github.com/kriskowal/commonjs.git
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ task :default => :spec
@@ -14,4 +14,8 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "commonjs"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = CommonJS::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec"
20
+ gem.add_development_dependency "therubyracer", ">= 0.10.0beta1"
17
21
  end
@@ -1,5 +1,6 @@
1
1
  require "commonjs/version"
2
2
 
3
3
  module CommonJS
4
-
4
+ autoload :Environment, 'commonjs/environment'
5
+ autoload :Module, 'commonjs/module'
5
6
  end
@@ -0,0 +1,53 @@
1
+ require 'pathname'
2
+ module CommonJS
3
+ class Environment
4
+
5
+ attr_reader :runtime
6
+
7
+ def initialize(options = {})
8
+ @runtime = choose
9
+ @path = Pathname(options[:path])
10
+ @modules = {}
11
+ end
12
+
13
+ def require(module_id)
14
+ unless mod = @modules[module_id]
15
+ filepath = find(module_id) or fail LoadError, "no such module '#{module_id}'"
16
+ load = @runtime.eval("(function(require, exports) {#{File.read(filepath)}})", filepath.expand_path)
17
+ @modules[module_id] = mod = Module.new(module_id, self)
18
+ load.call(mod.require_function, mod.exports)
19
+ end
20
+ return mod.exports
21
+ end
22
+
23
+ def native(module_id, impl)
24
+ @modules[module_id] = Module::Native.new(impl)
25
+ end
26
+
27
+ private
28
+
29
+ def choose
30
+ RubyRacerRuntime.new
31
+ end
32
+
33
+ def find(module_id)
34
+ filepath = @path.join("#{module_id}.js")
35
+ filepath if filepath.exist?
36
+ end
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
+ end
53
+ end
@@ -0,0 +1,47 @@
1
+
2
+ module CommonJS
3
+ class Module
4
+
5
+ attr_reader :exports
6
+
7
+ def initialize(id, env)
8
+ @id = id
9
+ @env = env
10
+ @exports = env.runtime.new_object
11
+ @segments = id.split('/')
12
+ end
13
+
14
+ def require_function
15
+ @require_function ||= lambda do |this, module_id|
16
+ @env.require(expand(module_id))
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def expand(module_id)
23
+ return module_id unless module_id =~ /(\.|\..)/
24
+ module_id.split('/').inject(@segments[0..-2]) do |path, element|
25
+ path.tap do
26
+ if element == '.'
27
+ #do nothing
28
+ elsif element == '..'
29
+ path.pop
30
+ else
31
+ path.push element
32
+ end
33
+ end
34
+ end.join('/')
35
+ end
36
+
37
+
38
+ class Native
39
+
40
+ attr_reader :exports
41
+
42
+ def initialize(impl)
43
+ @exports = impl
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module CommonJS
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,36 @@
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
@@ -0,0 +1,3 @@
1
+
2
+ require 'commonjs'
3
+ require 'pathname'
metadata CHANGED
@@ -1,74 +1,93 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: commonjs
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Charles Lowell
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-09-09 00:00:00 -05:00
19
- default_executable:
20
- dependencies: []
21
-
12
+ date: 2011-10-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &2152591120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152591120
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2152590620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152590620
36
+ - !ruby/object:Gem::Dependency
37
+ name: therubyracer
38
+ requirement: &2152590020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 0.10.0beta1
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152590020
22
47
  description: Host CommonJS JavaScript environments in Ruby
23
- email:
48
+ email:
24
49
  - cowboyd@thefrontside.net
25
50
  executables: []
26
-
27
51
  extensions: []
28
-
29
52
  extra_rdoc_files: []
30
-
31
- files:
53
+ files:
32
54
  - .gitignore
55
+ - .gitmodules
33
56
  - Gemfile
34
57
  - README.md
35
58
  - Rakefile
36
59
  - commonjs.gemspec
37
60
  - lib/commonjs.rb
61
+ - lib/commonjs/environment.rb
62
+ - lib/commonjs/module.rb
38
63
  - lib/commonjs/version.rb
39
- has_rdoc: true
64
+ - spec/commonjs/environment_spec.rb
65
+ - spec/spec_helper.rb
40
66
  homepage: http://github.com/cowboyd/commonjs
41
67
  licenses: []
42
-
43
68
  post_install_message:
44
69
  rdoc_options: []
45
-
46
- require_paths:
70
+ require_paths:
47
71
  - lib
48
- required_ruby_version: !ruby/object:Gem::Requirement
72
+ required_ruby_version: !ruby/object:Gem::Requirement
49
73
  none: false
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- hash: 3
54
- segments:
55
- - 0
56
- version: "0"
57
- required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
79
  none: false
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- hash: 3
63
- segments:
64
- - 0
65
- version: "0"
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
66
84
  requirements: []
67
-
68
85
  rubyforge_project:
69
- rubygems_version: 1.6.2
86
+ rubygems_version: 1.8.10
70
87
  signing_key:
71
88
  specification_version: 3
72
- summary: Provide access to your Ruby and Operating System runtime via the commonjs API
73
- test_files: []
74
-
89
+ summary: Provide access to your Ruby and Operating System runtime via the commonjs
90
+ API
91
+ test_files:
92
+ - spec/commonjs/environment_spec.rb
93
+ - spec/spec_helper.rb