Class: Isomorfeus::Speednode::Runtime::Context
- Inherits:
-
ExecJS::Runtime::Context
- Object
- ExecJS::Runtime::Context
- Isomorfeus::Speednode::Runtime::Context
- Defined in:
- lib/isomorfeus/speednode/runtime/context.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_script(key:, source:) ⇒ Object
-
#attach(func, procedure = nil, &block) ⇒ Object
def options @vm.context_options(@uuid) end.
- #await(source) ⇒ Object
- #bench(source, _options = nil) ⇒ Object
- #call(identifier, *args) ⇒ Object
- #eval(source, _options = nil) ⇒ Object
- #eval_script(key:) ⇒ Object
- #exec(source, _options = nil) ⇒ Object
-
#initialize(runtime, source = "", options = {}) ⇒ Context
constructor
A new instance of Context.
- #permissive? ⇒ Boolean
- #permissive_eval(source, _options = nil) ⇒ Object
- #permissive_exec(source, _options = nil) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(runtime, source = "", options = {}) ⇒ Context
Returns a new instance of Context.
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 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 11 def initialize(runtime, source = "", = {}) @runtime = runtime @uuid = SecureRandom.uuid @runtime.register_context(@uuid, self) @permissive = !!.delete(:permissive) @debug = @permissive ? !!.delete(:debug) { false } : false @debug = false unless ENV['NODE_OPTIONS']&.include?('--inspect') @vm = @runtime.vm @timeout = [:timeout] ? [:timeout]/1000 : 600 filename = .delete(:filename) source = File.read(filename) if filename begin source = encode(source) rescue source = source.force_encoding('UTF-8') end ObjectSpace.define_finalizer(self, self.class.finalize(@runtime, @uuid)) if @debug && @permissive raw_created(source, ) elsif @permissive raw_createp(source, ) else raw_create(source, ) end add_script(key: :_internal_exec_fin, source: '!global.__LastExecutionFinished') end |
Class Method Details
.finalize(runtime, uuid) ⇒ Object
5 6 7 8 9 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 5 def self.finalize(runtime, uuid) proc do runtime.unregister_context(uuid) end end |
Instance Method Details
#add_script(key:, source:) ⇒ Object
91 92 93 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 91 def add_script(key:, source:) extract_result(@vm.scsc(@uuid, key, encode(source))) end |
#attach(func, procedure = nil, &block) ⇒ Object
def options
@vm.(@uuid)
end
47 48 49 50 51 52 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 47 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 54 def await(source) raw_eval <<~JAVASCRIPT (async () => { global.__LastExecutionFinished = false; global.__LastResult = null; global.__LastErr = null; global.__LastResult = await #{source}; global.__LastExecutionFinished = true; })().catch(function(err) { global.__LastResult = null; global.__LastErr = err; global.__LastExecutionFinished = true; }) JAVASCRIPT await_result end |
#bench(source, _options = nil) ⇒ Object
71 72 73 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 71 def bench(source, = nil) raw_bench(source) if /\S/ =~ source end |
#call(identifier, *args) ⇒ Object
75 76 77 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 75 def call(identifier, *args) raw_eval("#{identifier}.apply(this, #{::Oj.dump(args, mode: :strict)})") end |
#eval(source, _options = nil) ⇒ Object
79 80 81 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 79 def eval(source, = nil) raw_eval(source) if /\S/ =~ source end |
#eval_script(key:) ⇒ Object
87 88 89 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 87 def eval_script(key:) extract_result(@vm.evsc(@uuid, key)) end |
#exec(source, _options = nil) ⇒ Object
83 84 85 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 83 def exec(source, = nil) raw_exec("(function(){#{source}})()") end |
#permissive? ⇒ Boolean
95 96 97 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 95 def permissive? @permissive end |
#permissive_eval(source, _options = nil) ⇒ Object
99 100 101 102 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 99 def permissive_eval(source, = nil) raise "Context not permissive!" unless @permissive raw_eval(source) if /\S/ =~ source end |
#permissive_exec(source, _options = nil) ⇒ Object
104 105 106 107 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 104 def permissive_exec(source, = nil) raise "Context not permissive!" unless @permissive raw_exec("(function(){#{source}})()") end |
#stop ⇒ Object
109 110 111 |
# File 'lib/isomorfeus/speednode/runtime/context.rb', line 109 def stop @runtime.unregister_context(@uuid) end |