Class: Isomorfeus::Speednode::Runtime::Context

Inherits:
ExecJS::Runtime::Context
  • Object
show all
Defined in:
lib/isomorfeus/speednode/runtime/context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime, source = "", options = {}) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 9

def initialize(runtime, source = "", options = {})

  @runtime = runtime
  @uuid = SecureRandom.uuid
  @permissive = !!options.delete(:permissive)
  @vm = @runtime.vm
  @context = nil
  @timeout = options[:timeout] ? options[:timeout]/1000 : 600

  ObjectSpace.define_finalizer(self, self.class.finalize(@runtime, @uuid))

  filename = options.delete(:filename)
  source = File.read(filename) if filename

  begin
    source = encode(source)
  rescue
    source = source.force_encoding('UTF-8')
  end

  @permissive ? raw_createp(source, options) : raw_create(source, options)
end

Class Method Details

.finalize(runtime, uuid) ⇒ Object



5
6
7
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 5

def self.finalize(runtime, uuid)
  proc { runtime.vm.delete_context(uuid) }
end

Instance Method Details

#attach(func, procedure = nil, &block) ⇒ Object

def options

@vm.context_options(@uuid)

end



36
37
38
39
40
41
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 36

def attach(func, procedure = nil, &block)
  raise "#attach requires a permissive context." unless @permissive
  run_block = block_given? ? block : procedure
  Isomorfeus::Speednode::Runtime.attach_proc(@uuid, func, run_block)
  @vm.attach(@uuid, func)
end

#await(source) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 43

def await(source)
  raw_eval "    (async () => {\n      global.__LastExecutionFinished = false;\n      global.__LastResult = null;\n      global.__LastErr = null;\n      global.__LastResult = await \#{source};\n      global.__LastExecutionFinished = true;\n    })().catch(function(err) {\n      global.__LastResult = null;\n      global.__LastErr = err;\n      global.__LastExecutionFinished = true;\n    })\n  JAVASCRIPT\n  await_result\nend\n"

#call(identifier, *args) ⇒ Object



60
61
62
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 60

def call(identifier, *args)
  raw_eval("#{identifier}.apply(this, #{::Oj.dump(args, mode: :strict)})")
end

#eval(source, _options = nil) ⇒ Object



64
65
66
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 64

def eval(source, _options = nil)
  raw_eval(source) if /\S/ =~ source
end

#exec(source, _options = nil) ⇒ Object



68
69
70
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 68

def exec(source, _options = nil)
  raw_exec("(function(){#{source}})()")
end

#permissive?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 72

def permissive?
  @permissive
end

#permissive_eval(source, _options = nil) ⇒ Object



76
77
78
79
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 76

def permissive_eval(source, _options = nil)
  raise "Context not permissive!" unless @permissive
  raw_eval(source) if /\S/ =~ source
end

#permissive_exec(source, _options = nil) ⇒ Object



81
82
83
84
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 81

def permissive_exec(source, _options = nil)
  raise "Context not permissive!" unless @permissive
  raw_exec("(function(){#{source}})()")
end