Class: JITBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/jit_buffer.rb

Defined Under Namespace

Modules: MMAP Classes: Exception, OutOfBoundsException, ReadOnlyException

Constant Summary collapse

VERSION =
'1.0.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memory, size) ⇒ JITBuffer



98
99
100
101
102
103
104
# File 'lib/jit_buffer.rb', line 98

def initialize memory, size
  @writeable = false
  @memory = memory
  @size   = size
  @pos    = 0
  executable!
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



96
97
98
# File 'lib/jit_buffer.rb', line 96

def pos
  @pos
end

Class Method Details

.new(size) ⇒ Object



92
93
94
# File 'lib/jit_buffer.rb', line 92

def self.new size
  super(MMAP.mmap_buffer(size), size)
end

Instance Method Details

#executable!Object



142
143
144
145
# File 'lib/jit_buffer.rb', line 142

def executable!
  MMAP.set_executable @memory.to_i
  @writeable = false
end

#getcObject



120
121
122
123
124
125
# File 'lib/jit_buffer.rb', line 120

def getc
  raise(OutOfBoundsException, "You've gone too far!") if pos >= @size
  x = @memory[pos]
  @pos += 1
  x
end

#putc(byte) ⇒ Object

Raises:



106
107
108
109
110
111
# File 'lib/jit_buffer.rb', line 106

def putc byte
  raise(ReadOnlyException, "Buffer is read only!") unless @writeable
  raise(OutOfBoundsException, "Buffer full! #{pos} - #{@size}") if pos >= @size
  @memory[pos] = byte
  @pos += 1
end

#read(len) ⇒ Object



127
128
129
130
131
132
# File 'lib/jit_buffer.rb', line 127

def read len
  raise(OutOfBoundsException, "You've gone too far!") if pos + len >= @size
  x = @memory[pos, pos + len]
  @pos += len
  x
end

#seek(pos, whence = IO::SEEK_SET) ⇒ Object

Raises:

  • (NotImplementedError)


134
135
136
137
138
139
140
# File 'lib/jit_buffer.rb', line 134

def seek pos, whence = IO::SEEK_SET
  raise NotImplementedError if whence != IO::SEEK_SET
  raise OutOfBoundsException if pos >= @size

  @pos = pos
  self
end

#to_function(params, ret) ⇒ Object



152
153
154
# File 'lib/jit_buffer.rb', line 152

def to_function params, ret
  Fiddle::Function.new @memory.to_i, params, ret
end

#to_iObject

Get the address of the executable memory



157
158
159
# File 'lib/jit_buffer.rb', line 157

def to_i
  @memory.to_i
end

#write(bytes) ⇒ Object

Raises:



113
114
115
116
117
118
# File 'lib/jit_buffer.rb', line 113

def write bytes
  raise(ReadOnlyException, "Buffer is read only!") unless @writeable
  raise OutOfBoundsException if pos + bytes.bytesize >= @size
  @memory[pos, bytes.length] = bytes
  @pos += bytes.bytesize
end

#writeable!Object



147
148
149
150
# File 'lib/jit_buffer.rb', line 147

def writeable!
  MMAP.set_writeable @memory.to_i
  @writeable = true
end