Skip to content

Commit

Permalink
Add Kernel#eval to stdlib/opal-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 20, 2013
1 parent a0b3b74 commit 2fcf583
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 47 deletions.
6 changes: 0 additions & 6 deletions corelib/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,6 @@ def frozen?
@___frozen___ || false
end

def eval(str)
raise NotImplementedError unless defined?(Opal::Parser)
code = Opal::Parser.new.parse str
`eval(#{code})`
end

def respond_to_missing? method_name
false
end
Expand Down
14 changes: 7 additions & 7 deletions spec/opal/parser/irb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@

it "creates Opal.irb_vars if it does not exist" do
$global["Opal"].irb_vars = nil
opal_eval_compiled(@parser.parse "nil", :irb => true)
eval_js(@parser.parse "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
opal_eval_compiled(@parser.parse "nil")
eval_js(@parser.parse "nil")

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

it "sets each s(:lasgn) in the top level onto irb_vars" do
opal_eval_compiled @parser.parse "foo = 42", :irb => true
eval_js @parser.parse "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
opal_eval_compiled @parser.parse "foo = 3.142; bar = foo", :irb => true
eval_js @parser.parse "foo = 3.142; bar = foo", :irb => true
$global["Opal"].irb_vars.bar.should == 3.142
end

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

it "can still call top level methods" do
opal_eval_compiled(@parser.parse("to_s", :irb => true)).should == "main"
eval_js(@parser.parse("to_s", :irb => true)).should == "main"
end
end
end
32 changes: 16 additions & 16 deletions spec/parser/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@
end

it "should parse simple ruby values" do
opal_eval('3.142').should == 3.142
opal_eval('123e1').should == 1230.0
opal_eval('123E+10').should == 1230000000000.0
opal_eval('123e-9').should == 0.000000123
opal_eval('false').should == false
opal_eval('true').should == true
opal_eval('nil').should == nil
eval('3.142').should == 3.142
eval('123e1').should == 1230.0
eval('123E+10').should == 1230000000000.0
eval('123e-9').should == 0.000000123
eval('false').should == false
eval('true').should == true
eval('nil').should == nil
end

it "should parse ruby strings" do
opal_eval('"hello world"').should == "hello world"
opal_eval('"hello #{100}"').should == "hello 100"
eval('"hello world"').should == "hello world"
eval('"hello #{100}"').should == "hello 100"
end

it "should parse method calls" do
opal_eval("[1, 2, 3, 4].inspect").should == "[1, 2, 3, 4]"
opal_eval("[1, 2, 3, 4].map { |a| a + 42 }").should == [43, 44, 45, 46]
eval("[1, 2, 3, 4].inspect").should == "[1, 2, 3, 4]"
eval("[1, 2, 3, 4].map { |a| a + 42 }").should == [43, 44, 45, 46]
end

it "should parse constant lookups" do
opal_eval("Object").should == Object
opal_eval("Array").should == Array
opal_eval("Opal::Parser").should == Opal::Parser
eval("Object").should == Object
eval("Array").should == Array
eval("Opal::Parser").should == Opal::Parser
end

it "should parse class and module definitions" do
opal_eval("class ParserModuleDefinition; end")
opal_eval <<-STR
eval("class ParserModuleDefinition; end")
eval <<-STR
class ParserClassDefinition
CONSTANT = 500
Expand Down
28 changes: 11 additions & 17 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
require 'opal'
require 'file'
require 'opal-parser'
require 'mspec'
require 'mspec/mock_install_method_patch'

ENV['MSPEC_RUNNER'] = true

module Kernel
def opal_parse(str, file='(string)')
Opal::Grammar.new.parse str, file
end

def eval_js(javascript)
`eval(javascript)`
end
end

class Encoding
class << self
attr_accessor :default_external
Expand Down Expand Up @@ -134,7 +145,6 @@ def finish_with_code(code)
end
end


class PhantomFormatter < BrowserFormatter
def green(str)
`console.log('\\033[32m' + str + '\\033[0m')`
Expand All @@ -149,10 +159,6 @@ def log(str)
end
end

class File
def self.expand_path(*a); nil; end
end

class ExceptionState
def initialize(state, location, exception)
@exception = exception
Expand All @@ -170,18 +176,6 @@ def initialize(state, location, exception)
end
end

module Kernel
def opal_parse(str, file='(string)')
Opal::Grammar.new.parse str, file
end

def opal_eval_compiled(javascript)
`eval(javascript)`
end

alias_method :opal_eval, :eval
end

module MSpec
def self.opal_runner
@env = Object.new
Expand Down
2 changes: 1 addition & 1 deletion stdlib/file.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class File
SEPARATOR = '/'

def self.expand_path(path)
def self.expand_path(path, *)
path
end
end
File renamed without changes.
8 changes: 8 additions & 0 deletions stdlib/opal-parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'opal-gem'

module Kernel
def eval(str)
code = Opal::Parser.new.parse str
`eval(#{code})`
end
end

0 comments on commit 2fcf583

Please sign in to comment.