class Lignite::DirectCommands

This enables sending commands without wrapping them in a .rbf program

Public Class Methods

new(conn = Connection.create) click to toggle source

@param conn [Connection]

# File lib/lignite/direct_commands.rb, line 15
def initialize(conn = Connection.create)
  @op_compiler = OpCompiler.new
  @conn = conn
  @globals = nil
end
run(conn = Connection.create, &block) click to toggle source
# File lib/lignite/direct_commands.rb, line 8
def self.run(conn = Connection.create, &block)
  dc = new(conn)
  dc.instance_exec(&block)
  dc.close
end

Public Instance Methods

block(&body) click to toggle source
# File lib/lignite/direct_commands.rb, line 38
def block(&body)
  locals = Variables.new
  bodyc = BodyCompiler.new(@globals, locals, RbfDeclarer::Dummy.new)
  bodyc.instance_exec(&body)

  bs = bodyc.bytes
  lsize = locals.bytesize
  if @globals
    direct_command_with_reply(bs, global_size: @globals.bytesize, local_size: lsize)
  else
    direct_command(bs, global_size: 0, local_size: lsize)
  end
end
close() click to toggle source
# File lib/lignite/direct_commands.rb, line 21
def close
  @conn.close
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/lignite/direct_commands.rb, line 52
def method_missing(name, *args)
  if @op_compiler.respond_to?(name)
    insb = @op_compiler.send(name, *args)
    direct_command(insb)
  else
    super
  end
end
respond_to_missing?(name, _include_private) click to toggle source
Calls superclass method
# File lib/lignite/direct_commands.rb, line 61
def respond_to_missing?(name, _include_private)
  @op_compiler.respond_to?(name) || super
end
variables() click to toggle source
# File lib/lignite/direct_commands.rb, line 25
def variables
  @globals
end
with_reply(&body) click to toggle source
# File lib/lignite/direct_commands.rb, line 30
def with_reply(&body)
  @globals = Variables.new
  ret_bytes = instance_exec(&body)
  ret = @globals.unpack(ret_bytes)
  @globals = nil
  ret # TODO: decode according to type
end

Private Instance Methods

assert_match(actual, expected, description) click to toggle source
# File lib/lignite/direct_commands.rb, line 95
def assert_match(actual, expected, description)
  return if actual == expected
  raise "#{description} does not match, expected #{expected}, actual #{actual}"
end
direct_command(instr_bytes, global_size: 0, local_size: 0) click to toggle source
# File lib/lignite/direct_commands.rb, line 67
def direct_command(instr_bytes, global_size: 0, local_size: 0)
  body = u16(var_alloc(global_size: global_size, local_size: local_size)) +
    instr_bytes
  cmd = Message.direct_command_no_reply(body)
  @conn.send(cmd.bytes)
end
direct_command_with_reply(instr_bytes, global_size: 0, local_size: 0) click to toggle source
# File lib/lignite/direct_commands.rb, line 74
def direct_command_with_reply(instr_bytes, global_size: 0, local_size: 0)
  body = u16(var_alloc(global_size: global_size, local_size: local_size)) +
    instr_bytes
  cmd = Message.direct_command_with_reply(body)
  @conn.send(cmd.bytes)

  reply = Message.reply_from_bytes(@conn.receive)
  assert_match(reply.msgid, cmd.msgid, "Reply id")
  if reply.error?
    raise VMError # no details?
  end

  reply.globals
end
var_alloc(global_size:, local_size:) click to toggle source
# File lib/lignite/direct_commands.rb, line 89
def var_alloc(global_size:, local_size:)
  var_alloc = global_size & 0x3ff
  var_alloc |= (local_size & 0x3f) << 10
  var_alloc
end