• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protocol Buffers - Google's data interchange format
2# Copyright 2022 Google Inc.  All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7
8module Google
9  module Protobuf
10    class FFI
11      # FileDescriptor
12      attach_function :file_def_name,   :upb_FileDef_Name,   [:FileDef], :string
13      attach_function :file_def_pool,   :upb_FileDef_Pool,   [:FileDef], :DefPool
14      attach_function :file_options,    :FileDescriptor_serialized_options,  [:FileDef, :pointer, Internal::Arena], :pointer
15    end
16
17    class FileDescriptor
18      attr :descriptor_pool, :file_def
19
20      def initialize(file_def, descriptor_pool)
21        @descriptor_pool = descriptor_pool
22        @file_def = file_def
23      end
24
25      def to_s
26        inspect
27      end
28
29      def inspect
30        "#{self.class.name}: #{name}"
31      end
32
33      def name
34        Google::Protobuf::FFI.file_def_name(@file_def)
35      end
36
37      def options
38        @options ||= begin
39          size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
40          temporary_arena = Google::Protobuf::FFI.create_arena
41          buffer = Google::Protobuf::FFI.file_options(@file_def, size_ptr, temporary_arena)
42          opts = Google::Protobuf::FileOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
43          opts.clear_features()
44          opts.freeze
45        end
46      end
47    end
48  end
49end
50