From a3b32250ccdf3a8d8c5f59c0e468a3ad62f810e8 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 9 Dec 2014 14:56:43 -0800 Subject: [PATCH] Express exec in terms of eval --- lib/execjs/duktape_runtime.rb | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/execjs/duktape_runtime.rb b/lib/execjs/duktape_runtime.rb index 4417cdb..bae656c 100644 --- a/lib/execjs/duktape_runtime.rb +++ b/lib/execjs/duktape_runtime.rb @@ -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