mirror of
https://github.com/idris-lang/Idris2.git
synced 2024-12-24 20:23:11 +03:00
Js char io (#2887)
* Implement `{get,put}Char` for javascript backend * Update changelog
This commit is contained in:
parent
3bccf8e212
commit
ff822a747b
@ -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
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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', '')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user