Class: YARP::LibRubyParser::YPString

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

Overview

This object represents a yp_string_t. We only use it as an opaque pointer, so it doesn’t have to be an FFI::Struct.

Constant Summary collapse

SIZEOF =
LibRubyParser.yp_string_sizeof

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ YPString



140
141
142
# File 'lib/yarp/ffi.rb', line 140

def initialize(pointer)
  @pointer = pointer
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



138
139
140
# File 'lib/yarp/ffi.rb', line 138

def pointer
  @pointer
end

Class Method Details

.with(filepath, &block) ⇒ Object

Yields a yp_string_t pointer to the given block.



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/yarp/ffi.rb', line 157

def self.with(filepath, &block)
  pointer = FFI::MemoryPointer.new(SIZEOF)

  begin
    raise unless LibRubyParser.yp_string_mapped_init(pointer, filepath)
    yield new(pointer)
  ensure
    LibRubyParser.yp_string_free(pointer)
    pointer.free
  end
end

Instance Method Details

#lengthObject



148
149
150
# File 'lib/yarp/ffi.rb', line 148

def length
  LibRubyParser.yp_string_length(pointer)
end

#readObject



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

def read
  source.read_string(length)
end

#sourceObject



144
145
146
# File 'lib/yarp/ffi.rb', line 144

def source
  LibRubyParser.yp_string_source(pointer)
end