Permalink
Browse files

Fix multijson api change

  • Loading branch information...
1 parent 1411da0 commit 59fb97075ab4d6c02c461a147a001c50428c8fed @josh josh committed Apr 26, 2012
Showing with 25 additions and 5 deletions.
  1. +25 −5 lib/execjs/external_runtime.rb
@@ -1,3 +1,4 @@
+require "multi_json"
require "shellwords"
require "tempfile"
@@ -15,7 +16,7 @@ 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
@@ -29,7 +30,7 @@ def exec(source, options = {})
end
def call(identifier, *args)
- eval "#{identifier}.apply(this, #{MultiJson.encode(args)})"
+ eval "#{identifier}.apply(this, #{json_encode(args)})"
end
protected
@@ -49,7 +50,7 @@ def compile(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")
@@ -58,7 +59,7 @@ def compile(source)
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:/
@@ -83,6 +84,26 @@ def encode_unicode_codepoints(str)
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
@@ -112,7 +133,6 @@ def compile(source)
end
def available?
- require "multi_json"
binary ? true : false
end

0 comments on commit 59fb970

Please sign in to comment.