-
Notifications
You must be signed in to change notification settings - Fork 282
/
duktape_runtime.rb
69 lines (61 loc) · 1.72 KB
/
duktape_runtime.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require "execjs/runtime"
require "json"
module ExecJS
class DuktapeRuntime < Runtime
class Context < Runtime::Context
def initialize(runtime, source = "", options = {})
@ctx = Duktape::Context.new(complex_object: nil)
@ctx.exec_string(source.encode(Encoding::UTF_8), '(execjs)')
rescue Exception => e
raise wrap_error(e)
end
def exec(source, options = {})
return unless /\S/ =~ source
@ctx.eval_string("(function(){#{source.encode(Encoding::UTF_8)}})()", '(execjs)')
rescue Exception => e
raise wrap_error(e)
end
def eval(source, options = {})
return unless /\S/ =~ source
@ctx.eval_string("(#{source.encode(Encoding::UTF_8)})", '(execjs)')
rescue Exception => e
raise wrap_error(e)
end
def call(identifier, *args)
@ctx.exec_string("__execjs_duktape_call = #{identifier}", '(execjs)')
@ctx.call_prop("__execjs_duktape_call", *args)
rescue Exception => e
raise wrap_error(e)
end
private
def wrap_error(e)
klass = case e
when Duktape::SyntaxError
RuntimeError
when Duktape::Error
ProgramError
when Duktape::InternalError
RuntimeError
end
if klass
re = / \(line (\d+)\)$/
lineno = e.message[re, 1] || 1
error = klass.new(e.message.sub(re, ""))
error.set_backtrace(["(execjs):#{lineno}"] + e.backtrace)
error
else
e
end
end
end
def name
"Duktape"
end
def available?
require "duktape"
true
rescue LoadError
false
end
end
end