coffee-script 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +27 -8
  2. data/lib/coffee_script.rb +54 -22
  3. metadata +4 -4
data/README.md CHANGED
@@ -11,28 +11,47 @@ Installation
11
11
 
12
12
  gem install coffee-script
13
13
 
14
- *This compiler lib has replaced the original CoffeeScript compiler that was written in Ruby*
14
+ *Note: This compiler library has replaced the original CoffeeScript
15
+ compiler that was written in Ruby.*
15
16
 
16
17
 
17
18
  Dependencies
18
19
  ------------
19
20
 
20
- The compiler depends on the `coffee-script-source` gem which is updated any time a new version of CoffeeScript is released. The gem's version number is sync'd up with the javascript release. This way you can build against different versions of CoffeeScript from this compiler.
21
+ This library depends on the `coffee-script-source` gem which is
22
+ updated any time a new version of CoffeeScript is released. (The
23
+ `coffee-script-source` gem's version number is synced with each
24
+ official CoffeeScript release.) This way you can build against
25
+ different versions of CoffeeScript by requiring the correct version of
26
+ the `coffee-script-source` gem.
21
27
 
22
- You can also use this lib with unreleased versions of CoffeeScript:
28
+ In addition, you can use this library with unreleased versions of
29
+ CoffeeScript by setting the `COFFEESCRIPT_SOURCE_PATH` environment
30
+ variable:
23
31
 
24
32
  export COFFEESCRIPT_SOURCE_PATH=/path/to/coffee-script/extras/coffee-script.js
25
33
 
26
- The json library is also required but is not explicity stated as a gem dependency. If you're on 1.8 you'll need to install the json or json_pure gem. Its already included in 1.9.
34
+ The `json` library is also required but is not explicitly stated as a
35
+ gem dependency. If you're on Ruby 1.8 you'll need to install the
36
+ `json` or `json_pure` gem. On Ruby 1.9, `json` is included in the
37
+ standard library.
27
38
 
28
39
 
29
40
  Engines
30
41
  -------
31
42
 
32
- The `coffee-script` lib will automatically choose the best JavaScript engine for your platform. The currently implemented engines are:
43
+ The `coffee-script` library will automatically choose the best
44
+ JavaScript engine for your platform. The currently implemented engines
45
+ are:
33
46
 
34
- * **Node.js**. If the `node` binary is available in $PATH, it will be used to invoke the CoffeeScript compiler.
47
+ * **Node.js**. If the `node` binary is available in `$PATH`, it will
48
+ be used to invoke the CoffeeScript compiler.
35
49
 
36
- * **JavaScript Core**. If you're on OS X and don't have Node.js installed, the lib will fall back to the built-in JavaScript Core binary, `jsc`. This way you don't need any additional dependencies.
50
+ * **JavaScript Core**. If you're on OS X and don't have Node.js
51
+ installed, the library will fall back to the built-in JavaScript
52
+ Core binary, `jsc`. This way you don't need to install any
53
+ additional dependencies.
37
54
 
38
- * **V8**. Shelling out to Node.js may be too slow for your production environment. In this case, install `therubyracer` gem, which provides a fast bridge between Ruby and V8.
55
+ * **V8**. Shelling out to Node.js may be too slow for your production
56
+ environment. In this case, install `therubyracer` gem, which
57
+ provides a fast bridge between Ruby and V8.
@@ -4,6 +4,10 @@ require 'tempfile'
4
4
  require 'coffee_script/source'
5
5
 
6
6
  module CoffeeScript
7
+ class Error < ::StandardError; end
8
+ class EngineError < Error; end
9
+ class CompilationError < Error; end
10
+
7
11
  module Source
8
12
  def self.path
9
13
  @path ||= ENV['COFFEESCRIPT_SOURCE_PATH'] || bundled_path
@@ -27,6 +31,45 @@ module CoffeeScript
27
31
  end
28
32
  end
29
33
 
34
+ module ExternalEngine
35
+ class << self
36
+ def compile(command, script, options)
37
+ yield f = Tempfile.open("coffee.js")
38
+ f.puts compile_js(script, options)
39
+ f.close
40
+
41
+ execute("#{command} #{f.path}")
42
+ ensure
43
+ f.close! if f
44
+ end
45
+
46
+ def execute(command)
47
+ out = `#{command}`.chomp
48
+ if $?.success?
49
+ status, result = out[0, 1], out[1..-1]
50
+ if status == "+"
51
+ result
52
+ else
53
+ raise CompilationError, result[/^(?:Error: )?(.*)/, 1]
54
+ end
55
+ else
56
+ raise EngineError, out
57
+ end
58
+ end
59
+
60
+ def compile_js(script, options)
61
+ options = options[:bare] ? "{#{Source.bare_option} : true}" : "{}"
62
+ <<-JS
63
+ try {
64
+ print('+' + CoffeeScript.compile(#{script.to_json}, #{options}));
65
+ } catch (e) {
66
+ print('-' + e);
67
+ }
68
+ JS
69
+ end
70
+ end
71
+ end
72
+
30
73
  module Engines
31
74
  module JavaScriptCore
32
75
  BIN = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
@@ -37,17 +80,9 @@ module CoffeeScript
37
80
  end
38
81
 
39
82
  def compile(script, options = {})
40
- options = options[:bare] ? "{#{Source.bare_option} : true}" : "{}"
41
-
42
- f = Tempfile.open("coffee.js")
43
- f.puts "load(#{Source.path.to_json});"
44
- f.puts "print(CoffeeScript.compile(#{script.to_json}, #{options}));"
45
- f.close
46
-
47
- out = `#{BIN} #{f.path}`
48
- $?.success? ? out.chomp : nil
49
- ensure
50
- f.close! if f
83
+ ExternalEngine.compile(BIN, script, options) do |f|
84
+ f.puts "load(#{Source.path.to_json});"
85
+ end
51
86
  end
52
87
  end
53
88
  end
@@ -60,17 +95,10 @@ module CoffeeScript
60
95
  end
61
96
 
62
97
  def compile(script, options = {})
63
- options = options[:bare] ? "{#{Source.bare_option} : true}" : "{}"
64
-
65
- f = Tempfile.open("coffee.js")
66
- f.puts Source.contents
67
- f.puts "console.log(this.CoffeeScript.compile(#{script.to_json}, #{options}));"
68
- f.close
69
-
70
- out = `node #{f.path}`
71
- $?.success? ? out.chomp : nil
72
- ensure
73
- f.close! if f
98
+ ExternalEngine.compile("node", script, options) do |f|
99
+ f.puts Source.contents
100
+ f.puts "var CoffeeScript = this.CoffeeScript, print = console.log;"
101
+ end
74
102
  end
75
103
  end
76
104
  end
@@ -86,6 +114,8 @@ module CoffeeScript
86
114
 
87
115
  def compile(script, options = {})
88
116
  coffee_module['compile'].call(script, Source.bare_option => options[:bare])
117
+ rescue ::V8::JSError => e
118
+ raise CompilationError, e.message
89
119
  end
90
120
 
91
121
  private
@@ -97,6 +127,8 @@ module CoffeeScript
97
127
  context = ::V8::Context.new
98
128
  context.eval(Source.contents)
99
129
  context['CoffeeScript']
130
+ rescue ::V8::JSError => e
131
+ raise EngineError, e.message
100
132
  end
101
133
  end
102
134
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coffee-script
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 2.0.0
10
+ version: 2.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Ashkenas
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-11-12 00:00:00 -06:00
20
+ date: 2010-11-14 00:00:00 -06:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency