#------ Standard NaTcl preamble
# core JS/Tcl interaction
proc jsquote s {
regsub -all {[''\\]} $s {\\&} s
regsub -all \n $s {'+"\\n"+'} s
return '${s}'
}
proc bgerror s {
printf "### BGERROR: $s\n# [info errorstack]"
set ::JS "alert([jsquote "BGERROR: $s"]);"
}
set ::naclhooks {}
proc naclwrap s {
set ::JS ""
if {[catch {
uplevel 1 $s
foreach x $::naclhooks {uplevel #0 $x}
} err]} {
printf "Wrapper error: $err"
bgerror $err
}
return $::JS
}
# Coro-based [source] necessary for bootstrapping
proc source url {
set j "tclsource([jsquote $url],[jsquote [info coroutine]]);\n"
append ::JS $j
set x [yield]
uplevel 1 $x
}
# Async [after] using JS's setTimeout()
proc after {ms script} {
append ::JS "setTimeout(function(){tclDo([jsquote $script]);},$ms);\n"
}
# Async [every] using JS's setInterval()
proc every {ms script} {
append ::JS "setInterval(function(){tclDo([jsquote $script]);},$ms);\n"
}
# Delayed DOM-setting through the tclDo() trampoline
proc domset {element inner} {
append ::JS "$element.innerHTML=[jsquote $inner];\n"
}