Class: ExecJS::PCRuntime::ContextProcessRuntime::JSRuntimeHandle
- Inherits:
-
Object
- Object
- ExecJS::PCRuntime::ContextProcessRuntime::JSRuntimeHandle
- 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
-
.finalizer(pid) ⇒ Object
Create a procedure to kill the Process that has specified pid.
-
.kill_process(pid) ⇒ StandardError?
Kill the Process that has specified pid.
Instance Method Summary collapse
-
#evaluate(source) ⇒ object
Evaluate JavaScript source code and return the result.
-
#initialize(binary, initial_source_path, compile_source, semaphore) ⇒ JSRuntimeHandle
constructor
A new instance of JSRuntimeHandle.
-
#recreate_process ⇒ [Integer, String]
kill JavaScript runtime process and re-create.
Constructor Details
#initialize(binary, initial_source_path, compile_source, semaphore) ⇒ JSRuntimeHandle
Returns a new instance of JSRuntimeHandle.
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.
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. unless err.nil? end end |
.kill_process(pid) ⇒ StandardError?
Kill the Process that has specified pid. If raised error then return it.
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.
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. 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
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. 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. retry end [runtime_pid, socket_path] end |