Class: ExecJS::PCRuntime::ContextProcessRuntime::JSRuntimeHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/execjs/pcruntime/context_process_runtime.rb

Overview

Handle of JavaScript Runtime launch Runtime by .new and finished on finalizer rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binary, initial_source_path, compile_source, semaphore) ⇒ JSRuntimeHandle

Returns a new instance of JSRuntimeHandle.

Parameters:

  • binary (Array<String>)

    Launch command for the node(or similar JavaScript Runtime) binary, such as [‘node’], [‘deno’, ‘run’].

  • initial_source_path (String)

    Path of .js Runtime loads at startup.



57
58
59
60
61
62
63
64
65
66
# File 'lib/execjs/pcruntime/context_process_runtime.rb', line 57

def initialize(binary, initial_source_path, compile_source, semaphore)
  @semaphore = semaphore
  @binary = binary
  @initial_source_path = initial_source_path
  @compile_source = compile_source
  @recreate_process_lock = Mutex.new
  @runtime_pid, @socket_path = initialize_process
  evaluate(@compile_source)
  ObjectSpace.define_finalizer(self, self.class.finalizer(@runtime_pid))
end

Class Method Details

.finalizer(pid) ⇒ Object

Create a procedure to kill the Process that has specified pid. It used as the finalizer of JSRuntimeHandle.

Parameters:

  • pid (Integer)


106
107
108
109
110
111
# File 'lib/execjs/pcruntime/context_process_runtime.rb', line 106

def self.finalizer(pid)
  proc do
    err = kill_process(pid)
    warn err.full_message unless err.nil?
  end
end

.kill_process(pid) ⇒ StandardError?

Kill the Process that has specified pid. If raised error then return it.

Parameters:

  • pid (Integer)

Returns:

  • (StandardError, nil)

    return error iff an error is raised



117
118
119
120
121
122
# File 'lib/execjs/pcruntime/context_process_runtime.rb', line 117

def self.kill_process(pid)
  Process.kill(:KILL, pid)
  nil
rescue StandardError => e
  e
end

Instance Method Details

#evaluate(source) ⇒ object

Evaluate JavaScript source code and return the result.

Parameters:

  • source (String)

    JavaScript source code

Returns:

  • (object)


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/execjs/pcruntime/context_process_runtime.rb', line 71

def evaluate(source)
  socket_path = @socket_path
  post_request(socket_path, '/eval', 'text/javascript', source)
rescue RuntimeError, ProgramError => e
  raise e
rescue StandardError => e
  warn e.full_message
  retry if socket_path != @socket_path
  @recreate_process_lock.synchronize do
    @runtime_pid, @socket_path = recreate_process if socket_path == @socket_path
  end
  retry
end

#recreate_process[Integer, String]

kill JavaScript runtime process and re-create

Returns:

  • ([Integer, String])
    runtime_pid, socket_path


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/execjs/pcruntime/context_process_runtime.rb', line 87

def recreate_process
  runtime_pid = @runtime_pid
  begin
    err = self.class.kill_process(runtime_pid)
    warn err.full_message unless err.nil?
    runtime_pid, socket_path = initialize_process
    post_request(socket_path, '/eval', 'text/javascript', @compile_source)
  rescue RuntimeError, ProgramError => e
    raise e
  rescue StandardError => e
    warn e.full_message
    retry
  end
  [runtime_pid, socket_path]
end