-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix deprecated APIs warnings #259
Changes from 2 commits
c6ee87e
781bd0a
d9634cc
0542ad7
230dccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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) => { | ||
|
@@ -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) => { | ||
|
@@ -55,30 +75,38 @@ 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); | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dyedgreen What do you think is the best way to detect the availability of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I feel like we’ll just need to do a version check here 😕 |
||
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: () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dyedgreen I don't know of a good way to make this method support Deno versions before v1.44.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dyedgreen Let me know when you get a chance to look at this. I'm thinking we may need to bump the minimum supported Deno version to get access to the new file flushing APIs. What are your thoughts?