Commit 63e76695 authored by Will Hilton's avatar Will Hilton

Add getbuffer for completeness

parent 76e066d5
......@@ -204,6 +204,11 @@ See the examples/stdin/stdout-buffered.js which runs 36x faster than the unbuffe
Returns the number of bytes written, which is equal to the size of the buffer.
I might add support for start / end indices.
### getbuffer : (Uint8Array) => Number | Error EOF | undefined
I just added this for symmetry with putbuffer. I imagine it has
similar performance benefits over reading bytes one at a time, but I haven't
tested that yet. See examples/stdin/stdin-buffered.js
## Parallel Computing
By default, Dukboot will start as many physical threads as you have logical cores.
......
function (o) {
// If you want to read bytes in order, I would recommend having
// a single master thread read the bytes. You can divide the work later.
if (o.OpenMP.id === 0) {
var UTF8decoder = new TextDecoder('utf-8')
var c
var buffer = new Uint8Array(8);
while (true) {
c = o.modules.getbuffer(buffer)
if (c instanceof Error) break
if (c === undefined) o.modules.sleep(100)
if (c > 0) {
o.modules.print(o.OpenMP.id + ': read ' + c + ' bytes')
o.modules.print(UTF8decoder.decode(buffer.subarray(0, c)))
}
}
}
}
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "duktape/duktape.h"
#include "c_getbuffer.h"
duk_size_t c_getbuffer (duk_context *ctx) {
char * buffer;
duk_size_t length;
buffer = duk_require_buffer_data(ctx, 0, &length);
size_t bytes_read = fread(buffer, sizeof(char), length, stdin);
if (bytes_read == 0) {
if (feof(stdin)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdin)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdin);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_getbuffer.");
}
duk_push_int(ctx, bytes_read);
return 1;
}
......@@ -101,6 +101,8 @@ int main(int argc, char *argv[] /* char *environ[] */) {
duk_put_prop_string(ctx, -2, "getchar");
duk_push_c_function(ctx, c_putchar, 1);
duk_put_prop_string(ctx, -2, "putchar");
duk_push_c_function(ctx, c_getbuffer, 1);
duk_put_prop_string(ctx, -2, "getbuffer");
duk_push_c_function(ctx, c_putbuffer, 1);
duk_put_prop_string(ctx, -2, "putbuffer");
app_push_netLib(ctx);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment