Class: Speednode::Runtime::Context

Inherits:
ExecJS::Runtime::Context
  • Object
show all
Defined in:
lib/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.



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 = "", options = {})
  @runtime = runtime
  @uuid = SecureRandom.uuid
  @runtime.register_context(@uuid, self)
  @permissive = !!options.delete(:permissive)
  @debug = @permissive ? !!options.delete(:debug) { false } : false
  @debug = false unless ENV['NODE_OPTIONS']&.include?('--inspect')
  @vm = @runtime.vm
  @timeout = options[:timeout] ? options[:timeout]/1000 : 600
  
  filename = options.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, options)
  elsif @permissive
    raw_createp(source, options)
  else
    raw_create(source, options)
  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.context_options(@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, _options = 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, _options = 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, _options = nil)
  raw_exec("(function(){#{source}})()")
end

#permissive?Boolean

Returns:



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, _options = 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, _options = nil)
  raise "Context not permissive!" unless @permissive
  raw_exec("(function(){#{source}})()")
end

#stopObject



108
109
110
# File 'lib/speednode/runtime/context.rb', line 108

def stop
  @runtime.unregister_context(@uuid)
end