lib/opal/context.rb in opal-0.3.15 vs lib/opal/context.rb in opal-0.3.16
- old
+ new
@@ -8,30 +8,34 @@
attr_reader :environment
##
# Glob may be a file or glob path, as a string.
- def self.runner(glob)
- ctx = self.new
- ctx.v8['opal_tmp_glob'] = Dir[glob]
+ def self.runner(glob, debug = true)
+ ctx = self.new debug
+ files = Dir[glob]
+ ctx.v8['opal_tmp_glob'] = files
+ main = File.expand_path files.first unless files.empty?
+
runner = <<-CODE
files = `opal_tmp_glob`
+ $0 = '#{main}'
files.each do |a|
require a
end
CODE
ctx.eval_irb runner, '(runner)'
ctx.finish
end
- def initialize root = Dir.getwd
- @environment = Environment.load root
- @root = root
- @parser = Opal::Parser.new :debug => true
+ def initialize(debug = true)
+ @debug = true
+ @environment = Environment.load Dir.getwd
+ @parser = Opal::Parser.new :debug => debug
@loaded_paths = false
setup_v8
end
@@ -44,11 +48,11 @@
# on SIGINT lets just return from the loop..
trap("SIGINT") { finish; return }
line = Readline.readline '>> ', true
# if we type exit, then we need to close down context
- if line == "exit"
+ if line == "exit" or line.nil?
break
end
puts "=> #{eval_irb line, '(irb)'}"
end
@@ -67,20 +71,27 @@
def eval_irb(content, file = '(irb)')
code = <<-CODE
(function() { try {
opal.FILE = '#{file}';
var res = #{ eval_builder content, file };
- return res.m$inspect();
+ return res.$inspect();
}
catch (e) {
- opal.bt(e);
+ console.log(e.$klass.$name + ': ' + e.message);
+ console.log("\\t" + e.$backtrace().join("\\n\\t"));
return "nil";
}
})()
CODE
@v8.eval code, file
+ rescue Opal::OpalParseError => e
+ puts "ParseError: #{e.message}"
+ "nil"
+ rescue V8::JSError => e
+ puts "SyntaxError: #{e.message}"
+ "nil"
end
# Finishes the context, i.e. tidy everything up. This will cause
# the opal runtime to do it's at_exit() calls (if applicable) and
# then the v8 context will de removed. It can be reset by calling
@@ -115,23 +126,20 @@
##
# Loads the runtime from build/*.js. This isnt included in the git repo,
# so needs to be built before running (gems should have these files included)
def load_runtime
- @v8.eval Opal.runtime_debug_code, '(runtime)'
+ code = @debug ? Opal.runtime_debug_code : Opal.runtime_code
+ @v8.eval code, '(runtime)'
end
##
# Load gem specific runtime.
def load_gem_runtime
- dir = File.join Opal.opal_dir, 'runtime', 'gemlib'
- order = File.read(File.join dir, 'load_order').strip.split("\n")
- order.each do |f|
- path = File.join dir, "#{f}.rb"
- eval File.read(path), path
- end
+ path = File.join Opal.opal_dir, 'core', 'gemlib.rb'
+ eval File.read(path), path
end
##
# Console class is used to mimic the console object in web browsers
# to allow simple debugging to the stdout.
@@ -160,18 +168,32 @@
# Used to bootstrap loadpaths.
def find_paths
return @paths if @paths
- paths = [File.join(Opal.opal_dir, 'runtime', 'stdlib')]
+ paths = []
@environment.require_paths.each do |p|
- paths << File.join(@environment.root, p)
+ dir = File.join(@environment.root, p)
+ paths << dir
+
+ opal_dir = File.join dir, 'opal'
+ paths << opal_dir if File.exists? opal_dir
end
@environment.specs.each do |spec|
- paths.push *spec.load_paths
+ gemspec = @environment.find_spec spec
+ next unless gemspec
+
+ gemspec.require_paths.each do |r|
+ dir = File.join(gemspec.full_gem_path, r)
+ paths << dir
+
+ opal_dir = File.join dir, 'opal'
+ paths << opal_dir if File.exists? opal_dir
+ end
+ #paths.push *gemspec.load_paths
end
@paths = @context.v8.eval paths.inspect
end
@@ -187,25 +209,24 @@
@cache[resolved] = true
@context.v8.eval "opal.FILE = '#{resolved}'"
@context.eval File.read(resolved), resolved
- true
+ resolved
end
def find_lib path, paths
paths.each do |l|
candidate = File.join l, "#{path}.rb"
return candidate if File.exists? candidate
candidate = File.join l, path
return candidate if File.exists? candidate
+ end
- candidate = File.expand_path path
- return candidate if File.exists? candidate
-
- candidate = File.expand_path("#{path}.rb")
- return candidate if File.exists? candidate
+ abs = File.expand_path path
+ [abs, abs + '.rb'].each do |c|
+ return c if File.exists?(c) && !File.directory?(c)
end
nil
end