Js char io (#2887)

* Implement `{get,put}Char` for javascript backend

* Update changelog
This commit is contained in:
Zoe Stafford 2023-02-13 15:48:07 +00:00 committed by GitHub
parent 3bccf8e212
commit ff822a747b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -29,10 +29,11 @@
* Non-recursive top-level constants are compiled to eagerly evaluated
constants in Chez Scheme.
#### Node.js
#### Node.js/Browser
* Generated JavaScript files now include a shebang when using the Node.js backend
* NodeJS now supports `popen`/`pclose` for the `Read` mode.
* `getChar` is now supported on Node.js and `putChar` is supported on both JavaScript backends.
### Compiler changes

View File

@ -75,8 +75,12 @@ export
prim__getString : Ptr String -> String
%foreign "C:putchar,libc 6"
"node:lambda:x=>process.stdout.write(x)"
"browser:lambda:x=>console.log(x)"
prim__putChar : Char -> (1 x : %World) -> IORes ()
%foreign "C:getchar,libc 6"
"node:support:getChar,support_system_file"
%extern prim__getChar : (1 x : %World) -> IORes Char
%foreign "C:idris2_getStr, libidris2_support, idris_support.h"

View File

@ -62,6 +62,16 @@ function support_system_file_getStr () {
return support_system_file_readLine({ fd: 0, buffer: Buffer.alloc(0), name: '<stdin>', eof: false })
}
function support_system_file_getChar() {
const readBuf = Buffer.alloc(1);
if (support_system_file_fs.readSync(process.stdin.fd, readBuf, 0, 1) === 0) {
// No bytes read, getChar from libc returns -1 in this case.
return String.fromCharCode(-1)
} else {
return readBuf.toString('utf-8')
}
}
function support_system_file_parseMode(mode) {
return mode.replace('b', '')
}