Skip to content

Commit 91f868e

Browse files
committedNov 28, 2014
Support RUBY_ENGINE/RUBY_PLATFORM != 'opal' directives
1 parent 7997567 commit 91f868e

File tree

4 files changed

+108
-67
lines changed

4 files changed

+108
-67
lines changed
 

Diff for: ‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## edge (upcoming 0.7)
22

3+
* Add support for `RUBY_ENGINE/RUBY_PLATFORM != "opal"` pre-processor directives.
4+
5+
if RUBY_ENGINE != "opal"
6+
# this code never compiles
7+
end
8+
39
* Fix donating methods defined in modules. This ensures that if a class includes more than one module, then the methods defined on the class respect the order in which the modules are included.
410

511
* Improved support for recursive `Hash` for both `#inspect` and `#hash`.

Diff for: ‎lib/opal/nodes/if.rb

+14
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@ class IfNode < Base
1313
RUBY_PLATFORM_CHECK = [:call, [:const, :RUBY_PLATFORM],
1414
:==, [:arglist, [:str, "opal"]]]
1515

16+
RUBY_ENGINE_CHECK_NOT = [:call, [:call, [:const, :RUBY_ENGINE], :==,
17+
[:arglist, [:str, "opal"]]], :'!', [:arglist]]
18+
19+
RUBY_PLATFORM_CHECK_NOT = [:call, [:call, [:const, :RUBY_PLATFORM], :==,
20+
[:arglist, [:str, "opal"]]], :'!', [:arglist]]
21+
1622
def compile
1723
truthy, falsy = self.truthy, self.falsy
1824

1925
if skip_check_present?
2026
falsy = nil
2127
end
2228

29+
if skip_check_present_not?
30+
truthy = nil
31+
end
32+
2333
push "if (", js_truthy(test), ") {"
2434

2535
# skip if-body if no truthy sexp
@@ -51,6 +61,10 @@ def skip_check_present?
5161
test == RUBY_ENGINE_CHECK or test == RUBY_PLATFORM_CHECK
5262
end
5363

64+
def skip_check_present_not?
65+
test == RUBY_ENGINE_CHECK_NOT or test == RUBY_PLATFORM_CHECK_NOT
66+
end
67+
5468
def truthy
5569
needs_wrapper? ? compiler.returns(true_body || s(:nil)) : true_body
5670
end

Diff for: ‎spec/lib/compiler/pre_processed_conditionals_spec.rb

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require 'lib/spec_helper'
2+
3+
describe Opal::Compiler do
4+
def expect_compiled(*args)
5+
expect(Opal::Compiler.new(*args).compile)
6+
end
7+
8+
describe "pre-processed if conditions" do
9+
it "compiles if blocks using RUBY_ENGINE/RUBY_PLATFORM == opal" do
10+
expect_compiled(<<-RUBY).to include("should_compile_fine")
11+
if RUBY_ENGINE == 'opal'
12+
:should_compile_fine
13+
end
14+
RUBY
15+
16+
expect_compiled(<<-RUBY).to include("so_should_this")
17+
if RUBY_PLATFORM == 'opal'
18+
:so_should_this
19+
end
20+
RUBY
21+
end
22+
23+
it "does not compile if blocks using RUBY_ENGINE/RUBY_PLATFORM != opal" do
24+
expect_compiled(<<-RUBY).to_not include('should_not_compile')
25+
if RUBY_ENGINE != 'opal'
26+
:should_not_compile
27+
end
28+
RUBY
29+
30+
expect_compiled(<<-RUBY).to_not include('should_not_compile')
31+
if RUBY_PLATFORM != 'opal'
32+
:should_not_compile
33+
end
34+
RUBY
35+
end
36+
37+
it "skips elsif/else parts for CONST == opal" do
38+
expect_compiled(<<-RUBY).to_not include("should_be_skipped")
39+
if RUBY_PLATFORM == "opal"
40+
:ok
41+
else
42+
:should_be_skipped
43+
end
44+
RUBY
45+
46+
result = expect_compiled(<<-RUBY)
47+
if RUBY_ENGINE == 'opal'
48+
:this_compiles
49+
elsif false
50+
:this_does_not_compile
51+
else
52+
:this_neither
53+
end
54+
RUBY
55+
56+
result.to_not include("this_does_not_compile", "this_neither")
57+
end
58+
59+
it "generates if-code as normal without check" do
60+
expect_compiled(<<-RUBY).to include("should_compile", "and_this")
61+
if some_conditional
62+
:should_compile
63+
else
64+
:and_this
65+
end
66+
RUBY
67+
end
68+
end
69+
70+
describe "pre-processed unless conditionals" do
71+
it "skips over if using RUBY_ENGINE/RUBY_PLATFORM == 'opal'" do
72+
expect_compiled(<<-RUBY).to_not include("should_not_compile")
73+
unless RUBY_ENGINE == 'opal'
74+
:should_not_compile
75+
end
76+
RUBY
77+
end
78+
79+
it "generates unless code as normal if no check" do
80+
expect_compiled(<<-RUBY).to include("this_should_compile")
81+
unless this_is_true
82+
:this_should_compile
83+
end
84+
RUBY
85+
end
86+
end
87+
end

Diff for: ‎spec/lib/compiler_spec.rb

+1-67
Original file line numberDiff line numberDiff line change
@@ -83,73 +83,7 @@
8383
end
8484
end
8585

86-
describe "pre-processed if conditions" do
87-
it "compiles if blocks using RUBY_ENGINE/RUBY_PLATFORM == opal" do
88-
expect_compiled(<<-RUBY).to include("should_compile_fine")
89-
if RUBY_ENGINE == 'opal'
90-
:should_compile_fine
91-
end
92-
RUBY
93-
94-
expect_compiled(<<-RUBY).to include("so_should_this")
95-
if RUBY_PLATFORM == 'opal'
96-
:so_should_this
97-
end
98-
RUBY
99-
end
100-
101-
it "skips elsif/else parts" do
102-
expect_compiled(<<-RUBY).to_not include("should_be_skipped")
103-
if RUBY_PLATFORM == "opal"
104-
:ok
105-
else
106-
:should_be_skipped
107-
end
108-
RUBY
109-
110-
result = expect_compiled(<<-RUBY)
111-
if RUBY_ENGINE == 'opal'
112-
:this_compiles
113-
elsif false
114-
:this_does_not_compile
115-
else
116-
:this_neither
117-
end
118-
RUBY
119-
120-
result.to_not include("this_does_not_compile", "this_neither")
121-
end
122-
123-
it "generates if-code as normal without check" do
124-
expect_compiled(<<-RUBY).to include("should_compile", "and_this")
125-
if some_conditional
126-
:should_compile
127-
else
128-
:and_this
129-
end
130-
RUBY
131-
end
132-
end
133-
134-
describe "pre-processed unless conditionals" do
135-
it "skips over if using RUBY_ENGINE/RUBY_PLATFORM == 'opal'" do
136-
expect_compiled(<<-RUBY).to_not include("should_not_compile")
137-
unless RUBY_ENGINE == 'opal'
138-
:should_not_compile
139-
end
140-
RUBY
141-
end
142-
143-
it "generates unless code as normal if no check" do
144-
expect_compiled(<<-RUBY).to include("this_should_compile")
145-
unless this_is_true
146-
:this_should_compile
147-
end
148-
RUBY
149-
end
150-
end
151-
152-
describe 'pre-processing require-ish methods' do
86+
describe 'pre-processing require-ish methods' do
15387
describe '#require' do
15488
it 'parses and resolve #require argument' do
15589
compiler = compiler_for(%Q{require "#{__FILE__}"})

0 commit comments

Comments
 (0)
Please sign in to comment.