Skip to content

Commit

Permalink
Merge branch 'hugopl:fix-#48' into fix-#48
Browse files Browse the repository at this point in the history
  • Loading branch information
BlobCodes authored Sep 2, 2023
2 parents d7560b6 + 2aaf8bd commit 37cb410
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 9 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Changes that change the generated API have a ⚠️.

## [0.17.0] 2023-07-14
### Added
- Annotate deprecated methods in bindings (#114).
- Add `#to_s`, `#==` and `.parse` to `GLib::Variant` (#113).
- Add GC resistant GObject subclasses 🎉️, thanks @BlobCodes (#107).
- Allow enum and flags to be ignored in binding.yml (#101).
- Print GI annotation info for vfunc, helping debugging.

### Fixed
- Bind false boolean constants to false (#111).
- Ensure Bool return type on vfuncs that return booleans (#110).
- Fix ownership transfer of vfunc return values (#102).
- Fix compilation for vfuncs returning nullable objects or strings (#104).

### Changed
- Ignore deprecated `GObject::ValueArray` object. (#115)

## [0.16.0] 2023-06-16
### Added
- Add test helper methods: `ClosureDataManager.count`, `ClosureDataManager.info` and `ClosureDataManager.deregister_all` (#92).
Expand Down
3 changes: 3 additions & 0 deletions ecr/method.ecr
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<% render_doc(object, method) -%>
<% if method.deprecated? -%>
@[Deprecated]
<% end -%>
def <%= method_identifier %>(<% render_args_declaration -%>) <%= method_return_type_declaration %>
<%= method_gi_annotations %>
<% if throws? -%>
Expand Down
1 change: 1 addition & 0 deletions ecr/property.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
def <%= to_call(prop.name) %>=(value : <%= prop_type_name %>) : <%= prop_type_name %>
<% if prop.type_info.array? -%>
# handle array
unsafe_value = value.to_a.map(&.to_unsafe).to_unsafe
<% else -%>
unsafe_value = value
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: gi-crystal
version: 0.16.0
version: 0.17.0

authors:
- Hugo Parente Lima <[email protected]>
Expand Down
10 changes: 10 additions & 0 deletions spec/basic_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ class SubjectChildStore
end

describe "GObject Binding" do
it "generate @[Deprecated] annotations on deprecated methods" do
deprecated_methods = [] of String
{% for method in Test::Subject.methods %}
{% if method.annotation(Deprecated) %}
deprecated_methods << {{ method.name.stringify }}
{% end %}
{% end %}
deprecated_methods.should eq(%w(deprecated_method))
end

describe "reference counting" do
it "accessible by ref_count method" do
subject = Test::Subject.new(boolean: true)
Expand Down
16 changes: 16 additions & 0 deletions spec/g_variant_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ describe "GVariant" do
test_variant.not_nil!.as_i.should eq(42)
end

it "have a string representation" do
v = GLib::Variant.new("hey")
v.to_s.should eq("'hey'")
v = GLib::Variant.new(42_u64)
v.to_s.should eq("uint64 42")
v.to_s(false).should eq("42")
end

it "can parse text representations of GVariant's" do
v = GLib::Variant.new("hey")
GLib::Variant.parse(v.to_s).should eq(v)

v = GLib::Variant.new(42_u64)
GLib::Variant.parse(v.to_s).should eq(v)
end

context "respond to as_* and as_*?" do
it { GLib::Variant.new(8_u8).as_u8.should eq(8_u8) }
it { GLib::Variant.new(16_i16).as_i16.should eq(16_i16) }
Expand Down
5 changes: 5 additions & 0 deletions spec/libtest/test_subject.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ static void test_subject_iface_interface_init(TestIfaceInterface* iface) {
G_DEFINE_TYPE_WITH_CODE(TestSubject, test_subject, G_TYPE_OBJECT,
G_ADD_PRIVATE(TestSubject) G_IMPLEMENT_INTERFACE(TEST_TYPE_IFACE, test_subject_iface_interface_init))

void test_subject_set_str_list(TestSubject* self, const char** list);

typedef enum {
PROP_STRING = 1,
PROP_BOOLEAN,
Expand Down Expand Up @@ -670,6 +672,9 @@ int test_subject_sum_array_of_4_ints(TestSubject* self, int* array) {
return acc;
}

void test_subject_deprecated_method(TestSubject* self) {
}

GError* test_subject_return_g_error() {
return g_error_new(G_FILE_ERROR, G_FILE_ERROR_FAILED, "whatever message");
}
Expand Down
17 changes: 9 additions & 8 deletions spec/libtest/test_subject.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ void test_subject_set_simple_func(TestSubject* self, TestSubjectSimpleFunc func,
*/
gboolean test_subject_call_simple_func(TestSubject* self, int number);

/**
* test_subject_set_str_list:
* @list: (array zero-terminated=1):
*
* Setter for str_list property.
*/
void test_subject_set_str_list(TestSubject* self, const char** list);

/**
* test_subject_may_return_null:
* @return_nil:
Expand Down Expand Up @@ -430,6 +422,15 @@ int test_subject_nullable_optimal_parameter(TestSubject* self, gchar** param);
*/
int test_subject_sum_array_of_4_ints(TestSubject* self, int* array);

/**
* test_subject_deprecated_method:
*
* Used to test generation of @[Deprecated] annotations.
*
* Deprecated: This method is deprecated
*/
void test_subject_deprecated_method(TestSubject* self);

/**
* test_subject_return_g_error:
* Returns: (transfer full): A GError
Expand Down
34 changes: 34 additions & 0 deletions src/bindings/g_lib/variant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ module GLib
GLib::VariantType.new(ptr, GICrystal::Transfer::None)
end

# Parses a GVariant from a text representation.
def self.parse(text : String) : Variant
error = Pointer(LibGLib::Error).null
ptr = LibGLib.g_variant_parse(nil, text, nil, nil, pointerof(error))
GLib.raise_gerror(error) if error
Variant.new(ptr, :full)
end

# Pretty-prints value in the format understood by `#parse`.
def to_s(type_annotate : Bool)
String.build do |io|
to_s(io, type_annotate)
end
end

# ditto
def to_s(io : IO, type_annotate : Bool)
c_str = LibGLib.g_variant_print(self, GICrystal.to_c_bool(type_annotate))
io.write(::Bytes.new(c_str, LibC.strlen(c_str).to_i))
ensure
LibGLib.g_free(c_str) if c_str
end

# ditto
def to_s(io : IO)
to_s(io, true)
end

# Returns true if *other* variant have the same type and value of this variant.
def ==(other : Variant) : Bool
GICrystal.to_bool(LibGLib.g_variant_equal(self, other))
end

# :nodoc:
def to_unsafe
@ptr
end
Expand Down
2 changes: 2 additions & 0 deletions src/bindings/g_object/binding.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ types:
- prerequisites
Value:
handmade: true
ValueArray:
ignore: true
WeakRef:
ignore: true

Expand Down

0 comments on commit 37cb410

Please sign in to comment.