Foreign constants

I’m trying to use WebGL in Hoot. I’m using define-foreign to get access to js functions. I don’t understand how I’m supposed to get access to webgl constants like COLOR_BUFFER_BIT. I tried something like

(define-foreign COLOR_BUFFER_BIT
  "gl" "COLOR_BUFFER_BIT"
  -> (ref extern))

without success.

The error I get is

LinkError: import object field 'COLOR_BUFFER_BIT' is not a Function

Hi @teddd, you can’t import a number, only functions. Just define the constant like (define var val) directly.

Ok I see. That will do for now. It would be nice not to have to redefine them. Is this planned / possible feature ?

Not a planned feature. This is the usual case for FFIs. Automating this with a code generation tool would be the way to go.

Makes sense. Maybe it could be part of this Generate JS for FFI imports (#159) · Issues · spritely / Guile Hoot · GitLab

That’s a separate but related thing that you could target with the code generation tool. All the Web APIs have IDL specifications, so you can parse those and generate bindings. This is what Kotlin Wasm does. Here is the WebGL IDL file and specifically a line that defines a constant: WebGL/specs/latest/1.0/webgl.idl at main · KhronosGroup/WebGL · GitHub

1 Like

Great! That looks promising. So we need an IDL parser first

Yup, a Scheme IDL parser would be a great thing to have.

Maybe that could be done using PEGs: PEG Parsing (Guile Reference Manual)