Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 4d492f0666e8
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 747c9cdb51e8
Choose a head ref
  • 3 commits
  • 9 files changed
  • 1 contributor

Commits on Apr 14, 2014

  1. Verified

    This commit was signed with the committer’s verified signature.
    sikmir Nikolay Korotkiy
    Copy the full SHA
    f1f6fe8 View commit details
  2. Whitespace

    elia committed Apr 14, 2014
    Copy the full SHA
    3b3bd0b View commit details
  3. Copy the full SHA
    747c9cd View commit details
30 changes: 5 additions & 25 deletions lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ def initialize(options = {})
@compiler_options = options.delete(:compiler_options) || {}
@stubbed_files = options.delete(:stubbed_files) || []
@path_reader = options.delete(:path_reader) || PathReader.new
@compiler_class = options.delete(:compiler_class) || CompilerWrapper
@compiler_class = options.delete(:compiler_class) || Compiler
@erb_compiler_class = options.delete(:erb_compiler_class) || Opal::ERB::Compiler
raise ArgumentError, "unknown options: #{options.keys.join(', ')}" unless options.empty?
end
@@ -20,13 +20,14 @@ def build(path, prerequired = [])

def build_str(source, path = '(file)', prerequired = [])
compiler = compiler_for(source, :file => path)
compiler.compile
sources = []
compiled_requires = {}
prerequired.each {|pr| compiled_requires[pr] = true}

compiler.requires.uniq.each { |r| compile_require(r, sources, compiled_requires) }

sources << compiler.compiled
sources << compiler.result
prerequired.concat(compiled_requires.keys)
sources.join("\n")
end
@@ -58,8 +59,9 @@ def compile_require r, sources, compiled_requires

require_source = prepare_erb(require_source, r) if erb?(r)
require_compiler = compiler_for(require_source, :file => r, :requirable => true)
require_compiler.compile
require_compiler.requires.each { |r| compile_require(r, sources, compiled_requires) }
sources << require_compiler.compiled
sources << require_compiler.result
end

def prepare_erb(source, path)
@@ -73,27 +75,5 @@ def compiler_for(source, options = {})

attr_reader :compiler_class, :path_reader, :compiler_options, :stubbed_files,
:erb_compiler_class

class CompilerWrapper
def initialize(source, options)
@source, @options = source, options
end

def compiled
@compiled ||= compiler.compile(source, options)
end

def requires
@requires ||= compiled && compiler.requires
end

private

def compiler
@compiler ||= Compiler.new
end

attr_reader :source, :options
end
end
end
4 changes: 2 additions & 2 deletions lib/opal/cli.rb
Original file line number Diff line number Diff line change
@@ -123,8 +123,8 @@ def set_processor_options
end

def map
compiler = Opal::Compiler.new
compiler.compile(filename, options)
compiler = Opal::Compiler.new(filename, options)
compiler.compile
compiler.source_map
end

15 changes: 9 additions & 6 deletions lib/opal/compiler.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

module Opal
def self.compile(source, options = {})
Compiler.new.compile(source, options)
Compiler.new(source, options).compile
end

class Compiler
@@ -55,16 +55,19 @@ def self.compiler_option(name, default_value, mid = nil)
# Any content in __END__ special construct
attr_reader :eof_content

def initialize
def initialize(source, options = {})
@source = source
@indent = ''
@unique = 0
@options = {}
@options = options
end

# Compile some ruby code to a string.
def compile(source, options = {})
@source = source
@options.update options
def compile(source = nil, options = nil)
if source or options
raise ArgumentError, 'passing "source" and "options" to #compile is deprecated, pass them to #new instead.'
end

@parser = Parser.new

@sexp = s(:top, @parser.parse(@source, self.file) || s(:nil))
4 changes: 2 additions & 2 deletions lib/opal/old_builder.rb
Original file line number Diff line number Diff line change
@@ -73,8 +73,8 @@ def build_asset(path)
def compile_ruby(str, options = nil)
options ||= @options.clone

compiler = Compiler.new
result = compiler.compile str, options
compiler = Compiler.new(str, options)
result = compiler.compile

compiler.requires.each do |r|
require_asset r
2 changes: 1 addition & 1 deletion lib/opal/sprockets/processor.rb
Original file line number Diff line number Diff line change
@@ -100,7 +100,7 @@ def evaluate(context, locals, &block)
result = builder.build_str(data, path, prerequired)

# prerequired is mutated by the builder
dependencies = prerequired.uniq - stubbed_files.to_a
dependencies = prerequired.uniq - stubbed_files.to_a
dependencies.each { |asset| context.depend_on(asset) }

if self.class.source_map_enabled
22 changes: 11 additions & 11 deletions spec/cli/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -5,12 +5,12 @@
describe Opal::Builder do
subject(:builder) { described_class.new(options) }

let(:options) { {
:path_reader => path_reader,
:compiler_class => compiler_class,
let(:options) { {
:path_reader => path_reader,
:compiler_class => compiler_class,
:erb_compiler_class => erb_compiler_class,
} }

let(:path_reader) { double('path reader') }
let(:compiler_class) { double('compiler_class') }
let(:erb_compiler_class) { double('erb_compiler_class') }
@@ -25,7 +25,7 @@
path_reader.stub(:read) { |path| raise ArgumentError, path }
path_reader.stub(:read).with(filepath) { source }
compiler_class.stub(:new).with(source, :file => filepath) do
double('compiler', :compiled => compiled_source, :requires => requires)
double('compiler', :compile => nil, :result => compiled_source, :requires => requires)
end
end

@@ -56,8 +56,8 @@
before do
path_reader.stub(:read).with(foo_path) { foo_contents }
path_reader.stub(:read).with(bar_path) { bar_contents }
foo_compiler = double('compiler', :compiled => required_foo, :requires => [])
bar_compiler = double('compiler', :compiled => required_bar, :requires => [])
foo_compiler = double('compiler', :compile => nil, :result => required_foo, :requires => [])
bar_compiler = double('compiler', :compile => nil, :result => required_bar, :requires => [])
compiler_class.stub(:new).with(foo_contents, :file => foo_path, :requirable => true) { foo_compiler }
compiler_class.stub(:new).with(bar_contents, :file => bar_path, :requirable => true) { bar_compiler }
end
@@ -86,7 +86,7 @@

before do
options.merge! stubbed_files: [foo_path]
foo_compiler = double('compiler', :compiled => foo_stubbed, :requires => [])
foo_compiler = double('compiler', :compile => nil, :result => foo_stubbed, :requires => [])
compiler_class.stub(:new).with('', :file => foo_path, :requirable => true) { foo_compiler }
end

@@ -110,7 +110,7 @@
let(:foo_stubbed) { 'foo stubbed' }

before do
foo_compiler = double('compiler', :compiled => foo_stubbed, :requires => [])
foo_compiler = double('compiler', :compile => nil, :result => foo_stubbed, :requires => [])
compiler_class.stub(:new).with('', :file => foo_path, :requirable => true) { foo_compiler }
end

@@ -134,10 +134,10 @@

before do
path_reader.stub(:read).with('erb') { erb_lib }
erb_lib_compiler = double('compiler', :compiled => required_erb_lib, :requires => ['erb'])
erb_lib_compiler = double('compiler', :compile => nil, :result => required_erb_lib, :requires => ['erb'])
compiler_class.stub(:new).with(erb_lib, :file => 'erb', :requirable => true) { erb_lib_compiler }

erb_template_compiler = double('compiler', :compiled => compiled_template, :requires => ['erb'])
erb_template_compiler = double('compiler', :compile => nil, :result => compiled_template, :requires => ['erb'])
compiler_class.stub(:new).with(prepared_foo_contents, :file => foo_path, :requirable => true) { erb_template_compiler }

erb_compiler_class.stub(:new).with(foo_contents, foo_path) { erb_compiler }
2 changes: 1 addition & 1 deletion spec/cli/compiler_spec.rb
Original file line number Diff line number Diff line change
@@ -150,6 +150,6 @@
end

def expect_compiled(*args)
expect(Opal::Compiler.new.compile *args)
expect(Opal::Compiler.new(*args).compile)
end
end
22 changes: 11 additions & 11 deletions spec/opal/compiler/irb_spec.rb
Original file line number Diff line number Diff line change
@@ -3,42 +3,42 @@

describe Opal::Compiler do
describe "irb parser option" do
before do
@compiler = Opal::Compiler.new
end

it "creates Opal.irb_vars if it does not exist" do
$global["Opal"].irb_vars = nil
eval_js(@compiler.compile "nil", :irb => true)
eval_js compile("nil", :irb => true)

($global["Opal"].irb_vars == nil).should be_false
end

it "does not create Opal.irb_vars if :irb option not passed" do
$global["Opal"].irb_vars = nil
eval_js(@compiler.compile "nil")
eval_js compile("nil")

($global["Opal"].irb_vars == nil).should be_true
end

it "sets each s(:lasgn) in the top level onto irb_vars" do
eval_js @compiler.compile "foo = 42", :irb => true
eval_js compile("foo = 42", :irb => true)
$global["Opal"].irb_vars.foo.should == 42
end

it "gets each s(:lvar) in the top level from irb_vars" do
eval_js @compiler.compile "foo = 3.142; bar = foo", :irb => true
eval_js compile("foo = 3.142; bar = foo", :irb => true)
$global["Opal"].irb_vars.bar.should == 3.142
end

it "persists local vars between parses" do
eval_js @compiler.compile "foo = 'hello world'", :irb => true
eval_js @compiler.compile "bar = foo.upcase", :irb => true
eval_js compile("foo = 'hello world'", :irb => true)
eval_js compile("bar = foo.upcase", :irb => true)
$global["Opal"].irb_vars.bar.should == "HELLO WORLD"
end

it "can still call top level methods" do
eval_js(@compiler.compile("to_s", :irb => true)).should == "main"
eval_js(compile("to_s", :irb => true)).should == "main"
end

def compile *args
Opal::Compiler.new(*args).compile
end
end
end
4 changes: 2 additions & 2 deletions spec/opal/core/source_map_spec.rb
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
describe Opal::SourceMap do
before do
pathname = 'foo.rb'
compiler = Opal::Compiler.new
@source = compiler.compile("1 + 1", :file => pathname)
compiler = Opal::Compiler.new("1 + 1", :file => pathname)
@source = compiler.compile
@map = Opal::SourceMap.new(compiler.fragments, pathname)
end