lib/execjs/external_runtime.rb in execjs-1.3.0 vs lib/execjs/external_runtime.rb in execjs-1.3.1

- old
+ new

@@ -1,5 +1,6 @@ +require "multi_json" require "shellwords" require "tempfile" module ExecJS class ExternalRuntime @@ -13,11 +14,11 @@ def eval(source, options = {}) source = source.encode('UTF-8') if source.respond_to?(:encode) if /\S/ =~ source - exec("return eval(#{MultiJson.encode("(#{source})")})") + exec("return eval(#{json_encode("(#{source})")})") end end def exec(source, options = {}) source = source.encode('UTF-8') if source.respond_to?(:encode) @@ -27,11 +28,11 @@ extract_result(@runtime.send(:exec_runtime, file.path)) end end def call(identifier, *args) - eval "#{identifier}.apply(this, #{MultiJson.encode(args)})" + eval "#{identifier}.apply(this, #{json_encode(args)})" end protected def compile_to_tempfile(source) tempfile = Tempfile.open(['execjs', '.js']) @@ -47,20 +48,20 @@ output.sub!('#{source}') do source end output.sub!('#{encoded_source}') do encoded_source = encode_unicode_codepoints(source) - MultiJson.encode("(function(){ #{encoded_source} })()") + json_encode("(function(){ #{encoded_source} })()") end output.sub!('#{json2_source}') do IO.read(ExecJS.root + "/support/json2.js") end end end def extract_result(output) - status, value = output.empty? ? [] : MultiJson.decode(output) + status, value = output.empty? ? [] : json_decode(output) if status == "ok" value elsif value =~ /SyntaxError:/ raise RuntimeError, value else @@ -81,10 +82,30 @@ [\xF0-\xF7][\x80-\xBF]{3})+/nx) do |ch| "\\u%04x" % ch.unpack("U*") end end end + + if MultiJson.respond_to?(:load) + def json_decode(obj) + MultiJson.load(obj) + end + else + def json_decode(obj) + MultiJson.decode(obj) + end + end + + if MultiJson.respond_to?(:dump) + def json_encode(obj) + MultiJson.dump(obj) + end + else + def json_encode(obj) + MultiJson.encode(obj) + end + end end attr_reader :name def initialize(options) @@ -110,10 +131,9 @@ def compile(source) Context.new(self, source) end def available? - require "multi_json" binary ? true : false end private def binary