summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGianni Ceccarelli <gianni.ceccarelli@broadbean.com>2023-07-14 11:56:20 +0100
committerGianni Ceccarelli <gianni.ceccarelli@broadbean.com>2023-07-14 11:56:20 +0100
commitdc627de951cb666b123b5b23c1e040dc9f77e696 (patch)
tree8cc4e0b93b497b9cf28fc61a3a8dc036f178ed95
parentflailing about with MOPs (diff)
downloadbash-object-system-dc627de951cb666b123b5b23c1e040dc9f77e696.tar.gz
bash-object-system-dc627de951cb666b123b5b23c1e040dc9f77e696.tar.bz2
bash-object-system-dc627de951cb666b123b5b23c1e040dc9f77e696.zip
method dispatch seems to work
-rw-r--r--bos-dispatch.sh2
-rw-r--r--bos-mop.sh14
-rw-r--r--bos-namespaces.sh22
-rw-r--r--test.sh28
4 files changed, 53 insertions, 13 deletions
diff --git a/bos-dispatch.sh b/bos-dispatch.sh
index 47b5f33..cb883d8 100644
--- a/bos-dispatch.sh
+++ b/bos-dispatch.sh
@@ -15,7 +15,7 @@ function bos-dispatch/invoke() {
fi
local metaclass_ref
- bos-namespaces/store-for-into meta "$class" metaclass_ref
+ bos-namespaces/store-scalar-for-into meta "$class" metaclass_ref
local -n metaclass_object="$metaclass_ref"
$metaclass_object invoke "$class" "$self_id" "$method" "$@"
diff --git a/bos-mop.sh b/bos-mop.sh
index f9faef2..e18baec 100644
--- a/bos-mop.sh
+++ b/bos-mop.sh
@@ -1,14 +1,12 @@
#!bash
-bos_class__bos__mop__base__meta="bos-dipatch/invoke bos/mop/base 0"
+bos_namespaces__bos__mop__base__meta="bos-dipatch/invoke bos/mop/base 0"
+declare -a bos_namespaces__bos__mop__base__mro=( "bos/mop/base" )
# these are instance methods of the base metaclass
function bos/mop/base/mro-for-into() {
- local name="bos_mop__mro__${1//\//__}"
- local -n result="$2"
- declare -ga "$name"
- result="$name"
+ bos-namespaces/store-array-for-into mro "$1" "$2"
}
# find the first class (in MRO) that defines a given method
@@ -22,14 +20,14 @@ function bos/mop/base/find-method-into() {
local method="$3"
local -n result="$4"
- local mro_store; $self mro-for-into mro_store
+ local mro_store; $self mro-for-into "$class" mro_store
local -n mro="$mro_store"
local idx=0
# if we don't have a $start_from_class, it means we're doing the
# initial method resolution, so don't skip any class in $mro
if [[ -n "$start_from_class" ]]; then
- for (( ; idx < "${#mro}" ; ++idx )); do
+ for (( ; idx <= "${#mro}" ; ++idx )); do
if [[ "${mro[$idx]}" == "$start_from_class" ]]; then
(( ++idx ))
break
@@ -37,7 +35,7 @@ function bos/mop/base/find-method-into() {
done
fi
- for (( ; idx < "${#mro}" ; ++idx )); do
+ for (( ; idx <= "${#mro}" ; ++idx )); do
local full_name="${mro[$idx]}/${method}"
if declare -F "$full_name" >/dev/null; then
result="${full_name}"
diff --git a/bos-namespaces.sh b/bos-namespaces.sh
index 3e9e711..9fbbb08 100644
--- a/bos-namespaces.sh
+++ b/bos-namespaces.sh
@@ -1,9 +1,23 @@
#!bash
## namespace handling
-function bos-namespaces/store-for-into() {
+function bos-namespaces/store-scalar-for-into() {
local name="bos_namespaces__${2//[^[:word:]/__}__${1//[^[:word:]]/__}"
- local -n result="$2"
+ local -n result="$3"
+ declare -g "$name"
+ result="$name"
+}
+
+function bos-namespaces/store-array-for-into() {
+ local name="bos_namespaces__${2//[^[:word:]/__}__${1//[^[:word:]]/__}"
+ local -n result="$3"
+ declare -ga "$name"
+ result="$name"
+}
+
+function bos-namespaces/store-dict-for-into() {
+ local name="bos_namespaces__${2//[^[:word:]/__}__${1//[^[:word:]]/__}"
+ local -n result="$3"
declare -gA "$name"
result="$name"
}
@@ -11,7 +25,7 @@ function bos-namespaces/store-for-into() {
# save all currently-visible functions in an associative array
function bos-namespaces/start() {
local namespace="$1";shift
- local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store
+ local ns_store; bos-namespaces/store-dict-for-into saved_funcs "$namespace" ns_store
local -n saved_funcs="$ns_store"
local _ funcname
@@ -27,7 +41,7 @@ function bos-namespaces/start() {
function bos-namespaces/list-new-funcs-into() {
local namespace="$1";shift
local -n result="$1"
- local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store
+ local ns_store; bos-namespaces/store-dict-for-into saved_funcs "$namespace" ns_store
local -n saved_funcs="$ns_store"
local _ funcname new_function
diff --git a/test.sh b/test.sh
new file mode 100644
index 0000000..6e7e49c
--- /dev/null
+++ b/test.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+. bos-namespaces.sh
+. bos-object-id.sh
+. bos-mop.sh
+. bos-dispatch.sh
+
+bos_namespaces__A__meta="bos-dispatch/invoke bos/mop/base 0"
+bos_namespaces__A__mro=( "A" )
+
+function A/thing() {
+ echo "<$self> A/thing ($*)"
+}
+
+bos_namespaces__B__meta="bos-dispatch/invoke bos/mop/base 0"
+bos_namespaces__B__mro=( "B" "A" )
+
+function B/other() {
+ echo "<$self> B/other ($*)"
+}
+
+bos-object-id/pack-self-into A 0 objA
+bos-object-id/pack-self-into B 0 objB
+
+$objA thing 1 2 3
+$objB thing 4 5 6
+$objB other 7 8 9
+