Module: Quickjs
- Defined in:
- lib/quickjs.rb,
lib/quickjs/version.rb,
lib/quickjs/function.rb,
lib/quickjs/runnable.rb,
lib/quickjs/crypto_key.rb,
lib/quickjs/subtle_crypto.rb,
ext/quickjsrb/quickjsrb.c
Defined Under Namespace
Modules: SubtleCrypto
Classes: Blob, CryptoKey, File, Function, Runnable, VM
Constant Summary
collapse
- VERSION =
"0.18.0"
Class Method Summary
collapse
Class Method Details
._build_import(imported) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/quickjs.rb', line 69
def _build_import(imported)
code_define_global = ->(name) { "globalThis['#{name}'] = #{name};" }
case imported
in String if matched = imported.match(/\* as (.+)/)
[imported, code_define_global.call(matched[1])]
in String
[imported, code_define_global.call(imported)]
in [*all] if all.all? {|e| e.is_a? String }
[
imported.join(', ').yield_self{|s| '{ %s }' % s },
imported.map(&code_define_global).join("\n")
]
in { ** }
imports, aliases = imported.to_a.map do |imp|
["#{imp[0]} as #{imp[1]}", imp[1].to_s]
end.transpose
[
imports.join(', ').yield_self{|s| '{ %s }' % s },
aliases.map(&code_define_global).join("\n")
]
else
raise 'Unsupported importing style'
end
end
|
._with_timeout(msec, proc, args) ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/quickjs.rb', line 42
def _with_timeout(msec, proc, args)
Timeout.timeout(msec / 1_000.0) { proc.call(*args) }
rescue Timeout::Error
raise Quickjs::InterruptedError.new('Ruby runtime got timeout', nil)
rescue
raise
end
|
._with_vm(on) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/quickjs.rb', line 51
def _with_vm(on)
case on
when Quickjs::VM
yield on
when nil
vm = Quickjs::VM.new
yield vm
when Hash
vm = Quickjs::VM.new(**on)
yield vm
else
raise ArgumentError, 'on: must be a Quickjs::VM, a Hash of VM options, or nil'
end
ensure
vm&.dispose!
end
|
.compile(source, **opts) ⇒ Object
32
33
34
35
36
37
38
39
|
# File 'lib/quickjs.rb', line 32
def compile(source, **opts)
compile_opts = {}
compile_opts[:filename] = opts.delete(:filename) if opts.key?(:filename)
vm = Quickjs::VM.new(**opts)
vm.compile(source, **compile_opts)
ensure
vm&.dispose!
end
|
.eval_code(code, overwrite_opts = {}) ⇒ Object
21
22
23
24
25
26
27
28
29
|
# File 'lib/quickjs.rb', line 21
def eval_code(code, overwrite_opts = {})
eval_opts = {}
eval_opts[:filename] = overwrite_opts.delete(:filename) if overwrite_opts.key?(:filename)
eval_opts[:async] = overwrite_opts.delete(:async) if overwrite_opts.key?(:async)
vm = Quickjs::VM.new(**overwrite_opts)
vm.eval_code(code, **eval_opts)
ensure
vm&.dispose!
end
|