lib/prism/ffi.rb in prism-0.24.0 vs lib/prism/ffi.rb in prism-0.25.0

- old
+ new

@@ -1,16 +1,15 @@ # frozen_string_literal: true +# typed: ignore # This file is responsible for mirroring the API provided by the C extension by # using FFI to call into the shared library. require "rbconfig" require "ffi" module Prism - BACKEND = :FFI - module LibRubyParser # :nodoc: extend FFI::Library # Define the library that we will be pulling functions from. Note that this # must align with the build shared library from make/rake. @@ -22,19 +21,25 @@ # const char * -> :pointer # bool -> :bool # size_t -> :size_t # void -> :void # - def self.resolve_type(type) + def self.resolve_type(type, callbacks) type = type.strip - type.end_with?("*") ? :pointer : type.delete_prefix("const ").to_sym + + if !type.end_with?("*") + type.delete_prefix("const ").to_sym + else + type = type.delete_suffix("*").rstrip + callbacks.include?(type.to_sym) ? type.to_sym : :pointer + end end # Read through the given header file and find the declaration of each of the # given functions. For each one, define a function with the same name and # signature as the C function. - def self.load_exported_functions_from(header, *functions) + def self.load_exported_functions_from(header, *functions, callbacks) File.foreach(File.expand_path("../../include/#{header}", __dir__)) do |line| # We only want to attempt to load exported functions. next unless line.start_with?("PRISM_EXPORTED_FUNCTION ") # We only want to load the functions that we are interested in. @@ -54,46 +59,52 @@ arg_types = arg_types.split(",").map(&:strip) arg_types = [] if arg_types == %w[void] # Resolve the type of the argument by dropping the name of the argument # first if it is present. - arg_types.map! { |type| resolve_type(type.sub(/\w+$/, "")) } + arg_types.map! { |type| resolve_type(type.sub(/\w+$/, ""), callbacks) } # Attach the function using the FFI library. - attach_function name, arg_types, resolve_type(return_type) + attach_function name, arg_types, resolve_type(return_type, []) end # If we didn't find all of the functions, raise an error. raise "Could not find functions #{functions.inspect}" unless functions.empty? end + callback :pm_parse_stream_fgets_t, [:pointer, :int, :pointer], :pointer + load_exported_functions_from( "prism.h", "pm_version", "pm_serialize_parse", + "pm_serialize_parse_stream", "pm_serialize_parse_comments", "pm_serialize_lex", "pm_serialize_parse_lex", - "pm_parse_success_p" + "pm_parse_success_p", + [:pm_parse_stream_fgets_t] ) load_exported_functions_from( "prism/util/pm_buffer.h", "pm_buffer_sizeof", "pm_buffer_init", "pm_buffer_value", "pm_buffer_length", - "pm_buffer_free" + "pm_buffer_free", + [] ) load_exported_functions_from( "prism/util/pm_string.h", "pm_string_mapped_init", "pm_string_free", "pm_string_source", "pm_string_length", - "pm_string_sizeof" + "pm_string_sizeof", + [] ) # This object represents a pm_buffer_t. We only use it as an opaque pointer, # so it doesn't need to know the fields of pm_buffer_t. class PrismBuffer # :nodoc: @@ -214,17 +225,40 @@ def parse(code, **options) LibRubyParser::PrismString.with_string(code) { |string| parse_common(string, code, options) } end # Mirror the Prism.parse_file API by using the serialization API. This uses - # native strings instead of Ruby strings because it allows us to use mmap when - # it is available. + # native strings instead of Ruby strings because it allows us to use mmap + # when it is available. def parse_file(filepath, **options) options[:filepath] = filepath LibRubyParser::PrismString.with_file(filepath) { |string| parse_common(string, string.read, options) } end + # Mirror the Prism.parse_stream API by using the serialization API. + def parse_stream(stream, **options) + LibRubyParser::PrismBuffer.with do |buffer| + source = +"" + callback = -> (string, size, _) { + raise "Expected size to be >= 0, got: #{size}" if size <= 0 + + if !(line = stream.gets(size - 1)).nil? + source << line + string.write_string("#{line}\x00", line.bytesize + 1) + end + } + + # In the pm_serialize_parse_stream function it accepts a pointer to the + # IO object as a void* and then passes it through to the callback as the + # third argument, but it never touches it itself. As such, since we have + # access to the IO object already through the closure of the lambda, we + # can pass a null pointer here and not worry. + LibRubyParser.pm_serialize_parse_stream(buffer.pointer, nil, callback, dump_options(options)) + Prism.load(source, buffer.read) + end + end + # Mirror the Prism.parse_comments API by using the serialization API. def parse_comments(code, **options) LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, options) } end @@ -312,10 +346,28 @@ def parse_file_success_common(string, options) # :nodoc: LibRubyParser.pm_parse_success_p(string.pointer, string.length, dump_options(options)) end + # Return the value that should be dumped for the command_line option. + def dump_options_command_line(options) + command_line = options.fetch(:command_line, "") + raise ArgumentError, "command_line must be a string" unless command_line.is_a?(String) + + command_line.each_char.inject(0) do |value, char| + case char + when "a" then value | 0b000001 + when "e" then value | 0b000010 + when "l" then value | 0b000100 + when "n" then value | 0b001000 + when "p" then value | 0b010000 + when "x" then value | 0b100000 + else raise ArgumentError, "invalid command_line option: #{char}" + end + end + end + # Convert the given options into a serialized options string. def dump_options(options) template = +"" values = [] @@ -339,9 +391,12 @@ values << 0 end template << "C" values << (options.fetch(:frozen_string_literal, false) ? 1 : 0) + + template << "C" + values << dump_options_command_line(options) template << "C" values << { nil => 0, "3.3.0" => 1, "3.4.0" => 0, "latest" => 0 }.fetch(options[:version]) template << "L"