mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Express exec in terms of eval
This commit is contained in:
parent
a8a2d80269
commit
a3b32250cc
1 changed files with 12 additions and 13 deletions
|
@ -10,14 +10,25 @@ module ExecJS
|
|||
end
|
||||
|
||||
def exec(source, options = {})
|
||||
eval "(function(){#{source}})()"
|
||||
end
|
||||
|
||||
def eval(source, options = {})
|
||||
source = encode(source)
|
||||
unwrap(@ctx.eval_string("(function() { #{source} })();", '(execjs)'))
|
||||
|
||||
if /\S/ =~ source
|
||||
unwrap(@ctx.eval_string("(#{source})", '(execjs)'))
|
||||
end
|
||||
rescue Duktape::SyntaxError => e
|
||||
raise RuntimeError, e.message
|
||||
rescue Duktape::Error => e
|
||||
raise ProgramError, e.message
|
||||
end
|
||||
|
||||
def call(identifier, *args)
|
||||
eval "#{identifier}.apply(this, #{::JSON.generate(args)})"
|
||||
end
|
||||
|
||||
def unwrap(obj)
|
||||
case obj
|
||||
when ::Duktape::ComplexObject
|
||||
|
@ -36,18 +47,6 @@ module ExecJS
|
|||
obj
|
||||
end
|
||||
end
|
||||
|
||||
def eval(source, options = {})
|
||||
source = encode(source)
|
||||
|
||||
if /\S/ =~ source
|
||||
exec("return eval(#{::JSON.generate("(#{source})", quirks_mode: true)})")
|
||||
end
|
||||
end
|
||||
|
||||
def call(identifier, *args)
|
||||
eval "#{identifier}.apply(this, #{::JSON.generate(args)})"
|
||||
end
|
||||
end
|
||||
|
||||
def name
|
||||
|
|
Loading…
Add table
Reference in a new issue