Class: Prism::LibRubyParser::PrismString

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

Overview

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

Constant Summary collapse

SIZEOF =

:nodoc:

LibRubyParser.pm_string_sizeof

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ PrismString

Returns a new instance of PrismString.



142
143
144
# File 'lib/prism/ffi.rb', line 142

def initialize(pointer)
  @pointer = pointer
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



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

def pointer
  @pointer
end

Class Method Details

.with(filepath, &block) ⇒ Object

Yields a pm_string_t pointer to the given block.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/prism/ffi.rb', line 159

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

  begin
    raise unless LibRubyParser.pm_string_mapped_init(pointer, filepath)
    yield new(pointer)
  ensure
    LibRubyParser.pm_string_free(pointer)
    pointer.free
  end
end

Instance Method Details

#lengthObject



150
151
152
# File 'lib/prism/ffi.rb', line 150

def length
  LibRubyParser.pm_string_length(pointer)
end

#readObject



154
155
156
# File 'lib/prism/ffi.rb', line 154

def read
  source.read_string(length)
end

#sourceObject



146
147
148
# File 'lib/prism/ffi.rb', line 146

def source
  LibRubyParser.pm_string_source(pointer)
end