123456789_123456789_123456789_123456789_123456789_

Class: Prism::LibRubyParser::PrismString

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(pointer, length, from_string) ⇒ PrismString

[ GitHub ]

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

def initialize(pointer, length, from_string)
  @pointer = pointer
  @length = length
  @from_string = from_string
end

Class Method Details

.with_file(filepath)

Yields a pm_string_t pointer to the given block.

Raises:

  • (TypeError)
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 176

def self.with_file(filepath)
  raise TypeError unless filepath.is_a?(String)

  FFI::MemoryPointer.new(SIZEOF) do |pm_string|
    if LibRubyParser.pm_string_mapped_init(pm_string, filepath)
      pointer = LibRubyParser.pm_string_source(pm_string)
      length = LibRubyParser.pm_string_length(pm_string)
      return yield new(pointer, length, false)
    else
      raise SystemCallError.new(filepath, FFI.errno)
    end
  ensure
    LibRubyParser.pm_string_free(pm_string)
  end
end

.with_string(string)

Yields a pm_string_t pointer to the given block.

Raises:

  • (TypeError)
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 162

def self.with_string(string)
  raise TypeError unless string.is_a?(String)

  length = string.bytesize
  # + 1 to never get an address of 0, which pm_parser_init() asserts
  FFI::MemoryPointer.new(:char, length + 1, false) do |pointer|
    pointer.write_string(string)
    # since we have the extra byte we might as well \0-terminate
    pointer.put_char(length, 0)
    return yield new(pointer, length, true)
  end
end

Instance Attribute Details

#length (readonly)

[ GitHub ]

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

attr_reader :pointer, :length

#pointer (readonly)

[ GitHub ]

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

attr_reader :pointer, :length

Instance Method Details

#read

[ GitHub ]

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

def read
  raise "should use the original String instead" if @from_string
  @pointer.read_string(@length)
end