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.17.0.pre"

Class Method Summary collapse

Class Method Details

._build_import(imported) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/quickjs.rb', line 55

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



32
33
34
35
36
37
38
# File 'lib/quickjs.rb', line 32

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



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/quickjs.rb', line 41

def _with_vm(on)
  case on
  when Quickjs::VM
    yield on
  when nil
    yield Quickjs::VM.new
  when Hash
    yield Quickjs::VM.new(**on)
  else
    raise ArgumentError, 'on: must be a Quickjs::VM, a Hash of VM options, or nil'
  end
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)
  res = vm.eval_code(code, **eval_opts)
  vm = nil
  res
end