Skip to content

Commit edddc0f

Browse files
committedOct 24, 2013
Explicitly add compiler options to compiler and cli runner
1 parent 3d13b52 commit edddc0f

File tree

6 files changed

+57
-53
lines changed

6 files changed

+57
-53
lines changed
 

‎bin/opal

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ options = Opal::CLIOptions.new
55
options.parse!
66

77
require 'opal/cli'
8-
cli = Opal::CLI.new options.options.merge(:filename => ARGV.first)
9-
begin
10-
cli.run
11-
rescue Opal::CLI::MissingNode => e
12-
$stderr.puts e.message
13-
exit 127
8+
9+
if ARGV.empty?
10+
p options
11+
else
12+
cli = Opal::CLI.new options.options.merge(:filename => ARGV.first)
13+
begin
14+
cli.run
15+
rescue Opal::CLI::MissingNode => e
16+
$stderr.puts e.message
17+
exit 127
18+
end
1419
end

‎lib/opal/cli_options.rb

+12-12
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,16 @@ def initialize
6060
opts.separator ''
6161
opts.separator 'Compilation Options:'
6262

63-
opts.on('-M', '--no-method-missing', 'Disable method missing') do |value|
64-
options[:method_missing] = false
63+
opts.on('-M', '--[no-]method-missing', 'Disable method missing') do |val|
64+
options[:method_missing] = val
6565
end
6666

67-
opts.on('-O', '--no-optimized-operators', 'Disable optimized operators') do |value|
68-
options[:optimized_operators_enabled] = false
67+
opts.on('-A', '--[no-]arity-check', 'Enable arity check') do |value|
68+
options[:arity_check] = value
6969
end
7070

71-
opts.on('-A', '--arity-check', 'Enable arity check') do |value|
72-
options[:arity_check] = true
73-
end
74-
75-
opts.on('-C', '--no-const-missing', 'Disable const missing') do |value|
76-
options[:const_missing] = false
71+
opts.on('-C', '--[no-]const-missing', 'Disable const missing') do |value|
72+
options[:const_missing] = value
7773
end
7874

7975
dynamic_require_levels = %w[error warning ignore]
@@ -86,8 +82,12 @@ def initialize
8682
options[:source_map_enabled] = false
8783
end
8884

89-
opts.on("--irb", "IRB var mode") do |i|
90-
options[:irb] = true
85+
opts.on('-F', '--file FILE', 'Set filename for compiled code') do |file|
86+
options[:file] = file
87+
end
88+
89+
opts.on("--[no-]irb", "IRB var mode") do |flag|
90+
options[:irb] = flag
9191
end
9292
end
9393
end

‎lib/opal/compiler.rb

+30-31
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@ class Compiler
1515
# math comparisons
1616
COMPARE = %w[< > <= >=]
1717

18+
# defines a compiler option, also creating method of form 'name?'
19+
def self.compiler_option(name, default_value)
20+
mid = [true, false].include?(default_value) ? "#{name}?" : name
21+
22+
define_method(mid) do
23+
@options.fetch(name) { default_value }
24+
end
25+
end
26+
27+
# used for __FILE__ directives as well as finding relative require()
28+
compiler_option :file, '(file)'
29+
30+
# adds method stubs for all used methods in file
31+
compiler_option :method_missing, true
32+
33+
# adds an arity check to every method definition
34+
compiler_option :arity_check, false
35+
36+
# checks every constant access, delagating to const_missing if needed
37+
compiler_option :const_missing, false
38+
39+
# compile top level local vars with support for irb style vars
40+
compiler_option :irb, false
41+
42+
# how to handle dynamic requires (:error, :warning, :ignore)
43+
compiler_option :dynamic_require_severity, :error
44+
1845
attr_reader :result, :fragments
1946

2047
# Current scope
@@ -33,6 +60,7 @@ def initialize
3360
@line = 1
3461
@indent = ''
3562
@unique = 0
63+
@options = {}
3664

3765
@method_calls = Set.new
3866
@helpers = Set.new([:breaker, :slice])
@@ -41,45 +69,16 @@ def initialize
4169
# Compile some ruby code to a string.
4270
def compile(source, options = {})
4371
@source = source
44-
setup_options options
72+
@options.update options
4573

46-
@sexp = Grammar.new.parse(@source, @file)
74+
@sexp = Grammar.new.parse(@source, self.file)
4775

4876
top_node = Nodes::TopNode.new(@sexp, :expr, self)
4977
@fragments = top_node.compile_to_fragments.flatten
5078

5179
@result = @fragments.map(&:code).join('')
5280
end
5381

54-
def setup_options(options = {})
55-
@file = (options[:file] || '(file)')
56-
@source_file = (options[:source_file] || @file)
57-
@method_missing = (options[:method_missing] != false)
58-
@arity_check = (options[:arity_check])
59-
@const_missing = (options[:const_missing] == true)
60-
@irb_vars = (options[:irb] == true)
61-
@dynamic_require_severity = (options[:dynamic_require_severity] || :error)
62-
end
63-
64-
# Is method_missing enabled for this file
65-
def method_missing?
66-
@method_missing
67-
end
68-
69-
# const_missing enabled or not
70-
def const_missing?
71-
@const_missing
72-
end
73-
74-
def arity_check?
75-
@arity_check
76-
end
77-
78-
# Are top level irb style vars enabled
79-
def irb_vars?
80-
@irb_vars
81-
end
82-
8382
def source_map
8483
Opal::SourceMap.new(@fragments, '(file)')
8584
end

‎lib/opal/nodes/call.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def recv_sexp
8888
end
8989

9090
def using_irb?
91-
@compiler.irb_vars? and scope.top? and arglist == s(:arglist) and recvr.nil? and iter.nil?
91+
@compiler.irb? and scope.top? and arglist == s(:arglist) and recvr.nil? and iter.nil?
9292
end
9393
end
9494

‎lib/opal/nodes/top.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def stmts
3737
end
3838

3939
def compile_irb_vars
40-
if compiler.irb_vars?
40+
if compiler.irb?
4141
line "if (!$opal.irb_vars) { $opal.irb_vars = {}; }"
4242
end
4343
end

‎lib/opal/nodes/variables.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class LocalVariableNode < Base
88
children :var_name
99

1010
def using_irb?
11-
compiler.irb_vars? and scope.top?
11+
compiler.irb? and scope.top?
1212
end
1313

1414
def compile
@@ -27,7 +27,7 @@ class LocalAssignNode < Base
2727
children :var_name, :value
2828

2929
def using_irb?
30-
compiler.irb_vars? and scope.top?
30+
compiler.irb? and scope.top?
3131
end
3232

3333
def compile

0 commit comments

Comments
 (0)
Please sign in to comment.