summaryrefslogtreecommitdiff
path: root/runtime/js/12_io.js
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2020-12-17 16:37:57 +0000
committerGitHub <noreply@github.com>2020-12-17 17:37:57 +0100
commitffb5f7a4e1d5d4ac488058ca3ec3c0805587fe44 (patch)
treedc7b79a699732680fa309d97e5b4c2bc5f486a4a /runtime/js/12_io.js
parent55dc467b419b8e5897b1c832b04d63e383253d84 (diff)
refactor: Rename runtime/rt to runtime/js (#8806)
Diffstat (limited to 'runtime/js/12_io.js')
-rw-r--r--runtime/js/12_io.js135
1 files changed, 135 insertions, 0 deletions
diff --git a/runtime/js/12_io.js b/runtime/js/12_io.js
new file mode 100644
index 000000000..006d51cdd
--- /dev/null
+++ b/runtime/js/12_io.js
@@ -0,0 +1,135 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
+// Interfaces 100% copied from Go.
+// Documentation liberally lifted from them too.
+// Thank you! We love Go! <3
+
+((window) => {
+ const DEFAULT_BUFFER_SIZE = 32 * 1024;
+ const { sendSync, sendAsync } = window.__bootstrap.dispatchMinimal;
+ // Seek whence values.
+ // https://golang.org/pkg/io/#pkg-constants
+ const SeekMode = {
+ 0: "Start",
+ 1: "Current",
+ 2: "End",
+
+ Start: 0,
+ Current: 1,
+ End: 2,
+ };
+
+ async function copy(
+ src,
+ dst,
+ options,
+ ) {
+ let n = 0;
+ const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
+ const b = new Uint8Array(bufSize);
+ let gotEOF = false;
+ while (gotEOF === false) {
+ const result = await src.read(b);
+ if (result === null) {
+ gotEOF = true;
+ } else {
+ let nwritten = 0;
+ while (nwritten < result) {
+ nwritten += await dst.write(b.subarray(nwritten, result));
+ }
+ n += nwritten;
+ }
+ }
+ return n;
+ }
+
+ async function* iter(
+ r,
+ options,
+ ) {
+ const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
+ const b = new Uint8Array(bufSize);
+ while (true) {
+ const result = await r.read(b);
+ if (result === null) {
+ break;
+ }
+
+ yield b.subarray(0, result);
+ }
+ }
+
+ function* iterSync(
+ r,
+ options,
+ ) {
+ const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
+ const b = new Uint8Array(bufSize);
+ while (true) {
+ const result = r.readSync(b);
+ if (result === null) {
+ break;
+ }
+
+ yield b.subarray(0, result);
+ }
+ }
+
+ function readSync(rid, buffer) {
+ if (buffer.length === 0) {
+ return 0;
+ }
+
+ const nread = sendSync("op_read", rid, buffer);
+ if (nread < 0) {
+ throw new Error("read error");
+ }
+
+ return nread === 0 ? null : nread;
+ }
+
+ async function read(
+ rid,
+ buffer,
+ ) {
+ if (buffer.length === 0) {
+ return 0;
+ }
+
+ const nread = await sendAsync("op_read", rid, buffer);
+ if (nread < 0) {
+ throw new Error("read error");
+ }
+
+ return nread === 0 ? null : nread;
+ }
+
+ function writeSync(rid, data) {
+ const result = sendSync("op_write", rid, data);
+ if (result < 0) {
+ throw new Error("write error");
+ }
+
+ return result;
+ }
+
+ async function write(rid, data) {
+ const result = await sendAsync("op_write", rid, data);
+ if (result < 0) {
+ throw new Error("write error");
+ }
+
+ return result;
+ }
+
+ window.__bootstrap.io = {
+ iterSync,
+ iter,
+ copy,
+ SeekMode,
+ read,
+ readSync,
+ write,
+ writeSync,
+ };
+})(this);