Skip to content
This repository
Newer
Older
100644 161 lines (136 sloc) 4.005 kb
0be476df »
2011-02-06 Extract ExecJS::Runtime
1 require "tempfile"
2
3 module ExecJS
c5e22c47 »
2011-02-06 Runtime -> ExternalRuntime
4 class ExternalRuntime
82773bfb »
2011-02-11 Context
5 class Context
c595c079 »
2011-03-10 Remove pure evaluation
6 def initialize(runtime, source = "")
043c06b6 »
2011-06-07 Encode strings to utf-8 that are passed to compile
7 source = source.encode('UTF-8') if source.respond_to?(:encode)
8
82773bfb »
2011-02-11 Context
9 @runtime = runtime
043c06b6 »
2011-06-07 Encode strings to utf-8 that are passed to compile
10 @source = source
50791dd9 »
2011-02-12 Add Context#call
11 end
12
4bc602f4 »
2011-02-11 Pure function hint
13 def eval(source, options = {})
ae253dfb »
2011-06-05 Fix typo in variable name
14 source = source.encode('UTF-8') if source.respond_to?(:encode)
0b4977e0 »
2011-05-12 Try to convert to UTF8 early
15
82773bfb »
2011-02-11 Context
16 if /\S/ =~ source
3097959f »
2011-04-14 Switch to multi_json lib
17 exec("return eval(#{MultiJson.encode("(#{source})")})")
82773bfb »
2011-02-11 Context
18 end
19 end
20
4bc602f4 »
2011-02-11 Pure function hint
21 def exec(source, options = {})
ae253dfb »
2011-06-05 Fix typo in variable name
22 source = source.encode('UTF-8') if source.respond_to?(:encode)
0b4977e0 »
2011-05-12 Try to convert to UTF8 early
23
c595c079 »
2011-03-10 Remove pure evaluation
24 compile_to_tempfile([@source, source].join("\n")) do |file|
0dc15c31 »
2011-03-10 Make runner_source and exec_runtime protected
25 extract_result(@runtime.send(:exec_runtime, file.path))
82773bfb »
2011-02-11 Context
26 end
27 end
28
f8152723 »
2011-03-10 properties -> identifier
29 def call(identifier, *args)
3097959f »
2011-04-14 Switch to multi_json lib
30 eval "#{identifier}.apply(this, #{MultiJson.encode(args)})"
50791dd9 »
2011-02-12 Add Context#call
31 end
32
82773bfb »
2011-02-11 Context
33 protected
34 def compile_to_tempfile(source)
a1277a6b »
2011-05-12 Style
35 tempfile = Tempfile.open(['execjs', '.js'])
82773bfb »
2011-02-11 Context
36 tempfile.write compile(source)
37 tempfile.close
38 yield tempfile
39 ensure
40 tempfile.close!
41 end
42
43 def compile(source)
0dc15c31 »
2011-03-10 Make runner_source and exec_runtime protected
44 @runtime.send(:runner_source).dup.tap do |output|
bd4c71c2 »
2011-03-10 Fix sub \ escaping
45 output.sub!('#{source}') do
46 source
47 end
82773bfb »
2011-02-11 Context
48 output.sub!('#{json2_source}') do
49 IO.read(ExecJS.root + "/support/json2.js")
50 end
51 end
52 end
53
54 def extract_result(output)
3097959f »
2011-04-14 Switch to multi_json lib
55 status, value = output.empty? ? [] : MultiJson.decode(output)
82773bfb »
2011-02-11 Context
56 if status == "ok"
57 value
58 else
59 raise ProgramError, value
60 end
61 end
62 end
63
d19c7601 »
2011-02-09 Less magic
64 attr_reader :name
65
954db082 »
2011-02-06 Instantiate with options instead of subclassing
66 def initialize(options)
d19c7601 »
2011-02-09 Less magic
67 @name = options[:name]
954db082 »
2011-02-06 Instantiate with options instead of subclassing
68 @command = options[:command]
69 @runner_path = options[:runner_path]
5d0d51b6 »
2011-02-06 therubyracer gem installs an incompatible `v8` binary - make sure we …
70 @test_args = options[:test_args]
71 @test_match = options[:test_match]
24a41dfe »
2011-02-10 Fix JSC encoding
72 @conversion = options[:conversion]
c00ee391 »
2011-02-07 Support "nodejs" binary name
73 @binary = locate_binary
954db082 »
2011-02-06 Instantiate with options instead of subclassing
74 end
75
82773bfb »
2011-02-11 Context
76 def exec(source)
77 context = Context.new(self)
c595c079 »
2011-03-10 Remove pure evaluation
78 context.exec(source)
82773bfb »
2011-02-11 Context
79 end
80
0be476df »
2011-02-06 Extract ExecJS::Runtime
81 def eval(source)
82773bfb »
2011-02-11 Context
82 context = Context.new(self)
c595c079 »
2011-03-10 Remove pure evaluation
83 context.eval(source)
0be476df »
2011-02-06 Extract ExecJS::Runtime
84 end
85
82773bfb »
2011-02-11 Context
86 def compile(source)
c595c079 »
2011-03-10 Remove pure evaluation
87 Context.new(self, source)
c5e22c47 »
2011-02-06 Runtime -> ExternalRuntime
88 end
89
b22082fc »
2011-02-06 Add Runtime#available?
90 def available?
3097959f »
2011-04-14 Switch to multi_json lib
91 require "multi_json"
c00ee391 »
2011-02-07 Support "nodejs" binary name
92 @binary ? true : false
b22082fc »
2011-02-06 Add Runtime#available?
93 end
94
0dc15c31 »
2011-03-10 Make runner_source and exec_runtime protected
95 protected
96 def runner_source
97 @runner_source ||= IO.read(@runner_path)
98 end
82773bfb »
2011-02-11 Context
99
0dc15c31 »
2011-03-10 Make runner_source and exec_runtime protected
100 def exec_runtime(filename)
101 output = sh("#{@binary} #{filename} 2>&1")
102 if $?.success?
103 output
104 else
105 raise RuntimeError, output
106 end
82773bfb »
2011-02-11 Context
107 end
108
c00ee391 »
2011-02-07 Support "nodejs" binary name
109 def locate_binary
3cf772ef »
2011-02-07 Add `which` support for Windows
110 if binary = which(@command)
c00ee391 »
2011-02-07 Support "nodejs" binary name
111 if @test_args
112 output = `#{binary} #{@test_args} 2>&1`
113 binary if output.match(@test_match)
114 else
115 binary
116 end
117 end
118 end
119
3cf772ef »
2011-02-07 Add `which` support for Windows
120 def which(command)
612c95e0 »
2011-04-22 Fix external runtime which to return the expanded path
121 Array(command).each do |name|
88fa6b7e »
2011-05-05 Preserve command flags passed to which
122 name, args = name.split(/\s+/, 2)
3cf772ef »
2011-02-07 Add `which` support for Windows
123 result = if ExecJS.windows?
124 `#{ExecJS.root}/support/which.bat #{name}`
125 else
da30b02a »
2011-06-21 Fix external runtime detection under zsh and other shells.
126 `command -v #{name} 2>/dev/null`
3cf772ef »
2011-02-07 Add `which` support for Windows
127 end
612c95e0 »
2011-04-22 Fix external runtime which to return the expanded path
128
129 if path = result.strip.split("\n").first
88fa6b7e »
2011-05-05 Preserve command flags passed to which
130 return args ? "#{path} #{args}" : path
612c95e0 »
2011-04-22 Fix external runtime which to return the expanded path
131 end
3cf772ef »
2011-02-07 Add `which` support for Windows
132 end
612c95e0 »
2011-04-22 Fix external runtime which to return the expanded path
133 nil
3cf772ef »
2011-02-07 Add `which` support for Windows
134 end
135
24a41dfe »
2011-02-10 Fix JSC encoding
136 if "".respond_to?(:force_encoding)
137 def sh(command)
138 output, options = nil, {}
17816663 »
2011-05-18 Force UTF-8 on external encoding
139 options[:external_encoding] = 'UTF-8'
24a41dfe »
2011-02-10 Fix JSC encoding
140 options[:internal_encoding] = @conversion[:from] if @conversion
141 IO.popen(command, options) { |f| output = f.read }
142 output.force_encoding(@conversion[:to]) if @conversion
143 output
144 end
145 else
146 require "iconv"
147
148 def sh(command)
149 output = nil
150 IO.popen(command) { |f| output = f.read }
151
152 if @conversion
153 Iconv.iconv(@conversion[:from], @conversion[:to], output).first
154 else
155 output
156 end
157 end
158 end
0be476df »
2011-02-06 Extract ExecJS::Runtime
159 end
160 end
Something went wrong with that request. Please try again.