Use Glk with JavaScript


I made a protocol for Glk dispatch with byte streams, and a program to use this with JavaScript. Now you can call the Glk API from JavaScript codes.

Find documentation at:

Here is an example JavaScript program which calls Glk: "use strict"; const Glk=require("glk"); let w=Glk.window_open(null,0,0,Glk.wintype_TextBuffer,0); let e=[]; let i; let line=new Uint8Array(80); if(!w) process.exit(1); Glk.stream_set_current(Glk.window_get_stream(w)); for(i=0;i<Glk.style_NUMSTYLES;i++) { Glk.set_style(i); Glk.put_string("Style "+i+"\n"); } Glk.set_style(Glk.style_Normal); for(;;) { Glk.put_string("> "); Glk.request_line_event(w,line,0); for(;;) {; if(e[0]==Glk.evtype_LineInput) { let s=Glk.bufToString(line,e[2]).trim(); if(s.toLowerCase()=="quit") return Glk.exit(); if(s) Glk.put_string("Hello, "+s+"!\n"); break; } } } Glk.exit();

If you have any suggestions, questions, comments, complaints, etc, you can please to write it on here or on ifMUD. I will also accept the patches if needing for working with Glk implementations other than GlkTerm.


How does this differ from the Glk implementation (glkapi.js) in GlkOte?


Well, this program is not itself a Glk implementation in JavaScript, but allows you to call other Glk implementations from JavaScript code (currently only GlkTerm, but it could easily be extended to work with any other UNIX-based Glk implementation). It does not use GlkOte (although you could use it with GlkOte too by using RemGlk, although this is different). The API also does not quite match glkapi.js (which I have not seen until now) as far as I can tell (although glkapi.js does not seem to be documented so well from what I can see, either). However, nothing seems to stop you from using the same API to implement Glk purely in JavaScript if you did want to do so, though. But since this program is using synchronous I/O, you may need to use a separate process even if it is implemented in JavaScript.


Is it for Node then? Do you have to recompile the Glk implementation?

The documentation at the npm site is hard to read as it’s all in a code block. I suggest using more markdown :slight_smile:


Yes, it is for Node.js. You only need to compile the Glk implementation if it is not already compiled; if you have the .o or .a files then you can use those. You could use any Glk implementation; you will only need to change the startup code and/or compilation instructions in glkrun.c if you use a different one than GlkTerm (if you do this, please post your patches on here, so that we can use them with other Glk implementations too); changing index.js should be unnecessary, although if your Glk implementation has additional functions and/or constants, you may wish to edit dispatch.js in order to add those additional functions/constants.


I looked at the glkapi.js in GlkOte more now, and here is a list of the differences between my program and that glkapi.js (although I may have missed some or made other mistakes):

  • There is no RefBox and RefStruct. Instead, this program uses arrays for references and structures.
  • Constants are not placed in a separate namespace.
  • Glk function names do not have “glk_” at front.
  • This program can use typed arrays when an array must be passed to/from Glk.
  • There is no “Do Not Return”.

Also, here is the output from “wc” to show the sizes of these files, in case anyone is interested: 385 699 9677 glkrun.c 274 684 11735 dispatch.js 294 621 9494 index.js 953 2004 30906 total


True. Difficult to read. In Firefox you can go view > page style > without style for now :slight_smile:


I do not find it difficult to read. However, if you have npm installed, you can also try “npm show glk readme”; that is another way to read it.

Edit: Someone says it is clipped on the right on the webpage. Not on my computer, though.


It’s because the code text is clipped at the right, so you need to scroll.
Thanks for the tip though