Class: Speednode::Runtime::Context

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Context.



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

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

  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

Instance Method Details

#add_script(key:, source:) ⇒ Object



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

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



38
39
40
41
42
43
# File 'lib/speednode/runtime/context.rb', line 38

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

#available?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/speednode/runtime/context.rb', line 100

def available?
  @runtime.context_registered?(@uuid)
end

#await(source) ⇒ Object



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

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



62
63
64
# File 'lib/speednode/runtime/context.rb', line 62

def bench(source, _options = nil)
  raw_bench(source) if /\S/ =~ source
end

#call(identifier, *args) ⇒ Object



66
67
68
# File 'lib/speednode/runtime/context.rb', line 66

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

#eval(source, _options = nil) ⇒ Object



70
71
72
# File 'lib/speednode/runtime/context.rb', line 70

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

#eval_script(key:) ⇒ Object



78
79
80
# File 'lib/speednode/runtime/context.rb', line 78

def eval_script(key:)
  extract_result(@vm.evsc(@uuid, key))
end

#exec(source, _options = nil) ⇒ Object



74
75
76
# File 'lib/speednode/runtime/context.rb', line 74

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

#permissive?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/speednode/runtime/context.rb', line 86

def permissive?
  @permissive
end

#permissive_eval(source, _options = nil) ⇒ Object



90
91
92
93
# File 'lib/speednode/runtime/context.rb', line 90

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



95
96
97
98
# File 'lib/speednode/runtime/context.rb', line 95

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

#stopObject



104
105
106
# File 'lib/speednode/runtime/context.rb', line 104

def stop
  @runtime.unregister_context(@uuid)
end