lib/opal/context.rb in opal-0.3.10 vs lib/opal/context.rb in opal-0.3.11

- old
+ new

@@ -1,61 +1,35 @@ module Opal class Context - - def initialize(root_dir = Dir.getwd) - @root_dir = root_dir - @builder = Opal::Builder.new + # Options are mainly just passed onto the builder/parser. + def initialize(options = {}) + @options = options + @root_dir = options[:dir] || Dir.getwd + @builder = Opal::Builder.new @loaded_paths = false - # special case: if we are running in opal root, then we dont want - # setup.rb to load the opal lib itself, so we do some "magic" - if @root_dir == OPAL_DIR - def self.setup_load_paths - return if @loaded_paths - Dir['packages/*/package.yml'].map do |package| - path = File.expand_path File.join(File.dirname(package), 'lib') - @v8.eval "opal.loader.paths.push('#{path}')" - end - end - end - setup_v8 end ## - # Looks through vendor/ directory and adds all relevant load paths - - def setup_load_paths - return if @loaded_paths - - setup = File.join @root_dir, 'packages', 'init.rb' - return [] unless File.exists? setup - - @v8.eval "opal.run(function() {opal.require('#{setup}');});", setup - end - - ## # Require the given id as if it was required in the context. This simply # passes the require through to the underlying context. def require_file(path) setup_v8 @v8.eval "opal.run(function() {opal.require('#{path}');});", path finish end - ## - # Set ARGV for the context - + # Set ARGV for the context. + # @param [Array<String>] args def argv=(args) puts "setting argv to #{args.inspect}" @v8.eval "opal.runtime.cs(opal.runtime.Object, 'ARGV', #{args.inspect});" end - ## # Start normal js repl - def start_repl require 'readline' setup_v8 loop do @@ -72,30 +46,24 @@ end finish end - def eval(content, file = nil, line = "") - begin - js = @builder.parse content - code = "opal.run(function() { var $rb = opal.runtime, self = $rb.top" - code += ", __FILE__ = '(opal)'; return (#{js})($rb, self, __FILE__); })" - # puts code - @v8['$opal_irb_result'] = @v8.eval(code, file) - @v8.eval "!($opal_irb_result == null || !$opal_irb_result.m$inspect) ? $opal_irb_result.m$inspect() : '(Object does not support #inspect)'" - rescue => e - puts e - puts("\t" + e.backtrace.join("\n\t")) - end + def eval(content, file = "(opal)", line = "") + js = @builder.parse content, @options + code = "opal.run(function() { var result = (#{js})(opal.runtime, " + code += "opal.runtime.top, '(opal)'); if (result == null || !result" + code += ".m$inspect) { return '(Object does not support #inspect)'; }" + code += "else { return result.m$inspect() } });" + + @v8.eval code, file 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 # #setup_v8 - def finish return unless @v8 @v8.eval "opal.runtime.do_at_exit()", "(opal)" @v8 = nil @@ -114,17 +82,15 @@ end @v8 = V8::Context.new @v8['console'] = Console.new - @v8.eval @builder.build_core, '(opal)' + @v8.eval File.read(OPAL_JS_PATH), "(opal)" opal = @v8['opal'] opal['fs'] = FileSystem.new self # FIXME: we cant use a ruby array as a js array :( opal['loader'] = Loader.new self, @v8.eval("[]") - - setup_load_paths end ## # Console class is used to mimic the console object in web browsers # to allow simple debugging to the stdout.