<!DOCTYPE html>
<html>
<!--
Copyright (c) 2010 The Native Client Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<head>
<title>NaTcl : Tcl in Nacl</title>
<script type="text/javascript">
// NaTcl -- JS glue
tclModule = null; // our singleton Tcl interp
// debugging stuff
function printf(s)
{
// I like debugging to stderr instead of console.log,
// because when things go wrong, the JS console is not
// alays reachable.
tclModule.eval('printf {'+s+'}');
}
function ljoin()
{
// yeah. 'arguments' is 'similar to an array but not quite'
// that's why I think JS has no soul.
return Array.prototype.slice.call(arguments).join("\n");
}
// --- tclDo is the main JS-Tcl trampoline.
//
// Its job is to pass a Tcl string to [eval] (through naclwrap, see
// init.nacl), and then take back the result as JS and eval() it.
// It also detects errors in the latter eval() and pipes them back
// to [bgerror].
tclReportError = "";
function tclDo(s) {
//printf("do:"+s);
try {
t = tclModule.eval("naclwrap {"+s+"}");
//printf("ret:"+t);
eval(t);
} catch(err) {
//printf("JS-err:"+err);
tclReportError="bgerror {"+err+" -- while doing: "+t+"}";
setTimeout('tclDo(tclReportError)',0);
}
}
// --- tclsource starts an XHR, and calls the given 'tcb' (Tcl
// --- Callback) on completion. A catchable Tcl-level error is raised
// --- in case of not-200. Used by [source].
function tclsource(url,tcb) {
//printf('tclsource');
xs = new XMLHttpRequest();
xs.open("GET",url,true);
xs.send(null);
xs.onreadystatechange = function() {
//printf("XHR-source:"+xs.readyState);
if (xs.readyState==4)
{
if (xs.status==200) {
tclDo(tcb+" {"+xs.responseText+"}");
} else {
tclDo(tcb+" {error \"Can't source -- "+xs.statusText+"\"}");
}
}
};
}
// ---------- GUI and standard NaCl-loading machinery --------
var statusField = null;
modstatus = 'NO-STATUS';
function updateStatus(opt_message) {
if (statusField) {
statusField.innerHTML = modstatus;
}
}
function moduleDidLoad() {
tclModule = document.getElementById('tcl');
updateStatus('NaTcl Loaded ; Fetching and running !');
tclDo('coroutine main_coro source balls.natcl');
}
// If the page loads before the Native Client module loads, then set the
// status message indicating that the module is still loading. Otherwise,
// do not change the status message.
function pageDidLoad() {
statusField = document.getElementById('modstatus');
if (tclModule == null) {
updateStatus('Loading Nacl...');
} else {
// It's possible that the Native Client module onload event fired
// before the page's onload event. In this case, the status message
// will reflect 'SUCCESS', but won't be displayed. This call will
// display the current message.
updateStatus();
}
}
function doeval() {
try {
alert(tclModule.eval(this.the_form.input_id.value));
} catch(e) {
alert(e.message);
}
}
</script>
</head>
<body onload="pageDidLoad()">
<h1>NaTcl -- Native Client Tcl Module</h1>
<p>
<form name="the_form" action="" method="get">
<textarea id="input_id" name="inputbox" rows="15" cols="80">list a b c
</textarea><p>
<input type="button" value="Call eval()" onclick="doeval()">
</form>
<!-- Load the published .nexe. This includes the 'nacl' attribute which
shows how to load multi-architecture modules. Each entry in the "nexes"
object in the .nmf manifest file is a key-value pair: the key is the runtime
('x86-32', 'x86-64', etc.); the value is a URL for the desired NaCl module.
To load the debug versions of your .nexes, set the 'nacl' attribute to the
_dbg.nmf version of the manifest file.
-->
<embed name="nacl_module"
id="tcl"
width=0 height=0
nacl="tcl.nmf"
type="application/x-nacl"
onload="moduleDidLoad();" />
</p>
<p>If the module is working correctly, a click on the "Call eval()" button
should open a popup dialog containing the Tcl result as its value.</p>
<h2>Status</h2>
<div id="modstatus">NO-STATUS</div>
<hr>
</body>
</html>