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.5'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memory, size) ⇒ JITBuffer

Returns a new instance of JITBuffer.



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

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.



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

def pos
  @pos
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Class Method Details

.new(size) ⇒ Object



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

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

Instance Method Details

#[](a, b) ⇒ Object



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

def [] a, b
  @memory[a, b]
end

#executable!Object



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

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

#getcObject



125
126
127
128
129
130
# File 'lib/jit_buffer.rb', line 125

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

#putc(byte) ⇒ Object

Raises:



111
112
113
114
115
116
# File 'lib/jit_buffer.rb', line 111

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



132
133
134
135
136
137
# File 'lib/jit_buffer.rb', line 132

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)


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

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



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

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

#to_iObject

Get the address of the executable memory



162
163
164
# File 'lib/jit_buffer.rb', line 162

def to_i
  @memory.to_i
end

#write(bytes) ⇒ Object

Raises:



118
119
120
121
122
123
# File 'lib/jit_buffer.rb', line 118

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



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

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