Class: RKelly::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/rkelly/runtime.rb,
lib/rkelly/runtime/scope_chain.rb,
lib/rkelly/runtime/ruby_function.rb

Defined Under Namespace

Classes: RubyFunction, ScopeChain

Constant Summary collapse

UNDEFINED =
RKelly::JS::Property.new(:undefined, :undefined)

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Returns a new instance of Runtime.



8
9
10
11
# File 'lib/rkelly/runtime.rb', line 8

def initialize
  @parser = Parser.new
  @scope  = ScopeChain.new
end

Instance Method Details

#call_function(function_name, *args) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/rkelly/runtime.rb', line 23

def call_function(function_name, *args)
  function = @scope[function_name].value
  @scope.new_scope { |chain|
    function.js_call(chain, *(args.map { |x|
      RKelly::JS::Property.new(:param, x)
    }))
  }.value
end

#define_function(function, &block) ⇒ Object



32
33
34
# File 'lib/rkelly/runtime.rb', line 32

def define_function(function, &block)
  @scope[function.to_s].function = block
end

#execute(js) ⇒ Object

Execute js



14
15
16
17
18
19
20
21
# File 'lib/rkelly/runtime.rb', line 14

def execute(js)
  function_visitor  = Visitors::FunctionVisitor.new(@scope)
  eval_visitor      = Visitors::EvaluationVisitor.new(@scope)
  tree = @parser.parse(js)
  function_visitor.accept(tree)
  eval_visitor.accept(tree)
  @scope
end