Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/ruby/templates/ObjectTemplate.rb

raw
1 class {{ obj.name()|class_name_rb }}
2
3 # A private helper for initializing instances of the class from a raw pointer,
4 # bypassing any initialization logic and ensuring they are GC'd properly.
5 def self.uniffi_allocate(pointer)
6 pointer.autorelease = false
7 inst = allocate
8 inst.instance_variable_set :@pointer, pointer
9 ObjectSpace.define_finalizer(inst, uniffi_define_finalizer_by_pointer(pointer, inst.object_id))
10 return inst
11 end
12
13 # A private helper for registering an object finalizer.
14 # N.B. it's important that this does not capture a reference
15 # to the actual instance, only its underlying pointer.
16 def self.uniffi_define_finalizer_by_pointer(pointer, object_id)
17 Proc.new do |_id|
18 {{ ci.namespace()|class_name_rb }}.rust_call(
19 :{{ obj.ffi_object_free().name() }},
20 pointer
21 )
22 end
23 end
24
25 # A private helper for lowering instances into a raw pointer.
26 # This does an explicit typecheck, because accidentally lowering a different type of
27 # object in a place where this type is expected, could lead to memory unsafety.
28 def self.uniffi_check_lower(inst)
29 if not inst.is_a? self
30 raise TypeError.new "Expected a {{ obj.name()|class_name_rb }} instance, got #{inst}"
31 end
32 end
33
34 def uniffi_clone_pointer()
35 return {{ ci.namespace()|class_name_rb }}.rust_call(
36 :{{ obj.ffi_object_clone().name() }},
37 @pointer
38 )
39 end
40
41 def self.uniffi_lower(inst)
42 return inst.uniffi_clone_pointer()
43 end
44
45 {%- match obj.primary_constructor() %}
46 {%- when Some with (cons) %}
47 def initialize({% call rb::arg_list_decl(cons) -%})
48 {%- call rb::setup_args_extra_indent(cons) %}
49 pointer = {% call rb::to_ffi_call(cons) %}
50 @pointer = pointer
51 ObjectSpace.define_finalizer(self, self.class.uniffi_define_finalizer_by_pointer(pointer, self.object_id))
52 end
53 {%- when None %}
54 {%- endmatch %}
55
56 {% for cons in obj.alternate_constructors() -%}
57 def self.{{ cons.name()|fn_name_rb }}({% call rb::arg_list_decl(cons) %})
58 {%- call rb::setup_args_extra_indent(cons) %}
59 # Call the (fallible) function before creating any half-baked object instances.
60 # Lightly yucky way to bypass the usual "initialize" logic
61 # and just create a new instance with the required pointer.
62 return uniffi_allocate({% call rb::to_ffi_call(cons) %})
63 end
64 {% endfor %}
65
66 {% for meth in obj.methods() -%}
67 {%- match meth.return_type() -%}
68
69 {%- when Some with (return_type) -%}
70 def {{ meth.name()|fn_name_rb }}({% call rb::arg_list_decl(meth) %})
71 {%- call rb::setup_args_extra_indent(meth) %}
72 result = {% call rb::to_ffi_call_with_prefix("uniffi_clone_pointer()", meth) %}
73 return {{ "result"|lift_rb(return_type) }}
74 end
75
76 {%- when None -%}
77 def {{ meth.name()|fn_name_rb }}({% call rb::arg_list_decl(meth) %})
78 {%- call rb::setup_args_extra_indent(meth) %}
79 {% call rb::to_ffi_call_with_prefix("uniffi_clone_pointer()", meth) %}
80 end
81 {% endmatch %}
82 {% endfor %}
83 end