<!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>Tcl in Nacl</title>
<script type="text/javascript">
tclModule = null; // Global application object.
xhr = null;
tclReportError = "";
statusText = 'NO-STATUS';
function tclDo(s) {
console.log("do:"+s);
try {
t = tclModule.eval("naclwrap {"+s+"}");
console.log("ret:"+t);
eval(t);
} catch(err) {
console.log("err:"+err);
tclReportError="bgerror {"+err+" -- while doing: "+t+"}";
setTimeout('tclDo(tclReportError)',0);
}
}
function moduleDidLoad() {
tclModule = document.getElementById('tcl');
tclModule.eval('proc bgerror s {printf "### BGERROR: $s";regsub -all {[\'\'\\\\]} $s {\\\\&} s;regsub -all \\n $s \t s;return "alert(\'### BGERROR: $s\');";}');
tclModule.eval('proc naclwrap s {set ::JS "";if {[catch {uplevel 1 $s} err]} {set ::JS [bgerror $err]};return $::JS} ');
updateStatus('NaTcl Loaded ; fetching script...');
xhr = new XMLHttpRequest();
xhr.open("GET","balls.natcl",true);
xhr.send(null);
xhr.onreadystatechange = function() {
if (xhr.readyState==4)
{
if (xhr.status==200) {
updateStatus("Running");
tclDo(xhr.responseText);
} else {
updateStatus("Cannot load script: "+xhr.status+" "+xhr.statusText);
}
}
};
}
var statusField = null;
// 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('status');
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);
}
}
// Set the global status message. If the element with id 'status'
// exists, then set its HTML to the status message as well.
// opt_message The message test. If this is null or undefined, then
// attempt to set the element with id 'status' to the value of
// |statusText|.
function updateStatus(opt_message) {
if (opt_message)
statusText = opt_message;
if (statusField) {
statusField.innerHTML = statusText;
}
}
</script>
</head>
<body onload="pageDidLoad()">
<h1>Native Client Tcl Module</h1>
<p>
<form name="the_form" action="" method="get">
<input type="text" id="input_id" name="inputbox" value="list a b c"><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="status">NO-STATUS</div>
<hr>
</body>
</html>