Debian Codegraph @

lib/prism/ffi.rb

raw symbols SCIP
1 # frozen_string_literal: true
2 # :markup: markdown
3 # typed: ignore
4
5 # This file is responsible for mirroring the API provided by the C extension by
6 # using FFI to call into the shared library.
7
8 require "rbconfig"
9 require "ffi"
10
11 # We want to eagerly load this file if there are Ractors so that it does not get
12 # autoloaded from within a non-main Ractor.
13 require "prism/serialize" if defined?(Ractor)
14
15 module Prism
16 module LibRubyParser # :nodoc:
17 extend FFI::Library
18
19 # Define the library that we will be pulling functions from. Note that this
20 # must align with the build shared library from make/rake.
21 libprism_in_build = File.expand_path("../../build/libprism.#{RbConfig::CONFIG["SOEXT"]}", __dir__)
22 libprism_in_libdir = "#{RbConfig::CONFIG["libdir"]}/prism/libprism.#{RbConfig::CONFIG["SOEXT"]}"
23
24 if File.exist?(libprism_in_build)
25 INCLUDE_DIR = File.expand_path("../../include", __dir__)
26 ffi_lib libprism_in_build
27 else
28 INCLUDE_DIR = "#{RbConfig::CONFIG["libdir"]}/prism/include"
29 ffi_lib libprism_in_libdir
30 end
31
32 # Convert a native C type declaration into a symbol that FFI understands.
33 # For example:
34 #
35 # const char * -> :pointer
36 # bool -> :bool
37 # size_t -> :size_t
38 # void -> :void
39 #
40 def self.resolve_type(type, callbacks)
41 type = type.strip
42
43 if !type.end_with?("*")
44 type.delete_prefix("const ").to_sym
45 else
46 type = type.delete_suffix("*").rstrip
47 callbacks.include?(type.to_sym) ? type.to_sym : :pointer
48 end
49 end
50
51 # Read through the given header file and find the declaration of each of the
52 # given functions. For each one, define a function with the same name and
53 # signature as the C function.
54 def self.load_exported_functions_from(header, *functions, callbacks)
55 File.foreach("#{INCLUDE_DIR}/#{header}") do |line|
56 # We only want to attempt to load exported functions.
57 next unless line.start_with?("PRISM_EXPORTED_FUNCTION ")
58
59 # We only want to load the functions that we are interested in.
60 next unless functions.any? { |function| line.include?(function) }
61
62 # Parse the function declaration.
63 unless /^PRISM_EXPORTED_FUNCTION (?<return_type>.+) (?<name>\w+)\((?<arg_types>.+)\);$/ =~ line
64 raise "Could not parse #{line}"
65 end
66
67 # Delete the function from the list of functions we are looking for to
68 # mark it as having been found.
69 functions.delete(name)
70
71 # Split up the argument types into an array, ensure we handle the case
72 # where there are no arguments (by explicit void).
73 arg_types = arg_types.split(",").map(&:strip)
74 arg_types = [] if arg_types == %w[void]
75
76 # Resolve the type of the argument by dropping the name of the argument
77 # first if it is present.
78 arg_types.map! { |type| resolve_type(type.sub(/\w+$/, ""), callbacks) }
79
80 # Attach the function using the FFI library.
81 attach_function name, arg_types, resolve_type(return_type, [])
82 end
83
84 # If we didn't find all of the functions, raise an error.
85 raise "Could not find functions #{functions.inspect}" unless functions.empty?
86 end
87
88 callback :pm_parse_stream_fgets_t, [:pointer, :int, :pointer], :pointer
89 callback :pm_parse_stream_feof_t, [:pointer], :int
90 enum :pm_string_init_result_t, %i[PM_STRING_INIT_SUCCESS PM_STRING_INIT_ERROR_GENERIC PM_STRING_INIT_ERROR_DIRECTORY]
91 enum :pm_string_query_t, [:PM_STRING_QUERY_ERROR, -1, :PM_STRING_QUERY_FALSE, :PM_STRING_QUERY_TRUE]
92
93 load_exported_functions_from(
94 "prism.h",
95 "pm_version",
96 "pm_serialize_parse",
97 "pm_serialize_parse_stream",
98 "pm_serialize_parse_comments",
99 "pm_serialize_lex",
100 "pm_serialize_parse_lex",
101 "pm_parse_success_p",
102 "pm_string_query_local",
103 "pm_string_query_constant",
104 "pm_string_query_method_name",
105 [:pm_parse_stream_fgets_t, :pm_parse_stream_feof_t]
106 )
107
108 load_exported_functions_from(
109 "prism/util/pm_buffer.h",
110 "pm_buffer_sizeof",
111 "pm_buffer_init",
112 "pm_buffer_value",
113 "pm_buffer_length",
114 "pm_buffer_free",
115 []
116 )
117
118 load_exported_functions_from(
119 "prism/util/pm_string.h",
120 "pm_string_mapped_init",
121 "pm_string_free",
122 "pm_string_source",
123 "pm_string_length",
124 "pm_string_sizeof",
125 []
126 )
127
128 # This object represents a pm_buffer_t. We only use it as an opaque pointer,
129 # so it doesn't need to know the fields of pm_buffer_t.
130 class PrismBuffer # :nodoc:
131 SIZEOF = LibRubyParser.pm_buffer_sizeof
132
133 attr_reader :pointer
134
135 def initialize(pointer)
136 @pointer = pointer
137 end
138
139 def value
140 LibRubyParser.pm_buffer_value(pointer)
141 end
142
143 def length
144 LibRubyParser.pm_buffer_length(pointer)
145 end
146
147 def read
148 value.read_string(length)
149 end
150
151 # Initialize a new buffer and yield it to the block. The buffer will be
152 # automatically freed when the block returns.
153 def self.with
154 FFI::MemoryPointer.new(SIZEOF) do |pointer|
155 raise unless LibRubyParser.pm_buffer_init(pointer)
156 return yield new(pointer)
157 ensure
158 LibRubyParser.pm_buffer_free(pointer)
159 end
160 end
161 end
162
163 # This object represents a pm_string_t. We only use it as an opaque pointer,
164 # so it doesn't have to be an FFI::Struct.
165 class PrismString # :nodoc:
166 SIZEOF = LibRubyParser.pm_string_sizeof
167
168 PLATFORM_EXPECTS_UTF8 =
169 RbConfig::CONFIG["host_os"].match?(/bccwin|cygwin|djgpp|mingw|mswin|wince|darwin/i)
170
171 attr_reader :pointer, :length
172
173 def initialize(pointer, length, from_string)
174 @pointer = pointer
175 @length = length
176 @from_string = from_string
177 end
178
179 def read
180 raise "should use the original String instead" if @from_string
181 @pointer.read_string(@length)
182 end
183
184 # Yields a pm_string_t pointer to the given block.
185 def self.with_string(string)
186 raise TypeError unless string.is_a?(String)
187
188 length = string.bytesize
189 # + 1 to never get an address of 0, which pm_parser_init() asserts
190 FFI::MemoryPointer.new(:char, length + 1, false) do |pointer|
191 pointer.write_string(string)
192 # since we have the extra byte we might as well \0-terminate
193 pointer.put_char(length, 0)
194 return yield new(pointer, length, true)
195 end
196 end
197
198 # Yields a pm_string_t pointer to the given block.
199 def self.with_file(filepath)
200 raise TypeError unless filepath.is_a?(String)
201
202 # On Windows and Mac, it's expected that filepaths will be encoded in
203 # UTF-8. If they are not, we need to convert them to UTF-8 before
204 # passing them into pm_string_mapped_init.
205 if PLATFORM_EXPECTS_UTF8 && (encoding = filepath.encoding) != Encoding::ASCII_8BIT && encoding != Encoding::UTF_8
206 filepath = filepath.encode(Encoding::UTF_8)
207 end
208
209 FFI::MemoryPointer.new(SIZEOF) do |pm_string|
210 case (result = LibRubyParser.pm_string_mapped_init(pm_string, filepath))
211 when :PM_STRING_INIT_SUCCESS
212 pointer = LibRubyParser.pm_string_source(pm_string)
213 length = LibRubyParser.pm_string_length(pm_string)
214 return yield new(pointer, length, false)
215 when :PM_STRING_INIT_ERROR_GENERIC
216 raise SystemCallError.new(filepath, FFI.errno)
217 when :PM_STRING_INIT_ERROR_DIRECTORY
218 raise Errno::EISDIR.new(filepath)
219 else
220 raise "Unknown error initializing pm_string_t: #{result.inspect}"
221 end
222 ensure
223 LibRubyParser.pm_string_free(pm_string)
224 end
225 end
226 end
227 end
228
229 # Mark the LibRubyParser module as private as it should only be called through
230 # the prism module.
231 private_constant :LibRubyParser
232
233 # The version constant is set by reading the result of calling pm_version.
234 VERSION = LibRubyParser.pm_version.read_string.freeze
235
236 class << self
237 # Mirror the Prism.dump API by using the serialization API.
238 def dump(source, **options)
239 LibRubyParser::PrismString.with_string(source) { |string| dump_common(string, options) }
240 end
241
242 # Mirror the Prism.dump_file API by using the serialization API.
243 def dump_file(filepath, **options)
244 options[:filepath] = filepath
245 LibRubyParser::PrismString.with_file(filepath) { |string| dump_common(string, options) }
246 end
247
248 # Mirror the Prism.lex API by using the serialization API.
249 def lex(code, **options)
250 LibRubyParser::PrismString.with_string(code) { |string| lex_common(string, code, options) }
251 end
252
253 # Mirror the Prism.lex_file API by using the serialization API.
254 def lex_file(filepath, **options)
255 options[:filepath] = filepath
256 LibRubyParser::PrismString.with_file(filepath) { |string| lex_common(string, string.read, options) }
257 end
258
259 # Mirror the Prism.parse API by using the serialization API.
260 def parse(code, **options)
261 LibRubyParser::PrismString.with_string(code) { |string| parse_common(string, code, options) }
262 end
263
264 # Mirror the Prism.parse_file API by using the serialization API. This uses
265 # native strings instead of Ruby strings because it allows us to use mmap
266 # when it is available.
267 def parse_file(filepath, **options)
268 options[:filepath] = filepath
269 LibRubyParser::PrismString.with_file(filepath) { |string| parse_common(string, string.read, options) }
270 end
271
272 # Mirror the Prism.parse_stream API by using the serialization API.
273 def parse_stream(stream, **options)
274 LibRubyParser::PrismBuffer.with do |buffer|
275 source = +""
276 callback = -> (string, size, _) {
277 raise "Expected size to be >= 0, got: #{size}" if size <= 0
278
279 if !(line = stream.gets(size - 1)).nil?
280 source << line
281 string.write_string("#{line}\x00", line.bytesize + 1)
282 end
283 }
284
285 eof_callback = -> (_) { stream.eof? }
286
287 # In the pm_serialize_parse_stream function it accepts a pointer to the
288 # IO object as a void* and then passes it through to the callback as the
289 # third argument, but it never touches it itself. As such, since we have
290 # access to the IO object already through the closure of the lambda, we
291 # can pass a null pointer here and not worry.
292 LibRubyParser.pm_serialize_parse_stream(buffer.pointer, nil, callback, eof_callback, dump_options(options))
293 Prism.load(source, buffer.read, options.fetch(:freeze, false))
294 end
295 end
296
297 # Mirror the Prism.parse_comments API by using the serialization API.
298 def parse_comments(code, **options)
299 LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, options) }
300 end
301
302 # Mirror the Prism.parse_file_comments API by using the serialization
303 # API. This uses native strings instead of Ruby strings because it allows us
304 # to use mmap when it is available.
305 def parse_file_comments(filepath, **options)
306 options[:filepath] = filepath
307 LibRubyParser::PrismString.with_file(filepath) { |string| parse_comments_common(string, string.read, options) }
308 end
309
310 # Mirror the Prism.parse_lex API by using the serialization API.
311 def parse_lex(code, **options)
312 LibRubyParser::PrismString.with_string(code) { |string| parse_lex_common(string, code, options) }
313 end
314
315 # Mirror the Prism.parse_lex_file API by using the serialization API.
316 def parse_lex_file(filepath, **options)
317 options[:filepath] = filepath
318 LibRubyParser::PrismString.with_file(filepath) { |string| parse_lex_common(string, string.read, options) }
319 end
320
321 # Mirror the Prism.parse_success? API by using the serialization API.
322 def parse_success?(code, **options)
323 LibRubyParser::PrismString.with_string(code) { |string| parse_file_success_common(string, options) }
324 end
325
326 # Mirror the Prism.parse_failure? API by using the serialization API.
327 def parse_failure?(code, **options)
328 !parse_success?(code, **options)
329 end
330
331 # Mirror the Prism.parse_file_success? API by using the serialization API.
332 def parse_file_success?(filepath, **options)
333 options[:filepath] = filepath
334 LibRubyParser::PrismString.with_file(filepath) { |string| parse_file_success_common(string, options) }
335 end
336
337 # Mirror the Prism.parse_file_failure? API by using the serialization API.
338 def parse_file_failure?(filepath, **options)
339 !parse_file_success?(filepath, **options)
340 end
341
342 # Mirror the Prism.profile API by using the serialization API.
343 def profile(source, **options)
344 LibRubyParser::PrismString.with_string(source) do |string|
345 LibRubyParser::PrismBuffer.with do |buffer|
346 LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
347 nil
348 end
349 end
350 end
351
352 # Mirror the Prism.profile_file API by using the serialization API.
353 def profile_file(filepath, **options)
354 LibRubyParser::PrismString.with_file(filepath) do |string|
355 LibRubyParser::PrismBuffer.with do |buffer|
356 options[:filepath] = filepath
357 LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
358 nil
359 end
360 end
361 end
362
363 private
364
365 def dump_common(string, options) # :nodoc:
366 LibRubyParser::PrismBuffer.with do |buffer|
367 LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
368
369 dumped = buffer.read
370 dumped.freeze if options.fetch(:freeze, false)
371
372 dumped
373 end
374 end
375
376 def lex_common(string, code, options) # :nodoc:
377 LibRubyParser::PrismBuffer.with do |buffer|
378 LibRubyParser.pm_serialize_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
379 Serialize.load_lex(code, buffer.read, options.fetch(:freeze, false))
380 end
381 end
382
383 def parse_common(string, code, options) # :nodoc:
384 serialized = dump_common(string, options)
385 Serialize.load_parse(code, serialized, options.fetch(:freeze, false))
386 end
387
388 def parse_comments_common(string, code, options) # :nodoc:
389 LibRubyParser::PrismBuffer.with do |buffer|
390 LibRubyParser.pm_serialize_parse_comments(buffer.pointer, string.pointer, string.length, dump_options(options))
391 Serialize.load_parse_comments(code, buffer.read, options.fetch(:freeze, false))
392 end
393 end
394
395 def parse_lex_common(string, code, options) # :nodoc:
396 LibRubyParser::PrismBuffer.with do |buffer|
397 LibRubyParser.pm_serialize_parse_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
398 Serialize.load_parse_lex(code, buffer.read, options.fetch(:freeze, false))
399 end
400 end
401
402 def parse_file_success_common(string, options) # :nodoc:
403 LibRubyParser.pm_parse_success_p(string.pointer, string.length, dump_options(options))
404 end
405
406 # Return the value that should be dumped for the command_line option.
407 def dump_options_command_line(options)
408 command_line = options.fetch(:command_line, "")
409 raise ArgumentError, "command_line must be a string" unless command_line.is_a?(String)
410
411 command_line.each_char.inject(0) do |value, char|
412 case char
413 when "a" then value | 0b000001
414 when "e" then value | 0b000010
415 when "l" then value | 0b000100
416 when "n" then value | 0b001000
417 when "p" then value | 0b010000
418 when "x" then value | 0b100000
419 else raise ArgumentError, "invalid command_line option: #{char}"
420 end
421 end
422 end
423
424 # Return the value that should be dumped for the version option.
425 def dump_options_version(version)
426 current = version == "current"
427
428 case current ? RUBY_VERSION : version
429 when nil, "latest"
430 0 # Handled in pm_parser_init
431 when /\A3\.3(\.\d+)?\z/
432 1
433 when /\A3\.4(\.\d+)?\z/
434 2
435 when /\A3\.5(\.\d+)?\z/, /\A4\.0(\.\d+)?\z/
436 3
437 when /\A4\.1(\.\d+)?\z/
438 4
439 else
440 if current
441 raise CurrentVersionError, RUBY_VERSION
442 else
443 raise ArgumentError, "invalid version: #{version}"
444 end
445 end
446 end
447
448 # Convert the given options into a serialized options string.
449 def dump_options(options)
450 template = +""
451 values = []
452
453 template << "L"
454 if (filepath = options[:filepath])
455 values.push(filepath.bytesize, filepath.b)
456 template << "A*"
457 else
458 values << 0
459 end
460
461 template << "l"
462 values << options.fetch(:line, 1)
463
464 template << "L"
465 if (encoding = options[:encoding])
466 name = encoding.is_a?(Encoding) ? encoding.name : encoding
467 values.push(name.bytesize, name.b)
468 template << "A*"
469 else
470 values << 0
471 end
472
473 template << "C"
474 values << (options.fetch(:frozen_string_literal, false) ? 1 : 0)
475
476 template << "C"
477 values << dump_options_command_line(options)
478
479 template << "C"
480 values << dump_options_version(options[:version])
481
482 template << "C"
483 values << (options[:encoding] == false ? 1 : 0)
484
485 template << "C"
486 values << (options.fetch(:main_script, false) ? 1 : 0)
487
488 template << "C"
489 values << (options.fetch(:partial_script, false) ? 1 : 0)
490
491 template << "C"
492 values << (options.fetch(:freeze, false) ? 1 : 0)
493
494 template << "L"
495 if (scopes = options[:scopes])
496 values << scopes.length
497
498 scopes.each do |scope|
499 locals = nil
500 forwarding = 0
501
502 case scope
503 when Array
504 locals = scope
505 when Scope
506 locals = scope.locals
507
508 scope.forwarding.each do |forward|
509 case forward
510 when :* then forwarding |= 0x1
511 when :** then forwarding |= 0x2
512 when :& then forwarding |= 0x4
513 when :"..." then forwarding |= 0x8
514 else raise ArgumentError, "invalid forwarding value: #{forward}"
515 end
516 end
517 else
518 raise TypeError, "wrong argument type #{scope.class.inspect} (expected Array or Prism::Scope)"
519 end
520
521 template << "L"
522 values << locals.length
523
524 template << "C"
525 values << forwarding
526
527 locals.each do |local|
528 name = local.name
529 template << "L"
530 values << name.bytesize
531
532 template << "A*"
533 values << name.b
534 end
535 end
536 else
537 values << 0
538 end
539
540 values.pack(template)
541 end
542 end
543
544 # Here we are going to patch StringQuery to put in the class-level methods so
545 # that it can maintain a consistent interface
546 class StringQuery
547 class << self
548 # Mirrors the C extension's StringQuery::local? method.
549 def local?(string)
550 query(LibRubyParser.pm_string_query_local(string, string.bytesize, string.encoding.name))
551 end
552
553 # Mirrors the C extension's StringQuery::constant? method.
554 def constant?(string)
555 query(LibRubyParser.pm_string_query_constant(string, string.bytesize, string.encoding.name))
556 end
557
558 # Mirrors the C extension's StringQuery::method_name? method.
559 def method_name?(string)
560 query(LibRubyParser.pm_string_query_method_name(string, string.bytesize, string.encoding.name))
561 end
562
563 private
564
565 # Parse the enum result and return an appropriate boolean.
566 def query(result)
567 case result
568 when :PM_STRING_QUERY_ERROR
569 raise ArgumentError, "Invalid or non ascii-compatible encoding"
570 when :PM_STRING_QUERY_FALSE
571 false
572 when :PM_STRING_QUERY_TRUE
573 true
574 end
575 end
576 end
577 end
578 end