summaryrefslogtreecommitdiff
path: root/bos-dispatch.sh
blob: 848976589ecae23ac751e1d0481d2cb5eebfdf70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!bash 
 
# return the closest non-BOS function name from the call stack 
function bos-dispatch/caller-into() {
    local -n bos_dispatch_caller_result="$1"
 
    local idx
    for caller in "${FUNCNAME[@]}"; do
        if [[ ! $caller =~ ^bos[_/-] ]]; then
            bos_dispatch_caller_result="$caller"
            return 0
        fi
    done
 
    return 1
}
 
# the bare-bones / fallback dispatcher 
function bos-dispatch/invoke() {
    local class="$1";shift
    local self_id="$1";shift
    local method="$1";shift
    local qualified_method="$class/$method"
 
    if [[ "$method" =~ / ]]; then
        qualified_method="$method"
    fi
 
    if declare -F "$qualified_method" >/dev/null; then
        local self
        bos-object-id/pack-self-into "$class" "$self_id" self
        "$qualified_method" "$@"
        return $?
    fi
 
    local 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" "$@"
    return $?
}