opal 0.6.3 → 0.7.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.spectator +2 -0
  4. data/.spectator-mspec +3 -0
  5. data/.travis.yml +8 -11
  6. data/CHANGELOG.md +33 -0
  7. data/CONTRIBUTING.md +8 -43
  8. data/Gemfile +15 -4
  9. data/Guardfile +77 -0
  10. data/README.md +15 -9
  11. data/Rakefile +36 -12
  12. data/benchmarks/operators.rb +11 -0
  13. data/bin/opal +10 -13
  14. data/bin/opal-build +4 -4
  15. data/bin/opal-mspec +10 -0
  16. data/bin/opal-repl +4 -3
  17. data/examples/sinatra/Gemfile +1 -1
  18. data/examples/sinatra/config.ru +3 -3
  19. data/lib/mspec/opal/main.rb.erb +2 -2
  20. data/lib/mspec/opal/rake_task.rb +31 -24
  21. data/lib/mspec/opal/runner.rb +18 -1
  22. data/lib/mspec/opal/sprockets.js +17 -0
  23. data/lib/opal.rb +1 -34
  24. data/lib/opal/builder.rb +92 -58
  25. data/lib/opal/builder_processors.rb +165 -0
  26. data/lib/opal/cli.rb +85 -144
  27. data/lib/opal/cli_options.rb +136 -90
  28. data/lib/opal/cli_runners.rb +10 -0
  29. data/lib/opal/cli_runners/nodejs.rb +56 -0
  30. data/lib/opal/cli_runners/phantom.js +35 -0
  31. data/lib/opal/cli_runners/phantomjs.rb +28 -0
  32. data/lib/opal/cli_runners/server.rb +54 -0
  33. data/lib/opal/compiler.rb +35 -16
  34. data/lib/opal/erb.rb +29 -15
  35. data/lib/opal/hike_path_finder.rb +18 -0
  36. data/lib/opal/nodes.rb +1 -0
  37. data/lib/opal/nodes/call.rb +107 -26
  38. data/lib/opal/nodes/call_special.rb +31 -6
  39. data/lib/opal/nodes/class.rb +2 -2
  40. data/lib/opal/nodes/constants.rb +5 -20
  41. data/lib/opal/nodes/def.rb +4 -4
  42. data/lib/opal/nodes/defined.rb +3 -3
  43. data/lib/opal/nodes/definitions.rb +1 -1
  44. data/lib/opal/nodes/for.rb +35 -0
  45. data/lib/opal/nodes/helpers.rb +2 -2
  46. data/lib/opal/nodes/iter.rb +3 -3
  47. data/lib/opal/nodes/literal.rb +10 -2
  48. data/lib/opal/nodes/masgn.rb +2 -2
  49. data/lib/opal/nodes/module.rb +2 -2
  50. data/lib/opal/nodes/scope.rb +1 -0
  51. data/lib/opal/nodes/singleton_class.rb +2 -2
  52. data/lib/opal/nodes/super.rb +2 -2
  53. data/lib/opal/nodes/top.rb +30 -3
  54. data/lib/opal/parser.rb +15 -1
  55. data/lib/opal/parser/grammar.rb +2571 -2452
  56. data/lib/opal/parser/grammar.y +37 -5
  57. data/lib/opal/parser/keywords.rb +2 -0
  58. data/lib/opal/parser/lexer.rb +21 -11
  59. data/lib/opal/path_reader.rb +28 -0
  60. data/lib/opal/paths.rb +38 -0
  61. data/lib/opal/source_map.rb +32 -15
  62. data/lib/opal/sprockets/environment.rb +9 -2
  63. data/lib/opal/sprockets/erb.rb +1 -2
  64. data/lib/opal/sprockets/path_reader.rb +34 -0
  65. data/lib/opal/sprockets/processor.rb +40 -39
  66. data/lib/opal/sprockets/server.rb +47 -33
  67. data/lib/opal/version.rb +1 -1
  68. data/opal.gemspec +10 -5
  69. data/opal/README.md +6 -0
  70. data/opal/corelib/array.rb +36 -4
  71. data/opal/corelib/array/inheritance.rb +6 -6
  72. data/opal/corelib/basic_object.rb +9 -9
  73. data/opal/corelib/boolean.rb +1 -1
  74. data/opal/corelib/class.rb +12 -12
  75. data/opal/corelib/dir.rb +20 -0
  76. data/opal/corelib/enumerable.rb +42 -42
  77. data/opal/corelib/enumerator.rb +1 -1
  78. data/opal/corelib/error.rb +2 -2
  79. data/opal/corelib/file.rb +56 -0
  80. data/opal/corelib/hash.rb +5 -5
  81. data/opal/corelib/helpers.rb +3 -3
  82. data/opal/corelib/io.rb +13 -10
  83. data/opal/corelib/kernel.rb +44 -68
  84. data/opal/corelib/method.rb +1 -1
  85. data/opal/corelib/module.rb +89 -114
  86. data/opal/corelib/nil_class.rb +1 -1
  87. data/opal/corelib/numeric.rb +27 -23
  88. data/opal/corelib/proc.rb +5 -5
  89. data/opal/corelib/range.rb +8 -4
  90. data/opal/corelib/regexp.rb +5 -5
  91. data/opal/corelib/runtime.js +589 -272
  92. data/opal/corelib/string.rb +52 -37
  93. data/opal/corelib/string/inheritance.rb +5 -5
  94. data/opal/corelib/time.rb +102 -52
  95. data/opal/corelib/variables.rb +3 -3
  96. data/opal/opal.rb +2 -0
  97. data/package.json +9 -0
  98. data/spec/filters/bugs/array.rb +0 -6
  99. data/spec/filters/bugs/language.rb +4 -0
  100. data/spec/filters/bugs/numeric.rb +7 -6
  101. data/spec/filters/bugs/opal.rb +2 -0
  102. data/spec/filters/bugs/regexp.rb +4 -0
  103. data/spec/filters/bugs/string.rb +0 -7
  104. data/spec/filters/bugs/stringscanner.rb +4 -1
  105. data/spec/filters/unsupported/private_methods.rb +2 -0
  106. data/spec/lib/builder_processors_spec.rb +27 -0
  107. data/spec/lib/builder_spec.rb +66 -0
  108. data/spec/{cli → lib}/cli_spec.rb +60 -5
  109. data/spec/{cli → lib}/compiler_spec.rb +66 -5
  110. data/spec/{cli → lib}/dependency_resolver_spec.rb +1 -1
  111. data/spec/lib/fixtures/no_requires.rb +1 -0
  112. data/spec/{cli → lib}/fixtures/opal_file.rb +0 -0
  113. data/spec/lib/fixtures/require_tree_test.rb +3 -0
  114. data/spec/lib/fixtures/required_tree_test/required_file1.rb +1 -0
  115. data/spec/lib/fixtures/required_tree_test/required_file2.rb +1 -0
  116. data/spec/lib/fixtures/requires.rb +7 -0
  117. data/spec/{cli → lib}/fixtures/sprockets_file.js.rb +0 -0
  118. data/spec/lib/fixtures/sprockets_require_tree_test.rb +3 -0
  119. data/spec/lib/hike_path_finder_spec.rb +23 -0
  120. data/spec/{cli → lib}/lexer_spec.rb +1 -1
  121. data/spec/{cli → lib}/parser/alias_spec.rb +1 -1
  122. data/spec/{cli → lib}/parser/and_spec.rb +1 -1
  123. data/spec/{cli → lib}/parser/attrasgn_spec.rb +1 -1
  124. data/spec/{cli → lib}/parser/begin_spec.rb +1 -1
  125. data/spec/{cli → lib}/parser/block_spec.rb +1 -1
  126. data/spec/{cli → lib}/parser/break_spec.rb +1 -1
  127. data/spec/{cli → lib}/parser/call_spec.rb +1 -1
  128. data/spec/{cli → lib}/parser/class_spec.rb +1 -1
  129. data/spec/{cli → lib}/parser/comments_spec.rb +1 -1
  130. data/spec/{cli → lib}/parser/def_spec.rb +1 -1
  131. data/spec/{cli → lib}/parser/if_spec.rb +1 -1
  132. data/spec/{cli → lib}/parser/iter_spec.rb +1 -1
  133. data/spec/{cli → lib}/parser/lambda_spec.rb +1 -1
  134. data/spec/{cli → lib}/parser/literal_spec.rb +1 -1
  135. data/spec/{cli → lib}/parser/masgn_spec.rb +1 -1
  136. data/spec/{cli → lib}/parser/module_spec.rb +1 -1
  137. data/spec/{cli → lib}/parser/not_spec.rb +1 -1
  138. data/spec/{cli → lib}/parser/op_asgn1_spec.rb +1 -1
  139. data/spec/{cli → lib}/parser/op_asgn2_spec.rb +1 -1
  140. data/spec/{cli → lib}/parser/or_spec.rb +1 -1
  141. data/spec/{cli → lib}/parser/return_spec.rb +1 -1
  142. data/spec/{cli → lib}/parser/sclass_spec.rb +1 -1
  143. data/spec/{cli → lib}/parser/string_spec.rb +8 -1
  144. data/spec/{cli → lib}/parser/super_spec.rb +1 -1
  145. data/spec/lib/parser/unary_spec.rb +48 -0
  146. data/spec/{cli → lib}/parser/undef_spec.rb +1 -1
  147. data/spec/{cli → lib}/parser/unless_spec.rb +1 -1
  148. data/spec/{cli → lib}/parser/variables_spec.rb +1 -1
  149. data/spec/{cli → lib}/parser/while_spec.rb +1 -1
  150. data/spec/{cli → lib}/parser/yield_spec.rb +1 -1
  151. data/spec/lib/path_reader_spec.rb +24 -0
  152. data/spec/lib/shared/path_finder_shared.rb +19 -0
  153. data/spec/lib/shared/path_reader_shared.rb +31 -0
  154. data/spec/lib/spec_helper.rb +9 -0
  155. data/spec/lib/sprockets/environment_spec.rb +30 -0
  156. data/spec/{cli → lib}/sprockets/erb_spec.rb +1 -1
  157. data/spec/lib/sprockets/path_reader_spec.rb +25 -0
  158. data/spec/{cli → lib}/sprockets/processor_spec.rb +9 -2
  159. data/spec/lib/sprockets/server_spec.rb +20 -0
  160. data/spec/opal/compiler/irb_spec.rb +11 -11
  161. data/spec/opal/core/fixtures/require_tree_files/file 1.rb +1 -0
  162. data/spec/opal/core/fixtures/require_tree_files/file 2.rb +1 -0
  163. data/spec/opal/core/fixtures/require_tree_files/file 3.rb +1 -0
  164. data/spec/opal/core/fixtures/require_tree_files/file 4.rb +1 -0
  165. data/spec/opal/core/fixtures/require_tree_files/file 5.rb +1 -0
  166. data/spec/opal/core/kernel/require_tree_spec.rb +7 -0
  167. data/spec/opal/core/kernel/respond_to_spec.rb +2 -2
  168. data/spec/opal/core/runtime/method_missing_spec.rb +19 -0
  169. data/spec/opal/core/source_map_spec.rb +2 -2
  170. data/spec/opal/core/string_spec.rb +11 -0
  171. data/spec/opal/stdlib/erb/erb_spec.rb +0 -1
  172. data/spec/opal/stdlib/thread/mutex_spec.rb +40 -0
  173. data/spec/opal/stdlib/thread/thread_queue_spec.rb +32 -0
  174. data/spec/opal/stdlib/thread/thread_spec.rb +60 -0
  175. data/spec/rubyspecs +54 -11
  176. data/spec/spec_helper.rb +18 -3
  177. data/spec/support/mspec_rspec_adapter.rb +33 -0
  178. data/spec/{cli/spec_helper.rb → support/parser_helpers.rb} +10 -10
  179. data/stdlib/README.md +3 -0
  180. data/stdlib/benchmark.rb +10 -0
  181. data/stdlib/date.rb +2 -2
  182. data/stdlib/dir.rb +1 -5
  183. data/stdlib/file.rb +1 -7
  184. data/stdlib/json.rb +10 -1
  185. data/stdlib/native.rb +5 -5
  186. data/stdlib/nodejs.rb +5 -0
  187. data/stdlib/nodejs/dir.rb +13 -0
  188. data/stdlib/nodejs/file.rb +98 -0
  189. data/stdlib/nodejs/fileutils.rb +26 -0
  190. data/stdlib/nodejs/io.rb +2 -0
  191. data/stdlib/nodejs/irb.rb +45 -0
  192. data/stdlib/nodejs/process.rb +16 -0
  193. data/stdlib/nodejs/require.rb +32 -0
  194. data/stdlib/nodejs/rubygems.rb +68 -0
  195. data/stdlib/nodejs/runtime.rb +25 -0
  196. data/stdlib/nodejs/yaml.rb +11 -0
  197. data/stdlib/opal-parser.rb +1 -2
  198. data/stdlib/opal-source-maps.rb +2 -0
  199. data/stdlib/phantomjs.rb +8 -0
  200. data/stdlib/process.rb +10 -0
  201. data/stdlib/promise.rb +12 -4
  202. data/stdlib/set.rb +27 -0
  203. data/stdlib/source_map.rb +5 -63
  204. data/stdlib/source_map/map.rb +220 -0
  205. data/stdlib/source_map/mapping.rb +26 -0
  206. data/stdlib/source_map/offset.rb +88 -0
  207. data/stdlib/source_map/version.rb +3 -0
  208. data/stdlib/source_map/vlq.rb +77 -101
  209. data/stdlib/sourcemap.rb +1 -0
  210. data/stdlib/strscan.rb +7 -1
  211. data/stdlib/template.rb +1 -1
  212. data/stdlib/thread.rb +147 -7
  213. metadata +238 -104
  214. data/lib/mspec/opal/mspec_fixes.rb +0 -87
  215. data/spec/cli/sprockets/environment_spec.rb +0 -14
  216. data/spec/filters/bugs/symbol.rb +0 -5
  217. data/spec/opal/core/kernel/warn_spec.rb +0 -83
  218. data/spec/opal/core/language/numbers_spec.rb +0 -60
  219. data/stdlib/opal-source-maps.js.erb +0 -2
  220. data/stdlib/source_map/generator.rb +0 -251
  221. data/stdlib/source_map/parser.rb +0 -102
@@ -0,0 +1,165 @@
1
+ require 'opal/compiler'
2
+ require 'opal/erb'
3
+ require 'source_map'
4
+
5
+ module Opal
6
+ module BuilderProcessors
7
+ DEFAULT_PROCESSORS = []
8
+
9
+ class Processor
10
+ def self.inherited(processor)
11
+ DEFAULT_PROCESSORS << processor
12
+ end
13
+
14
+ def initialize(source, filename, options = {})
15
+ @source, @filename, @options = source, filename, options
16
+ @requires = []
17
+ @required_trees = []
18
+ end
19
+ attr_reader :source, :filename, :options, :requires, :required_trees
20
+
21
+ def to_s
22
+ source.to_s
23
+ end
24
+
25
+ def self.handles(*extensions)
26
+ @extensions = extensions
27
+ matches = extensions.join('|')
28
+ matches = "(#{matches})" if extensions.size == 1
29
+
30
+ @match_regexp = Regexp.new "\\.#{matches}$"
31
+ end
32
+
33
+ def self.extensions
34
+ @extensions
35
+ end
36
+
37
+ def self.match? other
38
+ (other.is_a?(String) and other.match(match_regexp))
39
+ end
40
+
41
+ def self.match_regexp
42
+ @match_regexp or raise NotImplementedError
43
+ end
44
+
45
+ def source_map
46
+ @source_map ||= begin
47
+ mappings = []
48
+ source_file = filename+'.js'
49
+ line = source.count("\n")
50
+ column = source.scan("\n[^\n]*$").size
51
+ offset = ::SourceMap::Offset.new(line, column)
52
+ mappings << ::SourceMap::Mapping.new(source_file, offset, offset)
53
+
54
+ # Ensure mappings isn't empty: https://github.com/maccman/sourcemap/issues/11
55
+ unless mappings.any?
56
+ zero_offset = ::SourceMap::Offset.new(0,0)
57
+ mappings = [::SourceMap::Mapping.new(source_file,zero_offset,zero_offset)]
58
+ end
59
+
60
+ ::SourceMap::Map.new(mappings)
61
+ end
62
+ end
63
+
64
+ def mark_as_required(filename)
65
+ "Opal.mark_as_loaded(Opal.normalize_loadable_path(#{filename.to_s.inspect}));"
66
+ end
67
+ end
68
+
69
+ class JsProcessor < Processor
70
+ handles :js
71
+
72
+ def source
73
+ @source.to_s + mark_as_required(@filename)
74
+ end
75
+ end
76
+
77
+ class RubyProcessor < Processor
78
+ handles :rb, :opal
79
+
80
+ def source
81
+ compiled.result
82
+ end
83
+
84
+ def source_map
85
+ compiled.source_map.map
86
+ end
87
+
88
+ def compiled
89
+ @compiled ||= begin
90
+ compiler = compiler_for(@source, file: @filename)
91
+ compiler.compile
92
+ compiler
93
+ end
94
+ end
95
+
96
+ def compiler_for(source, options = {})
97
+ compiler_class.new(source, @options.merge(options))
98
+ end
99
+
100
+ def requires
101
+ compiled.requires
102
+ end
103
+
104
+ def required_trees
105
+ compiled.required_trees
106
+ end
107
+
108
+ def compiler_class
109
+ ::Opal::Compiler
110
+ end
111
+ end
112
+
113
+ class ERBProcessor < RubyProcessor
114
+ handles :opalerb
115
+
116
+ def initialize(*args)
117
+ super
118
+ @source = prepare(@source, @filename)
119
+ end
120
+
121
+ def requires
122
+ ['erb']+super
123
+ end
124
+
125
+ private
126
+
127
+ def erb_compiler_class
128
+ ::Opal::ERB::Compiler
129
+ end
130
+
131
+ def prepare(source, path)
132
+ erb_compiler = erb_compiler_class.new(source, path)
133
+ erb_compiler.prepared_source
134
+ end
135
+ end
136
+
137
+ class HamlProcessor < RubyProcessor
138
+ handles :haml
139
+
140
+ def initialize(*args)
141
+ super
142
+ @source = prepare(@source, @filename)
143
+ end
144
+
145
+ def requires
146
+ ['opal-haml'] + super
147
+ end
148
+
149
+ def prepare(source, path)
150
+ haml = ::Haml::Engine.new(source, :ugly => true).precompiled
151
+ haml = haml.gsub('_hamlout.buffer', '_hamlout')
152
+
153
+ ::Opal::Haml.wrap haml, path
154
+ end
155
+ end
156
+
157
+ class ERB_Processor < Processor
158
+ handles :erb
159
+
160
+ def source
161
+ "Opal.modules[#{@filename.inspect}] = function() {#{::ERB.new(@source.to_s).result}};"
162
+ end
163
+ end
164
+ end
165
+ end
data/lib/opal/cli.rb CHANGED
@@ -1,10 +1,24 @@
1
1
  require 'opal'
2
2
  require 'rack'
3
+ require 'opal/builder'
4
+ require 'opal/cli_runners'
3
5
 
4
6
  module Opal
5
7
  class CLI
6
- attr_reader :options, :filename
7
- attr_reader :evals, :load_paths, :output, :requires
8
+ attr_reader :options, :file, :compiler_options, :evals, :load_paths, :argv,
9
+ :output, :requires, :gems, :stubs, :verbose, :port, :preload
10
+
11
+ def compile?
12
+ @compile
13
+ end
14
+
15
+ def sexp?
16
+ @sexp
17
+ end
18
+
19
+ def skip_opal_require?
20
+ @skip_opal_require
21
+ end
8
22
 
9
23
  class << self
10
24
  attr_accessor :stdout
@@ -12,193 +26,120 @@ module Opal
12
26
 
13
27
  def initialize options = nil
14
28
  options ||= {}
29
+
30
+ # Runner
31
+ @runner_type = options.delete(:runner) || :nodejs
32
+ @port = options.delete(:port) || 3000
33
+
15
34
  @options = options
16
- @evals = options[:evals] || []
17
- @requires = options[:requires] || []
18
- @filename = options[:filename]
19
- @load_paths = options[:load_paths] || []
20
- @output = options[:output] || self.class.stdout || $stdout
21
- raise ArgumentError if @evals.empty? and @filename.nil?
35
+ @compile = !!options.delete(:compile)
36
+ @sexp = options.delete(:sexp)
37
+ @file = options.delete(:file)
38
+ @argv = options.delete(:argv) || []
39
+ @evals = options.delete(:evals) || []
40
+ @requires = options.delete(:requires) || []
41
+ @load_paths = options.delete(:load_paths) || []
42
+ @gems = options.delete(:gems) || []
43
+ @stubs = options.delete(:stubs) || []
44
+ @preload = options.delete(:preload) || []
45
+ @output = options.delete(:output) || self.class.stdout || $stdout
46
+ @verbose = options.fetch(:verbose, false); options.delete(:verbose)
47
+ @skip_opal_require = options.delete(:skip_opal_require)
48
+ @compiler_options = Hash[
49
+ *processor_option_names.map do |option|
50
+ key = option.to_sym
51
+ next unless options.has_key? key
52
+ value = options.delete(key)
53
+ [key, value]
54
+ end.compact.flatten
55
+ ]
56
+
57
+ raise ArgumentError, "no runnable code provided (evals or file)" if @evals.empty? and @file.nil?
58
+ raise ArgumentError, "unknown options: #{options.inspect}" unless @options.empty?
22
59
  end
23
60
 
24
61
  def run
25
- set_processor_options
26
-
27
62
  case
28
- when options[:sexp]; prepare_eval_code; show_sexp
29
- when options[:compile]; prepare_eval_code; show_compiled_source
30
- when options[:server]; prepare_eval_code; start_server
31
- else run_code
63
+ when sexp?; show_sexp
64
+ when compile?; show_compiled_source
65
+ else run_code
32
66
  end
33
67
  end
34
68
 
35
-
36
-
37
-
38
- # RUN CODE
39
-
40
- class PathFinder < Struct.new(:paths)
41
- def find(filename)
42
- full_path = nil
43
- _path = paths.find do |path|
44
- full_path = File.join(path, filename)
45
- File.exist? full_path
46
- end
47
- full_path or raise(ArgumentError, "file: #{filename} not found")
48
- end
69
+ def runner
70
+ @runner ||= case @runner_type
71
+ when :server; CliRunners::Server.new(output, port)
72
+ when :nodejs; CliRunners::Nodejs.new(output)
73
+ when :phantomjs; CliRunners::Phantomjs.new(output)
74
+ else raise ArgumentError, @runner_type.inspect
75
+ end
49
76
  end
50
77
 
51
78
  def run_code
79
+ runner.run(compiled_source, argv)
80
+ end
81
+
82
+ def compiled_source
52
83
  Opal.paths.concat load_paths
53
- path_finder = PathFinder.new(Opal.paths)
54
- builder = Opal::Builder.new
55
- full_source = builder.build('opal')
56
-
57
- require 'pathname'
58
- requires.each do |path|
59
- path = Pathname(path)
60
- path = Pathname(path_finder.find(path)) unless path.absolute?
61
- full_source << builder.build_str(path.read, :file => path.to_s)
62
- end
84
+ gems.each { |gem_name| Opal.use_gem gem_name }
63
85
 
64
- evals.each_with_index do |code, index|
65
- full_source << builder.build_str(code, :file => "(eval #{index+1})")
66
- end
86
+ builder = Opal::Builder.new stubs: stubs, compiler_options: compiler_options
67
87
 
68
- file = Pathname(filename.to_s)
69
- full_source << builder.build_str(file.read, :file => file.to_s) if file.exist?
88
+ builder.build 'opal' unless skip_opal_require?
70
89
 
71
- run_with_node(full_source)
72
- end
90
+ preload.each { |path| builder.build_require(path) }
73
91
 
74
- def run_with_node(code)
75
- require 'open3'
76
- begin
77
- stdin, stdout, stderr = Open3.popen3('node')
78
- rescue Errno::ENOENT
79
- raise MissingNodeJS, 'Please install Node.js to be able to run Opal scripts.'
92
+ # REQUIRES: -r
93
+ requires.each do |local_require|
94
+ builder.build_str("require #{local_require.inspect}", 'require')
80
95
  end
81
96
 
82
- stdin.write code
83
- stdin.close
84
-
85
- [stdout, stderr].each do |io|
86
- str = io.read
87
- puts str unless str.empty?
97
+ if evals.any?
98
+ builder.build_str(evals.join("\n"), '-e')
99
+ else
100
+ if file and (file.path != '-' or evals.empty?)
101
+ builder.build_str(file.read, file.path)
102
+ end
88
103
  end
89
- end
90
104
 
91
- class MissingNodeJS < StandardError
92
- end
93
-
94
- def start_server
95
- require 'rack'
96
- require 'webrick'
97
- require 'logger'
98
-
99
- Rack::Server.start(
100
- :app => server,
101
- :Port => options[:port] || 3000,
102
- :AccessLog => [],
103
- :Logger => Logger.new($stdout)
104
- )
105
+ builder.to_s
105
106
  end
106
107
 
107
108
  def show_compiled_source
108
- if sprockets[filename]
109
- puts sprockets[filename].to_a.last
110
- elsif File.exist?(filename)
111
- puts Opal.compile File.read(filename), options
112
- else
113
- puts Opal.compile(filename, options)
114
- end
109
+ puts compiled_source
115
110
  end
116
111
 
117
112
  def show_sexp
118
- puts sexp.inspect
119
- end
120
-
121
-
122
-
123
- # PROCESSOR
124
-
125
- def set_processor_options
126
- processor_options.each do |option|
127
- key = option.to_sym
128
- next unless options.has_key? key
129
- Opal::Processor.send("#{option}=", options[key])
113
+ if evals.any?
114
+ sexp = Opal::Parser.new.parse(evals.join("\n"), '-e')
115
+ else
116
+ if file and (file.path != '-' or evals.empty?)
117
+ sexp = Opal::Parser.new.parse(file.read, file.path)
118
+ end
130
119
  end
120
+ puts sexp.inspect
131
121
  end
132
122
 
133
123
  def map
134
- compiler = Opal::Compiler.new
135
- compiler.compile(filename, options)
124
+ compiler = Opal::Compiler.compile(file.read, options.merge(:file => file.path))
125
+ compiler.compile
136
126
  compiler.source_map
137
127
  end
138
128
 
139
- def source
140
- File.exist?(filename) ? File.read(filename) : filename
141
- end
142
-
143
- def processor_options
129
+ def processor_option_names
144
130
  %w[
145
131
  method_missing_enabled
146
132
  arity_check_enabled
147
- const_missing_enabled
148
133
  dynamic_require_severity
149
134
  source_map_enabled
150
135
  irb_enabled
136
+ inline_operators
151
137
  ]
152
138
  end
153
139
 
154
- ##
155
- # SPROCKETS
156
-
157
- def sprockets
158
- server.sprockets
159
- end
160
-
161
- def server
162
- @server ||= Opal::Server.new do |s|
163
- load_paths.each do |path|
164
- s.append_path path
165
- end
166
- s.main = File.basename(filename, '.rb')
167
- end
168
- end
169
-
170
- ##
171
- # OUTPUT
172
-
173
140
  def puts(*args)
174
141
  output.puts(*args)
175
142
  end
176
143
 
177
- ##
178
- # EVALS
179
-
180
- def evals_source
181
- evals.inject('', &:<<)
182
- end
183
-
184
- def prepare_eval_code
185
- if evals.any?
186
- require 'tmpdir'
187
- path = File.join(Dir.mktmpdir,"opal-#{$$}.js.rb")
188
- File.open(path, 'w') do |tempfile|
189
- load_paths << File.dirname(path)
190
- tempfile.puts 'require "opal"'
191
- tempfile.puts evals_source
192
- end
193
- @filename = File.basename(path)
194
- end
195
- end
196
-
197
- ##
198
- # SOURCE
199
-
200
- def sexp
201
- Opal::Parser.new.parse(source)
202
- end
203
144
  end
204
145
  end