Mercurial > web-gnusto
changeset 1:394ff157b665
Got Gnusto to load and 'run' a story file in web content.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sat, 10 May 2008 09:29:27 -0700 |
parents | 81546981cec4 |
children | 15589ed006ed |
files | SConstruct gnusto-engine.js gnusto.html server.py stories/curses.z5 test.js z5_to_js_array.py |
diffstat | 7 files changed, 60 insertions(+), 124 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SConstruct Sat May 10 09:29:27 2008 -0700 @@ -0,0 +1,3 @@ +Command("curses.js", "stories/curses.z5", + "python z5_to_js_array.py ${SOURCE} > ${TARGET}") +Depends("curses.js", "z5_to_js_array.py")
--- a/gnusto-engine.js Sat May 10 08:18:10 2008 -0700 +++ b/gnusto-engine.js Sat May 10 09:29:27 2008 -0700 @@ -19,9 +19,7 @@ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. const CVS_VERSION = '$Date: 2005/04/26 01:50:32 $'; -const ENGINE_COMPONENT_ID = Components.ID("{bf7a4808-211f-4c6c-827a-c0e5c51e27e1}"); const ENGINE_DESCRIPTION = "Gnusto's interactive fiction engine"; -const ENGINE_CONTRACT_ID = "@gnusto.org/engine;1?type=zcode"; //////////////////////////////////////////////////////////////// // @@ -214,30 +212,6 @@ //////////////////////////////////////////////////////////////// // -// burin -// -// Not the real burin, just something to dump test information. - -function burin(text) { - var f = new Components. - Constructor("@mozilla.org/network/file-output-stream;1", - "nsIFileOutputStream", - "init") - (new Components. - Constructor("@mozilla.org/file/local;1", - "nsILocalFile", - "initWithPath")('c:\\b2.txt'), - 0x1A, - 0644, - 0); - - text= text + '\n'; - f.write(text, text.length); - f.close(); -} - -//////////////////////////////////////////////////////////////// -// // PART THE SECOND // // THE HANDLERS AND HANDLER ARRAYS @@ -1296,18 +1270,13 @@ message += '\n\nJS call stack:' + procstring; -// Components.classes['@gnusto.org/errorbox;1']. -// getService(Components.interfaces.gnustoIErrorBox). -// alert(number, message); - - curErrorBox = new Components.Constructor('@gnusto.org/errorbox;1','gnustoIErrorBox')(); - curErrorBox.alert(number, message); - - dump('-- Temporary burin error: '); - dump(number); - dump(' '); - dump(message); - dump('\n'); + console.log("Error " + number + ": " + message); + + console.log('-- Temporary burin error: '); + console.log(number); + console.log(' '); + console.log(message); + console.log('\n'); throw 'Error '+number+': '+message; } @@ -1396,7 +1365,7 @@ //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// - loadStory: function ge_loadStory(len, sourceFile) { + loadStory: function ge_loadStory(sourceFile) { this.m_memory = sourceFile; this._initial_setup(); }, @@ -1558,7 +1527,7 @@ // that that seems inelegant.) this.m_pc = pc; - this.eval("var t=new Function('with(this){'+_brancher('1')+'}');t.call(this);"); + eval("var t=new Function('with(this){'+_brancher('1')+'}');t.call(this);", this); } else { // The PC we're given is actually pointing at the varcode @@ -1645,7 +1614,7 @@ if (this.m_jit[start_pc]) { jscode = this.m_jit[start_pc]; } else { - jscode=this.eval('dummy='+this._compile()); + jscode=eval('dummy='+this._compile(), this); // Store it away, if it's in static memory (there's // not much point caching JIT from dynamic memory!) @@ -4369,87 +4338,4 @@ }; -//////////////////////////////////////////////////////////////// -// -// The Factory -// -// A simple class to make Gnusto engines -// - -GnustoEngineFactory = new Object(); - -GnustoEngineFactory.createInstance = -function gef_createinstance(outer, interface_id) -{ - if (outer != null) { - dump("Don't squish us!\n"); - throw Components.results.NS_ERROR_NO_AGGREGATION; - } - - if (interface_id.equals(Components.interfaces.gnustoIEngine)) { - return new GnustoEngine(); - } - - // Otherwise... - dump("Ugh, weird interface.\n"); - throw Components.results.NS_ERROR_INVALID_ARG; -} - -//////////////////////////////////////////////////////////////// -// -// The Module -// -// This is the object we pass out to the environment when we get registered. - -var Module = new Object(); - -// Tells the environment where to find our factory. -Module.registerSelf = -function mod_regself(compMgr, fileSpec, location, type) { - reg = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); - reg.registerFactoryLocation(ENGINE_COMPONENT_ID, - ENGINE_DESCRIPTION, - ENGINE_CONTRACT_ID, - fileSpec, - location, - type); -} - -// Returns the factory for the given component and interface IDs. -Module.getClassObject = -function mod_getclassobj(compMgr, component_id, interface_id) { - - if (component_id.equals(ENGINE_COMPONENT_ID)) { - return GnustoEngineFactory; - } - - // okay, so something's weird. give up. - - // FIXME: why these errors in particular? - if (interface_id.equals(Components.interfaces.nsIFactory)) { - throw Components.results.NS_ERROR_NO_INTERFACE; - } else { - throw Components.results.NS_ERROR_NOT_IMPLEMENTED; - } - -} - -// Tells us we're being unloaded. (That's always OK with us.) -Module.canUnload = -function mod_canunload(compMgr) { - return true; -} - -//////////////////////////////////////////////////////////////// -// -// NSGetModule -// -// It all starts here! - -function NSGetModule(compMgr, fileSpec) { - return Module; -} - -// Removed bootstrap code. Should now be callable from profile via newer method. - // EOF gnusto-engine.js //
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnusto.html Sat May 10 09:29:27 2008 -0700 @@ -0,0 +1,16 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> + <link rel="stylesheet" type="text/css" media="all" + href="" /> + <title></title> +</head> +<body> + +</body> +<script type="text/javascript" src="curses.js"></script> +<script type="text/javascript" src="gnusto-engine.js"></script> +<script type="text/javascript" src="test.js"></script> +</html>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server.py Sat May 10 09:29:27 2008 -0700 @@ -0,0 +1,12 @@ +import SimpleHTTPServer +import SocketServer + +PORT = 8000 + +Handler = SimpleHTTPServer.SimpleHTTPRequestHandler + +httpd = SocketServer.TCPServer(("", PORT), Handler) + +print "Serving files at http://localhost:%d" % PORT + +httpd.serve_forever()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.js Sat May 10 09:29:27 2008 -0700 @@ -0,0 +1,8 @@ +var engine = new GnustoEngine(); + +console.log("Loading story..."); +engine.loadStory(curses_z5); + +console.log("Running story..."); +engine.run(); +console.log("Done.");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/z5_to_js_array.py Sat May 10 09:29:27 2008 -0700 @@ -0,0 +1,11 @@ +import os +import sys + +if __name__ == "__main__": + if len(sys.argv) != 2: + print "usage: %s <z5-file>" % sys.argv[0] + sys.exit(-1) + contents = open(sys.argv[1], "rb").read() + byte_contents = [str(ord(i)) for i in contents] + filename_var = os.path.basename(sys.argv[1]).replace(".", "_") + print "%s = [%s];" % (filename_var, ", ".join(byte_contents))