This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch eo-api-finalize
in repository ego.
View the commit online.
commit c33449572cd74b06c081a4d9245847969be94e93
Author: Cedric Bail <[email protected]>
AuthorDate: Thu Nov 26 02:01:53 2026 -0600
efl: a Handle is a value whose copies close the same callback once
Close had a pointer receiver, so the handle an On method returned could
not be closed where it was made, and a copy of a handle from an Add
method ran its removal a second time, which EFL takes for the removal
of a callback that is not there. Close also returned nothing, which
kept a Handle out of anything that takes an io.Closer.
A Handle is now two words that copies share the state behind: an event
handler is found by its id, which is never used again, and an Add
method's removal is kept in one place and cleared as it runs. Close
returns an error for io.Closer's sake, always nil. What generated code
calls keeps its signature, so no generated file changes.
efl/README.md is regenerated, which also brings it up to date with the
documentation changed since it was last generated.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
README.md | 9 +++--
efl/README.md | 103 ++++++++++++++++++++++++++++++++++++++++----------
efl/event.go | 61 ++++++++++++++++--------------
efl/event_test.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++
efl/loopcheck_test.go | 2 +-
5 files changed, 220 insertions(+), 52 deletions(-)
diff --git a/README.md b/README.md
index 9f6bf81c..9c96c599 100644
--- a/README.md
+++ b/README.md
@@ -135,10 +135,11 @@ if l, ok := ui.AsLabel(item.ExtraPart().Content()); ok { ... }
`textbox.PlaceholderPart().SetText("City")`.
**Events.** `OnClicked`, `OnChanged` and the other `On*` methods take a
-typed Go function and return an `efl.Handle` whose `Close` removes it. The
-handlers belong to the object and go with it. A handler that stays for the
-object's whole life can be given as it is made, with the `WithOn` options:
-`button.WithOnClicked(func(efl.ClickableClicked) { ... })`.
+typed Go function and return an `efl.Handle`, a value whose `Close` removes
+the handler: closing any copy closes them all, and `Close` satisfies
+`io.Closer`. The handlers belong to the object and go with it. A handler
+that stays for the object's whole life can be given as it is made, with the
+`WithOn` options: `button.WithOnClicked(func(efl.ClickableClicked) { ... })`.
**Layout.** Widgets in a box share its extra space according to their
weight, which defaults to 1. Give a label `label.WithHintWeight(1, 0)` to keep it
diff --git a/efl/README.md b/efl/README.md
index c2c42759..ffb26ca8 100644
--- a/efl/README.md
+++ b/efl/README.md
@@ -44,10 +44,12 @@ embedding a wrapper. Two wrappers of the same object are not the same Go
value -- a getter makes a new one each time -- so they are compared with
Same, not ==.
-A getter, an event or a model hands back an *Object when that is all its
-declared type promises: the content of a slot, the item a factory built,
-a model's child. Each generated type has an As function that checks what
-the object really is and returns its typed wrapper:
+An event gives its handler the type it declares -- a key handler gets an
+*input.Key -- except where naming it would make two packages import each
+other. A getter or a model hands back an *Object when that is all its
+declared type promises: the content of a slot, a model's child. Each
+generated type has an As function that checks what the object really is
+and returns its typed wrapper:
if l, ok := ui.AsLabel(item.ExtraPart().Content()); ok {
l.SetText("12.5")
@@ -446,10 +448,13 @@ DefineClass defines an EFL class called name, extending parent and
implementing the interfaces and mixins in implements, whose instances are
Go values of type T. New creates them.
-*T must embed efl.Object, or a type that does. A method of *T with the
+*T must embed efl.Object, or a type that does, such as a generated wrapper
+of the parent. A method that *T declares itself, with the
name and type of a function the class has -- Properties, Property,
ChildrenCount for an Efl.Model -- is that function's implementation, for
-EFL as much as for Go: EFL calling it on the object calls the method.
+EFL as much as for Go: EFL calling it on the object calls the method. A
+method *T has only because it embeds another type is not one: it stays
+the embedded type's, and calls what the parent class does.
Which functions can be implemented this way is up to the binding that
declares them; eo implements Efl.Model's.
@@ -473,20 +478,40 @@ Other goroutines may use PostContext and Sync to dispatch work to the EFL thread
queued tasks are processed once Wait enters the main loop.
Init, Wait, and Run must all be called from the same goroutine (typically
-the main goroutine). Calling Init more than once is a no-op.
+the main goroutine). Init does its work once: every later call returns what
+the first one did.
+
+Init returns an error when EFL could not start: elm_init reports failure by
+returning no init count, and the dispatch pipe may be refused. EFL is then
+left as it was before Init, the goroutine is no longer pinned to its thread,
+and Wait and Shutdown return at once while PostContext and Sync panic, as
+before Init. Nothing else of EFL may be used, InLoop included: Eina goes
+on taking the thread that tried for the loop's.
### MainLoop
MainLoop returns the process's main loop.
Anything extending Efl.Loop_Consumer -- an Efl.Exe, an Efl.Io.Copier, a
-timer -- has to be parented to something that provides a loop, and until
-this existed there was no way to name one from Go without first creating a
-window. The loop belongs to the process, not to the caller: the returned
+timer -- has to be parented to something that provides a loop, and this
+is one that needs no window. The loop belongs to the process, not to the caller: the returned
Object is borrowed and must not be unreferenced.
It is only valid between Init and Shutdown.
+### Must
+
+Must returns v, and panics if err is not nil. It is for setup code, where
+failing to build an object means the program cannot carry on, as
+[text/template.Must] and [regexp.MustCompile] are for a template or an
+_expression_ that is part of the source:
+
+ mainWindow := efl.Must(ui.NewWin(nil, win.WithWinName("app")))
+
+A constructor EFL refuses returns an [*ConstructError], which is an
+ordinary outcome for classes that do real work when they are finalized, so
+code that can do something about it handles the error instead.
+
### New
New creates an instance of c, a class DefineClass defined with T, under
@@ -498,6 +523,12 @@ constructors, New does not hand the caller a reference of its own, and
dropping the Go value does not delete anything. An object with no parent
lives until Del. Must be called on the EFL thread.
+The options are applied in the order given. They are for the generated
+class c extends. Those a class's package
+makes are typed for the class, so they are passed through Untyped:
+
+ efl.New[mine](cls, parent, textbox.WithText("x").Untyped())
+
### NewContent
NewContent allocates a new Eina_Content that copies data and records mimeType.
@@ -521,10 +552,10 @@ is owned, as NewValue's is.
### NewHandle
-NewHandle returns a Handle whose Close calls remove, once. It is what
-generated Add methods return, remove being the call to the matching Del
-with what that Add registered: EFL finds a callback by the data pointer it
-was given, which only the Add knows.
+NewHandle returns a Handle whose Close calls remove, once, however many of
+its copies are closed. It is what generated Add methods return, remove being
+the call to the matching Del with what that Add registered: EFL finds a
+callback by the data pointer it was given, which only the Add knows.
### NewObjectsValue
@@ -566,6 +597,12 @@ Returns nil if EFL fails to allocate or set up the array.
ObjectClass is Efl.Object, the root every class descends from.
+### OptionOf
+
+OptionOf is o as an option of class T. It is how the generated With
+functions make theirs, and it is not checked: nothing says o suits a T,
+so it is for code that has just made o for one.
+
### Part
Part returns obj's part called name, such as "first" on a Panes or
@@ -603,7 +640,8 @@ Panics if called before Init.
Run initialises EFL, calls setup on the EFL thread, enters the main loop, and
blocks until the main loop exits (i.e. until Shutdown is called). It combines
-Init + setup + Wait into a single convenience call.
+Init + setup + Wait into a single convenience call. When Init fails, Run
+returns its error and does not call setup.
### Same
@@ -646,8 +684,10 @@ returns the value produced by fn. It has the same constraints as Sync.
### ValueGet
-ValueGet retrieves the value stored in v as type T.
-Returns an error if T does not match the stored type or if v is nil.
+ValueGet retrieves the scalar stored in v as type T, by the rules
+ValueUnmarshal documents: it returns an error, and not a wrong number, when
+v holds another kind of value or a number T cannot hold. It also fails if v
+is nil.
### ValueMap
@@ -666,6 +706,15 @@ Supported input types: bool, string, all integer and float variants,
string keys, and structs (marshalled via Eina_Value_Struct). nil returns
an error. The returned Value is owned, as NewValue's is.
+### ValueMarshalable
+
+ValueMarshalable reports why a value of the type typ could not be encoded
+by ValueMarshal and read back by ValueUnmarshal, and is nil when every
+value of it can: a scalar or a named scalar, a slice or a map with string
+keys of such, or a struct of such with an exported field. A pointer, an
+interface, a func, an array, and a struct without an exported field, as
+time.Time is, are refused. It looks at the type alone.
+
### ValueSlice
ValueSlice reads the elements of the array Value v and returns them as a Go
@@ -681,8 +730,24 @@ ValueUnmarshal reads the scalar stored in val and writes it into the value
pointed to by dst. dst must be a non-nil pointer to one of the supported
scalar types (bool, string, int family, uint family, float family).
-The unmarshal uses the destination type to determine which ValueGet variant
-to call; if the stored type cannot be coerced by Eina, an error is returned.
+Scalars are read by the type they are stored as, which is never trusted to
+be what dst holds:
+ - an integer of any Eina type (int, short, long, int64, timestamp and the
+ unsigned ones, and a char, which is a byte, so unsigned) goes into any
+ Go integer type the number fits, and is an error where it does not: 3
+ fits an int8, 300 does not, and -1 does not fit a uint;
+ - a float or double goes into a float32 or float64 it does not overflow;
+ - a bool goes into a bool, and an unsigned char, which Eina does not tell
+ from one, also into an integer type its number fits;
+ - a string or stringshare goes into a string.
+
+Nothing else converts: a string does not become an int, a double is not
+truncated into an int, an int does not become a float, and an array, a
+struct or an error is refused by every scalar. The elements of a slice or a
+map and the members of a struct follow the same rules, whether the array
+holds them as their type or as Eina_Values. A destination that is not read
+is an error and is left as it was. A named type is read as its underlying
+type.
### Wait
diff --git a/efl/event.go b/efl/event.go
index 78352b6e..d03f0f5e 100644
--- a/efl/event.go
+++ b/efl/event.go
@@ -56,22 +56,28 @@ var (
)
// Handle represents a registered EFL callback: an event handler, or a
-// callback given to an Add method such as SignalCallbackAdd. Call Close to
-// remove the callback and release all associated resources. A zero Handle is
-// invalid.
+// callback given to an Add method such as SignalCallbackAdd. Close it to
+// remove the callback and release all associated resources. A Handle is a
+// value: its copies stand for the same callback, and closing any of them
+// closes it for all. The zero Handle stands for no callback.
type Handle struct {
- id uintptr // key into handlers
- // remove, when set, is how a callback given to an Add method comes off:
- // it calls the matching Del with what the Add registered.
- remove func()
+ id uintptr // key into handlers, for an event handler
+ // removal, when set, is how a callback given to an Add method comes off.
+ removal *removal
}
-// NewHandle returns a Handle whose Close calls remove, once. It is what
-// generated Add methods return, remove being the call to the matching Del
-// with what that Add registered: EFL finds a callback by the data pointer it
-// was given, which only the Add knows.
+// removal is the shared state of the copies of a Handle made by NewHandle.
+// Like the handlers, it is only touched on the loop thread.
+type removal struct {
+ remove func() // nil once called
+}
+
+// NewHandle returns a Handle whose Close calls remove, once, however many of
+// its copies are closed. It is what generated Add methods return, remove being
+// the call to the matching Del with what that Add registered: EFL finds a
+// callback by the data pointer it was given, which only the Add knows.
func NewHandle(remove func()) Handle {
- return Handle{remove: remove}
+ return Handle{removal: &removal{remove: remove}}
}
// registerCallback registers fn as an EFL event callback on obj for the event
@@ -99,34 +105,33 @@ func registerCallback(obj unsafe.Pointer, desc unsafe.Pointer, fn func(eventInfo
}
// Close removes the callback from the EFL object and deletes the Go-side
-// entry. Must be called on the EFL thread. Calling Close on a zero or
-// already-closed Handle is a no-op.
-func (h *Handle) Close() {
- if h == nil {
- return
- }
- if h.remove != nil {
- remove := h.remove
- h.remove = nil
- remove()
- return
+// entry. Must be called on the EFL thread. Closing a copy of a Handle closes
+// them all; closing the zero Handle, a Handle already closed, or an event
+// handler whose object EFL has deleted, does nothing. The error is always
+// nil: Close returns one to satisfy [io.Closer].
+func (h Handle) Close() error {
+ if h.removal != nil {
+ if remove := h.removal.remove; remove != nil {
+ h.removal.remove = nil
+ remove()
+ }
+ return nil
}
if h.id == 0 {
- return
+ return nil
}
// An object EFL has deleted took its callbacks with it, and the Go side
// was released then too: there is nothing left to remove, and the
- // pointer must not be used.
+ // pointer must not be used. Ids are never reused, so a closed handler
+ // is as absent as one that went with its object.
assertLoop()
if live := handlers[h.id]; live != nil {
unhook := forget(live)
live.detach()
unhookAll(unhook)
}
-
- // Zero the handle so subsequent calls are no-ops.
- h.id = 0
+ return nil
}
// forget drops the Go side of one handler, and returns the pointers whose
diff --git a/efl/event_test.go b/efl/event_test.go
index 3768f6a9..351e9719 100644
--- a/efl/event_test.go
+++ b/efl/event_test.go
@@ -1,6 +1,7 @@
package efl_test
import (
+ "io"
"runtime"
"sync/atomic"
"testing"
@@ -9,8 +10,12 @@ import (
"git.enlightenment.org/cedric/ego/efl"
"git.enlightenment.org/cedric/ego/efl/eort"
+ "git.enlightenment.org/cedric/ego/efl/ui"
+ "git.enlightenment.org/cedric/ego/efl/ui/win"
)
+var _ io.Closer = efl.Handle{}
+
// newTestObject allocates a loop-timer Eo object that is exclusively owned by
// the main loop (no extra caller reference). Calling efl.DelEoObject on the
// returned pointer sets the parent to null and drops the refcount to zero,
@@ -220,3 +225,95 @@ func TestRegisterMultipleCallbacks(t *testing.T) {
t.Fatalf("callbacks fired %d time(s), want 3", n)
}
}
+
+// Closing a copy of an event handler's Handle closes the handler once, and
+// leaves the object's other handlers alone, whichever copy is closed first.
+func TestCloseCopiesOfEventHandle(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ order func(original, duplicate efl.Handle)
+ }{
+ {"original first", func(original, duplicate efl.Handle) { original.Close(); duplicate.Close() }},
+ {"copy first", func(original, duplicate efl.Handle) { duplicate.Close(); original.Close() }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ var closed, kept atomic.Int32
+ efl.Sync(func() {
+ raw := newTestObject(t)
+ original := eort.RegisterCallback(raw, efl.EFLEventDelDesc(), func(unsafe.Pointer) { closed.Add(1) })
+ eort.RegisterCallback(raw, efl.EFLEventDelDesc(), func(unsafe.Pointer) { kept.Add(1) })
+ duplicate := original
+
+ tc.order(original, duplicate)
+ if n := efl.CallbacksOn(raw); n != 1 {
+ t.Errorf("%d handlers left after closing the copies, want the other one", n)
+ }
+ efl.DelEoObject(raw)
+ })
+ if closed.Load() != 0 || kept.Load() != 1 {
+ t.Errorf("closed handler ran %d time(s), the other %d, want 0 and 1", closed.Load(), kept.Load())
+ }
+ })
+ }
+}
+
+// Closing a copy of a NewHandle Handle calls its remove function once, in
+// either order, and the zero Handle closes to nothing.
+func TestCloseCopiesOfRemoveHandle(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ order func(original, duplicate efl.Handle)
+ }{
+ {"original first", func(original, duplicate efl.Handle) { original.Close(); duplicate.Close() }},
+ {"copy first", func(original, duplicate efl.Handle) { duplicate.Close(); original.Close() }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ calls := 0
+ original := efl.NewHandle(func() { calls++ })
+ duplicate := original
+ tc.order(original, duplicate)
+ if calls != 1 {
+ t.Errorf("remove called %d time(s), want 1", calls)
+ }
+ })
+ }
+ t.Run("zero", func(t *testing.T) {
+ var zero efl.Handle
+ if err := zero.Close(); err != nil {
+ t.Errorf("Close of the zero Handle: %v", err)
+ }
+ })
+}
+
+// A Handle is closed as it is returned, with no variable to hold it.
+func TestCloseTheValueReturned(t *testing.T) {
+ efl.Sync(func() {
+ raw := newTestObject(t)
+ eort.RegisterCallback(raw, efl.EFLEventDelDesc(), func(unsafe.Pointer) {}).Close()
+ if n := efl.CallbacksOn(raw); n != 0 {
+ t.Errorf("%d handlers left, want none", n)
+ }
+ efl.DelEoObject(raw)
+ })
+}
+
+// BenchmarkOnClickedClose measures a handler added and removed again.
+func BenchmarkOnClickedClose(b *testing.B) {
+ var greet *ui.Button
+ var mainWindow *ui.Win
+ efl.Sync(func() {
+ mainWindow = efl.Must(ui.NewWin(nil, win.WithWinName("bench-handle")))
+ greet = efl.Must(ui.NewButton(mainWindow))
+ })
+ fn := func(efl.ClickableClicked) {}
+ b.ReportAllocs()
+ efl.Sync(func() {
+ for b.Loop() {
+ greet.OnClicked(fn).Close()
+ }
+ })
+ efl.Sync(func() {
+ greet.Unref()
+ mainWindow.Unref()
+ })
+}
diff --git a/efl/loopcheck_test.go b/efl/loopcheck_test.go
index 34bef52e..f9ad72a2 100644
--- a/efl/loopcheck_test.go
+++ b/efl/loopcheck_test.go
@@ -37,7 +37,7 @@ func TestBookkeepingOffTheLoopPanics(t *testing.T) {
for name, fn := range map[string]func(){
"register a handler": func() { eort.RegisterCallback(raw, efl.EFLEventDelDesc(), func(unsafe.Pointer) {}) },
- "close a handler": handle.Close,
+ "close a handler": func() { handle.Close() },
"wrap an object": func() { eort.Wrap(raw) },
"register a closure": func() { eort.CallbackRegister(func() {}) },
"remove a closure": func() { eort.CallbackRemove(1) },
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.