Class: Speednode::Runtime::Context
- Inherits:
-
ExecJS::Runtime::Context
- Object
- ExecJS::Runtime::Context
- Speednode::Runtime::Context
- Defined in:
- lib/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.
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 |
# File 'lib/speednode/runtime/context.rb', line 10 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 = source.encode(Encoding::UTF_8) 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
4 5 6 7 8 |
# File 'lib/speednode/runtime/context.rb', line 4 def self.finalize(runtime, uuid) proc do runtime.unregister_context(uuid) end end |
Instance Method Details
#add_script(key:, source:) ⇒ Object
90 91 92 |
# File 'lib/speednode/runtime/context.rb', line 90 def add_script(key:, source:) extract_result(@vm.scsc(@uuid, key, source.encode(Encoding::UTF_8))) end |
#attach(func, procedure = nil, &block) ⇒ Object
def options
@vm.(@uuid)
end
46 47 48 49 50 51 |
# File 'lib/speednode/runtime/context.rb', line 46 def attach(func, procedure = nil, &block) raise "#attach requires a permissive context." unless @permissive run_block = block_given? ? block : procedure ::Speednode::Runtime.attach_proc(@uuid, func, run_block) @vm.attach(@uuid, func) end |
#await(source) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/speednode/runtime/context.rb', line 53 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" |
#bench(source, _options = nil) ⇒ Object
70 71 72 |
# File 'lib/speednode/runtime/context.rb', line 70 def bench(source, = nil) raw_bench(source) if /\S/ =~ source end |
#call(identifier, *args) ⇒ Object
74 75 76 |
# File 'lib/speednode/runtime/context.rb', line 74 def call(identifier, *args) raw_eval("#{identifier}.apply(this, #{::Oj.dump(args, mode: :strict)})") end |
#eval(source, _options = nil) ⇒ Object
78 79 80 |
# File 'lib/speednode/runtime/context.rb', line 78 def eval(source, = nil) raw_eval(source) if /\S/ =~ source end |
#eval_script(key:) ⇒ Object
86 87 88 |
# File 'lib/speednode/runtime/context.rb', line 86 def eval_script(key:) extract_result(@vm.evsc(@uuid, key)) end |
#exec(source, _options = nil) ⇒ Object
82 83 84 |
# File 'lib/speednode/runtime/context.rb', line 82 def exec(source, = nil) raw_exec("(function(){#{source}})()") end |
#permissive? ⇒ Boolean
94 95 96 |
# File 'lib/speednode/runtime/context.rb', line 94 def permissive? @permissive end |
#permissive_eval(source, _options = nil) ⇒ Object
98 99 100 101 |
# File 'lib/speednode/runtime/context.rb', line 98 def permissive_eval(source, = nil) raise "Context not permissive!" unless @permissive raw_eval(source) if /\S/ =~ source end |
#permissive_exec(source, _options = nil) ⇒ Object
103 104 105 106 |
# File 'lib/speednode/runtime/context.rb', line 103 def permissive_exec(source, = nil) raise "Context not permissive!" unless @permissive raw_exec("(function(){#{source}})()") end |
#stop ⇒ Object
108 109 110 |
# File 'lib/speednode/runtime/context.rb', line 108 def stop @runtime.unregister_context(@uuid) end |