summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2016-02-24 03:24:45 (EST)
committer P. J. McDermott <pj@pehjota.net>2016-02-24 03:24:45 (EST)
commite9549f56d03e0113b85e45f8d1e5257e3a28f0a9 (patch)
treeaf660f30eb3bd1e1f63085a8da189598ac02e0c9
parent125adb4bcd29df7b74d5e5aa34c527c2f68826c6 (diff)
downloadeggshell-e9549f56d03e0113b85e45f8d1e5257e3a28f0a9.zip
eggshell-e9549f56d03e0113b85e45f8d1e5257e3a28f0a9.tar.gz
eggshell-e9549f56d03e0113b85e45f8d1e5257e3a28f0a9.tar.bz2
eshld/eshrt: Encode stack in vars with a stack pointer
String manipulations are slow and require fork()ing for sed to escape/encode input data.
-rw-r--r--eshld/eshrt.esh73
1 files changed, 29 insertions, 44 deletions
diff --git a/eshld/eshrt.esh b/eshld/eshrt.esh
index bcc69b4..ad03b09 100644
--- a/eshld/eshrt.esh
+++ b/eshld/eshrt.esh
@@ -24,8 +24,7 @@ eshrtbegin()
__ESC="$(printf '\033.')"; __ESC="${__ESC%.}"
__RS="$(printf '\036.')"; __RS="${__RS%.}"
__US="$(printf '\037.')"; __US="${__US%.}"
-__fn_stack=''
-__fn_frame=''
+__sp=0
__fn_name=''
__fn_tu=''
__fn_vars=''
@@ -35,49 +34,35 @@ __prev_tu=''
__prev_vars=''
__var=''
__val=''
+__fn_frame=0
__fn_begin_hooks=''
__fn_end_hooks=''
__init_funcs=''
-__fn_stack_push()
+__sp_inc()
{
- __fn_stack="${__fn_stack}${__RS}${__fn_frame}"
+ __sp=$((${__sp} + 1))
}
-__fn_stack_peek()
+__sp_dec()
{
- __fn_frame="${__fn_stack##*${__RS}}"
+ __sp=$((${__sp} - 1))
}
-__fn_stack_pop()
+__frame_set()
{
- __fn_stack_peek
- __fn_stack="${__fn_stack%${__RS}*}"
+ eval "__frame_${__sp}_name=\${__fn_name}"
+ eval "__frame_${__sp}_tu=\${__fn_tu}"
+ eval "__frame_${__sp}_vars=\${__fn_vars}"
+ eval "__frame_${__sp}_var_vals=\${__fn_var_vals}"
}
-__fn_frame_encode()
+__frame_get()
{
- # RS and US in frame variable values must be encoded.
- __fn_var_vals="$(printf '%s' "${__fn_var_vals}" | sed "
- s|${__ESC}|${__ESC}0|g;
- s|${__RS}|${__ESC}1|g; s|${__US}|${__ESC}2|g;
- ")"
- __fn_frame="${__fn_name}${__US}${__fn_tu}"
- __fn_frame="${__fn_frame}${__US}${__fn_vars}${__US}${__fn_var_vals}"
-}
-
-__fn_frame_decode()
-{
- __fn_name="${__fn_frame%%${__US}*}"
- __fn_frame="${__fn_frame#*${__US}}"
- __fn_tu="${__fn_frame%%${__US}*}"
- __fn_frame="${__fn_frame#*${__US}}"
- __fn_vars="${__fn_frame%%${__US}*}"
- __fn_var_vals="${__fn_frame#*${__US}}"
- __fn_var_vals="$(printf '%s' "${__fn_var_vals}" | sed "
- s|${__ESC}0|${__ESC}|g;
- s|${__ESC}1|${__RS}|g; s|${__ESC}2|${__US}|g;
- ")"
+ eval "__fn_name=\${__frame_${__sp}_name}"
+ eval "__fn_tu=\${__frame_${__sp}_tu}"
+ eval "__fn_vars=\${__frame_${__sp}_vars}"
+ eval "__fn_var_vals=\${__frame_${__sp}_var_vals}"
}
__fn_ctxsw()
@@ -135,19 +120,18 @@ __fn_start()
__prev_vars="${__fn_vars}"
# Update local vars in stack.
- __fn_stack_pop
- __fn_vars="${__prev_vars}"
__fn_update_vars
- __fn_frame_encode
- __fn_stack_push
+ __frame_set
+
+ # Set stack pointer.
+ __sp_inc
# New state.
__fn_name="${2}"
__fn_tu="${1}"
__fn_vars=
__fn_var_vals=
- __fn_frame_encode
- __fn_stack_push
+ __frame_set
# Switch.
__fn_ctxsw
@@ -161,14 +145,14 @@ __fn_end()
done
# Old state.
- __fn_stack_pop
- __fn_frame_decode
__prev_tu="${__fn_tu}"
__prev_vars="${__fn_vars}"
+ # Set stack pointer.
+ __sp_dec
+
# New state.
- __fn_stack_peek
- __fn_frame_decode
+ __frame_get
# Switch.
__fn_ctxsw
@@ -190,9 +174,10 @@ __tu_end()
__stack_trace()
{
- IFS="${__RS}"
- for __fn_frame in ${__fn_stack#${__RS}}; do
- printf '%s\n' "${__fn_frame%%${__US}*}"
+ __fn_frame=0
+ while [ ${__fn_frame} -le ${__sp} ]; do
+ eval "printf '%s\n' \"\${__frame_${__fn_frame}_name}\""
+ __fn_frame=$((${__fn_frame} + 1))
done
}