class ExecJS::FastNode::ExternalPipedRuntime::VM

Public Class Methods

finalize(socket_path) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 65
def self.finalize(socket_path)
  proc {
    VMCommand.new(socket_path, "exit", [0]).execute
  }
end
new(options) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 55
def initialize(options)
  @mutex = Mutex.new
  @socket_path = nil
  @options = options
end

Public Instance Methods

delete_context(context) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 75
def delete_context(context)
  command("deleteContext", context)
end
exec(context, source) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 71
def exec(context, source)
  command("exec", {context: context, source: source})
end
start() click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 79
def start
  @mutex.synchronize do
    start_without_synchronization
  end
end
started?() click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 61
def started?
  !!@socket_path
end

Private Instance Methods

command(cmd, *arguments) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 106
def command(cmd, *arguments)
  @mutex.synchronize do
    start_without_synchronization
    VMCommand.new(@socket_path, cmd, arguments).execute
  end
end
start_without_synchronization() click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 87
def start_without_synchronization
  return if started?
  dir = Dir.mktmpdir("execjs-fastnode-")
  @socket_path = File.join(dir, "socket")
  @pid = Process.spawn({"PORT" => @socket_path.to_s}, @options[:binary], @options[:runner_path])

  retries = 20
  while !File.exists?(@socket_path)
    sleep 0.05
    retries -= 1

    if retries == 0
      raise "Unable to start nodejs process"
    end
  end

  ObjectSpace.define_finalizer(self, self.class.finalize(@socket_path))
end