Skip to content
Tcl Source Code Artifact [2c6c33a41e]
Login

Artifact [2c6c33a41e]

Artifact 2c6c33a41ec69c9245bd49e559da18ee45853388:


#------ 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} {
    if {[regexp \n $script]} {error "JS hates multiline :)"}
    append ::JS "setTimeout(\"tclDo([jsquote $script])\",$ms);\n"
}

# Async [every] using JS's setInterval()
proc every {ms script} {
    if {[regexp \n $script]} {error "JS hates multiline :)"}
    append ::JS "setInterval(\"tclDo([jsquote $script])\",$ms);\n"
}

# Delayed DOM-setting through the tclDo() trampoline
proc domset {element inner} {
    append ::JS "$element.innerHTML=[jsquote $inner];\n"
}