Files
metasploit-gs/lib/rkelly/js/function.rb
T
James Lee 7458abc8b3 add rkelly, a javascript parsing library. this version fixes several bugs in the abandoned gem, see https://github.com/tenderlove/rkelly/pull/6
git-svn-id: file:///home/svn/framework3/trunk@12815 4d416f70-5f16-0410-b530-b9f4589650da
2011-06-02 06:31:34 +00:00

40 lines
1.2 KiB
Ruby

module RKelly
module JS
class Function < Base
class << self
def create(*args)
if args.length > 0
parser = RKelly::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
end
attr_reader :body, :arguments
def initialize(body = nil, arguments = [])
super()
@body = body
@arguments = arguments
self['prototype'] = JS::FunctionPrototype.new(self)
self['toString'] = :undefined
self['length'] = arguments.length
end
def js_call(scope_chain, *params)
arguments.each_with_index { |name, i|
scope_chain[name.value] = params[i] || RKelly::Runtime::UNDEFINED
}
function_visitor = RKelly::Visitors::FunctionVisitor.new(scope_chain)
eval_visitor = RKelly::Visitors::EvaluationVisitor.new(scope_chain)
body.accept(function_visitor) if body
body.accept(eval_visitor) if body
end
end
end
end