summaryrefslogtreecommitdiff
path: root/bos-dispatch.sh
blob: cf73e91b6b0a24e64ac3a01dbf79570c7bac9bd3 (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
#!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 [[ "$class" == bos/meta/class ]] && declare -F "$qualified_method" >/dev/null; then
        local self
        bos-object-id/pack-self-into self "$class" "$self_id"
        "$qualified_method" "$@"
        return $?
    fi
 
    local metaclass_instance; bos-meta/metaclass-instance-for-class-into metaclass_instance "$class"
 
    $metaclass_instance invoke "$class" "$self_id" "$method" "$@"
    return $?
}