Class: Prism::LibRubyParser::PrismString
- Inherits:
-
Object
- Object
- Prism::LibRubyParser::PrismString
- 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
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#pointer ⇒ Object
readonly
Returns the value of attribute pointer.
Class Method Summary collapse
-
.with_file(filepath) ⇒ Object
Yields a pm_string_t pointer to the given block.
-
.with_string(string) ⇒ Object
Yields a pm_string_t pointer to the given block.
Instance Method Summary collapse
-
#initialize(pointer, length, from_string) ⇒ PrismString
constructor
A new instance of PrismString.
- #read ⇒ Object
Constructor Details
#initialize(pointer, length, from_string) ⇒ PrismString
Returns a new instance of PrismString.
150 151 152 153 154 |
# File 'lib/prism/ffi.rb', line 150 def initialize(pointer, length, from_string) @pointer = pointer @length = length @from_string = from_string end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
148 149 150 |
# File 'lib/prism/ffi.rb', line 148 def length @length end |
#pointer ⇒ Object (readonly)
Returns the value of attribute pointer.
148 149 150 |
# File 'lib/prism/ffi.rb', line 148 def pointer @pointer end |
Class Method Details
.with_file(filepath) ⇒ Object
Yields a pm_string_t pointer to the given block.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# 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) ⇒ Object
Yields a pm_string_t pointer to the given block.
162 163 164 165 166 167 168 169 170 171 172 173 |
# 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 Method Details
#read ⇒ Object
156 157 158 159 |
# File 'lib/prism/ffi.rb', line 156 def read raise "should use the original String instead" if @from_string @pointer.read_string(@length) end |