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: 57082385c382^
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 57d77fb97c84
Choose a head ref
  • 14 commits
  • 27 files changed
  • 1 contributor

Commits on Oct 11, 2014

  1. Fix require_tree

    elia committed Oct 11, 2014

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    5708238 View commit details
  2. Add #paths to PathReader classes

    elia committed Oct 11, 2014

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    7393e1c View commit details
  3. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    30d36b4 View commit details
  4. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    90814a0 View commit details
  5. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    d5b32f8 View commit details
  6. Add File.basename

    elia committed Oct 11, 2014

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    8f5b2c2 View commit details
  7. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    c9dc008 View commit details
  8. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    d7d9f23 View commit details
  9. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    b726b15 View commit details
  10. Fix opal-mspec glob

    elia committed Oct 11, 2014

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    b305294 View commit details
  11. Remove debug output

    elia committed Oct 11, 2014

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    4d2bb0d View commit details
  12. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    1efb729 View commit details
  13. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    5d8dddd View commit details
  14. Remove debug output

    elia committed Oct 11, 2014

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    57d77fb View commit details
2 changes: 1 addition & 1 deletion bin/opal-mspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

specs = ARGV.map do |s|
s.end_with?('.rb') ? s : "#{s}/**/**/*_spec.rb"
s.end_with?('.rb') ? s : "#{s}/**/*_spec.rb"
end
pattern = %Q{MSPEC_PATTERN="{#{specs.join(',')}}"} if specs.any?
command = [pattern, 'rake mspec'].compact.join(' ')
3 changes: 2 additions & 1 deletion lib/mspec/opal/rake_task.rb
Original file line number Diff line number Diff line change
@@ -205,7 +205,8 @@ def files_to_run(pattern=nil)

if pattern
# add custom opal specs from spec/
add_files paths_from_glob(pattern) & rubyspec_white_list, :custom_pattern
add_files paths_from_glob(pattern) & rubyspec_white_list, :rubyspec_custom_pattern
add_files paths_from_glob(pattern).grep(/(?!spec\/(corelib|stdlib)\/)/), :other_custom_pattern

else
# add opal specific specs
9 changes: 9 additions & 0 deletions lib/mspec/opal/runner.rb
Original file line number Diff line number Diff line change
@@ -133,6 +133,15 @@ def red(str)
def log(str)
`console.log(str)`
end

def after(state)
super
unless exception?
print '.'
else
print failure? ? 'F' : 'E'
end
end
end

class PhantomDebugFormatter < PhantomFormatter
17 changes: 17 additions & 0 deletions lib/mspec/opal/sprockets.js
Original file line number Diff line number Diff line change
@@ -14,6 +14,23 @@ page.onInitialized = function() {
});
};

var system = require('system');
page.onCallback = function(data) {
switch (data[0]) {
case 'exit':
var status = data[1] || 0;
phantom.exit(status);
case 'stdout':
system.stdout.write(data[1] || '');
break;
case 'stderr':
system.stderr.write(data[1] || '');
break;
default:
console.error('Unknown callback data: ', data);
}
};

page.open(args[0], function(status) {
if (status !== 'success') {
console.error("Cannot load: " + args[0]);
45 changes: 34 additions & 11 deletions lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ def self.build(*args, &block)
end

def build(path, options = {})
source = path_reader.read(path)
source = read(path)
build_str(source, path, options)
end

@@ -63,11 +63,25 @@ def source_map
private

def tree_requires(asset, path)
if path.nil? or path.empty?
dirname = Dir.pwd
else
dirname = File.dirname(File.expand_path(path))
end

paths = path_reader.paths.map{|p| File.expand_path(p)}

asset.required_trees.flat_map do |tree|
base = File.expand_path(File.dirname(path))
expanded = File.expand_path File.join(base, tree, '*.rb')
Dir[expanded].map do |file|
file.gsub(/(\.js)?(\.(?:rb|opal))/, '')[(base.size+1)..-1]
expanded = File.expand_path(tree, dirname)
base = paths.find { |p| expanded.start_with?(p) }
next [] if base.nil?

globs = []
globs << File.join(base, tree, '*.rb')
globs << File.join(base, tree, '*.opal')
globs << File.join(base, tree, '*.js')
Dir[*globs].map do |file|
Pathname(file).relative_path_from(Pathname(base)).to_s.gsub(/(\.js)?(\.(?:rb|opal))/, '')
end
end
end
@@ -78,25 +92,34 @@ def processor_for(source, filename, path, options)
return processor.new(source, filename, compiler_options.merge(options))
end

def read(path)
path_reader.read(path) or
raise ArgumentError, "can't find file: #{path.inspect} in #{path_reader.paths.inspect}"
end

def process_require(filename, options)
return if prerequired.include?(filename)
return if already_processed.include?(filename)
already_processed << filename

source = stub?(filename) ? '' : path_reader.read(filename)
source = stub?(filename) ? '' : read(filename)

if source.nil? && @compiler_options[:dynamic_require_severity] != :error
raise LoadError, "can't find file: #{filename.inspect}"
if source.nil?
message = "can't find file: #{filename.inspect}"
case @compiler_options[:dynamic_require_severity]
when :error then raise LoadError, message
when :warning then warn "can't find file: #{filename.inspect}"
end
end

path = path_reader.expand(filename).to_s unless stub?(filename)
asset = processor_for(source, filename, path, options.merge(requirable: true))
process_requires(asset, options)
process_requires(asset.requires+tree_requires(asset, path), options)
processed << asset
end

def process_requires(asset, options)
asset.requires.map { |r| process_require(r, options) }
def process_requires(requires, options)
requires.map { |r| process_require(r, options) }
end

def already_processed
5 changes: 5 additions & 0 deletions lib/opal/cli_runners/phantom.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,11 @@ page.onCallback = function(data) {
case 'exit':
var status = data[1] || 0;
phantom.exit(status);
case 'stdout':
system.stdout.write(data[1] || '');
break;
case 'stderr':
system.stderr.write(data[1] || '');
break;
default:
console.error('Unknown callback data: ', data);
7 changes: 5 additions & 2 deletions lib/opal/nodes/call.rb
Original file line number Diff line number Diff line change
@@ -187,12 +187,15 @@ def compile_default?
end

add_special :require_tree do
compile_default!
arg = arglist[1]
if arg[0] == :str
dir = File.dirname(compiler.file)
compiler.required_trees << arg[1]
relative_path = arg[1]
full_path = Pathname(dir).join(relative_path).cleanpath.to_s
compiler.required_trees << full_path
arg[1] = full_path
end
compile_default!
push fragment('')
end

4 changes: 4 additions & 0 deletions lib/opal/path_reader.rb
Original file line number Diff line number Diff line change
@@ -16,6 +16,10 @@ def expand(path)
file_finder.find(path)
end

def paths
file_finder.paths
end


private

4 changes: 4 additions & 0 deletions lib/opal/sprockets/path_reader.rb
Original file line number Diff line number Diff line change
@@ -23,6 +23,10 @@ def expand path
env.resolve(path)
end

def paths
env.paths
end

attr_reader :env, :context
end

56 changes: 56 additions & 0 deletions opal/corelib/file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class File < IO
Separator = SEPARATOR = '/'

class << self
def expand_path(path, basedir = nil)
path = [basedir, path].compact.join(SEPARATOR)
parts = path.split(SEPARATOR)
new_parts = []
parts[0] = Dir.home if parts.first == '~'

parts.each do |part|
if part == '..'
new_parts.pop
else
new_parts << part
end
end
new_parts.join(SEPARATOR)
end

def dirname(path)
split(path)[0..-2]
end

def basename(path)
split(path)[-1]
end

def exist? path
`Opal.modules[#{path}] != null`
end
alias exists? exist?

def directory?(path)
files = []
%x{
for (var key in Opal.modules) {
#{files}.push(key)
}
}
path = path.gsub(%r{(^.#{SEPARATOR}+|#{SEPARATOR}+$)})
file = files.find do |file|
file =~ /^#{path}/
end
file
end

def join(*paths)
paths.join(SEPARATOR).gsub(%r{#{SEPARATOR}+}, SEPARATOR)
end

def split(path)
path.split(SEPARATOR)
end
end
end
16 changes: 16 additions & 0 deletions opal/corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -553,6 +553,22 @@ def require_relative file
`$opal.require( $opal.normalize_loadable_path(#{file}) )`
end

# `path` should be the ful path to be found in registered modules (`Opal.modules`)
def require_tree path
path = File.expand_path(path)
%x{
var file_name, file_names = Object.keys($opal.modules);
path = path.replace(/\/?$/, '/');
for (var i = 0; i < file_names.length; i++) {
file_name = file_names[i];
if (file_name['$start_with?'](path)) {
$opal.require(file_name);
}
}
}
nil
end

def load file
`$opal.load( $opal.normalize_loadable_path(#{file}) )`
end
1 change: 1 addition & 0 deletions opal/opal.rb
Original file line number Diff line number Diff line change
@@ -29,3 +29,4 @@
require 'corelib/main'
require 'corelib/variables'
require 'corelib/dir'
require 'corelib/file'
2 changes: 1 addition & 1 deletion spec/lib/builder_processors_spec.rb
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@

it 'fills required_trees' do
processor = described_class.new('require_tree "./pippo"', '-e')
expect(processor.required_trees).to eq(['./pippo'])
expect(processor.required_trees).to eq(['pippo'])
end
end

4 changes: 2 additions & 2 deletions spec/lib/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -12,8 +12,8 @@

it 'respect #require_tree calls' do
begin
Opal.append_path(File.expand_path('../fixtures/', __FILE__))
expect(builder.build('require_tree_test').to_s).to match('required_file1')
Opal.append_path(File.expand_path('..', __FILE__))
expect(builder.build('fixtures/require_tree_test').to_s).to include('Opal.modules["fixtures/required_tree_test/required_file1"]')
ensure
Opal.instance_variable_set('@paths', nil)
end
2 changes: 1 addition & 1 deletion spec/lib/compiler_spec.rb
Original file line number Diff line number Diff line change
@@ -182,7 +182,7 @@

it 'parses and resolve #require argument' do
compiler = compiler_for(file.read)
expect(compiler.required_trees).to eq(['./required_tree_test'])
expect(compiler.required_trees).to eq(['../fixtures/required_tree_test'])
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/fixtures/require_tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require_tree './required_tree_test'
require_tree '../fixtures/required_tree_test'

puts 5
1 change: 1 addition & 0 deletions spec/lib/path_reader_spec.rb
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
before do
path_finder.stub(:find) {|path| nil}
path_finder.stub(:find).with(path).and_return(full_path)
path_finder.stub(:paths).and_return(Opal.paths)
end

include_examples :path_finder
34 changes: 24 additions & 10 deletions spec/lib/shared/path_reader_shared.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
require 'lib/spec_helper'

# Below the helpers expected from a spec that
# includes these shared examples:
#
# @object [PathReader] path_reader the object under test
# @method [String] path the path we want to read
# @method [String] contents the contents we expect to be read
#
shared_examples :path_reader do
# @param path
# the path we want to read
#
# @param contents
# the contents we expect to be read
#
it 'responds to #path' do
expect(path_reader.read(path)).to eq(contents)
describe '#paths' do
it 'is an Enumberable' do
expect(path_reader.paths).to be_an(Enumerable)
end

it 'includes Opal.paths' do
paths = path_reader.paths.to_a
Opal.paths.each { |path| expect(paths).to include(path) }
end
end

it 'returns nil if the file is missing' do
expect(path_reader.read('unexpected-path!')).to be_nil
describe '#read' do
it 'responds to #path' do
expect(path_reader.read(path)).to eq(contents)
end

it 'returns nil if the file is missing' do
expect(path_reader.read('unexpected-path!')).to be_nil
end
end
end
1 change: 1 addition & 0 deletions spec/opal/core/fixtures/require_tree_files/file 1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ScratchPad << File.basename(__FILE__)
1 change: 1 addition & 0 deletions spec/opal/core/fixtures/require_tree_files/file 2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ScratchPad << File.basename(__FILE__)
1 change: 1 addition & 0 deletions spec/opal/core/fixtures/require_tree_files/file 3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ScratchPad << File.basename(__FILE__)
1 change: 1 addition & 0 deletions spec/opal/core/fixtures/require_tree_files/file 4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ScratchPad << File.basename(__FILE__)
1 change: 1 addition & 0 deletions spec/opal/core/fixtures/require_tree_files/file 5.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ScratchPad << File.basename(__FILE__)
7 changes: 7 additions & 0 deletions spec/opal/core/kernel/require_tree_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe 'Kernel.require_tree' do
it 'loads all the files in a directory' do
$ScratchPad = []
require_tree '../fixtures/require_tree_files'
$ScratchPad.should == ['file 1', 'file 2', 'file 3', 'file 4', 'file 5']
end
end
7 changes: 6 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -27,7 +27,12 @@ def at_exit(&block)
end
end

formatter_class = `!!window.OPAL_SPEC_PHANTOM` ? PhantomFormatter : BrowserFormatter
if `!!window.OPAL_SPEC_PHANTOM`
require 'phantomjs'
formatter_class = PhantomFormatter
else
formatter_class = BrowserFormatter
end

# Uncomment the following to see example titles when they're executed.
# (useful to relate debug output to the example that generated it)
53 changes: 1 addition & 52 deletions stdlib/file.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1 @@
class File < IO
Separator = SEPARATOR = '/'

class << self
def expand_path(path, basedir = nil)
path = [basedir, path].compact.join(SEPARATOR)
parts = path.split(SEPARATOR)
new_parts = []
parts[0] = Dir.home if parts.first == '~'

parts.each do |part|
if part == '..'
new_parts.pop
else
new_parts << part
end
end
new_parts.join(SEPARATOR)
end

def dirname(path)
split(path)[-2]
end

def exist? path
`Opal.modules[#{path}] != null`
end
alias exists? exist?

def directory?(path)
files = []
%x{
for (var key in Opal.modules) {
#{files}.push(key)
}
}
path = path.gsub(%r{(^.#{SEPARATOR}+|#{SEPARATOR}+$)})
file = files.find do |file|
file =~ /^#{path}/
end
file
end

def join(*paths)
paths.join(SEPARATOR).gsub(%r{#{SEPARATOR}+}, SEPARATOR)
end

def split(path)
path.split(SEPARATOR)
end
end
end
warn "File is already part of corelib now, you don't need to require it anymore."
5 changes: 4 additions & 1 deletion stdlib/phantomjs.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Kernel
def exit status = 0
`callPhantom(["exit", #{status}]);`
`callPhantom(['exit', status]);`
end
end

STDOUT.write_proc = `function(str){callPhantom(['stdout', str])}`
STDERR.write_proc = `function(str){callPhantom(['stderr', str])}`