Class: Prism::LibRubyParser::PrismBuffer

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

Overview

This object represents a pm_buffer_t. We only use it as an opaque pointer, so it doesn’t need to know the fields of pm_buffer_t.

Constant Summary collapse

SIZEOF =

:nodoc:

LibRubyParser.pm_buffer_sizeof

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ PrismBuffer

Returns a new instance of PrismBuffer.



115
116
117
# File 'lib/prism/ffi.rb', line 115

def initialize(pointer)
  @pointer = pointer
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



113
114
115
# File 'lib/prism/ffi.rb', line 113

def pointer
  @pointer
end

Class Method Details

.withObject

Initialize a new buffer and yield it to the block. The buffer will be automatically freed when the block returns.



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

def self.with
  FFI::MemoryPointer.new(SIZEOF) do |pointer|
    raise unless LibRubyParser.pm_buffer_init(pointer)
    return yield new(pointer)
  ensure
    LibRubyParser.pm_buffer_free(pointer)
  end
end

Instance Method Details

#lengthObject



123
124
125
# File 'lib/prism/ffi.rb', line 123

def length
  LibRubyParser.pm_buffer_length(pointer)
end

#readObject



127
128
129
# File 'lib/prism/ffi.rb', line 127

def read
  value.read_string(length)
end

#valueObject



119
120
121
# File 'lib/prism/ffi.rb', line 119

def value
  LibRubyParser.pm_buffer_value(pointer)
end