Skip to content

Commit

Permalink
Opal::Compiler as sole compiler class
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 23, 2013
1 parent b069185 commit 961416a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 93 deletions.
1 change: 0 additions & 1 deletion lib/opal.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'opal/compiler'
require 'opal/dependency_compiler'
require 'opal/builder'
require 'opal/erb'
require 'opal/version'
Expand Down
4 changes: 2 additions & 2 deletions lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'opal/dependency_compiler'
require 'opal/compiler'
require 'erb'

module Opal
Expand Down Expand Up @@ -70,7 +70,7 @@ def build_asset(path)
end

def compile_ruby(str, options={})
compiler = DependencyCompiler.new
compiler = Compiler.new
result = compiler.compile str, options

compiler.requires.each do |r|
Expand Down
77 changes: 70 additions & 7 deletions lib/opal/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ def compile(source, options = {})
end

def setup_options(options = {})
@file = (options[:file] || '(file)')
@source_file = (options[:source_file] || @file)
@method_missing = (options[:method_missing] != false)
@arity_check = (options[:arity_check])
@const_missing = (options[:const_missing] == true)
@irb_vars = (options[:irb] == true)
@file = (options[:file] || '(file)')
@source_file = (options[:source_file] || @file)
@method_missing = (options[:method_missing] != false)
@arity_check = (options[:arity_check])
@const_missing = (options[:const_missing] == true)
@irb_vars = (options[:irb] == true)
@dynamic_require_severity = (options[:dynamic_require_severity] || :error)
end

# Is method_missing enabled for this file
Expand Down Expand Up @@ -217,7 +218,69 @@ def handlers
# this method. If this method returns nil, then the method will continue
# to be generated by CallNode.
def handle_call(sexp)
nil
case sexp[2]
when :require
return handle_require(sexp[3][1])
when :autoload
if @scope.class_scope?
return handle_require(sexp[3][2])
end
else
nil
end
end

# An array of requires used in this file
def requires
@requires ||= []
end

def handle_require(sexp)
str = handle_require_sexp sexp
requires << str unless str.nil?
fragment("", sexp)
end

def handle_require_sexp(sexp)
type = sexp.shift

if type == :str
return sexp[0]
elsif type == :call
recv, meth, args = sexp
parts = args[1..-1].map { |s| handle_require_sexp s }

if recv == [:const, :File]
if meth == :expand_path
return handle_expand_path(*parts)
elsif meth == :join
return handle_expand_path parts.join("/")
elsif meth == :dirname
return handle_expand_path parts[0].split("/")[0...-1].join("/")
end
end
end

case @dynamic_require_severity
when :error
error "Cannot handle dynamic require"
when :warning
warning "Cannot handle dynamic require"
end
end

def handle_expand_path(path, base = '')
"#{base}/#{path}".split("/").inject([]) do |p, part|
if part == ''
# we had '//', so ignore
elsif part == '..'
p.pop
else
p << part
end

p
end.join "/"
end

# The last sexps in method bodies, for example, need to be returned
Expand Down
82 changes: 0 additions & 82 deletions lib/opal/dependency_compiler.rb

This file was deleted.

2 changes: 1 addition & 1 deletion stdlib/opal-source-maps.js.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<% require_asset 'source_map' %>
<%= Opal::DependencyCompiler.compile File.read(File.join Opal.core_dir, '..', 'lib', "opal/source_map.rb") %>
<%= Opal.compile File.read(File.join Opal.core_dir, '..', 'lib', "opal/source_map.rb") %>

0 comments on commit 961416a

Please sign in to comment.