Skip to content

Commit

Permalink
Be more consistent with Ruby variable prefixes
Browse files Browse the repository at this point in the history
Let's always use `uniffi`, `Uniffi`, or `UNIFFI`. Before some of the
variable names had leading underscores, which seems redundant and not
idomatic Kotlin.  My guess is that it comes from copy and pasted Python
code.

I think the general contract with our users should be that we own the
`uniffi` prefix.  If they want to name something `UniffiFoo`, then they
take the risk of a name collision.
  • Loading branch information
bendk committed Dec 28, 2023
1 parent b705005 commit fb8dd5c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions uniffi_bindgen/src/bindings/ruby/gen_ruby/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ mod filters {
pub fn check_lower_rb(nm: &str, type_: &Type) -> Result<String, askama::Error> {
Ok(match type_ {
Type::Object { name, .. } => {
format!("({}._uniffi_check_lower {nm})", class_name_rb(name)?)
format!("({}.uniffi_check_lower {nm})", class_name_rb(name)?)
}
Type::Enum { .. }
| Type::Record { .. }
Expand Down Expand Up @@ -295,7 +295,7 @@ mod filters {
Type::Boolean => format!("({nm} ? 1 : 0)"),
Type::String => format!("RustBuffer.allocFromString({nm})"),
Type::Bytes => format!("RustBuffer.allocFromBytes({nm})"),
Type::Object { name, .. } => format!("({}._uniffi_lower {nm})", class_name_rb(name)?),
Type::Object { name, .. } => format!("({}.uniffi_lower {nm})", class_name_rb(name)?),
Type::CallbackInterface { .. } => {
panic!("No support for lowering callback interfaces yet")
}
Expand Down Expand Up @@ -329,7 +329,7 @@ mod filters {
Type::Boolean => format!("1 == {nm}"),
Type::String => format!("{nm}.consumeIntoString"),
Type::Bytes => format!("{nm}.consumeIntoBytes"),
Type::Object { name, .. } => format!("{}._uniffi_allocate({nm})", class_name_rb(name)?),
Type::Object { name, .. } => format!("{}.uniffi_allocate({nm})", class_name_rb(name)?),
Type::CallbackInterface { .. } => {
panic!("No support for lifting callback interfaces, yet")
}
Expand Down
22 changes: 11 additions & 11 deletions uniffi_bindgen/src/bindings/ruby/templates/ObjectTemplate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ class {{ obj.name()|class_name_rb }}

# A private helper for initializing instances of the class from a raw pointer,
# bypassing any initialization logic and ensuring they are GC'd properly.
def self._uniffi_allocate(pointer)
def self.uniffi_allocate(pointer)
pointer.autorelease = false
inst = allocate
inst.instance_variable_set :@pointer, pointer
ObjectSpace.define_finalizer(inst, _uniffi_define_finalizer_by_pointer(pointer, inst.object_id))
ObjectSpace.define_finalizer(inst, uniffi_define_finalizer_by_pointer(pointer, inst.object_id))
return inst
end

# A private helper for registering an object finalizer.
# N.B. it's important that this does not capture a reference
# to the actual instance, only its underlying pointer.
def self._uniffi_define_finalizer_by_pointer(pointer, object_id)
def self.uniffi_define_finalizer_by_pointer(pointer, object_id)
Proc.new do |_id|
{{ ci.namespace()|class_name_rb }}.rust_call(
:{{ obj.ffi_object_free().name() }},
Expand All @@ -25,21 +25,21 @@ def self._uniffi_define_finalizer_by_pointer(pointer, object_id)
# A private helper for lowering instances into a raw pointer.
# This does an explicit typecheck, because accidentally lowering a different type of
# object in a place where this type is expected, could lead to memory unsafety.
def self._uniffi_check_lower(inst)
def self.uniffi_check_lower(inst)
if not inst.is_a? self
raise TypeError.new "Expected a {{ obj.name()|class_name_rb }} instance, got #{inst}"
end
end

def _uniffi_clone_pointer()
def uniffi_clone_pointer()
return {{ ci.namespace()|class_name_rb }}.rust_call(
:{{ obj.ffi_object_clone().name() }},
@pointer
)
end

def self._uniffi_lower(inst)
return inst._uniffi_clone_pointer()
def self.uniffi_lower(inst)
return inst.uniffi_clone_pointer()
end

{%- match obj.primary_constructor() %}
Expand All @@ -48,7 +48,7 @@ def initialize({% call rb::arg_list_decl(cons) -%})
{%- call rb::setup_args_extra_indent(cons) %}
pointer = {% call rb::to_ffi_call(cons) %}
@pointer = pointer
ObjectSpace.define_finalizer(self, self.class._uniffi_define_finalizer_by_pointer(pointer, self.object_id))
ObjectSpace.define_finalizer(self, self.class.uniffi_define_finalizer_by_pointer(pointer, self.object_id))
end
{%- when None %}
{%- endmatch %}
Expand All @@ -59,7 +59,7 @@ def self.{{ cons.name()|fn_name_rb }}({% call rb::arg_list_decl(cons) %})
# Call the (fallible) function before creating any half-baked object instances.
# Lightly yucky way to bypass the usual "initialize" logic
# and just create a new instance with the required pointer.
return _uniffi_allocate({% call rb::to_ffi_call(cons) %})
return uniffi_allocate({% call rb::to_ffi_call(cons) %})
end
{% endfor %}
Expand All @@ -69,14 +69,14 @@ def self.{{ cons.name()|fn_name_rb }}({% call rb::arg_list_decl(cons) %})
{%- when Some with (return_type) -%}
def {{ meth.name()|fn_name_rb }}({% call rb::arg_list_decl(meth) %})
{%- call rb::setup_args_extra_indent(meth) %}
result = {% call rb::to_ffi_call_with_prefix("_uniffi_clone_pointer()", meth) %}
result = {% call rb::to_ffi_call_with_prefix("uniffi_clone_pointer()", meth) %}
return {{ "result"|lift_rb(return_type) }}
end
{%- when None -%}
def {{ meth.name()|fn_name_rb }}({% call rb::arg_list_decl(meth) %})
{%- call rb::setup_args_extra_indent(meth) %}
{% call rb::to_ffi_call_with_prefix("_uniffi_clone_pointer()", meth) %}
{% call rb::to_ffi_call_with_prefix("uniffi_clone_pointer()", meth) %}
end
{% endmatch %}
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def write_{{ canonical_type_name }}(v)
# The Object type {{ object_name }}.
def write_{{ canonical_type_name }}(obj)
pointer = {{ object_name|class_name_rb}}._uniffi_lower obj
pointer = {{ object_name|class_name_rb}}.uniffi_lower obj
pack_into(8, 'Q>', pointer.address)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def read{{ canonical_type_name }}
def read{{ canonical_type_name }}
pointer = FFI::Pointer.new unpack_from 8, 'Q>'
return {{ object_name|class_name_rb }}._uniffi_allocate(pointer)
return {{ object_name|class_name_rb }}.uniffi_allocate(pointer)
end
{% when Type::Enum { name, module_path } -%}
Expand Down

0 comments on commit fb8dd5c

Please sign in to comment.