Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Implement __hscore_ftruncate #657

Merged
merged 1 commit into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions asterius/rts/browser/default.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Posix {
close() {
throw WebAssembly.RuntimeError("Unsupported rts interface: close");
}
ftruncate() {
throw WebAssembly.RuntimeError("Unsupported rts interface: ftruncate");
}
stat() {
throw WebAssembly.RuntimeError("Unsupported rts interface: stat");
}
Expand Down
11 changes: 10 additions & 1 deletion asterius/rts/node/default.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Posix {
argv_total_size =
argv_header_size +
// All strings are \0-terminated, hence the +1
arg_bufs.reduce((acc, buf) => acc + buf.byteLength + 1, 0);
arg_bufs.reduce((acc, buf) => acc + buf.byteLength + 1, 0);
// The total size (in bytes) of the runtime arguments cannot exceed the 1KB
// size of the data segment we have reserved. If you wish to change this
// number, you should also update envArgvBuf in Asterius.Builtins.Env.
Expand Down Expand Up @@ -64,6 +64,15 @@ class Posix {
return -1;
}
}
ftruncate(fd, len) {
try {
fs.ftruncateSync(fd, len);
return 0;
} catch (err) {
this.set_errno(-err.errno);
return -1;
}
}
stat(f, b) {
try {
const r = fs.statSync(this.memory.strLoad(f));
Expand Down
22 changes: 22 additions & 0 deletions asterius/src/Asterius/Builtins/Posix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ posixImports =
externalBaseName = "close",
functionType = FunctionType {paramTypes = [F64], returnTypes = [F64]}
},
FunctionImport
{ internalName = "__asterius_posix_ftruncate",
externalModuleName = "posix",
externalBaseName = "ftruncate",
functionType =
FunctionType
{ paramTypes = [F64, F64],
returnTypes = [F64]
}
},
FunctionImport
{ internalName = "__asterius_posix_stat",
externalModuleName = "posix",
Expand Down Expand Up @@ -136,6 +146,7 @@ posixCBits :: AsteriusModule
posixCBits =
posixOpen
<> posixClose
<> posixFtruncate
<> posixStat
<> posixFstat
<> posixFstatGetters
Expand Down Expand Up @@ -175,6 +186,17 @@ posixClose = runEDSL "close" $ do
<$> callImport' "__asterius_posix_close" [convertSInt64ToFloat64 fd] F64
>>= emit

posixFtruncate :: AsteriusModule
posixFtruncate = runEDSL "__hscore_ftruncate" $ do
setReturnTypes [I64]
args <- params [I64, I64]
truncSFloat64ToInt64
<$> callImport'
"__asterius_posix_ftruncate"
(map convertSInt64ToFloat64 args)
F64
>>= emit

posixStat :: AsteriusModule
posixStat = runEDSL "__hscore_stat" $ do
setReturnTypes [I64]
Expand Down