Skip to content

Commit

Permalink
StringName Part 3
Browse files Browse the repository at this point in the history
I thought I was done. Nay.
  • Loading branch information
ParadoxV5 committed Sep 15, 2023
1 parent 9e05af0 commit 1890704
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
8 changes: 6 additions & 2 deletions sig/variants.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ module Godot

class String < Variant
VARIANT_TYPE: 4
def to_s: () -> String
def to_str: () -> String
def to_s: () -> ::String
def to_str: () -> ::String
end

class StringName < Variant
VARIANT_TYPE: 21
def to_s: () -> ::String
def to_str: () -> ::String
def to_sym: () -> Symbol
end

class Object < Variant
Expand Down
9 changes: 9 additions & 0 deletions src/ruby/mixins.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ __attribute__((used)) VALUE godot_rb_mString_i_to_godot(VALUE self) {
return godot_rb_wrap_variant(godot_rb_cVariants[GDEXTENSION_VARIANT_TYPE_STRING], self_variant);
}

__attribute__((used)) VALUE godot_rb_mSymbol_i_to_godot(VALUE self) {
GDExtensionStringName string = godot_rb_obj_to_string_name(self);
GDExtensionVariantPtr self_variant = godot_rb_variant_alloc();
godot_rb_gdextension.variant_from_string_name(self_variant, &string);
godot_rb_gdextension.string_name_destroy(&string);
return godot_rb_wrap_variant(godot_rb_cVariants[GDEXTENSION_VARIANT_TYPE_STRING_NAME], self_variant);
}

void godot_rb_init_Mixins() {
godot_rb_require_relative(mixins);
const VALUE space = rb_const_get(godot_rb_mGodot, rb_intern("Mixins"));
Expand Down Expand Up @@ -49,4 +57,5 @@ void godot_rb_init_Mixins() {
godot_rb_cVariants[GDEXTENSION_VARIANT_TYPE_BOOL] = godot_rb_cVariant;

d(String)
d(Symbol)
}
24 changes: 15 additions & 9 deletions src/ruby/string_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@ GDExtensionStringName godot_rb_obj_to_string_name(VALUE self) {
if RB_LIKELY(rb_obj_is_instance_of(self, godot_rb_cVariants[GDEXTENSION_VARIANT_TYPE_STRING_NAME]))
godot_rb_gdextension.string_name_from_variant(&string_name, godot_rb_cVariant_get_variant(self));
else {
// Specialized Strings come from regular Strings, so we convert to String rather than Symbol
VALUE self_string;
if RB_LIKELY(SYMBOL_P(self))
self_string = rb_sym2str(self);
GDExtensionString string;
// Second, optimize for {String}s
if RB_UNLIKELY(rb_obj_is_instance_of(self, godot_rb_cVariants[GDEXTENSION_VARIANT_TYPE_STRING]))
godot_rb_gdextension.string_name_from_variant(&string, godot_rb_cVariant_get_variant(self));
else {
self_string = rb_check_string_type(self);
if RB_UNLIKELY(NIL_P(self_string))
rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string", self);
VALUE self_string;
// Specialized Strings come from regular Strings, so we convert to String rather than Symbol
if RB_LIKELY(SYMBOL_P(self))
self_string = rb_sym2str(self);
else {
self_string = rb_check_string_type(self);
if RB_UNLIKELY(NIL_P(self_string))
rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string", self);
}
string = godot_rb_obj_to_string(self_string);
}
GDExtensionString string = godot_rb_obj_to_string(self_string);
godot_rb_gdextension.string_name_from_string(&string_name, (GDExtensionConstStringNamePtr[]){&string});
godot_rb_gdextension.string_name_from_string(&string_name, (GDExtensionConstStringNamePtr[]) {&string});
godot_rb_gdextension.string_destroy(&string);
}
return string_name;
Expand Down

0 comments on commit 1890704

Please sign in to comment.