From e6ab65a1a4f4a94c5a75e1934521bf4a44611df9 Mon Sep 17 00:00:00 2001 From: Shao Cheng Date: Tue, 19 May 2020 14:15:40 +0200 Subject: [PATCH] Add __hscore_ftruncate --- asterius/rts/browser/default.mjs | 3 +++ asterius/rts/node/default.mjs | 11 ++++++++++- asterius/src/Asterius/Builtins/Posix.hs | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/asterius/rts/browser/default.mjs b/asterius/rts/browser/default.mjs index d58dcd658a..53bd24f237 100644 --- a/asterius/rts/browser/default.mjs +++ b/asterius/rts/browser/default.mjs @@ -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"); } diff --git a/asterius/rts/node/default.mjs b/asterius/rts/node/default.mjs index 65ed813db3..ee667c2c99 100644 --- a/asterius/rts/node/default.mjs +++ b/asterius/rts/node/default.mjs @@ -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. @@ -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)); diff --git a/asterius/src/Asterius/Builtins/Posix.hs b/asterius/src/Asterius/Builtins/Posix.hs index 8ae1571616..ea15c942fa 100644 --- a/asterius/src/Asterius/Builtins/Posix.hs +++ b/asterius/src/Asterius/Builtins/Posix.hs @@ -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", @@ -136,6 +146,7 @@ posixCBits :: AsteriusModule posixCBits = posixOpen <> posixClose + <> posixFtruncate <> posixStat <> posixFstat <> posixFstatGetters @@ -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]