How to pass text through Glk/Quixe (encoding)

I’m modifying my earlier interpreter I made to pass strings to javascript.

I figured I’d just try the best I can and see where I mess up.

My Inform 7 code is:

Colorsetting is an action applying to a topic. Understand "cs [text]" as colorsetting.

Carry out colorsetting:
	let temp be the topic understood;
	colorset temp;

To colorset (T - a text):
	(-
	if(glk_gestalt(311, 0)){
	glk($0170, {- T -});
	}
	-)

Here 311 is the gestalt code I’m using for all my stuff and $0170 is a specific call I programmed. These numbers will change if I get a block assigned.

My quixe code changes are:

368:new FuncSpec(368,"set_color",new Prototype([new ArgString()],null))

...


function glk_set_color(val){
	var elements = document.getElementsByClassName("WindowFrame")
        for (var i = 0; i < elements.length; i++) {
            elements[i].style.background=val;
		}
	
}

...

glk_set_color:glk_set_color

This compiles just fine and the gestalt works great. But it runs into an error thrown by Quixe:

Quixe run: glk set_color: string argument must be unencoded

and the console gives:

VM stack dump: 0x3c, 0x8fb2, 0x21ebc, 0x17817, 0x91b1, 0x1bf67, 0x1bfd4, 0x21ebc, 0x17c09, 0x1f3f, 0xc0fc, 0x1c643, 0x21ebc, 0x17f53, 0xc89c, 0x21ebc, 0x1b91d, 0x15e5b, 0x53ab4

The relevant quixe code that throws that error is:

if(arg.retained){if(arraycount==0)
out.push('  self.temp_arg_arrays.length = 0;');arraycount+=1;out.push('  self.make_arg_array('+tmpvar+', callargs['+argpos+'], glklen, self.'+refarg.literal+');');}
out.push('}');argpos+=2;}
else if((arg instanceof ArgString)||(arg instanceof ArgUnicode)){locals['ix']=true;locals['jx']=true;var confunc,checkbyte;if(arg instanceof ArgString){checkbyte='0xE0';confunc='byte_array_to_string';}
else{checkbyte='0xE2';confunc='uni_array_to_string';}
out.push(tmpvar+' = Array();');out.push('jx = callargs['+argpos+'];');out.push('if (self.VM.ReadByte(jx) != '+checkbyte+') throw("glk '+func.name+': string argument must be unencoded");');out.push('for (jx+='+arg.refsize+'; true; jx+='+arg.refsize+') {');out.push('  ix = self.VM.Read'+arg.macro+'(jx);');out.push('  if (ix == 0) break;');out.push('  '+tmpvar+'.push(ix);');out.push('}');out.push(tmpvar+' = Glk.'+confunc+'('+tmpvar+');');argpos+=1;}

What do I need to do here? I’m sure it’s related to either Inform’s ‘text’ vs ‘snippet’ vs whatever or something more boring like pointer vs reference or unicode vs ascii.

What do I need to change here?

1 Like

I think what it wants is a C string rather than an Inform string. To get that, instead of {T}, use Glulx_ChangeAnyToCString(TEXT_TY_Say, {T}). (In other words, “call the function TEXT_TY_Say on the argument T, then convert the result to a C string”.)

2 Likes

Thanks! It’s working great! Now I just have to figure out the Javascript side, which should be easier given all the documentation out there.

1 Like