Class: RBelly::JS::Function

Inherits:
Base
  • Object
show all
Defined in:
lib/rbelly/js/function.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#properties, #return, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #can_put?, #default_value, #delete, #has_property?, #returned?

Constructor Details

#initialize(body = nil, arguments = []) ⇒ Function

Returns a new instance of Function.



19
20
21
22
23
24
25
26
# File 'lib/rbelly/js/function.rb', line 19

def initialize(body = nil, arguments = [])
  super()
  @body = body
  @arguments = arguments
  self['prototype'] = JS::FunctionPrototype.new(self)
  self['toString'] = :undefined
  self['length'] = arguments.length
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



18
19
20
# File 'lib/rbelly/js/function.rb', line 18

def arguments
  @arguments
end

#bodyObject (readonly)

Returns the value of attribute body.



18
19
20
# File 'lib/rbelly/js/function.rb', line 18

def body
  @body
end

Class Method Details

.create(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rbelly/js/function.rb', line 5

def create(*args)
  if args.length > 0
    parser = RBelly::Parser.new
    body = args.pop
    tree = parser.parse("function x(#{args.join(',')}) { #{body} }")
    func = tree.value.first
    self.new(func.function_body, func.arguments)
  else
    self.new
  end
end

Instance Method Details

#js_call(scope_chain, *params) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/rbelly/js/function.rb', line 28

def js_call(scope_chain, *params)
  arguments.each_with_index { |name, i|
    scope_chain[name.value] = params[i] || RBelly::Runtime::UNDEFINED
  }
  function_visitor  = RBelly::Visitors::FunctionVisitor.new(scope_chain)
  eval_visitor      = RBelly::Visitors::EvaluationVisitor.new(scope_chain)
  body.accept(function_visitor) if body
  body.accept(eval_visitor) if body
end