intfiction.org

The Interactive Fiction Community Forum
It is currently Sat Feb 23, 2019 5:07 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Use Glk with JavaScript
PostPosted: Fri Dec 28, 2018 12:27 am 
Offline

Joined: Sat Oct 04, 2008 9:21 pm
Posts: 257
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: https://www.npmjs.com/package/glk

Here is an example JavaScript program which calls Glk:
Code:
  "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(;;) {
      Glk.select(e);
      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.

_________________
.


Top
 Profile Send private message  
Reply with quote  
PostPosted: Fri Dec 28, 2018 1:55 am 
Offline
User avatar

Joined: Wed Oct 14, 2009 4:02 am
Posts: 2600
How does this differ from the Glk implementation (glkapi.js) in GlkOte?


Top
 Profile Send private message  
Reply with quote  
PostPosted: Fri Dec 28, 2018 2:09 am 
Offline

Joined: Sat Oct 04, 2008 9:21 pm
Posts: 257
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.

_________________
.


Top
 Profile Send private message  
Reply with quote  
PostPosted: Fri Dec 28, 2018 2:31 am 
Offline
User avatar

Joined: Wed Oct 14, 2009 4:02 am
Posts: 2600
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 :)


Top
 Profile Send private message  
Reply with quote  
PostPosted: Fri Dec 28, 2018 2:44 am 
Offline

Joined: Sat Oct 04, 2008 9:21 pm
Posts: 257
Dannii wrote:
Is it for Node then? Do you have to recompile the Glk implementation?
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.

_________________
.


Top
 Profile Send private message  
Reply with quote  
PostPosted: Fri Dec 28, 2018 3:12 am 
Offline

Joined: Sat Oct 04, 2008 9:21 pm
Posts: 257
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:
Code:
  385   699  9677 glkrun.c
  274   684 11735 dispatch.js
  294   621  9494 index.js
  953  2004 30906 total

_________________
.


Top
 Profile Send private message  
Reply with quote  
PostPosted: Sat Jan 12, 2019 2:12 pm 
Offline

Joined: Tue Nov 29, 2016 8:56 am
Posts: 93
Dannii wrote:
The documentation at the npm site is hard to read as it's all in a code block. I suggest using more markdown :)

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


Top
 Profile Send private message  
Reply with quote  
PostPosted: Sat Jan 12, 2019 11:02 pm 
Offline

Joined: Sat Oct 04, 2008 9:21 pm
Posts: 257
cibersheep wrote:
Dannii wrote:
The documentation at the npm site is hard to read as it's all in a code block. I suggest using more markdown :)

True. Difficult to read. In Firefox you can go view > page style > without style for now :)
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.

_________________
.


Last edited by zzo38 on Sun Jan 13, 2019 10:43 pm, edited 1 time in total.

Top
 Profile Send private message  
Reply with quote  
PostPosted: Sun Jan 13, 2019 7:20 pm 
Offline

Joined: Tue Nov 29, 2016 8:56 am
Posts: 93
zzo38 wrote:
cibersheep wrote:
Dannii wrote:
The documentation at the npm site is hard to read as it's all in a code block. I suggest using more markdown :)

True. Difficult to read. In Firefox you can go view > page style > without style for now :)
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.


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

Image


Top
 Profile Send private message  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group