From 9f25e49bbfc7dbec06f50918377565621a72da6e Mon Sep 17 00:00:00 2001 From: milkeclair Date: Wed, 18 Nov 2026 13:42:50 +0900 Subject: [PATCH 01/11] feat: collect class instances with a single owner Tie each owner to its variable lifetime and sweep expired instances as allocations continue. Require predeclared destinations and support transferring ownership without relying on prompts, loops, or call framing. --- lib/cls/README.md | 32 ++++++- lib/cls/gc/process.zsh | 27 ++++++ lib/cls/instance/process.zsh | 13 ++- lib/cls/new/process.zsh | 22 ++--- lib/cls/owner/operator.zsh | 59 +++++++++++++ lib/cls/owner/process.zsh | 53 ++++++++++++ lib/cls/process.zsh | 17 +--- lib/cls/store/process.zsh | 8 +- test/lib/cls/fixtures/exported-child.zsh | 1 + test/lib/cls/fixtures/method-errexit.zsh | 1 + test/lib/cls/gc/process_test.zsh | 59 +++++++++++++ test/lib/cls/new/process_test.zsh | 18 ++++ test/lib/cls/owner/operator_test.zsh | 43 ++++++++++ test/lib/cls/owner/process_test.zsh | 85 +++++++++++++++++++ test/lib/cls/process_test.zsh | 10 ++- test/lib/cls/store/process_test.zsh | 15 ++++ .../completion/cache/build/process_test.zsh | 2 +- 17 files changed, 424 insertions(+), 41 deletions(-) create mode 100644 lib/cls/gc/process.zsh create mode 100644 lib/cls/owner/operator.zsh create mode 100644 lib/cls/owner/process.zsh create mode 100644 test/lib/cls/gc/process_test.zsh create mode 100644 test/lib/cls/owner/operator_test.zsh create mode 100644 test/lib/cls/owner/process_test.zsh diff --git a/lib/cls/README.md b/lib/cls/README.md index 5e9b71cf..6fc3d2d6 100644 --- a/lib/cls/README.md +++ b/lib/cls/README.md @@ -30,6 +30,7 @@ z.cls.drew ```zsh source ./counter.zsh +typeset counter="" z.cls.new Counter counter counter.increment @@ -62,13 +63,14 @@ setterが成功すると、その時点で保存値へ反映します。直後 ## 参照と変数のスコープ `z.cls.new Class variable`は、指定した変数と`REPLY`へ実体参照を代入し、 -指定した変数をreadonlyにします。参照を持つ変数は通常のZshのスコープに従います。 +指定した変数をreadonlyにし、実体の唯一の所有変数として登録します。変数は呼出元で先に宣言します。 同じ変数への再代入や、同じ変数を使った実体の再生成は拒否されます。 以下の例は、それぞれの前の実行例とは独立したシェルで、Counterの定義を 読み込んだ状態から実行します。ローカルな参照を作る場合は、呼出元で変数をlocal宣言します。 ```zsh +typeset counter="" z.cls.new Counter counter counter.count.set 10 @@ -85,7 +87,7 @@ counter.count.get print -r -- "$REPLY" # 10 ``` -続けて参照をコピーすると、同じ実体の状態を共有できます。 +続けて参照をコピーすると、同じ実体の状態を共有できます。コピーは借用参照で、実体の寿命を延ばしません。 参照を展開したコマンド名は、その参照を持つ変数からメソッドや属性を呼び出します。 ```zsh @@ -102,7 +104,29 @@ print -r -- "$REPLY" # 11 `z.cls.delete variable`または`z.cls.delete "$reference"`で実体を解放します。 削除後も参照変数の値とreadonly属性は残りますが、その実体への参照は全て無効になります。 -変数のスコープを抜けるだけでは実体の状態を解放しないため、使い終わったら明示的に削除します。 +所有変数のスコープを抜けた実体は、前回の回収から64件生成された後の次のnewで自動回収します。 +実行中の実体は、メソッドが終了した後に回収できます。 +属性や配列に入れた実体IDも借用参照で、実体の寿命は所有変数に従います。 + +実体を呼出元に渡す場合は、所有権を宣言済みの変数へ移します。 + +```zsh +prepare_counter() { + local counter="" + z.cls.new Counter counter || return 1 + counter.count.set 5 + z.cls.owner.move from=counter "to=$1" +} + +example() { + local saved="" + prepare_counter saved || return 1 + $saved.count.get + # REPLY: 5 +} +``` + +移動元に残る値は借用参照です。移動先からは実体IDを展開して呼び出します。 ## 定義と呼出しの規則 @@ -133,7 +157,7 @@ print -r -- "$REPLY" # 11 ## 公開APIと内部関数 -公開APIはdraw、attr、fn、fn.done、drew、abort、new、deleteの8関数です。 +公開APIはdraw、attr、fn、fn.done、drew、abort、new、owner.move、deleteの9関数です。 内部の補助関数は、zの既存規約に従い、関数名の最後の区切りをアンダースコアから始めます。 シェル補完とz-lsは通常、この補助関数を候補から除外します。 z-lsではshowPrivateFunctions設定で表示できます。この規約は、Zshからの直接呼出しを制限するものではありません。 diff --git a/lib/cls/gc/process.zsh b/lib/cls/gc/process.zsh new file mode 100644 index 00000000..dcdb93ce --- /dev/null +++ b/lib/cls/gc/process.zsh @@ -0,0 +1,27 @@ +# 生成が続いていれば、所有者がスコープを抜けた非実行中の実体を解放する。 +# +# REPLY: null +# return: 0 +# +# example: +# # 前提: 前回の回収から64個の実体を生成し、一部は所有者のスコープを抜けている +# z.cls.gc._collect +# # return: 0 +# # 変更後: 所有者が消えた非実行中の実体を解放済み +z.cls.gc._collect() { + z.int.is.lt $z_cls_gc_allocations 64 && return 0 + z_cls_gc_allocations=0 + + local REPLY="" + local z_cls_gc_id + + z.hash.keys z_cls_owner_markers + for z_cls_gc_id in "${REPLY[@]}"; do + z.var.exists "$z_cls_owner_markers[$z_cls_gc_id]" && continue + z.is.not.null "${z_cls_active[$z_cls_gc_id]:-}" && continue + + z.cls.instance._release "$z_cls_gc_id" + done + + return 0 +} diff --git a/lib/cls/instance/process.zsh b/lib/cls/instance/process.zsh index c42d926c..65e44b48 100644 --- a/lib/cls/instance/process.zsh +++ b/lib/cls/instance/process.zsh @@ -152,5 +152,16 @@ z.cls.instance._release() { done # 未設定のactive要素も管理ノードを残すため、一緒に削除する。 - unset "z_cls_instances[$z_cls_id]" "z_cls_members[$z_cls_id]" "z_cls_active[$z_cls_id]" + unset "z_cls_instances[$z_cls_id]" "z_cls_members[$z_cls_id]" "z_cls_active[$z_cls_id]" "z_cls_owner_markers[$z_cls_id]" + + if z.int.is.zero "${#z_cls_instances}"; then + # 最後の実体を片付けたら、空のテーブルの容量も解放する。 + z_cls_instances=() + z_cls_values=() + z_cls_members=() + z_cls_active=() + z_cls_wrapper_refs=() + z_cls_owner_markers=() + z_cls_gc_allocations=0 + fi } diff --git a/lib/cls/new/process.zsh b/lib/cls/new/process.zsh index 01336586..84191490 100644 --- a/lib/cls/new/process.zsh +++ b/lib/cls/new/process.zsh @@ -1,7 +1,7 @@ # 実体の作成前に、引数と呼出元の代入先変数を検証する。 # # $1: 公開済みのクラス名 -# $2: readonly・型変換・桁揃えのない通常のスカラー変数名 +# $2: 宣言済みの書き込み可能なスカラー変数名 # REPLY: null # return: 0|1 # @@ -11,25 +11,13 @@ # # return: 1 # # 変更後: objectと実体の登録は元のまま z.cls.new._validate() { - if ! z.int.is.eq $# 2 || z.is.null "${z_cls_classes[$1]:-}" || ! z.cls.is._attribute "$2"; then - z.io.error "Expected an existing class and an ordinary instance variable name." + if ! z.int.is.eq $# 2 || z.is.null "${z_cls_classes[$1]:-}"; then + z.io.error "Expected an existing class and an instance variable name." return 1 fi - local z_cls_binding=$2 - local z_cls_binding_type=${parameters[$z_cls_binding]:-} - - case $z_cls_binding_type in - *readonly*|*array*|*association*|*integer*|*float*|*tied*|*upper*|*lower*|*left*|*right_blanks*|*right_zeros*) - z.io.error "Instance variable $z_cls_binding must be an ordinary writable scalar." - return 1 - ;; - esac - - if z.is.not.null "${z_cls_instances[${(P)z_cls_binding:-}]:-}"; then - z.io.error "Instance variable $z_cls_binding already holds a reference." + if ! z.cls.owner._writable "$2"; then + z.io.error "Instance variable $2 must be a declared writable scalar without a reference." return 1 fi - - return 0 } diff --git a/lib/cls/owner/operator.zsh b/lib/cls/owner/operator.zsh new file mode 100644 index 00000000..81b95db1 --- /dev/null +++ b/lib/cls/owner/operator.zsh @@ -0,0 +1,59 @@ +# 宣言済みの変数が所有参照を受け取れるか検証する。 +# +# $1: 代入先変数名 +# REPLY: null +# return: 0|1 +# +# example: +# local counter="" +# z.cls.owner._writable counter +# # return: 0 +z.cls.owner._writable() { + local z_cls_binding=$1 + + z.cls.is._attribute "$z_cls_binding" || return 1 + + local z_cls_binding_type=${parameters[$z_cls_binding]:-} + case $z_cls_binding_type in + *readonly*|*tied*|*upper*|*lower*|*left*|*right_blanks*|*right_zeros*) + return 1 + ;; + scalar|scalar-*) + ;; + *) + return 1 + ;; + esac + + local z_cls_ref=${(P)z_cls_binding:-} + z.is.null "$z_cls_ref" || z.is.null "${z_cls_instances[$z_cls_ref]:-}" +} + +# 指定した変数が実体の現在の所有変数か検証する。 +# +# $1: 変数名 +# $2: 実体参照 +# REPLY: null +# return: 0|1 +# +# example: +# # 前提: counterはnewで生成した実体の所有変数 +# z.cls.owner._is_current counter "$counter" +# # return: 0 +z.cls.owner._is_current() { + local z_cls_marker=${z_cls_owner_markers[$2]:-} + z.is.not.null "$z_cls_marker" || return 1 + z.str.includes "${parameters[$1]:-}" tied || return 1 + + local z_cls_decl=$(builtin typeset -p -- "$1") + local REPLY="" + z.str.split.shell "$z_cls_decl" + local -a z_cls_words=("${REPLY[@]}") + local -i z_cls_index=2 + while z.str.start_with "${z_cls_words[z_cls_index]:-}" -; do + (( ++z_cls_index )) + done + + z.is.eq "${z_cls_words[z_cls_index]:-}" "$1" && + z.str.start_with "${z_cls_words[z_cls_index+1]:-}" "$z_cls_marker=" +} diff --git a/lib/cls/owner/process.zsh b/lib/cls/owner/process.zsh new file mode 100644 index 00000000..14a2da26 --- /dev/null +++ b/lib/cls/owner/process.zsh @@ -0,0 +1,53 @@ +# 実体の所有権を別の宣言済み変数へ移す。 +# +# $from: 現在の所有変数名 +# $to: 実体を受け取る宣言済みのスカラー変数名 +# REPLY: 移した実体参照 +# return: 0|1 +# +# example: +# # 前提: counterがCounterの実体を所有している +# local saved="" +# z.cls.owner.move from=counter to=saved +# # return: 0 +# # 変更後: savedだけが実体の寿命を保持し、counterは借用参照になる +z.cls.owner.move() { + z.cls.store._ensure + z.arg.named from "$@" && local z_cls_from=$REPLY + z.arg.named to "$@" && local z_cls_to=$REPLY + + if ! z.cls.is._attribute "$z_cls_from" || ! z.cls.owner._writable "$z_cls_to"; then + z.io.error "Expected an owner and a declared writable destination variable." + return 1 + fi + + local z_cls_id=${(P)z_cls_from:-} + if ! z.cls.owner._is_current "$z_cls_from" "$z_cls_id"; then + z.io.error "Instance variable $z_cls_from is not the current owner." + return 1 + fi + + z.cls.owner._bind "$z_cls_id" "$z_cls_to" || return 1 + REPLY=$z_cls_id +} + +# 実体参照を代入し、代入先と同じ寿命の配列を所有者の目印にする。 +# +# $1: 実体参照 +# $2: 検証済みの代入先変数名 +# REPLY: null +# return: 0|1 +# +# example: +# # 前提: 保存領域を初期化済み +# local counter="" +# z.cls.owner._bind z_cls_12345_1 counter +# # return: 0 +# # 変更後: counterはreadonlyの実体参照となり、所有者の目印が登録される +z.cls.owner._bind() { + local z_cls_marker="z_cls_owner_$(( ++z_cls_next_owner_id ))" + + builtin typeset -gT "$2=$1" "$z_cls_marker" || return 1 + builtin typeset -gr -- "$2" || return 1 + z_cls_owner_markers[$1]=$z_cls_marker +} diff --git a/lib/cls/process.zsh b/lib/cls/process.zsh index c93d102a..e2cca150 100644 --- a/lib/cls/process.zsh +++ b/lib/cls/process.zsh @@ -198,7 +198,7 @@ z.cls.abort() { # 独立した実体を作成し、参照をreadonly変数へ代入する。 # # $1: 公開済みのクラス名 -# $2: 実体参照を受け取る変数名。必要に応じて呼出元でlocal宣言する +# $2: 実体を所有する、呼出元で宣言済みの変数名 # REPLY: 実体参照 # return: 0|1 # @@ -213,6 +213,7 @@ z.cls.new() { z.cls.store._ensure z.cls.new._validate "$@" || return 1 + z.cls.gc._collect local z_cls_class=$1 local z_cls_binding=$2 @@ -226,6 +227,7 @@ z.cls.new() { local -a z_cls_wrappers=("${(@)REPLY}") z.cls.instance.commands._validate "${z_cls_wrappers[@]}" || return 1 + z.cls.owner._bind "$z_cls_id" "$z_cls_binding" || return 1 local z_cls_member @@ -237,8 +239,7 @@ z.cls.new() { z_cls_members[$z_cls_id]="${(j: :)z_cls_wrappers}" z_cls_instances[$z_cls_id]=$z_cls_class - : ${(P)z_cls_binding::=$z_cls_id} - typeset -gr -- "$z_cls_binding" + (( ++z_cls_gc_allocations )) REPLY=$z_cls_id } @@ -272,15 +273,5 @@ z.cls.delete() { z.cls.instance._release "$z_cls_id" - z.hash.count z_cls_instances - if z.int.is.zero $REPLY; then - # 全実体の削除後に空テーブルを作り直し、バケットと未設定ノードを解放する。 - z_cls_instances=() - z_cls_values=() - z_cls_members=() - z_cls_active=() - z_cls_wrapper_refs=() - fi - return 0 } diff --git a/lib/cls/store/process.zsh b/lib/cls/store/process.zsh index 019a591a..15fec6b4 100644 --- a/lib/cls/store/process.zsh +++ b/lib/cls/store/process.zsh @@ -9,7 +9,10 @@ # # return: 0 # # 変更後: z_cls_instances=()とz_cls_values=()を初期化済み z.cls.store._ensure() { - z.is.eq "${z_cls_store_ready:-0}" 1 && z.var.exists z_cls_dispatch_programs && return 0 + if z.is.eq "${z_cls_store_ready:-0}" 1 && + z.var.exists z_cls_dispatch_programs && z.var.exists z_cls_owner_markers; then + return 0 + fi zmodload zsh/parameter z.var.exists z_cls_classes || typeset -gA z_cls_classes @@ -23,6 +26,9 @@ z.cls.store._ensure() { z.var.exists z_cls_wrapper_refs || typeset -gA z_cls_wrapper_refs z.var.exists z_cls_dispatch_programs || typeset -gA z_cls_dispatch_programs z.var.exists z_cls_next_id || typeset -gi z_cls_next_id=0 + z.var.exists z_cls_owner_markers || typeset -gA z_cls_owner_markers + z.var.exists z_cls_next_owner_id || typeset -gi +x z_cls_next_owner_id=0 + z.var.exists z_cls_gc_allocations || typeset -gi +x z_cls_gc_allocations=0 z.var.exists z_cls_active || typeset -gA z_cls_active z.var.exists z_cls_drawing || typeset -g +x z_cls_drawing='' z.var.exists z_cls_capture_method || typeset -g +x z_cls_capture_method='' diff --git a/test/lib/cls/fixtures/exported-child.zsh b/test/lib/cls/fixtures/exported-child.zsh index 8c2fdffb..757eae0c 100644 --- a/test/lib/cls/fixtures/exported-child.zsh +++ b/test/lib/cls/fixtures/exported-child.zsh @@ -2,6 +2,7 @@ unset z_mode source "$Z_ROOT/main.zsh" source "$Z_ROOT/test/lib/cls/fixtures/counter.zsh" +local child_instance="" z.cls.new TestCounter child_instance || exit 1 child_instance.increment print -r -- "$REPLY" diff --git a/test/lib/cls/fixtures/method-errexit.zsh b/test/lib/cls/fixtures/method-errexit.zsh index 49239cde..8f1c7587 100644 --- a/test/lib/cls/fixtures/method-errexit.zsh +++ b/test/lib/cls/fixtures/method-errexit.zsh @@ -12,6 +12,7 @@ z.cls.draw StopEarly z.cls.fn.done z.cls.drew +local object="" z.cls.new StopEarly object setopt errexit object.run diff --git a/test/lib/cls/gc/process_test.zsh b/test/lib/cls/gc/process_test.zsh new file mode 100644 index 00000000..1dbe0c2b --- /dev/null +++ b/test/lib/cls/gc/process_test.zsh @@ -0,0 +1,59 @@ +source "$z_main" + +z.t.describe "z.cls.gc._collect"; { + z.t.context "関数内で短命の実体を繰り返し生成する場合"; { + z.t.it "実行中に解放し、同名の外側の所有者を保つ"; { + local -A z_cls_instances=() z_cls_values=() z_cls_members=() z_cls_active=() z_cls_owner_markers=() + local -A z_cls_wrapper_refs=() + local -i z_cls_gc_allocations=0 + z.cls.draw CollectInLoop + z.cls.attr count default=10 + z.cls.drew + + local counter="" + z.cls.new CollectInLoop counter || return 1 + local -i i + for (( i=0; i<70; i++ )); do + () { + local counter="" + z.cls.new CollectInLoop counter + } || return 1 + done + local instances=${#z_cls_instances} + counter.count.get + z.t.expect.reply 10 + z.int.is.lt "$instances" 70 + z.t.expect.status.is.true + z.cls.delete "$counter" + } + } + + z.t.context "所有変数が消えた実体が実行中の場合"; { + z.t.it "実行中は回収を待ち、実行後の次の回収で解放する"; { + local -A z_cls_instances=() z_cls_values=() z_cls_members=() z_cls_active=() z_cls_owner_markers=() + local -A z_cls_wrapper_refs=() + local -i z_cls_gc_allocations=0 + z.cls.draw CollectAfterReturn + z.cls.drew + + local borrowed="" + () { + local target="" + z.cls.new CollectAfterReturn target + borrowed=$target + } + z_cls_active[$borrowed]=1 + + z_cls_gc_allocations=64 + z.cls.gc._collect + z.var.exists "z_cls_instances[$borrowed]" + z.t.expect.status.is.true + + unset "z_cls_active[$borrowed]" + z_cls_gc_allocations=64 + z.cls.gc._collect + z.var.exists "z_cls_instances[$borrowed]" + z.t.expect.status.is.false + } + } +} diff --git a/test/lib/cls/new/process_test.zsh b/test/lib/cls/new/process_test.zsh index 398206bf..b92c8b91 100644 --- a/test/lib/cls/new/process_test.zsh +++ b/test/lib/cls/new/process_test.zsh @@ -109,4 +109,22 @@ z.t.describe "z.cls.new._validate"; { } } + + z.t.context "代入先が宣言されていない場合"; { + z.t.it "実体もグローバル変数も作らない"; { + () { + z.cls.draw UndeclaredNew + z.cls.drew + z.t.mock name=z.io.error + + z.cls.new UndeclaredNew missing_gc_binding + + z.t.expect.status.is.false + z.var.exists missing_gc_binding + z.t.expect.status.is.false + z.hash.count z_cls_instances + z.t.expect.reply 0 + } + } + } } diff --git a/test/lib/cls/owner/operator_test.zsh b/test/lib/cls/owner/operator_test.zsh new file mode 100644 index 00000000..5428ab44 --- /dev/null +++ b/test/lib/cls/owner/operator_test.zsh @@ -0,0 +1,43 @@ +source "$z_main" +z.cls.store._ensure + +z.t.describe "z.cls.owner._writable"; { + z.t.context "宣言済みの通常のスカラーの場合"; { + z.t.it "書き込みを許可する"; { + local target="" + + z.cls.owner._writable target + + z.t.expect.status.is.true + } + } + + z.t.context "代入先に変数が宣言されていない場合"; { + z.t.it "書き込みを拒否する"; { + z.cls.owner._writable undeclared_gc_target + + z.t.expect.status.is.false + } + } +} + +z.t.describe "z.cls.owner._is_current"; { + z.t.context "所有権を移した場合"; { + z.t.it "新しい目印のある変数だけを所有者と判定する"; { + () { + z.cls.draw CheckCurrentOwner + z.cls.drew + local source_owner="" destination="" + z.cls.new CheckCurrentOwner source_owner || return 1 + z.cls.owner.move from=source_owner to=destination || return 1 + + z.cls.owner._is_current source_owner "$source_owner" + z.t.expect.status.is.false + z.cls.owner._is_current destination "$destination" + z.t.expect.status.is.true + + z.cls.delete "$destination" + } + } + } +} diff --git a/test/lib/cls/owner/process_test.zsh b/test/lib/cls/owner/process_test.zsh new file mode 100644 index 00000000..f179f893 --- /dev/null +++ b/test/lib/cls/owner/process_test.zsh @@ -0,0 +1,85 @@ +source "$z_main" + +z.t.describe "z.cls.owner.move"; { + z.t.context "所有権を呼び出し元の変数へ移した場合"; { + z.t.it "移動元のスコープが終わっても移動先から同じ実体を操作できる"; { + () { + z.cls.draw TransferOwner + z.cls.attr count default=5 + z.cls.drew + + local saved="" + () { + local counter="" + z.cls.new TransferOwner counter || return 1 + z.cls.owner.move from=counter to=saved + } + z.t.expect.status.is.true + + $saved.count.get + z.t.expect.reply 5 + z.cls.delete "$saved" + } + } + } + + z.t.context "借用した変数を移動元にした場合"; { + z.t.it "既存の所有者を残して拒否する"; { + () { + z.cls.draw BorrowedMove + z.cls.drew + + local owner="" saved="" + z.cls.new BorrowedMove owner || return 1 + local borrowed=$owner + + z.cls.owner.move from=borrowed to=saved 2>/dev/null + + z.t.expect.status.is.false + z.t.expect.is.null "$saved" + z.cls.delete "$owner" + } + } + } + + z.t.context "移動先が宣言されていない場合"; { + z.t.it "新しい変数を作らず、元の所有者を保持する"; { + () { + z.cls.draw UndeclaredMoveTarget + z.cls.drew + + local holder="" + z.cls.new UndeclaredMoveTarget holder || return 1 + z.cls.owner.move from=holder to=missing_move_binding 2>/dev/null + + z.t.expect.status.is.false + z.var.exists missing_move_binding + z.t.expect.status.is.false + z.cls.owner._is_current holder "$holder" + z.t.expect.status.is.true + + z.cls.delete "$holder" + } + } + } +} + +z.t.describe "z.cls.owner._bind"; { + z.t.context "寿命を持つ代入先へ参照を渡す場合"; { + z.t.it "所有者のスコープが終わると目印も消える"; { + local -A z_cls_owner_markers=() + () { + local binding="" + z.cls.owner._bind test_reference binding + + z.t.expect.status.is.true + z.t.expect "$binding" test_reference + z.var.exists "$z_cls_owner_markers[test_reference]" + z.t.expect.status.is.true + } + + z.var.exists "$z_cls_owner_markers[test_reference]" + z.t.expect.status.is.false + } + } +} diff --git a/test/lib/cls/process_test.zsh b/test/lib/cls/process_test.zsh index e7cc046a..fb95a6e2 100644 --- a/test/lib/cls/process_test.zsh +++ b/test/lib/cls/process_test.zsh @@ -467,6 +467,7 @@ z.t.describe "z.cls.new"; { REPLY=original } + local first="" z.cls.new TestCounter first 2>/dev/null z.t.expect.status.is.false @@ -602,15 +603,16 @@ z.t.describe "z.cls.delete"; { local index for (( index=1; index<=72; index++ )); do + local "owner_$index=" () { local object="" + z.cls.new TestCounter object || return 1 - z.cls.new TestCounter object - - z.t.expect.status.is.true - + z.cls.owner.move from=object "to=owner_$index" || return 1 saved_refs+=("$object") } + + z.t.expect.status.is.true done local reference diff --git a/test/lib/cls/store/process_test.zsh b/test/lib/cls/store/process_test.zsh index b0399fcf..85f86c52 100644 --- a/test/lib/cls/store/process_test.zsh +++ b/test/lib/cls/store/process_test.zsh @@ -33,6 +33,21 @@ z.t.describe "z.cls.store._ensure"; { } } + z.t.context "既存の保存領域に所有者の管理表がない場合"; { + z.t.it "再読込時に既存のクラスを保ちながら所有者の管理表を初期化する"; { + ( + z.cls.store._ensure + z_cls_classes[StoreBeforeGc]=1 + unset z_cls_owner_markers + z.cls.store._ensure + z.var.exists z_cls_owner_markers && + z.is.eq "$z_cls_classes[StoreBeforeGc]" 1 + ) + + z.t.expect.status.is.true + } + } + z.t.context "初期化フラグだけが残る場合"; { z.t.it "不足したストアを初期化する"; { unset z_cls_dispatch_programs diff --git a/test/mod/completion/cache/build/process_test.zsh b/test/mod/completion/cache/build/process_test.zsh index a433c1e8..2c2e9256 100644 --- a/test/mod/completion/cache/build/process_test.zsh +++ b/test/mod/completion/cache/build/process_test.zsh @@ -18,7 +18,7 @@ z.t.describe "z.completion.cache.build._names"; { REPLY=("${class_names[@]}") - z.t.expect.reply.is.arr z.cls.abort z.cls.attr z.cls.delete z.cls.draw z.cls.drew z.cls.fn z.cls.fn.done z.cls.new + z.t.expect.reply.is.arr z.cls.abort z.cls.attr z.cls.delete z.cls.draw z.cls.drew z.cls.fn z.cls.fn.done z.cls.new z.cls.owner.move } } From 4289138aea72f7f4905bf70e5f8927e773db9dc3 Mon Sep 17 00:00:00 2001 From: milkeclair Date: Wed, 18 Nov 2026 15:18:31 +0900 Subject: [PATCH 02/11] refactor: register class member commands by instance ID --- lib/cls/README.md | 23 ++--- lib/cls/instance/analysis.zsh | 27 ++--- lib/cls/process.zsh | 2 +- test/lib/cls/capture/process_test.zsh | 28 ++--- test/lib/cls/fixtures/exported-child.zsh | 2 +- test/lib/cls/fixtures/method-errexit.zsh | 2 +- test/lib/cls/gc/process_test.zsh | 2 +- test/lib/cls/instance/analysis_test.zsh | 8 +- test/lib/cls/instance/call/process_test.zsh | 82 +++++++-------- test/lib/cls/instance/process_test.zsh | 4 +- test/lib/cls/new/process_test.zsh | 2 +- test/lib/cls/process_test.zsh | 107 ++++++++++---------- 12 files changed, 140 insertions(+), 149 deletions(-) diff --git a/lib/cls/README.md b/lib/cls/README.md index 6fc3d2d6..0733312d 100644 --- a/lib/cls/README.md +++ b/lib/cls/README.md @@ -26,20 +26,20 @@ z.cls.draw Counter z.cls.drew ``` -実体を作成すると、メソッドと属性の取得・更新用コマンドを呼び出せます。 +実体を作成すると、変数を展開してメソッドや属性の取得・更新を呼び出せます。 ```zsh source ./counter.zsh typeset counter="" z.cls.new Counter counter -counter.increment -counter.add count=4 -counter.count.get +$counter.increment +$counter.add count=4 +$counter.count.get print -r -- "$REPLY" # 5 -counter.count.set 10 -counter.increment +$counter.count.set 10 +$counter.increment print -r -- "$REPLY" # 11 z.cls.delete counter @@ -72,18 +72,18 @@ setterが成功すると、その時点で保存値へ反映します。直後 ```zsh typeset counter="" z.cls.new Counter counter -counter.count.set 10 +$counter.count.set 10 example() { local counter="" z.cls.new Counter counter - counter.increment + $counter.increment print -r -- "$REPLY" # 1 z.cls.delete counter } example -counter.count.get +$counter.count.get print -r -- "$REPLY" # 10 ``` @@ -97,8 +97,7 @@ typeset -r other=$counter print -r -- "$REPLY" # 11 ``` -短い形式の`counter.increment`は、呼出し時に見えるcounter変数から実体を解決します。 -`"$other.increment"`は、コピー済みの参照から直接呼び出します。 +`$counter.increment`と`$other.increment`は、それぞれの変数に保存した実体参照を展開して呼び出します。 新しい実体には、内側のスコープで宣言した同名localなど、新しい代入先が必要です。 コピーした参照も再代入から保護する場合は、`typeset -r`または`local -r`を使います。 @@ -114,7 +113,7 @@ print -r -- "$REPLY" # 11 prepare_counter() { local counter="" z.cls.new Counter counter || return 1 - counter.count.set 5 + $counter.count.set 5 z.cls.owner.move from=counter "to=$1" } diff --git a/lib/cls/instance/analysis.zsh b/lib/cls/instance/analysis.zsh index 8e6baffc..9123ef1d 100644 --- a/lib/cls/instance/analysis.zsh +++ b/lib/cls/instance/analysis.zsh @@ -1,33 +1,28 @@ -# 実体の変数名と参照から呼べるコマンド名を列挙する。 +# 実体IDで呼び出すメソッドと属性のコマンドを列挙する。 # # $1: クラス名 -# $2: 実体の参照を持つ変数名 -# $3: 実体参照 +# $2: 実体参照 # REPLY: コマンド名の配列。メンバーがない場合は空配列 # return: 0 # # example: # # 前提: Counterは属性countだけを持つ -# z.cls.instance._commands Counter counter ref -# # REPLY: ("counter.count.get" "counter.count.set" "ref.count.get" "ref.count.set") +# z.cls.instance._commands Counter ref +# # REPLY: ("ref.count.get" "ref.count.set") # # return: 0 z.cls.instance._commands() { local z_cls_class=$1 - local z_cls_binding=$2 - local z_cls_id=$3 + local z_cls_id=$2 local -a z_cls_wrappers=() - local z_cls_prefix local z_cls_member - for z_cls_prefix in "$z_cls_binding" "$z_cls_id"; do - for z_cls_member in ${=z_cls_class_methods[$z_cls_class]}; do - z_cls_wrappers+=("$z_cls_prefix.$z_cls_member") - done + for z_cls_member in ${=z_cls_class_methods[$z_cls_class]}; do + z_cls_wrappers+=("$z_cls_id.$z_cls_member") + done - for z_cls_member in ${=z_cls_class_attrs[$z_cls_class]}; do - z_cls_wrappers+=("$z_cls_prefix.$z_cls_member.get") - z_cls_wrappers+=("$z_cls_prefix.$z_cls_member.set") - done + for z_cls_member in ${=z_cls_class_attrs[$z_cls_class]}; do + z_cls_wrappers+=("$z_cls_id.$z_cls_member.get") + z_cls_wrappers+=("$z_cls_id.$z_cls_member.set") done REPLY=("${z_cls_wrappers[@]}") diff --git a/lib/cls/process.zsh b/lib/cls/process.zsh index e2cca150..cb34cb15 100644 --- a/lib/cls/process.zsh +++ b/lib/cls/process.zsh @@ -222,7 +222,7 @@ z.cls.new() { local z_cls_id="z_cls_${$}_$z_cls_next_id" - z.cls.instance._commands "$z_cls_class" "$z_cls_binding" "$z_cls_id" + z.cls.instance._commands "$z_cls_class" "$z_cls_id" local -a z_cls_wrappers=("${(@)REPLY}") diff --git a/test/lib/cls/capture/process_test.zsh b/test/lib/cls/capture/process_test.zsh index d2c83e78..8a38fe7f 100644 --- a/test/lib/cls/capture/process_test.zsh +++ b/test/lib/cls/capture/process_test.zsh @@ -66,7 +66,7 @@ z.t.describe "z.cls.capture._stop"; { local calls=$trap_calls local first="" z.cls.new TestCounter first - first.increment + $first.increment local call_reply=$REPLY unfunction TRAPDEBUG print -rl -- "${(qqqq)before}" "${(qqqq)after}" "$calls" "$call_reply" @@ -91,7 +91,7 @@ z.t.describe "z.cls.capture._stop"; { local after=$(trap) local first="" z.cls.new TestCounter first - first.increment + $first.increment local call_reply=$REPLY trap - DEBUG print -rl -- "${(qqqq)before}" "${(qqqq)after}" "$call_reply" @@ -152,7 +152,7 @@ z.t.describe "z.cls.capture._debug"; { local first="" z.cls.new TestCounter first print -rl -- "$?" - first.loop + $first.loop print -rl -- "$REPLY" "$count" )}") @@ -225,11 +225,11 @@ z.t.describe "z.cls.capture._debug"; { print -r -- "${+z_cls_classes[MarkerInner]}" local outer="" z.cls.new MarkerOuter outer - outer.build + $outer.build local inner="" z.cls.new MarkerInner inner - inner.value.get + $inner.value.get print -r -- "$REPLY" )}") @@ -244,7 +244,7 @@ z.t.describe "z.cls.capture._debug"; { source "$z_root/test/lib/cls/fixtures/body-syntax.zsh" local first="" z.cls.new SyntaxBox first - first.run test + $first.run test local call_status=$? local call_reply=$REPLY print -rl -- "$call_status" "$call_reply" @@ -280,9 +280,9 @@ z.t.describe "z.cls.capture._skip"; { local object="" z.cls.new Outer object - object.kept.get + $object.kept.get print -r -- "$REPLY" - object.added.get + $object.added.get print -r -- "$REPLY" print -r -- "${+z_cls_classes[Rejected]}" "${+z_cls_classes[Deeper]}" "${+z_cls_classes[InMethod]}" )}") @@ -309,7 +309,7 @@ z.t.describe "z.cls.capture._skip"; { print -r -- "$?" local object="" z.cls.new Rejected object - object.kept.get + $object.kept.get print -r -- "$REPLY" )}") @@ -331,7 +331,7 @@ z.t.describe "z.cls.capture._watch"; { print -rl -- "$?" "$before" "$options[localoptions]:$options[localtraps]:$options[debugbeforecmd]:$options[functionargzero]" local first="" z.cls.new TestCounter first - first.increment + $first.increment print -rl -- "$REPLY" )}") @@ -351,7 +351,7 @@ z.t.describe "z.cls.capture._watch"; { print -rl -- "$?" "$before" "$options[localoptions]:$options[localtraps]:$options[debugbeforecmd]:$options[functionargzero]" local first="" z.cls.new TestCounter first - first.increment + $first.increment print -rl -- "$REPLY" )}") @@ -371,7 +371,7 @@ z.t.describe "z.cls.capture._watch"; { print -rl -- "$?" "$before" "$options[localoptions]:$options[localtraps]:$options[debugbeforecmd]:$options[functionargzero]" local first="" z.cls.new TestCounter first - first.increment + $first.increment print -rl -- "$REPLY" )}") @@ -391,7 +391,7 @@ z.t.describe "z.cls.capture._watch"; { print -rl -- "$?" "$before" "$options[localoptions]:$options[localtraps]:$options[debugbeforecmd]:$options[functionargzero]" local first="" z.cls.new TestCounter first - first.increment + $first.increment print -rl -- "$REPLY" )}") @@ -411,7 +411,7 @@ z.t.describe "z.cls.capture._watch"; { print -rl -- "$?" "$before" "$options[localoptions]:$options[localtraps]:$options[debugbeforecmd]:$options[functionargzero]" local first="" z.cls.new TestCounter first - first.increment + $first.increment print -rl -- "$REPLY" )}") diff --git a/test/lib/cls/fixtures/exported-child.zsh b/test/lib/cls/fixtures/exported-child.zsh index 757eae0c..98e2bd1a 100644 --- a/test/lib/cls/fixtures/exported-child.zsh +++ b/test/lib/cls/fixtures/exported-child.zsh @@ -4,5 +4,5 @@ source "$Z_ROOT/main.zsh" source "$Z_ROOT/test/lib/cls/fixtures/counter.zsh" local child_instance="" z.cls.new TestCounter child_instance || exit 1 -child_instance.increment +$child_instance.increment print -r -- "$REPLY" diff --git a/test/lib/cls/fixtures/method-errexit.zsh b/test/lib/cls/fixtures/method-errexit.zsh index 8f1c7587..8297160d 100644 --- a/test/lib/cls/fixtures/method-errexit.zsh +++ b/test/lib/cls/fixtures/method-errexit.zsh @@ -15,5 +15,5 @@ z.cls.drew local object="" z.cls.new StopEarly object setopt errexit -object.run +$object.run print -r -- SHOULD_NOT_RUN diff --git a/test/lib/cls/gc/process_test.zsh b/test/lib/cls/gc/process_test.zsh index 1dbe0c2b..34f33a85 100644 --- a/test/lib/cls/gc/process_test.zsh +++ b/test/lib/cls/gc/process_test.zsh @@ -20,7 +20,7 @@ z.t.describe "z.cls.gc._collect"; { } || return 1 done local instances=${#z_cls_instances} - counter.count.get + $counter.count.get z.t.expect.reply 10 z.int.is.lt "$instances" 70 z.t.expect.status.is.true diff --git a/test/lib/cls/instance/analysis_test.zsh b/test/lib/cls/instance/analysis_test.zsh index f7917da7..f34c3933 100644 --- a/test/lib/cls/instance/analysis_test.zsh +++ b/test/lib/cls/instance/analysis_test.zsh @@ -2,14 +2,14 @@ source ${z_main} z.t.describe "z.cls.instance._commands"; { z.t.context "属性とメソッドがある場合"; { - z.t.it "両方の接頭辞のコマンドを列挙する"; { + z.t.it "実体ID付きのコマンドだけを列挙する"; { z.cls.store._ensure z_cls_class_methods[CommandList]='increment ' z_cls_class_attrs[CommandList]='count ' - z.cls.instance._commands CommandList object ref + z.cls.instance._commands CommandList ref - z.t.expect.reply.is.arr object.increment object.count.get object.count.set ref.increment ref.count.get ref.count.set + z.t.expect.reply.is.arr ref.increment ref.count.get ref.count.set } } @@ -19,7 +19,7 @@ z.t.describe "z.cls.instance._commands"; { z_cls_class_methods[EmptyCommandList]='' z_cls_class_attrs[EmptyCommandList]='' - z.cls.instance._commands EmptyCommandList object ref + z.cls.instance._commands EmptyCommandList ref local reply_type=${(t)REPLY} local reply_count=${#REPLY} diff --git a/test/lib/cls/instance/call/process_test.zsh b/test/lib/cls/instance/call/process_test.zsh index 263af568..5afcf972 100644 --- a/test/lib/cls/instance/call/process_test.zsh +++ b/test/lib/cls/instance/call/process_test.zsh @@ -60,12 +60,12 @@ z.t.describe "z.cls.instance.call._access"; { z_cls_values[${object}:count]='typeset -i count=0' z.cls.instance.call._access "$object" set count +7 - object.count.get + $object.count.get z.t.expect.reply 7 z.cls.instance.call._access "$object" set count -3 - object.count.get + $object.count.get z.t.expect.reply -3 @@ -112,7 +112,7 @@ z.t.describe "z.cls.instance.call._access"; { z_cls_values[${object}:count]='typeset -i count=1' z.cls.instance.call._access "$object" set count 'other+1' - object.count.get + $object.count.get z.t.expect.reply 14 @@ -137,7 +137,7 @@ z.t.describe "z.cls.instance.call._access"; { local -i count=4 z.cls.instance._snapshot "$object" count } - object.prepare + $object.prepare local original_count=$z_cls_values[${object}:count] local original_other=$z_cls_values[${object}:other] @@ -147,11 +147,11 @@ z.t.describe "z.cls.instance.call._access"; { z.t.expect "$z_cls_values[${object}:count]" "$original_count" z.t.expect "$z_cls_values[${object}:other]" "$original_other" - object.count.get + $object.count.get z.t.expect.reply 4 - object.other.get + $object.other.get z.t.expect.reply 13 @@ -231,7 +231,7 @@ z.t.describe "z.cls.instance.call._run"; { z.t.expect.status.is.false - object.count.get + $object.count.get z.t.expect.reply 4 @@ -256,29 +256,29 @@ z.t.describe "z.cls.instance.call._run"; { local second="" z.cls.new TestCounter first z.cls.new TestCounter second - first.increment - first.add count=4 + $first.increment + $first.add count=4 z.t.expect.reply 5 - second.count.get + $second.count.get z.t.expect.reply 0 - first.count.set 10 - first.increment - first.value + $first.count.set 10 + $first.increment + $first.value z.t.expect.reply 11 local literal=$'space "quote" \\ slash\n$(print SHOULD_NOT_RUN)' - first.label.set "$literal" - first.label.get + $first.label.set "$literal" + $first.label.get z.t.expect.reply "$literal" - first.label.set "" - first.label.get + $first.label.set "" + $first.label.get z.t.expect.reply.is.null @@ -293,8 +293,8 @@ z.t.describe "z.cls.instance.call._run"; { () { local first="" z.cls.new TestCounter first - first.twice - first.value + $first.twice + $first.value z.t.expect.reply 2 @@ -308,14 +308,14 @@ z.t.describe "z.cls.instance.call._run"; { () { local first="" z.cls.new TestCounter first - first.fail reason + $first.fail reason local call_status=$? local call_reply=$REPLY z.t.expect "$call_status" 7 z.t.expect "$call_reply" failure:reason - first.value + $first.value z.t.expect.reply 1 @@ -333,8 +333,8 @@ z.t.describe "z.cls.instance.call._run"; { local -a items=() z.cls.instance._snapshot "$first" items } - first.fill - first.items.get + $first.fill + $first.items.get local -a initial_items=("${REPLY[@]}") z.t.expect "${#initial_items}" 3 @@ -342,16 +342,16 @@ z.t.describe "z.cls.instance.call._run"; { z.t.expect "$initial_items[2]" "two words" z.t.expect "$initial_items[3]" last - first.items.set "" replaced - first.items.get + $first.items.set "" replaced + $first.items.get local -a updated_items=("${REPLY[@]}") z.t.expect "${#updated_items}" 2 z.t.expect.is.null "$updated_items[1]" z.t.expect "$updated_items[2]" replaced - first.items.set - first.items.get + $first.items.set + $first.items.get local empty_count=${#REPLY} z.t.expect "$empty_count" 0 @@ -382,8 +382,8 @@ z.t.describe "z.cls.instance.call._run"; { z.cls.instance._snapshot "$typed" values z.cls.instance._snapshot "$typed" missing } - typed.fill - typed.verify + $typed.fill + $typed.verify z.t.expect.status.is.true @@ -431,16 +431,16 @@ z.t.describe "z.cls.instance.call._run"; { local saved_values=$z_cls_values[${typed}:values] local saved_missing=$z_cls_values[${typed}:missing] - typed.table.set odd + $typed.table.set odd z.t.expect.status.is.false z.t.expect "$z_cls_values[${typed}:table]" "$saved_table" - typed.plain.get + $typed.plain.get z.t.expect.reply $'quote\' slash\\ newline\n$(print SHOULD_NOT_RUN)' - typed.verify + $typed.verify z.t.expect.status.is.true z.t.expect "$z_cls_values[${typed}:table]" "$saved_table" @@ -466,9 +466,9 @@ z.t.describe "z.cls.instance.call._run"; { local -i count=0 z.cls.instance._snapshot "$arithmetic" count } - arithmetic.prepare - arithmetic.count.set 'other+1' - arithmetic.count.get + $arithmetic.prepare + $arithmetic.count.set 'other+1' + $arithmetic.count.get z.t.expect.reply 14 @@ -494,9 +494,9 @@ z.t.describe "z.cls.instance.call._run"; { local count=3 local counter="" z.cls.new TestCounter counter - counter.count.set 10 + $counter.count.set 10 - counter.add count=3 + $counter.add count=3 local add_status=$? local add_result=$REPLY @@ -504,7 +504,7 @@ z.t.describe "z.cls.instance.call._run"; { z.t.expect "$add_result" 13 z.t.expect "$count" 3 - counter.count.get + $counter.count.get z.t.expect.reply 13 @@ -532,7 +532,7 @@ z.t.describe "z.cls.instance.call._run"; { local object="" z.cls.new SameSelf object - object.nested + $object.nested local result=("${REPLY[@]}") z.t.expect "${#result}" 4 @@ -566,7 +566,7 @@ z.t.describe "z.cls.instance.call._run"; { z.cls.new DifferentSelf first z.cls.new DifferentSelf second - first.bounce "$second" + $first.bounce "$second" local result=("${REPLY[@]}") z.t.expect "${#result}" 4 @@ -596,7 +596,7 @@ z.t.describe "z.cls.instance.call._run"; { local object="" z.cls.new ReadonlySelf object - object.inspect + $object.inspect local result=("${REPLY[@]}") z.t.expect "${#result}" 4 diff --git a/test/lib/cls/instance/process_test.zsh b/test/lib/cls/instance/process_test.zsh index d7da5893..e5005308 100644 --- a/test/lib/cls/instance/process_test.zsh +++ b/test/lib/cls/instance/process_test.zsh @@ -16,7 +16,7 @@ z.t.describe "z.cls.instance._call"; { z.t.expect.reply 4 - object.count.get + $object.count.get z.t.expect.reply 4 @@ -122,7 +122,7 @@ z.t.describe "z.cls.instance._release"; { z.t.expect.status.is.false - second.count.get + $second.count.get z.t.expect.reply 3 diff --git a/test/lib/cls/new/process_test.zsh b/test/lib/cls/new/process_test.zsh index b92c8b91..9088012c 100644 --- a/test/lib/cls/new/process_test.zsh +++ b/test/lib/cls/new/process_test.zsh @@ -98,7 +98,7 @@ z.t.describe "z.cls.new._validate"; { z.t.expect.status.is.true - object.count.get + $object.count.get z.t.expect.reply 3 diff --git a/test/lib/cls/process_test.zsh b/test/lib/cls/process_test.zsh index fb95a6e2..6c0e9286 100644 --- a/test/lib/cls/process_test.zsh +++ b/test/lib/cls/process_test.zsh @@ -66,11 +66,11 @@ z.t.describe "z.cls.attr"; { () { local object="" z.cls.new EmptyDefault object - object.default.get + $object.default.get z.t.expect.reply.is.null - object.count.get + $object.count.get local reply_kind=${(t)REPLY} local count_value=$REPLY @@ -179,9 +179,9 @@ z.t.describe "z.cls.new"; { z.cls.new TestCounter counter local -r other=$counter "$other.increment" - counter.count.get + $counter.count.get print -rl -- "$REPLY" - counter.add count=4 + $counter.add count=4 "$other.value" print -rl -- "$REPLY" z.cls.delete other @@ -206,7 +206,7 @@ z.t.describe "z.cls.new"; { local object="" z.cls.new false object - object.literal.get + $object.literal.get z.t.expect.reply null @@ -227,7 +227,7 @@ z.t.describe "z.cls.new"; { local object="" z.cls.new ScopedInternals object - object.increment + $object.increment z.t.expect.reply 1 z.t.expect "$z_cls_instance" caller_instance @@ -253,11 +253,11 @@ z.t.describe "z.cls.new"; { local object="" z.cls.new FormerPrefix object - object.increment + $object.increment z.t.expect.reply 1 - object.__z_cls_value.get + $object.__z_cls_value.get z.t.expect.reply 1 @@ -281,7 +281,7 @@ z.t.describe "z.cls.new"; { z.t.expect.status.is.true - arg.count.get + $arg.count.get z.t.expect.reply 3 @@ -306,7 +306,7 @@ z.t.describe "z.cls.new"; { z.t.expect.status.is.true - object.count.get + $object.count.get z.t.expect.reply 7 @@ -341,7 +341,7 @@ z.t.describe "z.cls.new"; { z.t.expect.reply "$object" z.t.expect.includes "$parameters[object]" readonly - object.count.get + $object.count.get z.t.expect.reply 0 @@ -356,7 +356,7 @@ z.t.describe "z.cls.new"; { local counter="" z.cls.new TestCounter counter local outer_id=$counter - counter.count.set 10 + $counter.count.set 10 () { local counter="" @@ -366,8 +366,8 @@ z.t.describe "z.cls.new"; { z.t.expect.status.is.true z.t.expect.not "$counter" "$outer_id" - counter.increment - counter.count.get + $counter.increment + $counter.count.get z.t.expect.reply 1 @@ -378,12 +378,12 @@ z.t.describe "z.cls.new"; { z.t.expect "$counter" "$outer_id" - counter.count.get + $counter.count.get z.t.expect.reply 10 z.cls.delete counter - z.var.exists 'functions[counter.increment]' + z.var.exists "functions[$counter.increment]" z.t.expect.status.is.false } @@ -393,7 +393,7 @@ z.t.describe "z.cls.new"; { () { local counter="" z.cls.new TestCounter counter - counter.count.set 8 + $counter.count.set 8 () { local counter="" @@ -406,20 +406,20 @@ z.t.describe "z.cls.new"; { local -a items=() z.cls.instance._snapshot "$counter" items } - counter.fill - counter.items.get + $counter.fill + $counter.items.get local item_count=${#REPLY} z.t.expect "$item_count" 3 - counter.count.get 2>/dev/null + z.var.exists "functions[$counter.count.get]" z.t.expect.status.is.false z.cls.delete counter } - counter.count.get + $counter.count.get z.t.expect.reply 8 @@ -451,8 +451,8 @@ z.t.describe "z.cls.new"; { z.t.expect.status.is.false - counter.count.set 3 - counter.increment + $counter.count.set 3 + $counter.increment z.t.expect.reply 4 @@ -461,25 +461,25 @@ z.t.describe "z.cls.new"; { } } - z.t.context "作成先のコマンド名に既存の関数がある場合"; { - z.t.it "既存の関数を保ち、実体のコマンドを部分的に残さない"; { + z.t.context "参照変数と同じ接頭辞を持つ関数がある場合"; { + z.t.it "既存の関数を保ち、実体ID付きの関数だけを登録する"; { first.value() { REPLY=original } local first="" - z.cls.new TestCounter first 2>/dev/null - - z.t.expect.status.is.false + z.cls.new TestCounter first && $first.value + local result_code=$? result_value=$REPLY + z.t.expect "$result_code" 0 + z.t.expect "$result_value" 0 first.value - z.t.expect.reply original z.var.exists 'functions[first.increment]' - z.t.expect.status.is.false + z.cls.delete first unfunction first.value } } @@ -493,9 +493,9 @@ z.t.describe "z.cls.delete"; { setopt errexit local counter="" z.cls.new TestCounter counter - counter.increment + $counter.increment z.cls.delete counter - print -rl -- "${#z_cls_instances}" "${+functions[counter.increment]}" + print -rl -- "${#z_cls_instances}" "${+functions[$counter.increment]}" )}") z.t.expect.status.is.true @@ -522,7 +522,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.false unset "z_cls_active[$object]" - object.count.get + $object.count.get z.t.expect.reply 3 @@ -546,7 +546,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.reply false - z.var.exists 'functions[object.count.get]' + z.var.exists "functions[$object.count.get]" z.t.expect.status.is.false @@ -562,21 +562,19 @@ z.t.describe "z.cls.delete"; { () { local first="" z.cls.new TestCounter first - first.value() { - REPLY=replacement - } + functions[$first.value]='REPLY=replacement' - first.increment - first.count.get + $first.increment + $first.count.get z.t.expect.reply 1 z.cls.delete first - first.value + $first.value z.t.expect.reply replacement - unfunction first.value + unfunction "$first.value" } } @@ -584,14 +582,14 @@ z.t.describe "z.cls.delete"; { () { local first="" z.cls.new TestCounter first - functions -c first.increment first.value - local expected=$functions[first.value] + functions -c "$first.increment" "$first.value" + local expected=$functions[$first.value] z.cls.delete first - z.t.expect "${functions[first.value]-}" "$expected" + z.t.expect "${functions[$first.value]-}" "$expected" - unfunction first.value + unfunction "$first.value" } } } @@ -629,9 +627,8 @@ z.t.describe "z.cls.delete"; { z.t.expect.reply 42 - object.increment() { - REPLY=custom - } + local replaced="$reference.increment" + functions[$replaced]='REPLY=custom' for reference in "${(@)saved_refs[37,72]}"; do z.cls.delete "$reference" @@ -639,7 +636,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true done - object.increment + "$replaced" z.t.expect.reply custom @@ -653,7 +650,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true - fresh.increment + $fresh.increment z.t.expect.reply 1 @@ -661,7 +658,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true - unfunction object.increment + unfunction "$replaced" } } } @@ -686,15 +683,15 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true - z.var.exists 'functions[first.increment]' + z.var.exists "functions[$first.increment]" z.t.expect.status.is.false - z.var.exists 'functions[first.count.get]' + z.var.exists "functions[$first.count.get]" z.t.expect.status.is.false - z.var.exists 'functions[first.count.set]' + z.var.exists "functions[$first.count.set]" z.t.expect.status.is.false @@ -705,7 +702,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true - first.value + $first.value z.t.expect.reply 0 From 8e2ce6653965529001fa7d784a39b7a9ea9dd879 Mon Sep 17 00:00:00 2001 From: milkeclair Date: Wed, 18 Nov 2026 18:12:58 +0900 Subject: [PATCH 03/11] perf: store scalar class attributes without shell declarations --- lib/cls/instance/call/process.zsh | 72 +++++++++++++++----------- lib/cls/instance/process.zsh | 22 ++++---- lib/cls/store/process.zsh | 15 +++--- test/lib/cls/instance/process_test.zsh | 8 +-- test/lib/cls/store/process_test.zsh | 11 ++-- 5 files changed, 68 insertions(+), 60 deletions(-) diff --git a/lib/cls/instance/call/process.zsh b/lib/cls/instance/call/process.zsh index faa66f67..41e1ff54 100644 --- a/lib/cls/instance/call/process.zsh +++ b/lib/cls/instance/call/process.zsh @@ -54,44 +54,48 @@ z.cls.instance.call._access() { local z_cls_instance=$1 local z_cls_operation=$2 local z_cls_member=$3 - local z_cls_declaration=$z_cls_values[$1:$3] - local z_cls_value_type="" - - if z.str.start_with "$z_cls_declaration" "typeset -- $z_cls_member="; then - z_cls_value_type=scalar - elif z.str.start_with "$z_cls_declaration" "typeset -i $z_cls_member="; then - z_cls_value_type=integer - fi + local z_cls_key="$1:$3" shift 3 - case "$z_cls_operation:$z_cls_value_type" in - get:scalar|get:integer) - z.int.is.zero $# || return 1 + if (( ! ${+z_cls_values[$z_cls_key]} )); then + case $z_cls_operation in + get) + z.int.is.zero $# || return 1 + REPLY=$z_cls_scalars[$z_cls_key] + return 0 + ;; + set) + z.int.is.eq $# 1 || return 1 + z.cls.store._scalar "$z_cls_instance" "$z_cls_member" "$1" + return 0 + ;; + esac + fi - local z_cls_literal=${z_cls_declaration#*=} + local z_cls_declaration=$z_cls_values[$z_cls_key] - z.str.unquote "$z_cls_literal" - return $? - ;; - set:scalar) - z.int.is.eq $# 1 || return 1 - z.cls.store._scalar "$z_cls_instance" "$z_cls_member" "$1" - return 0 - ;; - set:integer) - z.int.is.eq $# 1 || return 1 + if z.str.start_with "$z_cls_declaration" "typeset -i $z_cls_member="; then + case $z_cls_operation in + get) + z.int.is.zero $# || return 1 + z.str.unquote "${z_cls_declaration#*=}" + return $? + ;; + set) + z.int.is.eq $# 1 || return 1 - if z.int.is.match "$1"; then - local -i z_cls_numeric=0 + if z.int.is.match "$1"; then + local -i z_cls_numeric=0 - (( z_cls_numeric = $1, 1 )) || return 1 + (( z_cls_numeric = $1, 1 )) || return 1 - z.cls.store._scalar "$z_cls_instance" "$z_cls_member" "$z_cls_numeric" -i - return 0 - fi - ;; - esac + z_cls_values[$z_cls_key]="typeset -i $z_cls_member=$z_cls_numeric" + return 0 + fi + ;; + esac + fi z.cls.instance.call._access_typed "$z_cls_instance" "$z_cls_operation" "$z_cls_member" "$@" } @@ -124,9 +128,15 @@ z.cls.instance.call._access_typed() { fi local z_cls_attribute + local z_cls_key for z_cls_attribute in "${z_cls_attributes[@]}"; do - eval "$z_cls_values[$z_cls_instance:$z_cls_attribute]" + z_cls_key="$z_cls_instance:$z_cls_attribute" + if (( ${+z_cls_values[$z_cls_key]} )); then + eval "$z_cls_values[$z_cls_key]" + else + local -- "$z_cls_attribute=${z_cls_scalars[$z_cls_key]}" + fi done case $z_cls_operation in diff --git a/lib/cls/instance/process.zsh b/lib/cls/instance/process.zsh index 65e44b48..1b8b04c9 100644 --- a/lib/cls/instance/process.zsh +++ b/lib/cls/instance/process.zsh @@ -57,8 +57,7 @@ z.cls.instance._execute() { } "$@" } -# 復元済みの属性を、同じ型で再作成できる宣言として保存する。 -# 通常の値はパラメータ展開を使い、特殊な属性フラグだけtypesetで取得する。 +# 復元済みの属性を保存する。通常のスカラーは文字列、型付き属性は宣言として保持する。 # # $1: 実体参照 # $2: 属性名 @@ -75,24 +74,26 @@ z.cls.instance._snapshot() { local z_cls_snapshot_name=$2 if ! z.var.exists "$z_cls_snapshot_name"; then + unset "z_cls_scalars[$z_cls_snapshot_key]" z_cls_values[$z_cls_snapshot_key]="typeset -- $z_cls_snapshot_name; unset $z_cls_snapshot_name" return 0 fi local z_cls_snapshot_type=${(tP)z_cls_snapshot_name} - case $z_cls_snapshot_type in - scalar|scalar-local) - local z_cls_snapshot_value=${(P)z_cls_snapshot_name} + if [[ $z_cls_snapshot_type == scalar || $z_cls_snapshot_type == scalar-local ]]; then + z.cls.store._scalar "$1" "$z_cls_snapshot_name" "${(P)z_cls_snapshot_name}" + return 0 + fi - z.cls.store._scalar "$1" "$z_cls_snapshot_name" "$z_cls_snapshot_value" - return 0 - ;; + unset "z_cls_scalars[$z_cls_snapshot_key]" + + case $z_cls_snapshot_type in integer|integer-local) local z_cls_snapshot_value=${(P)z_cls_snapshot_name} if z.int.is.match "$z_cls_snapshot_value"; then - z.cls.store._scalar "$1" "$z_cls_snapshot_name" "$z_cls_snapshot_value" -i + z_cls_values[$z_cls_snapshot_key]="typeset -i $z_cls_snapshot_name=$z_cls_snapshot_value" return 0 fi @@ -148,7 +149,7 @@ z.cls.instance._release() { local z_cls_attribute for z_cls_attribute in ${=z_cls_class_attrs[${z_cls_instances[$z_cls_id]}]}; do - unset "z_cls_values[$z_cls_id:$z_cls_attribute]" + unset "z_cls_scalars[$z_cls_id:$z_cls_attribute]" "z_cls_values[$z_cls_id:$z_cls_attribute]" done # 未設定のactive要素も管理ノードを残すため、一緒に削除する。 @@ -157,6 +158,7 @@ z.cls.instance._release() { if z.int.is.zero "${#z_cls_instances}"; then # 最後の実体を片付けたら、空のテーブルの容量も解放する。 z_cls_instances=() + z_cls_scalars=() z_cls_values=() z_cls_members=() z_cls_active=() diff --git a/lib/cls/store/process.zsh b/lib/cls/store/process.zsh index 15fec6b4..a69e9a7a 100644 --- a/lib/cls/store/process.zsh +++ b/lib/cls/store/process.zsh @@ -10,7 +10,8 @@ # # 変更後: z_cls_instances=()とz_cls_values=()を初期化済み z.cls.store._ensure() { if z.is.eq "${z_cls_store_ready:-0}" 1 && - z.var.exists z_cls_dispatch_programs && z.var.exists z_cls_owner_markers; then + z.var.exists z_cls_dispatch_programs && z.var.exists z_cls_owner_markers && + z.var.exists z_cls_scalars; then return 0 fi @@ -21,6 +22,7 @@ z.cls.store._ensure() { z.var.exists z_cls_defaults || typeset -gA z_cls_defaults z.var.exists z_cls_bodies || typeset -gA z_cls_bodies z.var.exists z_cls_instances || typeset -gA z_cls_instances + z.var.exists z_cls_scalars || typeset -gA z_cls_scalars z.var.exists z_cls_values || typeset -gA z_cls_values z.var.exists z_cls_members || typeset -gA z_cls_members z.var.exists z_cls_wrapper_refs || typeset -gA z_cls_wrapper_refs @@ -36,24 +38,21 @@ z.cls.store._ensure() { typeset -gi +x z_cls_store_ready=1 } -# 通常のスカラーと十進整数を、復元可能な宣言へ引用して保存する。 +# 実体のスカラー属性を文字列として保存する。 # # $1: 実体参照 # $2: 属性名 # $3: 保存する値 -# $4?: 宣言フラグ(既定: --、整数は-i) # REPLY: null # return: 0 # # example: -# # 前提: z.cls.store._ensureで保存領域を初期化している # z.cls.store._scalar object label "two words" # # return: 0 -# # 変更後: z_cls_values[object:label]="typeset -- label='two words'" +# # 変更後: z_cls_scalars[object:label]="two words" z.cls.store._scalar() { - local z_cls_declaration_flag=${4:---} - - z_cls_values[$1:$2]="typeset $z_cls_declaration_flag $2=${(qq)3}" + z_cls_scalars[$1:$2]=$3 + unset "z_cls_values[$1:$2]" } # クラスのメソッド一覧と、対応する本体を保存する。 diff --git a/test/lib/cls/instance/process_test.zsh b/test/lib/cls/instance/process_test.zsh index e5005308..d5f0d9fe 100644 --- a/test/lib/cls/instance/process_test.zsh +++ b/test/lib/cls/instance/process_test.zsh @@ -63,18 +63,18 @@ z.t.describe "z.cls.instance._execute"; { z.t.describe "z.cls.instance._snapshot"; { z.t.context "リテラル文字列の場合"; { - z.t.it "保存後に同じ値を復元できる"; { + z.t.it "保存後に変更しても値を保持する"; { () { z.cls.store._ensure local literal=null z.cls.instance._snapshot snapshot_ref literal literal=changed - eval "$z_cls_values[snapshot_ref:literal]" - z.t.expect "$literal" null + z.t.expect "$z_cls_scalars[snapshot_ref:literal]" null + z.t.expect "$literal" changed - unset 'z_cls_values[snapshot_ref:literal]' + unset 'z_cls_scalars[snapshot_ref:literal]' } } } diff --git a/test/lib/cls/store/process_test.zsh b/test/lib/cls/store/process_test.zsh index 85f86c52..a750be5e 100644 --- a/test/lib/cls/store/process_test.zsh +++ b/test/lib/cls/store/process_test.zsh @@ -65,26 +65,23 @@ z.t.describe "z.cls.store._ensure"; { z.t.describe "z.cls.store._scalar"; { z.t.context "空白と空値を保存する場合"; { - z.t.it "引用して復元でき、呼出元のREPLYを保持する"; { + z.t.it "値をそのまま保存し、呼出元のREPLYを保持する"; { () { z.cls.store._ensure - local label=old REPLY=before z.cls.store._scalar declaration_ref label "two words" z.t.expect.reply before - eval "$z_cls_values[declaration_ref:label]" - z.t.expect "$label" "two words" + z.t.expect "$z_cls_scalars[declaration_ref:label]" "two words" z.cls.store._scalar declaration_ref label "" - eval "$z_cls_values[declaration_ref:label]" - z.t.expect.is.null "$label" + z.t.expect.is.null "$z_cls_scalars[declaration_ref:label]" - unset 'z_cls_values[declaration_ref:label]' + unset 'z_cls_scalars[declaration_ref:label]' } } } From 7eaeed5188bc9f47161bebf7f079f984b855f807 Mon Sep 17 00:00:00 2001 From: milkeclair Date: Wed, 18 Nov 2026 18:17:52 +0900 Subject: [PATCH 04/11] perf: dispatch class member commands by operation --- lib/cls/instance/call/process.zsh | 94 +++++++++++++------ lib/cls/instance/commands/process.zsh | 14 +-- lib/cls/instance/process.zsh | 21 +++-- test/lib/cls/gc/process_test.zsh | 4 +- test/lib/cls/instance/call/process_test.zsh | 65 +++++++++++-- .../cls/instance/commands/process_test.zsh | 6 +- 6 files changed, 146 insertions(+), 58 deletions(-) diff --git a/lib/cls/instance/call/process.zsh b/lib/cls/instance/call/process.zsh index 41e1ff54..7b5a5691 100644 --- a/lib/cls/instance/call/process.zsh +++ b/lib/cls/instance/call/process.zsh @@ -1,32 +1,22 @@ -# 呼出しや属性の復元に進む前に、解決済みの処理を検証する。 +# 生存中の実体かを確認し、他の実体の呼び出しからの再入を拒否する。 # # $1: 解決済みの実体参照 -# $2: method、get、setのいずれか -# $3: メンバー名 -# $4: エラーに表示する、元の変数名または参照 +# $2?: エラーに表示する元の変数名 # REPLY: null # return: 0|1 # # example: # # 前提: objectは非実行中で、countを持つ実体の参照変数 -# z.cls.instance.call._validate "$object" get count object +# z.cls.instance.call._validate "$object" # # return: 0 z.cls.instance.call._validate() { local z_cls_instance=$1 if z.is.null "${z_cls_instances[$z_cls_instance]:-}"; then - z.io.error "Instance reference $4 is not available in this scope." + z.io.error "Instance reference ${2:-$1} is not available in this scope." return 1 fi - local z_cls_key="${z_cls_instances[$z_cls_instance]}:$3" - - if z.is.eq "$2" method; then - z.var.exists "z_cls_bodies[$z_cls_key]" || return 1 - else - z.var.exists "z_cls_defaults[$z_cls_key]" || return 1 - fi - if z.is.not.null "${z_cls_active[$z_cls_instance]:-}" && z.is.not.eq "${z_cls_current_instance:-}" "$z_cls_instance"; then z.io.error "Instance $z_cls_instance is active in another instance call." @@ -36,7 +26,62 @@ z.cls.instance.call._validate() { return 0 } -# 保存済みの通常スカラーを直接扱い、それ以外は属性のスコープを復元して処理する。 +# 実体IDから属性を読み、値をREPLYへ返す。 +# +# $1: 実体参照 +# $2: 属性名 +# REPLY: 属性値 +# return: 0|1 +# +# example: +# # 前提: objectはlabelの値がhelloである実体の参照変数 +# z.cls.instance.call._get "$object" label +# # REPLY: hello +# # return: 0 +z.cls.instance.call._get() { + z.cls.instance.call._validate "$1" || return 1 + + local z_cls_key="$1:$2" + if (( ${+z_cls_values[$z_cls_key]} )); then + z.cls.instance.call._access "$1" get "$2" "${@:3}" + return $? + fi + + z.int.is.eq $# 2 || return 1 + (( ${+z_cls_scalars[$z_cls_key]} )) || return 1 + + REPLY=$z_cls_scalars[$z_cls_key] +} + +# 実体IDから属性を更新する。 +# +# $1: 実体参照 +# $2: 属性名 +# $@?: 新しい値または配列の要素 +# REPLY: null +# return: 0|1 +# +# example: +# # 前提: objectはlabelを持つ実体の参照変数 +# z.cls.instance.call._set "$object" label hello +# # return: 0 +# # 変更後: labelの値はhello +z.cls.instance.call._set() { + z.cls.instance.call._validate "$1" || return 1 + + local z_cls_key="$1:$2" + if (( ${+z_cls_values[$z_cls_key]} )); then + z.cls.instance.call._access "$1" set "$2" "${@:3}" + return $? + fi + + z.int.is.eq $# 3 || return 1 + (( ${+z_cls_scalars[$z_cls_key]} )) || return 1 + + z_cls_scalars[$z_cls_key]=$3 +} + +# 十進整数は保存形式から直接処理し、他の宣言は属性を復元して処理する。 # # $1: 実体参照 # $2: getまたはset @@ -46,7 +91,7 @@ z.cls.instance.call._validate() { # return: 取得・更新の終了コード # # example: -# # 前提: objectは初期値0のcountを持つ実体の参照変数 +# # 前提: objectはcountを十進整数の0として保存している実体の参照変数 # z.cls.instance.call._access "$object" get count # # REPLY: 0 # # return: 0 @@ -58,21 +103,6 @@ z.cls.instance.call._access() { shift 3 - if (( ! ${+z_cls_values[$z_cls_key]} )); then - case $z_cls_operation in - get) - z.int.is.zero $# || return 1 - REPLY=$z_cls_scalars[$z_cls_key] - return 0 - ;; - set) - z.int.is.eq $# 1 || return 1 - z.cls.store._scalar "$z_cls_instance" "$z_cls_member" "$1" - return 0 - ;; - esac - fi - local z_cls_declaration=$z_cls_values[$z_cls_key] if z.str.start_with "$z_cls_declaration" "typeset -i $z_cls_member="; then @@ -164,6 +194,8 @@ z.cls.instance.call._access_typed() { # # return: 7 # # 変更後: countは4、実行中フラグは解除済み z.cls.instance.call._run() { + z.cls.instance.call._validate "$1" || return 1 + local z_cls_instance=$1 if z.is.not.null "${z_cls_active[$z_cls_instance]:-}"; then diff --git a/lib/cls/instance/commands/process.zsh b/lib/cls/instance/commands/process.zsh index cd6ca45f..049c8291 100644 --- a/lib/cls/instance/commands/process.zsh +++ b/lib/cls/instance/commands/process.zsh @@ -5,8 +5,8 @@ # return: 全ての名前が利用可能または所有済みなら0、それ以外はエラーを表示して1 # # example: -# # 前提: counter.incrementというコマンドや別名は未登録 -# z.cls.instance.commands._validate counter.increment +# # 前提: z_cls_12345_1.incrementというコマンドや別名は未登録 +# z.cls.instance.commands._validate z_cls_12345_1.increment # # return: 0 z.cls.instance.commands._validate() { local REPLY="" @@ -39,16 +39,16 @@ z.cls.instance.commands._validate() { # return: 0 # # example: -# # 前提: 保存領域を初期化し、counter.incrementの名前を検証済み -# z.cls.instance.commands._install counter.increment +# # 前提: 保存領域を初期化し、z_cls_12345_1.incrementの名前を検証済み +# z.cls.instance.commands._install z_cls_12345_1.increment # # return: 0 -# # 変更後: counter.incrementを登録し、参照数は1 +# # 変更後: z_cls_12345_1.incrementを登録し、参照数は1 z.cls.instance.commands._install() { local z_cls_command for z_cls_command in "$@"; do local z_cls_member=${z_cls_command#*.} - local z_cls_operation=method + local z_cls_operation=run if z.str.includes "$z_cls_member" .; then z_cls_operation=${z_cls_member##*.} @@ -58,7 +58,7 @@ z.cls.instance.commands._install() { local z_cls_template="z_cls_dispatch_${z_cls_operation}_$z_cls_member" if ! z.var.exists "functions[$z_cls_template]"; then - functions[$z_cls_template]='z.cls.instance._call "${funcstack[1]%%.*}" '"$z_cls_operation ${(q)z_cls_member}"' "$@"' + functions[$z_cls_template]='z.cls.instance.call._'"$z_cls_operation"' "${funcstack[1]%%.*}" '"${(q)z_cls_member}"' "$@"' z_cls_dispatch_programs[${z_cls_command#*.}]=$functions[$z_cls_template] fi diff --git a/lib/cls/instance/process.zsh b/lib/cls/instance/process.zsh index 1b8b04c9..04502d19 100644 --- a/lib/cls/instance/process.zsh +++ b/lib/cls/instance/process.zsh @@ -18,15 +18,19 @@ z.cls.instance._call() { local z_cls_member=$3 z.is.not.null "${z_cls_instances[$1]:-}" && z_cls_instance=$1 - z.cls.instance.call._validate "$z_cls_instance" "$z_cls_operation" "$z_cls_member" "$1" || return 1 + if z.is.null "$z_cls_instance"; then + z.cls.instance.call._validate "$z_cls_instance" "$1" + return 1 + fi shift 3 - if z.is.eq "$z_cls_operation" method; then - z.cls.instance.call._run "$z_cls_instance" "$z_cls_member" "$@" - else - z.cls.instance.call._access "$z_cls_instance" "$z_cls_operation" "$z_cls_member" "$@" - fi + case $z_cls_operation in + method) z.cls.instance.call._run "$z_cls_instance" "$z_cls_member" "$@" ;; + get) z.cls.instance.call._get "$z_cls_instance" "$z_cls_member" "$@" ;; + set) z.cls.instance.call._set "$z_cls_instance" "$z_cls_member" "$@" ;; + *) return 1 ;; + esac } # メソッド本体を、readonlyのselfとメソッド引数を持つ関数スコープで実行する。 @@ -45,8 +49,11 @@ z.cls.instance._call() { z.cls.instance._execute() { local z_cls_instance=$1 local z_cls_member=$2 - local z_cls_body=$z_cls_bodies[${z_cls_instances[$z_cls_instance]}:$z_cls_member] + local z_cls_key="${z_cls_instances[$z_cls_instance]}:$z_cls_member" + + (( ${+z_cls_bodies[$z_cls_key]} )) || return 1 + local z_cls_body=$z_cls_bodies[$z_cls_key] shift 2 # EXITトラップもメソッド引数で動くよう、本体専用のスコープを作る。 diff --git a/test/lib/cls/gc/process_test.zsh b/test/lib/cls/gc/process_test.zsh index 34f33a85..801a7567 100644 --- a/test/lib/cls/gc/process_test.zsh +++ b/test/lib/cls/gc/process_test.zsh @@ -3,7 +3,7 @@ source "$z_main" z.t.describe "z.cls.gc._collect"; { z.t.context "関数内で短命の実体を繰り返し生成する場合"; { z.t.it "実行中に解放し、同名の外側の所有者を保つ"; { - local -A z_cls_instances=() z_cls_values=() z_cls_members=() z_cls_active=() z_cls_owner_markers=() + local -A z_cls_instances=() z_cls_scalars=() z_cls_values=() z_cls_members=() z_cls_active=() z_cls_owner_markers=() local -A z_cls_wrapper_refs=() local -i z_cls_gc_allocations=0 z.cls.draw CollectInLoop @@ -30,7 +30,7 @@ z.t.describe "z.cls.gc._collect"; { z.t.context "所有変数が消えた実体が実行中の場合"; { z.t.it "実行中は回収を待ち、実行後の次の回収で解放する"; { - local -A z_cls_instances=() z_cls_values=() z_cls_members=() z_cls_active=() z_cls_owner_markers=() + local -A z_cls_instances=() z_cls_scalars=() z_cls_values=() z_cls_members=() z_cls_active=() z_cls_owner_markers=() local -A z_cls_wrapper_refs=() local -i z_cls_gc_allocations=0 z.cls.draw CollectAfterReturn diff --git a/test/lib/cls/instance/call/process_test.zsh b/test/lib/cls/instance/call/process_test.zsh index 5afcf972..8e13d2b4 100644 --- a/test/lib/cls/instance/call/process_test.zsh +++ b/test/lib/cls/instance/call/process_test.zsh @@ -1,17 +1,16 @@ source ${z_main} z.t.describe "z.cls.instance.call._validate"; { - z.t.context "公開済みのメソッドの場合"; { + z.t.context "生存中の実体の場合"; { z.t.it "実行を許可する"; { z.cls.store._ensure z_cls_instances[validate_ref]=ValidateCall - z_cls_bodies[ValidateCall:run]=':' - z.cls.instance.call._validate validate_ref method run object + z.cls.instance.call._validate validate_ref z.t.expect.status.is.true - unset 'z_cls_instances[validate_ref]' 'z_cls_bodies[ValidateCall:run]' + unset 'z_cls_instances[validate_ref]' } } @@ -20,7 +19,7 @@ z.t.describe "z.cls.instance.call._validate"; { z.cls.store._ensure z.t.mock name=z.io.error - z.cls.instance.call._validate "" method run missing + z.cls.instance.call._validate "" missing z.t.expect.status.is.false } @@ -30,19 +29,69 @@ z.t.describe "z.cls.instance.call._validate"; { z.t.it "実行中の実体を保護する"; { z.cls.store._ensure z_cls_instances[reentry_ref]=ValidateReentry - z_cls_bodies[ValidateReentry:run]=':' z_cls_active[reentry_ref]=1 z.t.mock name=z.io.error () { local z_cls_current_instance=another_ref - z.cls.instance.call._validate reentry_ref method run object + z.cls.instance.call._validate reentry_ref z.t.expect.status.is.false } - unset 'z_cls_instances[reentry_ref]' 'z_cls_bodies[ValidateReentry:run]' 'z_cls_active[reentry_ref]' + unset 'z_cls_instances[reentry_ref]' 'z_cls_active[reentry_ref]' + } + } +} + +z.t.describe "z.cls.instance.call._get"; { + z.t.context "スカラーを取得する場合"; { + z.t.it "シェル構文を含む文字列をそのまま返す"; { + () { + z.cls.draw DirectGet + z.cls.attr label default=old + z.cls.drew + local object="" + z.cls.new DirectGet object + + local literal=$'one \'two\'\n$(print -r -- expanded) "three" *' + $object.label.set "$literal" + z.cls.instance.call._get "$object" label + + z.t.expect.reply "$literal" + + z.cls.instance.call._get "$object" label extra + + z.t.expect.status.is.false + z.cls.delete object + } + } + } +} + +z.t.describe "z.cls.instance.call._set"; { + z.t.context "スカラーを更新する場合"; { + z.t.it "空文字を保存し、余分な引数がある更新を拒否する"; { + () { + z.cls.draw DirectSet + z.cls.attr label default=old + z.cls.drew + local object="" + z.cls.new DirectSet object + + z.cls.instance.call._set "$object" label "" + + z.t.expect.status.is.true + + z.cls.instance.call._set "$object" label new extra + + z.t.expect.status.is.false + $object.label.get + z.t.expect.reply.is.null + + z.cls.delete object + } } } } diff --git a/test/lib/cls/instance/commands/process_test.zsh b/test/lib/cls/instance/commands/process_test.zsh index 8f18874a..e1dbefae 100644 --- a/test/lib/cls/instance/commands/process_test.zsh +++ b/test/lib/cls/instance/commands/process_test.zsh @@ -36,14 +36,14 @@ z.t.describe "z.cls.instance.commands._install"; { z.t.context "検証済みのメソッド名の場合"; { z.t.it "呼び出し先と引数を渡す"; { z.cls.store._ensure - z.t.mock name=z.cls.instance._call + z.t.mock name=z.cls.instance.call._run z.cls.instance.commands._install installed_cls.run installed_cls.run one two - z.t.mock.result name=z.cls.instance._call + z.t.mock.result name=z.cls.instance.call._run - z.t.expect.reply.is.arr installed_cls method run one two + z.t.expect.reply.is.arr installed_cls run one two unfunction installed_cls.run unset 'z_cls_wrapper_refs[installed_cls.run]' From 638544af802b8beb1b42843254930502a50a9020 Mon Sep 17 00:00:00 2001 From: milkeclair Date: Wed, 18 Nov 2026 18:18:56 +0900 Subject: [PATCH 05/11] perf: share class defaults across instances --- lib/cls/instance/call/process.zsh | 20 ++++++++++++----- lib/cls/process.zsh | 6 ------ test/lib/cls/process_test.zsh | 36 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/lib/cls/instance/call/process.zsh b/lib/cls/instance/call/process.zsh index 7b5a5691..5fbeeaff 100644 --- a/lib/cls/instance/call/process.zsh +++ b/lib/cls/instance/call/process.zsh @@ -48,9 +48,16 @@ z.cls.instance.call._get() { fi z.int.is.eq $# 2 || return 1 - (( ${+z_cls_scalars[$z_cls_key]} )) || return 1 - REPLY=$z_cls_scalars[$z_cls_key] + if (( ${+z_cls_scalars[$z_cls_key]} )); then + REPLY=$z_cls_scalars[$z_cls_key] + return 0 + fi + + local z_cls_default_key="${z_cls_instances[$1]}:$2" + (( ${+z_cls_defaults[$z_cls_default_key]} )) || return 1 + + REPLY=$z_cls_defaults[$z_cls_default_key] } # 実体IDから属性を更新する。 @@ -76,7 +83,9 @@ z.cls.instance.call._set() { fi z.int.is.eq $# 3 || return 1 - (( ${+z_cls_scalars[$z_cls_key]} )) || return 1 + + local z_cls_default_key="${z_cls_instances[$1]}:$2" + (( ${+z_cls_defaults[$z_cls_default_key]} )) || return 1 z_cls_scalars[$z_cls_key]=$3 } @@ -148,13 +157,14 @@ z.cls.instance.call._access_typed() { local z_cls_instance=$1 local z_cls_operation=$2 local z_cls_member=$3 + local z_cls_class=$z_cls_instances[$z_cls_instance] local -a z_cls_attributes=("$z_cls_member") shift 3 if z.is.eq "$z_cls_operation" set; then # 数値式は、同じ実体の他の属性を参照できる。 - z_cls_attributes=(${=z_cls_class_attrs[${z_cls_instances[$z_cls_instance]}]}) + z_cls_attributes=(${=z_cls_class_attrs[$z_cls_class]}) fi local z_cls_attribute @@ -165,7 +175,7 @@ z.cls.instance.call._access_typed() { if (( ${+z_cls_values[$z_cls_key]} )); then eval "$z_cls_values[$z_cls_key]" else - local -- "$z_cls_attribute=${z_cls_scalars[$z_cls_key]}" + local -- "$z_cls_attribute=${z_cls_scalars[$z_cls_key]-${z_cls_defaults[$z_cls_class:$z_cls_attribute]}}" fi done diff --git a/lib/cls/process.zsh b/lib/cls/process.zsh index cb34cb15..880444ef 100644 --- a/lib/cls/process.zsh +++ b/lib/cls/process.zsh @@ -229,12 +229,6 @@ z.cls.new() { z.cls.instance.commands._validate "${z_cls_wrappers[@]}" || return 1 z.cls.owner._bind "$z_cls_id" "$z_cls_binding" || return 1 - local z_cls_member - - for z_cls_member in ${=z_cls_class_attrs[$z_cls_class]}; do - z.cls.store._scalar "$z_cls_id" "$z_cls_member" "$z_cls_defaults[$z_cls_class:$z_cls_member]" - done - z.cls.instance.commands._install "${z_cls_wrappers[@]}" z_cls_members[$z_cls_id]="${(j: :)z_cls_wrappers}" diff --git a/test/lib/cls/process_test.zsh b/test/lib/cls/process_test.zsh index 6c0e9286..bbe78583 100644 --- a/test/lib/cls/process_test.zsh +++ b/test/lib/cls/process_test.zsh @@ -170,6 +170,42 @@ z.t.describe "z.cls.abort"; { } z.t.describe "z.cls.new"; { + z.t.context "既定値を共有する場合"; { + z.t.it "未変更の属性を保持せず、明示した空文字だけを実体に保存する"; { + () { + z.cls.draw SharedDefaults + z.cls.attr label default=initial + z.cls.drew + + local first="" second="" + z.cls.new SharedDefaults first + z.cls.new SharedDefaults second + + $first.label.get + + z.t.expect.reply initial + z.t.expect "${+z_cls_scalars[${first}:label]}" 0 + z.t.expect "${+z_cls_values[${first}:label]}" 0 + + $first.label.set "" + $first.label.get + + z.t.expect.reply.is.null + z.t.expect "${+z_cls_scalars[${first}:label]}" 1 + + $second.label.get + + z.t.expect.reply initial + z.t.expect "${+z_cls_scalars[${second}:label]}" 0 + + z.cls.delete first + + z.t.expect "${+z_cls_scalars[${first}:label]}" 0 + z.cls.delete second + } + } + } + z.t.context "nounsetの下で参照をコピーした場合"; { z.t.it "nounset下でコピーした参照の状態共有と削除ができる"; { local -a output=() From b64d91ad67fa63fd5a866a887bfe8ab2d64ff2cc Mon Sep 17 00:00:00 2001 From: milkeclair Date: Wed, 18 Nov 2026 18:20:27 +0900 Subject: [PATCH 06/11] fix: keep existing instance commands valid after reload --- lib/cls/instance/commands/process.zsh | 7 +++++-- lib/cls/instance/process.zsh | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/cls/instance/commands/process.zsh b/lib/cls/instance/commands/process.zsh index 049c8291..02927ff6 100644 --- a/lib/cls/instance/commands/process.zsh +++ b/lib/cls/instance/commands/process.zsh @@ -48,7 +48,7 @@ z.cls.instance.commands._install() { for z_cls_command in "$@"; do local z_cls_member=${z_cls_command#*.} - local z_cls_operation=run + local z_cls_operation=method if z.str.includes "$z_cls_member" .; then z_cls_operation=${z_cls_member##*.} @@ -58,7 +58,10 @@ z.cls.instance.commands._install() { local z_cls_template="z_cls_dispatch_${z_cls_operation}_$z_cls_member" if ! z.var.exists "functions[$z_cls_template]"; then - functions[$z_cls_template]='z.cls.instance.call._'"$z_cls_operation"' "${funcstack[1]%%.*}" '"${(q)z_cls_member}"' "$@"' + local z_cls_handler=$z_cls_operation + z.is.eq "$z_cls_operation" method && z_cls_handler=run + + functions[$z_cls_template]='z.cls.instance.call._'"$z_cls_handler"' "${funcstack[1]%%.*}" '"${(q)z_cls_member}"' "$@"' z_cls_dispatch_programs[${z_cls_command#*.}]=$functions[$z_cls_template] fi diff --git a/lib/cls/instance/process.zsh b/lib/cls/instance/process.zsh index 04502d19..308f0cf8 100644 --- a/lib/cls/instance/process.zsh +++ b/lib/cls/instance/process.zsh @@ -13,6 +13,11 @@ # # REPLY: 1 # # return: 0 z.cls.instance._call() { + # 再読込前に生成したコマンドも、新しい保存領域を使って属性を更新する。 + if (( ! ${+z_cls_scalars} )); then + z.cls.store._ensure + fi + local z_cls_instance=${(P)1:-} local z_cls_operation=$2 local z_cls_member=$3 From 633d8fe0bf2aba94361737ce1c4516b7a88a27bd Mon Sep 17 00:00:00 2001 From: milkeclair Date: Wed, 18 Nov 2026 18:23:07 +0900 Subject: [PATCH 07/11] refactor: place class getter with value analysis --- lib/cls/instance/call/analysis.zsh | 33 +++++++++++++++++++ lib/cls/instance/call/process.zsh | 34 -------------------- lib/cls/store/process.zsh | 3 +- test/lib/cls/instance/call/analysis_test.zsh | 26 +++++++++++++++ test/lib/cls/instance/call/process_test.zsh | 25 -------------- 5 files changed, 61 insertions(+), 60 deletions(-) create mode 100644 lib/cls/instance/call/analysis.zsh create mode 100644 test/lib/cls/instance/call/analysis_test.zsh diff --git a/lib/cls/instance/call/analysis.zsh b/lib/cls/instance/call/analysis.zsh new file mode 100644 index 00000000..38a05012 --- /dev/null +++ b/lib/cls/instance/call/analysis.zsh @@ -0,0 +1,33 @@ +# 実体IDから属性を読み、値をREPLYへ返す。 +# +# $1: 実体参照 +# $2: 属性名 +# REPLY: 属性値 +# return: 0|1 +# +# example: +# # 前提: objectはlabelの値がhelloである実体の参照変数 +# z.cls.instance.call._get "$object" label +# # REPLY: hello +# # return: 0 +z.cls.instance.call._get() { + z.cls.instance.call._validate "$1" || return 1 + + local z_cls_key="$1:$2" + if (( ${+z_cls_values[$z_cls_key]} )); then + z.cls.instance.call._access "$1" get "$2" "${@:3}" + return $? + fi + + z.int.is.eq $# 2 || return 1 + + if (( ${+z_cls_scalars[$z_cls_key]} )); then + REPLY=$z_cls_scalars[$z_cls_key] + return 0 + fi + + local z_cls_default_key="${z_cls_instances[$1]}:$2" + (( ${+z_cls_defaults[$z_cls_default_key]} )) || return 1 + + REPLY=$z_cls_defaults[$z_cls_default_key] +} diff --git a/lib/cls/instance/call/process.zsh b/lib/cls/instance/call/process.zsh index 5fbeeaff..25b4353f 100644 --- a/lib/cls/instance/call/process.zsh +++ b/lib/cls/instance/call/process.zsh @@ -26,40 +26,6 @@ z.cls.instance.call._validate() { return 0 } -# 実体IDから属性を読み、値をREPLYへ返す。 -# -# $1: 実体参照 -# $2: 属性名 -# REPLY: 属性値 -# return: 0|1 -# -# example: -# # 前提: objectはlabelの値がhelloである実体の参照変数 -# z.cls.instance.call._get "$object" label -# # REPLY: hello -# # return: 0 -z.cls.instance.call._get() { - z.cls.instance.call._validate "$1" || return 1 - - local z_cls_key="$1:$2" - if (( ${+z_cls_values[$z_cls_key]} )); then - z.cls.instance.call._access "$1" get "$2" "${@:3}" - return $? - fi - - z.int.is.eq $# 2 || return 1 - - if (( ${+z_cls_scalars[$z_cls_key]} )); then - REPLY=$z_cls_scalars[$z_cls_key] - return 0 - fi - - local z_cls_default_key="${z_cls_instances[$1]}:$2" - (( ${+z_cls_defaults[$z_cls_default_key]} )) || return 1 - - REPLY=$z_cls_defaults[$z_cls_default_key] -} - # 実体IDから属性を更新する。 # # $1: 実体参照 diff --git a/lib/cls/store/process.zsh b/lib/cls/store/process.zsh index a69e9a7a..639045e8 100644 --- a/lib/cls/store/process.zsh +++ b/lib/cls/store/process.zsh @@ -7,7 +7,7 @@ # # 前提: 保存領域は未初期化 # z.cls.store._ensure # # return: 0 -# # 変更後: z_cls_instances=()とz_cls_values=()を初期化済み +# # 変更後: z_cls_instances、z_cls_scalars、z_cls_valuesを空の連想配列で初期化 z.cls.store._ensure() { if z.is.eq "${z_cls_store_ready:-0}" 1 && z.var.exists z_cls_dispatch_programs && z.var.exists z_cls_owner_markers && @@ -47,6 +47,7 @@ z.cls.store._ensure() { # return: 0 # # example: +# # 前提: z.cls.store._ensureで保存領域を初期化している # z.cls.store._scalar object label "two words" # # return: 0 # # 変更後: z_cls_scalars[object:label]="two words" diff --git a/test/lib/cls/instance/call/analysis_test.zsh b/test/lib/cls/instance/call/analysis_test.zsh new file mode 100644 index 00000000..23de3963 --- /dev/null +++ b/test/lib/cls/instance/call/analysis_test.zsh @@ -0,0 +1,26 @@ +source ${z_main} + +z.t.describe "z.cls.instance.call._get"; { + z.t.context "スカラーを取得する場合"; { + z.t.it "シェル構文を含む文字列をそのまま返す"; { + () { + z.cls.draw DirectGet + z.cls.attr label default=old + z.cls.drew + local object="" + z.cls.new DirectGet object + + local literal=$'one \'two\'\n$(print -r -- expanded) "three" *' + $object.label.set "$literal" + z.cls.instance.call._get "$object" label + + z.t.expect.reply "$literal" + + z.cls.instance.call._get "$object" label extra + + z.t.expect.status.is.false + z.cls.delete object + } + } + } +} diff --git a/test/lib/cls/instance/call/process_test.zsh b/test/lib/cls/instance/call/process_test.zsh index 8e13d2b4..4ef11e50 100644 --- a/test/lib/cls/instance/call/process_test.zsh +++ b/test/lib/cls/instance/call/process_test.zsh @@ -45,31 +45,6 @@ z.t.describe "z.cls.instance.call._validate"; { } } -z.t.describe "z.cls.instance.call._get"; { - z.t.context "スカラーを取得する場合"; { - z.t.it "シェル構文を含む文字列をそのまま返す"; { - () { - z.cls.draw DirectGet - z.cls.attr label default=old - z.cls.drew - local object="" - z.cls.new DirectGet object - - local literal=$'one \'two\'\n$(print -r -- expanded) "three" *' - $object.label.set "$literal" - z.cls.instance.call._get "$object" label - - z.t.expect.reply "$literal" - - z.cls.instance.call._get "$object" label extra - - z.t.expect.status.is.false - z.cls.delete object - } - } - } -} - z.t.describe "z.cls.instance.call._set"; { z.t.context "スカラーを更新する場合"; { z.t.it "空文字を保存し、余分な引数がある更新を拒否する"; { From 9982993b7b4c7c2b175594b3123122432b11ec2b Mon Sep 17 00:00:00 2001 From: milkeclair Date: Mon, 23 Nov 2026 12:45:34 +0900 Subject: [PATCH 08/11] feat: expire class references when ownership changes or ends --- lib/cls/instance/call/analysis.zsh | 9 ++--- lib/cls/instance/call/process.zsh | 35 +++++++++++------- lib/cls/instance/commands/process.zsh | 35 +++++++++++++++++- lib/cls/instance/process.zsh | 24 +++++-------- lib/cls/owner/operator.zsh | 8 +++-- lib/cls/owner/process.zsh | 36 +++++++++++++++---- lib/cls/process.zsh | 18 ++++++---- lib/cls/store/process.zsh | 6 +++- test/lib/cls/instance/call/process_test.zsh | 4 +++ test/lib/cls/owner/process_test.zsh | 39 +++++++++++++++++++++ test/lib/cls/process_test.zsh | 2 +- 11 files changed, 167 insertions(+), 49 deletions(-) diff --git a/lib/cls/instance/call/analysis.zsh b/lib/cls/instance/call/analysis.zsh index 38a05012..c1aaf949 100644 --- a/lib/cls/instance/call/analysis.zsh +++ b/lib/cls/instance/call/analysis.zsh @@ -11,11 +11,12 @@ # # REPLY: hello # # return: 0 z.cls.instance.call._get() { - z.cls.instance.call._validate "$1" || return 1 + local z_cls_id=${z_cls_references[$1]:-$1} + z.cls.instance.call._validate "$z_cls_id" "$1" || return 1 - local z_cls_key="$1:$2" + local z_cls_key="$z_cls_id:$2" if (( ${+z_cls_values[$z_cls_key]} )); then - z.cls.instance.call._access "$1" get "$2" "${@:3}" + z.cls.instance.call._access "$z_cls_id" get "$2" "${@:3}" return $? fi @@ -26,7 +27,7 @@ z.cls.instance.call._get() { return 0 fi - local z_cls_default_key="${z_cls_instances[$1]}:$2" + local z_cls_default_key="${z_cls_instances[$z_cls_id]}:$2" (( ${+z_cls_defaults[$z_cls_default_key]} )) || return 1 REPLY=$z_cls_defaults[$z_cls_default_key] diff --git a/lib/cls/instance/call/process.zsh b/lib/cls/instance/call/process.zsh index 25b4353f..bb02c56c 100644 --- a/lib/cls/instance/call/process.zsh +++ b/lib/cls/instance/call/process.zsh @@ -1,7 +1,8 @@ # 生存中の実体かを確認し、他の実体の呼び出しからの再入を拒否する。 # # $1: 解決済みの実体参照 -# $2?: エラーに表示する元の変数名 +# $2?: 呼び出しに使った参照 +# $3?: writeの場合は更新できる所有者参照に限る # REPLY: null # return: 0|1 # @@ -11,9 +12,19 @@ # # return: 0 z.cls.instance.call._validate() { local z_cls_instance=$1 + local z_cls_ref=${2:-$1} + local z_cls_owner=${z_cls_owner_refs[$z_cls_instance]:-$z_cls_instance} + local z_cls_marker=${z_cls_owner_markers[$z_cls_instance]-} + + if [[ -z ${z_cls_instances[$z_cls_instance]-} || -z $z_cls_marker ]] || + (( ! ${+parameters[$z_cls_marker]} )) || + [[ $z_cls_owner != "$z_cls_ref" && ${z_cls_references[$z_cls_ref]-} != "$z_cls_instance" ]]; then + z.io.error "Instance reference $z_cls_ref is not available in this scope." + return 1 + fi - if z.is.null "${z_cls_instances[$z_cls_instance]:-}"; then - z.io.error "Instance reference ${2:-$1} is not available in this scope." + if [[ ${3:-} == write && ( $z_cls_owner != "$z_cls_ref" || -n ${z_cls_borrow_counts[$z_cls_instance]-} ) ]]; then + z.io.error "Instance reference $z_cls_ref cannot modify a borrowed instance." return 1 fi @@ -40,17 +51,18 @@ z.cls.instance.call._validate() { # # return: 0 # # 変更後: labelの値はhello z.cls.instance.call._set() { - z.cls.instance.call._validate "$1" || return 1 + local z_cls_id=${z_cls_references[$1]:-$1} + z.cls.instance.call._validate "$z_cls_id" "$1" write || return 1 - local z_cls_key="$1:$2" + local z_cls_key="$z_cls_id:$2" if (( ${+z_cls_values[$z_cls_key]} )); then - z.cls.instance.call._access "$1" set "$2" "${@:3}" + z.cls.instance.call._access "$z_cls_id" set "$2" "${@:3}" return $? fi z.int.is.eq $# 3 || return 1 - local z_cls_default_key="${z_cls_instances[$1]}:$2" + local z_cls_default_key="${z_cls_instances[$z_cls_id]}:$2" (( ${+z_cls_defaults[$z_cls_default_key]} )) || return 1 z_cls_scalars[$z_cls_key]=$3 @@ -170,12 +182,11 @@ z.cls.instance.call._access_typed() { # # return: 7 # # 変更後: countは4、実行中フラグは解除済み z.cls.instance.call._run() { - z.cls.instance.call._validate "$1" || return 1 - - local z_cls_instance=$1 + local z_cls_instance=${z_cls_references[$1]:-$1} + z.cls.instance.call._validate "$z_cls_instance" "$1" write || return 1 if z.is.not.null "${z_cls_active[$z_cls_instance]:-}"; then - z.cls.instance._execute "$@" + z.cls.instance._execute "$z_cls_instance" "${@:2}" return $? fi @@ -184,7 +195,7 @@ z.cls.instance.call._run() { z_cls_active[$z_cls_instance]=1 { - z.cls.instance._execute "$@" + z.cls.instance._execute "$z_cls_instance" "${@:2}" } always { z_cls_result=$? unset "z_cls_active[$z_cls_instance]" diff --git a/lib/cls/instance/commands/process.zsh b/lib/cls/instance/commands/process.zsh index 02927ff6..96339b1f 100644 --- a/lib/cls/instance/commands/process.zsh +++ b/lib/cls/instance/commands/process.zsh @@ -44,6 +44,7 @@ z.cls.instance.commands._validate() { # # return: 0 # # 変更後: z_cls_12345_1.incrementを登録し、参照数は1 z.cls.instance.commands._install() { + local -a z_cls_installed=() local z_cls_command for z_cls_command in "$@"; do @@ -65,9 +66,41 @@ z.cls.instance.commands._install() { z_cls_dispatch_programs[${z_cls_command#*.}]=$functions[$z_cls_template] fi - functions -c "$z_cls_template" "$z_cls_command" + if ! functions -c "$z_cls_template" "$z_cls_command"; then + z.cls.instance.commands._uninstall "${z_cls_installed[@]}" + return 1 + fi + + z_cls_installed+=("$z_cls_command") (( z_cls_wrapper_refs[$z_cls_command] += 1 )) done return 0 } + +# 生成時に登録したコマンドを手放し、置き換えられていない本体を解放する。 +# +# $@: 登録済みのコマンド名 +# REPLY: null +# return: 0 +# +# example: +# # 前提: z_cls_12345_1.incrementが実体用に1回登録されている +# z.cls.instance.commands._uninstall z_cls_12345_1.increment +# # 変更後: z_cls_12345_1.incrementの生成済みコマンドを削除 +z.cls.instance.commands._uninstall() { + local z_cls_command + + for z_cls_command in "$@"; do + z_cls_wrapper_refs[$z_cls_command]=$(( z_cls_wrapper_refs[$z_cls_command] - 1 )) + z.int.is.zero $z_cls_wrapper_refs[$z_cls_command] || continue + + if z.cls.instance._owns "$z_cls_command"; then + unfunction "$z_cls_command" + fi + + unset "z_cls_wrapper_refs[$z_cls_command]" + done + + return 0 +} diff --git a/lib/cls/instance/process.zsh b/lib/cls/instance/process.zsh index 308f0cf8..6608f0bc 100644 --- a/lib/cls/instance/process.zsh +++ b/lib/cls/instance/process.zsh @@ -22,9 +22,9 @@ z.cls.instance._call() { local z_cls_operation=$2 local z_cls_member=$3 - z.is.not.null "${z_cls_instances[$1]:-}" && z_cls_instance=$1 + [[ $1 == z_cls_* ]] && z_cls_instance=$1 if z.is.null "$z_cls_instance"; then - z.cls.instance.call._validate "$z_cls_instance" "$1" + z.io.error "Instance reference $1 is not available in this scope." return 1 fi @@ -63,7 +63,7 @@ z.cls.instance._execute() { # EXITトラップもメソッド引数で動くよう、本体専用のスコープを作る。 () { - local -r self=$z_cls_instance + local -r self=${z_cls_owner_refs[$z_cls_instance]:-$z_cls_instance} eval "$z_cls_body" } "$@" @@ -144,19 +144,10 @@ z.cls.instance._snapshot() { # # 変更後: 実体と、参照がなくなった生成コマンドを解放済み z.cls.instance._release() { local z_cls_id=$1 - local z_cls_command + local z_cls_ref=${z_cls_owner_refs[$z_cls_id]:-$z_cls_id} - for z_cls_command in ${=z_cls_members[$z_cls_id]}; do - z_cls_wrapper_refs[$z_cls_command]=$(( z_cls_wrapper_refs[$z_cls_command] - 1 )) - - z.int.is.zero $z_cls_wrapper_refs[$z_cls_command] || continue - - if z.cls.instance._owns "$z_cls_command"; then - unfunction "$z_cls_command" - fi - - unset "z_cls_wrapper_refs[$z_cls_command]" - done + unset "z_cls_references[$z_cls_ref]" "z_cls_owner_refs[$z_cls_id]" + z.cls.instance.commands._uninstall ${=z_cls_members[$z_cls_id]} local z_cls_attribute @@ -176,6 +167,9 @@ z.cls.instance._release() { z_cls_active=() z_cls_wrapper_refs=() z_cls_owner_markers=() + z_cls_references=() + z_cls_owner_refs=() + z_cls_borrow_counts=() z_cls_gc_allocations=0 fi } diff --git a/lib/cls/owner/operator.zsh b/lib/cls/owner/operator.zsh index 81b95db1..334f4945 100644 --- a/lib/cls/owner/operator.zsh +++ b/lib/cls/owner/operator.zsh @@ -26,7 +26,8 @@ z.cls.owner._writable() { esac local z_cls_ref=${(P)z_cls_binding:-} - z.is.null "$z_cls_ref" || z.is.null "${z_cls_instances[$z_cls_ref]:-}" + local z_cls_id=${z_cls_references[$z_cls_ref]:-$z_cls_ref} + z.is.null "$z_cls_ref" || z.is.null "${z_cls_instances[$z_cls_id]:-}" } # 指定した変数が実体の現在の所有変数か検証する。 @@ -41,7 +42,10 @@ z.cls.owner._writable() { # z.cls.owner._is_current counter "$counter" # # return: 0 z.cls.owner._is_current() { - local z_cls_marker=${z_cls_owner_markers[$2]:-} + local z_cls_id=${z_cls_references[$2]:-$2} + [[ "${z_cls_owner_refs[$z_cls_id]:-$z_cls_id}" == "$2" ]] || return 1 + + local z_cls_marker=${z_cls_owner_markers[$z_cls_id]:-} z.is.not.null "$z_cls_marker" || return 1 z.str.includes "${parameters[$1]:-}" tied || return 1 diff --git a/lib/cls/owner/process.zsh b/lib/cls/owner/process.zsh index 14a2da26..0d558280 100644 --- a/lib/cls/owner/process.zsh +++ b/lib/cls/owner/process.zsh @@ -10,7 +10,7 @@ # local saved="" # z.cls.owner.move from=counter to=saved # # return: 0 -# # 変更後: savedだけが実体の寿命を保持し、counterは借用参照になる +# # 変更後: savedから同じ実体を操作でき、counterの参照は失効する z.cls.owner.move() { z.cls.store._ensure z.arg.named from "$@" && local z_cls_from=$REPLY @@ -21,20 +21,44 @@ z.cls.owner.move() { return 1 fi - local z_cls_id=${(P)z_cls_from:-} - if ! z.cls.owner._is_current "$z_cls_from" "$z_cls_id"; then + local z_cls_old_ref=${(P)z_cls_from:-} + local z_cls_id=${z_cls_references[$z_cls_old_ref]:-$z_cls_old_ref} + if ! z.cls.owner._is_current "$z_cls_from" "$z_cls_old_ref"; then z.io.error "Instance variable $z_cls_from is not the current owner." return 1 fi - z.cls.owner._bind "$z_cls_id" "$z_cls_to" || return 1 - REPLY=$z_cls_id + if [[ -n ${z_cls_active[$z_cls_id]-} || -n ${z_cls_borrow_counts[$z_cls_id]-} ]]; then + z.io.error "Cannot move an active or borrowed instance." + return 1 + fi + + local z_cls_new_ref="z_cls_ref_${$}_$(( ++z_cls_next_reference ))" + z.cls.instance._commands "$z_cls_instances[$z_cls_id]" "$z_cls_new_ref" + local -a z_cls_new_commands=("${(@)REPLY}") + + z.cls.instance.commands._validate "${z_cls_new_commands[@]}" || return 1 + z.cls.instance.commands._install "${z_cls_new_commands[@]}" || return 1 + + if ! z.cls.owner._bind "$z_cls_new_ref" "$z_cls_to" "$z_cls_id"; then + z.cls.instance.commands._uninstall "${z_cls_new_commands[@]}" + return 1 + fi + + unset "z_cls_references[$z_cls_old_ref]" + z_cls_references[$z_cls_new_ref]=$z_cls_id + z_cls_owner_refs[$z_cls_id]=$z_cls_new_ref + z.cls.instance.commands._uninstall ${=z_cls_members[$z_cls_id]} + z_cls_members[$z_cls_id]="${(j: :)z_cls_new_commands}" + + REPLY=$z_cls_new_ref } # 実体参照を代入し、代入先と同じ寿命の配列を所有者の目印にする。 # # $1: 実体参照 # $2: 検証済みの代入先変数名 +# $3?: 参照に対応する内部の実体ID # REPLY: null # return: 0|1 # @@ -49,5 +73,5 @@ z.cls.owner._bind() { builtin typeset -gT "$2=$1" "$z_cls_marker" || return 1 builtin typeset -gr -- "$2" || return 1 - z_cls_owner_markers[$1]=$z_cls_marker + z_cls_owner_markers[${3:-$1}]=$z_cls_marker } diff --git a/lib/cls/process.zsh b/lib/cls/process.zsh index 880444ef..7e0803df 100644 --- a/lib/cls/process.zsh +++ b/lib/cls/process.zsh @@ -227,9 +227,11 @@ z.cls.new() { local -a z_cls_wrappers=("${(@)REPLY}") z.cls.instance.commands._validate "${z_cls_wrappers[@]}" || return 1 - z.cls.owner._bind "$z_cls_id" "$z_cls_binding" || return 1 - - z.cls.instance.commands._install "${z_cls_wrappers[@]}" + z.cls.instance.commands._install "${z_cls_wrappers[@]}" || return 1 + if ! z.cls.owner._bind "$z_cls_id" "$z_cls_binding"; then + z.cls.instance.commands._uninstall "${z_cls_wrappers[@]}" + return 1 + fi z_cls_members[$z_cls_id]="${(j: :)z_cls_wrappers}" z_cls_instances[$z_cls_id]=$z_cls_class @@ -256,12 +258,14 @@ z.cls.delete() { z.int.is.eq $# 1 || return 1 - local z_cls_id=${(P)1:-} + local z_cls_ref=${(P)1:-} + [[ $1 == z_cls_* ]] && z_cls_ref=$1 + local z_cls_id=${z_cls_references[$z_cls_ref]:-$z_cls_ref} - z.is.not.null "${z_cls_instances[$1]:-}" && z_cls_id=$1 + z.cls.instance.call._validate "$z_cls_id" "$z_cls_ref" write || return 1 - if z.is.null "${z_cls_instances[$z_cls_id]:-}" || z.is.not.null "${z_cls_active[$z_cls_id]:-}"; then - z.io.error "Expected an existing, inactive instance reference." + if [[ -n ${z_cls_active[$z_cls_id]-} ]]; then + z.io.error "Expected an inactive instance reference." return 1 fi diff --git a/lib/cls/store/process.zsh b/lib/cls/store/process.zsh index 639045e8..586aef5a 100644 --- a/lib/cls/store/process.zsh +++ b/lib/cls/store/process.zsh @@ -11,7 +11,7 @@ z.cls.store._ensure() { if z.is.eq "${z_cls_store_ready:-0}" 1 && z.var.exists z_cls_dispatch_programs && z.var.exists z_cls_owner_markers && - z.var.exists z_cls_scalars; then + z.var.exists z_cls_scalars && z.var.exists z_cls_references; then return 0 fi @@ -30,6 +30,10 @@ z.cls.store._ensure() { z.var.exists z_cls_next_id || typeset -gi z_cls_next_id=0 z.var.exists z_cls_owner_markers || typeset -gA z_cls_owner_markers z.var.exists z_cls_next_owner_id || typeset -gi +x z_cls_next_owner_id=0 + z.var.exists z_cls_next_reference || typeset -gi +x z_cls_next_reference=0 + z.var.exists z_cls_references || typeset -gA z_cls_references + z.var.exists z_cls_owner_refs || typeset -gA z_cls_owner_refs + z.var.exists z_cls_borrow_counts || typeset -gA z_cls_borrow_counts z.var.exists z_cls_gc_allocations || typeset -gi +x z_cls_gc_allocations=0 z.var.exists z_cls_active || typeset -gA z_cls_active z.var.exists z_cls_drawing || typeset -g +x z_cls_drawing='' diff --git a/test/lib/cls/instance/call/process_test.zsh b/test/lib/cls/instance/call/process_test.zsh index 4ef11e50..66a7015b 100644 --- a/test/lib/cls/instance/call/process_test.zsh +++ b/test/lib/cls/instance/call/process_test.zsh @@ -4,6 +4,8 @@ z.t.describe "z.cls.instance.call._validate"; { z.t.context "生存中の実体の場合"; { z.t.it "実行を許可する"; { z.cls.store._ensure + local validate_marker="" + local -A z_cls_owner_markers=([validate_ref]=validate_marker) z_cls_instances[validate_ref]=ValidateCall z.cls.instance.call._validate validate_ref @@ -28,6 +30,8 @@ z.t.describe "z.cls.instance.call._validate"; { z.t.context "別の実体の呼出しから再入する場合"; { z.t.it "実行中の実体を保護する"; { z.cls.store._ensure + local reentry_marker="" + local -A z_cls_owner_markers=([reentry_ref]=reentry_marker) z_cls_instances[reentry_ref]=ValidateReentry z_cls_active[reentry_ref]=1 z.t.mock name=z.io.error diff --git a/test/lib/cls/owner/process_test.zsh b/test/lib/cls/owner/process_test.zsh index f179f893..9fdc31fa 100644 --- a/test/lib/cls/owner/process_test.zsh +++ b/test/lib/cls/owner/process_test.zsh @@ -23,6 +23,45 @@ z.t.describe "z.cls.owner.move"; { } } + z.t.context "移動元より移動先のスコープが短い場合"; { + z.t.it "古い参照を即座に失効させ、移動先の終了後も呼び出しを拒否する"; { + () { + z.cls.draw OwnerLifetime + z.cls.attr count default=5 + z.cls.fn value '$self.count.get' + z.cls.drew + + local counter="" escaped="" + z.cls.new OwnerLifetime counter || return 1 + local original=$counter + + () { + local saved="" + z.cls.owner.move from=counter to=saved || return 1 + escaped=$saved + + $saved.count.set 7 + $saved.value + z.t.expect.reply 7 + + z.t.expect "$counter" "$original" + [[ $saved != "$counter" ]] + z.t.expect.status.is.true + + local old_command=$counter.value + z.t.expect "${+functions[$old_command]}" 0 + + z.cls.delete "$original" 2>/dev/null + z.t.expect.status.is.false + } + + $escaped.value 2>/dev/null + z.t.expect.status.is.false + z.t.expect "${+z_cls_instances[$original]}" 1 + } + } + } + z.t.context "借用した変数を移動元にした場合"; { z.t.it "既存の所有者を残して拒否する"; { () { diff --git a/test/lib/cls/process_test.zsh b/test/lib/cls/process_test.zsh index bbe78583..be6b7bfc 100644 --- a/test/lib/cls/process_test.zsh +++ b/test/lib/cls/process_test.zsh @@ -643,7 +643,7 @@ z.t.describe "z.cls.delete"; { z.cls.new TestCounter object || return 1 z.cls.owner.move from=object "to=owner_$index" || return 1 - saved_refs+=("$object") + saved_refs+=("$REPLY") } z.t.expect.status.is.true From 1a061bb1bea3eb50570f6bd5c4666ad0e71bb143 Mon Sep 17 00:00:00 2001 From: milkeclair Date: Mon, 23 Nov 2026 12:47:08 +0900 Subject: [PATCH 09/11] feat: scope read-only class borrows to callbacks --- lib/cls/README.md | 22 +++++++-- lib/cls/gc/process.zsh | 2 +- lib/cls/instance/analysis.zsh | 11 +++-- lib/cls/instance/commands/process.zsh | 1 + lib/cls/process.zsh | 55 ++++++++++++++++++++++ test/lib/cls/process_test.zsh | 66 +++++++++++++++++++++++++++ 6 files changed, 149 insertions(+), 8 deletions(-) diff --git a/lib/cls/README.md b/lib/cls/README.md index 0733312d..21c08587 100644 --- a/lib/cls/README.md +++ b/lib/cls/README.md @@ -87,7 +87,7 @@ $counter.count.get print -r -- "$REPLY" # 10 ``` -続けて参照をコピーすると、同じ実体の状態を共有できます。コピーは借用参照で、実体の寿命を延ばしません。 +続けて参照をコピーすると、同じ実体の状態を共有できます。コピーは同じ呼び出し用IDを保持します。実体の寿命は所有者に従います。 参照を展開したコマンド名は、その参照を持つ変数からメソッドや属性を呼び出します。 ```zsh @@ -105,7 +105,7 @@ print -r -- "$REPLY" # 11 削除後も参照変数の値とreadonly属性は残りますが、その実体への参照は全て無効になります。 所有変数のスコープを抜けた実体は、前回の回収から64件生成された後の次のnewで自動回収します。 実行中の実体は、メソッドが終了した後に回収できます。 -属性や配列に入れた実体IDも借用参照で、実体の寿命は所有変数に従います。 +所有者がスコープを抜けた時点で、残っている参照からのアクセスを拒否します。 実体を呼出元に渡す場合は、所有権を宣言済みの変数へ移します。 @@ -125,7 +125,23 @@ example() { } ``` -移動元に残る値は借用参照です。移動先からは実体IDを展開して呼び出します。 +移動先は同じ実体に対する新しい参照を受け取ります。移動前の参照とそのコピーは移動直後に失効します。 + +## 読み取り専用の借用 + +`z.cls.borrow from=saved read_count` で、関数の実行中だけ使える読み取り専用の参照を渡せます。 + +```zsh +read_count() { + local -r borrowed=$1 + $borrowed.count.get +} + +z.cls.borrow from=saved read_count +# REPLY: 5 +``` + +借用参照から呼べるのは属性の getter です。借用中は所有者側も実体の更新、メソッド呼び出し、所有権の移動、削除ができません。関数から戻ると借用参照とそのコピーが失効し、所有者は再び実体を更新できます。 ## 定義と呼出しの規則 diff --git a/lib/cls/gc/process.zsh b/lib/cls/gc/process.zsh index dcdb93ce..3a8b000d 100644 --- a/lib/cls/gc/process.zsh +++ b/lib/cls/gc/process.zsh @@ -18,7 +18,7 @@ z.cls.gc._collect() { z.hash.keys z_cls_owner_markers for z_cls_gc_id in "${REPLY[@]}"; do z.var.exists "$z_cls_owner_markers[$z_cls_gc_id]" && continue - z.is.not.null "${z_cls_active[$z_cls_gc_id]:-}" && continue + [[ -n ${z_cls_active[$z_cls_gc_id]-} || -n ${z_cls_borrow_counts[$z_cls_gc_id]-} ]] && continue z.cls.instance._release "$z_cls_gc_id" done diff --git a/lib/cls/instance/analysis.zsh b/lib/cls/instance/analysis.zsh index 9123ef1d..5951c764 100644 --- a/lib/cls/instance/analysis.zsh +++ b/lib/cls/instance/analysis.zsh @@ -2,6 +2,7 @@ # # $1: クラス名 # $2: 実体参照 +# $3?: getの場合は属性の読み取り用コマンドだけを列挙 # REPLY: コマンド名の配列。メンバーがない場合は空配列 # return: 0 # @@ -16,13 +17,15 @@ z.cls.instance._commands() { local -a z_cls_wrappers=() local z_cls_member - for z_cls_member in ${=z_cls_class_methods[$z_cls_class]}; do - z_cls_wrappers+=("$z_cls_id.$z_cls_member") - done + if [[ ${3:-} != get ]]; then + for z_cls_member in ${=z_cls_class_methods[$z_cls_class]}; do + z_cls_wrappers+=("$z_cls_id.$z_cls_member") + done + fi for z_cls_member in ${=z_cls_class_attrs[$z_cls_class]}; do z_cls_wrappers+=("$z_cls_id.$z_cls_member.get") - z_cls_wrappers+=("$z_cls_id.$z_cls_member.set") + [[ ${3:-} == get ]] || z_cls_wrappers+=("$z_cls_id.$z_cls_member.set") done REPLY=("${z_cls_wrappers[@]}") diff --git a/lib/cls/instance/commands/process.zsh b/lib/cls/instance/commands/process.zsh index 96339b1f..7a055561 100644 --- a/lib/cls/instance/commands/process.zsh +++ b/lib/cls/instance/commands/process.zsh @@ -89,6 +89,7 @@ z.cls.instance.commands._install() { # z.cls.instance.commands._uninstall z_cls_12345_1.increment # # 変更後: z_cls_12345_1.incrementの生成済みコマンドを削除 z.cls.instance.commands._uninstall() { + local REPLY="" local z_cls_command for z_cls_command in "$@"; do diff --git a/lib/cls/process.zsh b/lib/cls/process.zsh index 7e0803df..add6b7a2 100644 --- a/lib/cls/process.zsh +++ b/lib/cls/process.zsh @@ -240,6 +240,61 @@ z.cls.new() { REPLY=$z_cls_id } +# 指定した関数の実行中だけ、実体の属性を読み取れる参照を渡す。 +# +# $from: 現在の所有変数名 +# $1: 借用参照を第一引数で受け取る関数名 +# REPLY: 関数の結果 +# return: 関数の終了コード。借用を開始できない場合は1 +# +# example: +# # 前提: savedはcount=7の実体を所有している +# read_count() { local borrowed=$1; $borrowed.count.get } +# z.cls.borrow from=saved read_count +# # REPLY: 7 +z.cls.borrow() { + z.cls.store._ensure + + z.int.is.eq $# 2 || return 1 + z.arg.named from "$@" && local z_cls_from=$REPLY + z.arg.named.shift from "$@" + local z_cls_callback=$REPLY[1] + if ! z.cls.is._attribute "$z_cls_from" || ! z.var.exists "functions[$z_cls_callback]"; then + z.io.error "Expected an owner and a callback function." + return 1 + fi + + local z_cls_owner=${(P)z_cls_from:-} + local z_cls_id=${z_cls_references[$z_cls_owner]:-$z_cls_owner} + if ! z.cls.owner._is_current "$z_cls_from" "$z_cls_owner" || + [[ -n ${z_cls_active[$z_cls_id]-} ]]; then + z.io.error "Expected the owner of an inactive instance." + return 1 + fi + + local z_cls_ref="z_cls_ref_${$}_$(( ++z_cls_next_reference ))" + z.cls.instance._commands "$z_cls_instances[$z_cls_id]" "$z_cls_ref" get + local -a z_cls_commands=("${(@)REPLY}") + + z.cls.instance.commands._validate "${z_cls_commands[@]}" || return 1 + z.cls.instance.commands._install "${z_cls_commands[@]}" || return 1 + + z_cls_references[$z_cls_ref]=$z_cls_id + z_cls_borrow_counts[$z_cls_id]=$(( ${z_cls_borrow_counts[$z_cls_id]:-0} + 1 )) + local z_cls_result=0 + { + "$z_cls_callback" "$z_cls_ref" + } always { + z_cls_result=$? + unset "z_cls_references[$z_cls_ref]" + z.cls.instance.commands._uninstall "${z_cls_commands[@]}" + z_cls_borrow_counts[$z_cls_id]=$(( z_cls_borrow_counts[$z_cls_id] - 1 )) + (( z_cls_borrow_counts[$z_cls_id] )) || unset "z_cls_borrow_counts[$z_cls_id]" + } + + return $z_cls_result +} + # 実体、生成したコマンド、保存済みの属性を削除する。 # # $1: 実体の変数名または参照 diff --git a/test/lib/cls/process_test.zsh b/test/lib/cls/process_test.zsh index be6b7bfc..ff0d80c2 100644 --- a/test/lib/cls/process_test.zsh +++ b/test/lib/cls/process_test.zsh @@ -521,6 +521,72 @@ z.t.describe "z.cls.new"; { } } +z.t.describe "z.cls.borrow"; { + z.t.context "読み取り専用で貸し出す場合"; { + z.t.it "借用中だけgetterを使え、所有者側の変更も拒否する"; { + () { + z.cls.draw ReadOnlyBorrow + z.cls.attr count default=5 + z.cls.fn value '$self.count.get' + z.cls.drew + + local owner="" destination="" escaped="" + z.cls.new ReadOnlyBorrow owner || return 1 + read_borrowed_count() { + local borrowed=$1 + escaped=$borrowed + local setter=$borrowed.count.set + z.t.expect "${+functions[$setter]}" 0 + + $owner.count.set 7 2>/dev/null + z.t.expect.status.is.false + $owner.value 2>/dev/null + z.t.expect.status.is.false + z.cls.owner.move from=owner to=destination 2>/dev/null + z.t.expect.status.is.false + z.cls.delete owner 2>/dev/null + z.t.expect.status.is.false + + $borrowed.count.get || return 1 + return 7 + } + + local result=0 + z.cls.borrow from=owner read_borrowed_count || result=$? + local reply=$REPLY + z.t.expect "$result" 7 + z.t.expect "$reply" 5 + + local getter=$escaped.count.get + z.t.expect "${+functions[$getter]}" 0 + z.t.expect "${+z_cls_references[$escaped]}" 0 + + $owner.count.set 8 + $owner.value + z.t.expect.reply 8 + z.cls.delete owner + unfunction read_borrowed_count + } + } + } + + z.t.context "コールバックを特定できない場合"; { + z.t.it "所有者の更新権限を保ったまま失敗する"; { + () { + local owner="" + z.cls.new TestCounter owner || return 1 + z.cls.borrow from=owner missing_borrow_callback 2>/dev/null + z.t.expect.status.is.false + + $owner.count.set 6 + $owner.count.get + z.t.expect.reply 6 + z.cls.delete owner + } + } + } +} + z.t.describe "z.cls.delete"; { z.t.context "errexitが有効な場合"; { z.t.it "errexit下で生成した関数と状態を削除する"; { From 5d5fe6e18b7e1ba08af90884db1bae8942fe5712 Mon Sep 17 00:00:00 2001 From: milkeclair Date: Mon, 23 Nov 2026 12:56:30 +0900 Subject: [PATCH 10/11] test: cover nested read-only class borrows --- test/lib/cls/process_test.zsh | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/lib/cls/process_test.zsh b/test/lib/cls/process_test.zsh index ff0d80c2..39163dc4 100644 --- a/test/lib/cls/process_test.zsh +++ b/test/lib/cls/process_test.zsh @@ -570,6 +570,42 @@ z.t.describe "z.cls.borrow"; { } } + z.t.context "借用中に同じ実体をもう一度貸し出す場合"; { + z.t.it "内側の借用だけを終了し、外側が戻るまで所有者の更新を拒否する"; { + () { + local owner="" escaped="" + z.cls.new TestCounter owner || return 1 + + read_inner_borrow() { + local borrowed=$1 + escaped=$borrowed + $borrowed.count.get + } + + read_outer_borrow() { + local borrowed=$1 + z.cls.borrow from=owner read_inner_borrow || return 1 + local inner_getter=$escaped.count.get + z.t.expect "${+functions[$inner_getter]}" 0 + + $owner.count.set 9 2>/dev/null + z.t.expect.status.is.false + + $borrowed.count.get + } + + z.cls.borrow from=owner read_outer_borrow + z.t.expect.reply 0 + + $owner.count.set 9 + $owner.count.get + z.t.expect.reply 9 + z.cls.delete owner + unfunction read_outer_borrow read_inner_borrow + } + } + } + z.t.context "コールバックを特定できない場合"; { z.t.it "所有者の更新権限を保ったまま失敗する"; { () { From e1dca79db51831703ee02f3a1669fb5e0b1c1f4a Mon Sep 17 00:00:00 2001 From: milkeclair Date: Mon, 23 Nov 2026 12:58:07 +0900 Subject: [PATCH 11/11] test: include class borrow in public completion --- test/mod/completion/cache/build/process_test.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mod/completion/cache/build/process_test.zsh b/test/mod/completion/cache/build/process_test.zsh index 2c2e9256..00206ba1 100644 --- a/test/mod/completion/cache/build/process_test.zsh +++ b/test/mod/completion/cache/build/process_test.zsh @@ -2,7 +2,7 @@ source ${z_main} z.t.describe "z.completion.cache.build._names"; { z.t.context "補完の関数名キャッシュを構築する場合"; { - z.t.it "公開DSLの8関数だけを候補にする"; { + z.t.it "公開DSLだけを候補にする"; { source "$z_root/mod/completion/function/is/operator.zsh" source "$z_root/mod/completion/cache/build/process.zsh" local -a z_completion_function_names=() @@ -18,7 +18,7 @@ z.t.describe "z.completion.cache.build._names"; { REPLY=("${class_names[@]}") - z.t.expect.reply.is.arr z.cls.abort z.cls.attr z.cls.delete z.cls.draw z.cls.drew z.cls.fn z.cls.fn.done z.cls.new z.cls.owner.move + z.t.expect.reply.is.arr z.cls.abort z.cls.attr z.cls.borrow z.cls.delete z.cls.draw z.cls.drew z.cls.fn z.cls.fn.done z.cls.new z.cls.owner.move } }