diff --git a/lib/cls/README.md b/lib/cls/README.md index 5e9b71cf..21c08587 100644 --- a/lib/cls/README.md +++ b/lib/cls/README.md @@ -26,19 +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 @@ -62,30 +63,31 @@ setterが成功すると、その時点で保存値へ反映します。直後 ## 参照と変数のスコープ `z.cls.new Class variable`は、指定した変数と`REPLY`へ実体参照を代入し、 -指定した変数をreadonlyにします。参照を持つ変数は通常のZshのスコープに従います。 +指定した変数をreadonlyにし、実体の唯一の所有変数として登録します。変数は呼出元で先に宣言します。 同じ変数への再代入や、同じ変数を使った実体の再生成は拒否されます。 以下の例は、それぞれの前の実行例とは独立したシェルで、Counterの定義を 読み込んだ状態から実行します。ローカルな参照を作る場合は、呼出元で変数をlocal宣言します。 ```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 ``` -続けて参照をコピーすると、同じ実体の状態を共有できます。 +続けて参照をコピーすると、同じ実体の状態を共有できます。コピーは同じ呼び出し用IDを保持します。実体の寿命は所有者に従います。 参照を展開したコマンド名は、その参照を持つ変数からメソッドや属性を呼び出します。 ```zsh @@ -95,14 +97,51 @@ typeset -r other=$counter print -r -- "$REPLY" # 11 ``` -短い形式の`counter.increment`は、呼出し時に見えるcounter変数から実体を解決します。 -`"$other.increment"`は、コピー済みの参照から直接呼び出します。 +`$counter.increment`と`$other.increment`は、それぞれの変数に保存した実体参照を展開して呼び出します。 新しい実体には、内側のスコープで宣言した同名localなど、新しい代入先が必要です。 コピーした参照も再代入から保護する場合は、`typeset -r`または`local -r`を使います。 `z.cls.delete variable`または`z.cls.delete "$reference"`で実体を解放します。 削除後も参照変数の値とreadonly属性は残りますが、その実体への参照は全て無効になります。 -変数のスコープを抜けるだけでは実体の状態を解放しないため、使い終わったら明示的に削除します。 +所有変数のスコープを抜けた実体は、前回の回収から64件生成された後の次のnewで自動回収します。 +実行中の実体は、メソッドが終了した後に回収できます。 +所有者がスコープを抜けた時点で、残っている参照からのアクセスを拒否します。 + +実体を呼出元に渡す場合は、所有権を宣言済みの変数へ移します。 + +```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 +} +``` + +移動先は同じ実体に対する新しい参照を受け取ります。移動前の参照とそのコピーは移動直後に失効します。 + +## 読み取り専用の借用 + +`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 です。借用中は所有者側も実体の更新、メソッド呼び出し、所有権の移動、削除ができません。関数から戻ると借用参照とそのコピーが失効し、所有者は再び実体を更新できます。 ## 定義と呼出しの規則 @@ -133,7 +172,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..3a8b000d --- /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 + [[ -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 + + return 0 +} diff --git a/lib/cls/instance/analysis.zsh b/lib/cls/instance/analysis.zsh index 8e6baffc..5951c764 100644 --- a/lib/cls/instance/analysis.zsh +++ b/lib/cls/instance/analysis.zsh @@ -1,33 +1,31 @@ -# 実体の変数名と参照から呼べるコマンド名を列挙する。 +# 実体IDで呼び出すメソッドと属性のコマンドを列挙する。 # # $1: クラス名 -# $2: 実体の参照を持つ変数名 -# $3: 実体参照 +# $2: 実体参照 +# $3?: getの場合は属性の読み取り用コマンドだけを列挙 # 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 + if [[ ${3:-} != get ]]; then for z_cls_member in ${=z_cls_class_methods[$z_cls_class]}; do - z_cls_wrappers+=("$z_cls_prefix.$z_cls_member") + 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_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") + [[ ${3:-} == get ]] || z_cls_wrappers+=("$z_cls_id.$z_cls_member.set") done REPLY=("${z_cls_wrappers[@]}") diff --git a/lib/cls/instance/call/analysis.zsh b/lib/cls/instance/call/analysis.zsh new file mode 100644 index 00000000..c1aaf949 --- /dev/null +++ b/lib/cls/instance/call/analysis.zsh @@ -0,0 +1,34 @@ +# 実体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() { + local z_cls_id=${z_cls_references[$1]:-$1} + z.cls.instance.call._validate "$z_cls_id" "$1" || return 1 + + local z_cls_key="$z_cls_id:$2" + if (( ${+z_cls_values[$z_cls_key]} )); then + z.cls.instance.call._access "$z_cls_id" 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[$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 faa66f67..bb02c56c 100644 --- a/lib/cls/instance/call/process.zsh +++ b/lib/cls/instance/call/process.zsh @@ -1,30 +1,31 @@ -# 呼出しや属性の復元に進む前に、解決済みの処理を検証する。 +# 生存中の実体かを確認し、他の実体の呼び出しからの再入を拒否する。 # # $1: 解決済みの実体参照 -# $2: method、get、setのいずれか -# $3: メンバー名 -# $4: エラーに表示する、元の変数名または参照 +# $2?: 呼び出しに使った参照 +# $3?: writeの場合は更新できる所有者参照に限る # 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." + 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 - 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 + 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 if z.is.not.null "${z_cls_active[$z_cls_instance]:-}" && @@ -36,7 +37,38 @@ z.cls.instance.call._validate() { return 0 } -# 保存済みの通常スカラーを直接扱い、それ以外は属性のスコープを復元して処理する。 +# 実体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() { + 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="$z_cls_id:$2" + if (( ${+z_cls_values[$z_cls_key]} )); then + 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[$z_cls_id]}:$2" + (( ${+z_cls_defaults[$z_cls_default_key]} )) || return 1 + + z_cls_scalars[$z_cls_key]=$3 +} + +# 十進整数は保存形式から直接処理し、他の宣言は属性を復元して処理する。 # # $1: 実体参照 # $2: getまたはset @@ -46,7 +78,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 @@ -54,44 +86,33 @@ 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 + local z_cls_declaration=$z_cls_values[$z_cls_key] - local z_cls_literal=${z_cls_declaration#*=} + 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 - 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.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" "$@" } @@ -114,19 +135,26 @@ 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 + 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]-${z_cls_defaults[$z_cls_class:$z_cls_attribute]}}" + fi done case $z_cls_operation in @@ -154,10 +182,11 @@ z.cls.instance.call._access_typed() { # # return: 7 # # 変更後: countは4、実行中フラグは解除済み z.cls.instance.call._run() { - 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 @@ -166,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 cd6ca45f..7a055561 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,11 +39,12 @@ 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 -a z_cls_installed=() local z_cls_command for z_cls_command in "$@"; do @@ -58,13 +59,49 @@ 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}"' "$@"' + 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 - 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 REPLY="" + 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 c42d926c..6608f0bc 100644 --- a/lib/cls/instance/process.zsh +++ b/lib/cls/instance/process.zsh @@ -13,20 +13,29 @@ # # 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 - 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 + [[ $1 == z_cls_* ]] && z_cls_instance=$1 + if z.is.null "$z_cls_instance"; then + z.io.error "Instance reference $1 is not available in this scope." + 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,20 +54,22 @@ 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トラップもメソッド引数で動くよう、本体専用のスコープを作る。 () { - local -r self=$z_cls_instance + local -r self=${z_cls_owner_refs[$z_cls_instance]:-$z_cls_instance} eval "$z_cls_body" } "$@" } -# 復元済みの属性を、同じ型で再作成できる宣言として保存する。 -# 通常の値はパラメータ展開を使い、特殊な属性フラグだけtypesetで取得する。 +# 復元済みの属性を保存する。通常のスカラーは文字列、型付き属性は宣言として保持する。 # # $1: 実体参照 # $2: 属性名 @@ -75,24 +86,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 @@ -131,26 +144,32 @@ z.cls.instance._snapshot() { # # 変更後: 実体と、参照がなくなった生成コマンドを解放済み z.cls.instance._release() { local z_cls_id=$1 - local z_cls_command - - 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 )) + local z_cls_ref=${z_cls_owner_refs[$z_cls_id]:-$z_cls_id} - 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 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要素も管理ノードを残すため、一緒に削除する。 - 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_scalars=() + z_cls_values=() + z_cls_members=() + 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/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..334f4945 --- /dev/null +++ b/lib/cls/owner/operator.zsh @@ -0,0 +1,63 @@ +# 宣言済みの変数が所有参照を受け取れるか検証する。 +# +# $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:-} + 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]:-}" +} + +# 指定した変数が実体の現在の所有変数か検証する。 +# +# $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_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 + + 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..0d558280 --- /dev/null +++ b/lib/cls/owner/process.zsh @@ -0,0 +1,77 @@ +# 実体の所有権を別の宣言済み変数へ移す。 +# +# $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_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 + + 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 +# +# 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[${3:-$1}]=$z_cls_marker +} diff --git a/lib/cls/process.zsh b/lib/cls/process.zsh index c93d102a..add6b7a2 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 @@ -221,28 +222,79 @@ 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}") z.cls.instance.commands._validate "${z_cls_wrappers[@]}" || 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.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 - : ${(P)z_cls_binding::=$z_cls_id} - typeset -gr -- "$z_cls_binding" + (( ++z_cls_gc_allocations )) 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: 実体の変数名または参照 @@ -261,26 +313,18 @@ 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 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..586aef5a 100644 --- a/lib/cls/store/process.zsh +++ b/lib/cls/store/process.zsh @@ -7,9 +7,13 @@ # # 前提: 保存領域は未初期化 # 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() { - 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 && + z.var.exists z_cls_scalars && z.var.exists z_cls_references; then + return 0 + fi zmodload zsh/parameter z.var.exists z_cls_classes || typeset -gA z_cls_classes @@ -18,11 +22,19 @@ 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 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_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='' z.var.exists z_cls_capture_method || typeset -g +x z_cls_capture_method='' @@ -30,12 +42,11 @@ z.cls.store._ensure() { typeset -gi +x z_cls_store_ready=1 } -# 通常のスカラーと十進整数を、復元可能な宣言へ引用して保存する。 +# 実体のスカラー属性を文字列として保存する。 # # $1: 実体参照 # $2: 属性名 # $3: 保存する値 -# $4?: 宣言フラグ(既定: --、整数は-i) # REPLY: null # return: 0 # @@ -43,11 +54,10 @@ z.cls.store._ensure() { # # 前提: 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/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 8c2fdffb..98e2bd1a 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 +$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..8297160d 100644 --- a/test/lib/cls/fixtures/method-errexit.zsh +++ b/test/lib/cls/fixtures/method-errexit.zsh @@ -12,7 +12,8 @@ z.cls.draw StopEarly z.cls.fn.done 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 new file mode 100644 index 00000000..801a7567 --- /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_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 + 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_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 + 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/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/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 263af568..66a7015b 100644 --- a/test/lib/cls/instance/call/process_test.zsh +++ b/test/lib/cls/instance/call/process_test.zsh @@ -1,17 +1,18 @@ source ${z_main} z.t.describe "z.cls.instance.call._validate"; { - z.t.context "公開済みのメソッドの場合"; { + 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_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 +21,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 } @@ -29,20 +30,47 @@ 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_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._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 + } } } } @@ -60,12 +88,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 +140,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 +165,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 +175,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 +259,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 +284,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 +321,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 +336,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 +361,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 +370,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 +410,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 +459,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 +494,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 +522,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 +532,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 +560,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 +594,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 +624,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/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]' diff --git a/test/lib/cls/instance/process_test.zsh b/test/lib/cls/instance/process_test.zsh index d7da5893..d5f0d9fe 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 @@ -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]' } } } @@ -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 398206bf..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 @@ -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..9fdc31fa --- /dev/null +++ b/test/lib/cls/owner/process_test.zsh @@ -0,0 +1,124 @@ +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 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 "既存の所有者を残して拒否する"; { + () { + 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..39163dc4 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 @@ -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=() @@ -179,9 +215,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 +242,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 +263,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 +289,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 +317,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 +342,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 +377,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 +392,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 +402,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 +414,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 +429,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 +442,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 +487,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,29 +497,132 @@ z.t.describe "z.cls.new"; { } } - z.t.context "作成先のコマンド名に既存の関数がある場合"; { - z.t.it "既存の関数を保ち、実体のコマンドを部分的に残さない"; { + z.t.context "参照変数と同じ接頭辞を持つ関数がある場合"; { + z.t.it "既存の関数を保ち、実体ID付きの関数だけを登録する"; { first.value() { REPLY=original } - z.cls.new TestCounter first 2>/dev/null - - z.t.expect.status.is.false + local first="" + 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 } } } +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="" 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 "所有者の更新権限を保ったまま失敗する"; { + () { + 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下で生成した関数と状態を削除する"; { @@ -492,9 +631,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 @@ -521,7 +660,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 @@ -545,7 +684,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 @@ -561,21 +700,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" } } @@ -583,14 +720,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" } } } @@ -602,15 +739,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 - - saved_refs+=("$object") + z.cls.owner.move from=object "to=owner_$index" || return 1 + saved_refs+=("$REPLY") } + + z.t.expect.status.is.true done local reference @@ -627,9 +765,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" @@ -637,7 +774,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true done - object.increment + "$replaced" z.t.expect.reply custom @@ -651,7 +788,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true - fresh.increment + $fresh.increment z.t.expect.reply 1 @@ -659,7 +796,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true - unfunction object.increment + unfunction "$replaced" } } } @@ -684,15 +821,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 @@ -703,7 +840,7 @@ z.t.describe "z.cls.delete"; { z.t.expect.status.is.true - first.value + $first.value z.t.expect.reply 0 diff --git a/test/lib/cls/store/process_test.zsh b/test/lib/cls/store/process_test.zsh index b0399fcf..a750be5e 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 @@ -50,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]' } } } diff --git a/test/mod/completion/cache/build/process_test.zsh b/test/mod/completion/cache/build/process_test.zsh index a433c1e8..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.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 } }