Skip to content

Commit

Permalink
Fix deprecated APIs warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Apr 15, 2024
1 parent 1e98e83 commit c6ee87e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ jobs:
run: /home/runner/.deno/bin/deno test --allow-read --allow-write
- name: Run tests (unstable)
run: /home/runner/.deno/bin/deno test --unstable --allow-read --allow-write
- name: Run tests (DENO_FUTURE)
run: DENO_FUTURE=1 /home/runner/.deno/bin/deno test --unstable --allow-read --allow-write
- name: Run benchmarks
run: /home/runner/.deno/bin/deno run bench.ts
56 changes: 45 additions & 11 deletions build/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import { getStr } from "../src/wasm.ts";

const isWindows = Deno.build.os === "windows";

const OPEN_FILES = new Map();

function nextRid() {
const rid = (nextRid?.LAST_RID ?? 0) + 1;
nextRid.LAST_RID = rid;
return rid;
}

function getOpenFile(rid) {
if (!OPEN_FILES.has(rid)) {
throw new Error(`Resource ID ${rid} does not exist.`);
}
return OPEN_FILES.get(rid);
}

// Closure to return an environment that links
// the current wasm context
export default function env(inst) {
Expand All @@ -26,12 +41,16 @@ export default function env(inst) {

const write = !!(flags & 0x00000002);
const create = !!(flags & 0x00000004);
const rid = Deno.openSync(path, { read: true, write, create }).rid;
const file = Deno.openSync(path, { read: true, write, create });
const rid = nextRid();
OPEN_FILES.set(rid, file);
return rid;
},
// Close a file
js_close: (rid) => {
Deno.close(rid);
const file = getOpenFile(rid);
file.close();
OPEN_FILES.delete(rid);
},
// Delete file at path
js_delete: (path_ptr) => {
Expand All @@ -45,8 +64,9 @@ export default function env(inst) {
buffer_ptr,
amount,
);
Deno.seekSync(rid, offset, Deno.SeekMode.Start);
return Deno.readSync(rid, buffer);
const file = getOpenFile(rid);
file.seekSync(offset, Deno.SeekMode.Start);
return file.readSync(buffer);
},
// Write to a file from a buffer in the module
js_write: (rid, buffer_ptr, offset, amount) => {
Expand All @@ -55,30 +75,44 @@ export default function env(inst) {
buffer_ptr,
amount,
);
Deno.seekSync(rid, offset, Deno.SeekMode.Start);
return Deno.writeSync(rid, buffer);
const file = getOpenFile(rid);
file.seekSync(offset, Deno.SeekMode.Start);
return file.writeSync(buffer);
},
// Truncate the given file
js_truncate: (rid, size) => {
Deno.ftruncateSync(rid, size);
const file = getOpenFile(rid);
file.truncateSync(size);
},
// Sync file data to disk
js_sync: (rid) => {
Deno.fdatasyncSync(rid);
const file = getOpenFile(rid);
if (typeof file.rid === "number") {
// Uses deprecated Deno.FsFile.rid, which will be removed in Deno 2.0
Deno.fdatasyncSync(file.rid);
} else {
// Support DENO_FUTURE, but requires --unstable-fs
file.syncDataSync();
}
},
// Retrieve the size of the given file
js_size: (rid) => {
return Deno.fstatSync(rid).size;
const file = getOpenFile(rid);
return file.statSync().size;
},
// Acquire a SHARED or EXCLUSIVE file lock
js_lock: (rid, exclusive) => {
// this is unstable and has issues on Windows ...
if (Deno.flockSync && !isWindows) Deno.flockSync(rid, exclusive !== 0);
if (Deno.flockSync && !isWindows) {
getOpenFile(rid).lockSync(exclusive !== 0);
}
},
// Release a file lock
js_unlock: (rid) => {
// this is unstable and has issues on Windows ...
if (Deno.funlockSync && !isWindows) Deno.funlockSync(rid);
if (Deno.funlockSync && !isWindows) {
getOpenFile(rid).unlockSync();
}
},
// Return current time in ms since UNIX epoch
js_time: () => {
Expand Down

0 comments on commit c6ee87e

Please sign in to comment.