From cc46a3b620d1e3e5750ee66a2c539b70f2a7f195 Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Sat, 12 Dec 2020 10:46:03 +0800 Subject: [PATCH 1/5] Return copies of argv, execArgv and env --- src/Node/Process.js | 12 ++++++++++++ src/Node/Process.purs | 14 +++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Node/Process.js b/src/Node/Process.js index 27b3de7..1834150 100644 --- a/src/Node/Process.js +++ b/src/Node/Process.js @@ -65,3 +65,15 @@ exports.exit = function (code) { process.exit(code); }; }; + +exports.copyArray = function (xs) { + return function () { + return xs.slice(); + }; +}; + +exports.copyObject = function (o) { + return function () { + return Object.assign({}, o); + }; +}; diff --git a/src/Node/Process.purs b/src/Node/Process.purs index e20e31c..7569779 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -89,12 +89,12 @@ nextTick callback = mkEffect \_ -> process.nextTick callback -- | Get an array containing the command line arguments. Be aware -- | that this can change over the course of the program. argv :: Effect (Array String) -argv = mkEffect \_ -> process.argv +argv = copyArray process.argv -- | Node-specific options passed to the `node` executable. Be aware that -- | this can change over the course of the program. execArgv :: Effect (Array String) -execArgv = mkEffect \_ -> process.execArgv +execArgv = copyArray process.execArgv -- | The absolute pathname of the `node` executable that started the -- | process. @@ -111,7 +111,7 @@ cwd = process.cwd -- | Get a copy of the current environment. getEnv :: Effect (FO.Object String) -getEnv = mkEffect \_ -> process.env +getEnv = copyObject process.env -- | Lookup a particular environment variable. lookupEnv :: String -> Effect (Maybe String) @@ -168,3 +168,11 @@ stderrIsTTY = process.stderr.isTTY -- | Get the Node.js version. version :: String version = process.version + +-- Utils + +foreign import data MutableArray :: Type -> Type +foreign import data MutableObject :: Type -> Type + +foreign import copyArray :: forall a. MutableArray a -> Effect (Array a) +foreign import copyObject :: forall a. MutableObject a -> Effect (FO.Object a) From 081e297b5c883b3ba50f606323dad7fb046f54f1 Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Sat, 12 Dec 2020 10:56:08 +0800 Subject: [PATCH 2/5] Change lookupEnv to lookup without copying first --- src/Node/Process.purs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Node/Process.purs b/src/Node/Process.purs index 7569779..881e11a 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -115,7 +115,7 @@ getEnv = copyObject process.env -- | Lookup a particular environment variable. lookupEnv :: String -> Effect (Maybe String) -lookupEnv k = FO.lookup k <$> getEnv +lookupEnv k = lookupMutableObject k process.env -- | Set an environment variable. foreign import setEnv :: String -> String -> Effect Unit @@ -176,3 +176,7 @@ foreign import data MutableObject :: Type -> Type foreign import copyArray :: forall a. MutableArray a -> Effect (Array a) foreign import copyObject :: forall a. MutableObject a -> Effect (FO.Object a) + +lookupMutableObject :: forall a. String -> MutableObject a -> Effect (Maybe a) +lookupMutableObject k o = + mkEffect \_ -> FO.lookup k (unsafeCoerce o) From 086dd7d9a4e963525ee63227c53cc3c1618136e6 Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Sat, 12 Dec 2020 10:57:03 +0800 Subject: [PATCH 3/5] Remove doc warnings --- src/Node/Process.purs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Node/Process.purs b/src/Node/Process.purs index 881e11a..39099d9 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -86,13 +86,11 @@ onSignal sig = onSignalImpl (Signal.toString sig) nextTick :: Effect Unit -> Effect Unit nextTick callback = mkEffect \_ -> process.nextTick callback --- | Get an array containing the command line arguments. Be aware --- | that this can change over the course of the program. +-- | Get an array containing the command line arguments. argv :: Effect (Array String) argv = copyArray process.argv --- | Node-specific options passed to the `node` executable. Be aware that --- | this can change over the course of the program. +-- | Node-specific options passed to the `node` executable. execArgv :: Effect (Array String) execArgv = copyArray process.execArgv From f0f96c7dd80c8a85baefd639d1f200cf3ee1591f Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Sat, 12 Dec 2020 11:21:52 +0800 Subject: [PATCH 4/5] Add setArgv, setExecArgv --- src/Node/Process.js | 9 +++++++++ src/Node/Process.purs | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Node/Process.js b/src/Node/Process.js index 1834150..ae4545b 100644 --- a/src/Node/Process.js +++ b/src/Node/Process.js @@ -72,6 +72,15 @@ exports.copyArray = function (xs) { }; }; +exports.replaceArray = function (source) { + return function (dest) { + return function () { + dest.splice(0); + Array.prototype.push.apply(dest, source); + }; + }; +}; + exports.copyObject = function (o) { return function () { return Object.assign({}, o); diff --git a/src/Node/Process.purs b/src/Node/Process.purs index 39099d9..cba021f 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -6,7 +6,9 @@ module Node.Process , onUncaughtException , onUnhandledRejection , argv + , setArgv , execArgv + , setExecArgv , execPath , chdir , cwd @@ -90,10 +92,18 @@ nextTick callback = mkEffect \_ -> process.nextTick callback argv :: Effect (Array String) argv = copyArray process.argv +-- | Overwrite the array containing the command line arguments. +setArgv :: Array String -> Effect Unit +setArgv as = replaceArray as process.argv + -- | Node-specific options passed to the `node` executable. execArgv :: Effect (Array String) execArgv = copyArray process.execArgv +-- | Overwrite the array containing the node-specific options. +setExecArgv :: Array String -> Effect Unit +setExecArgv as = replaceArray as process.execArgv + -- | The absolute pathname of the `node` executable that started the -- | process. execPath :: Effect String @@ -173,6 +183,7 @@ foreign import data MutableArray :: Type -> Type foreign import data MutableObject :: Type -> Type foreign import copyArray :: forall a. MutableArray a -> Effect (Array a) +foreign import replaceArray :: forall a. Array a -> MutableArray a -> Effect Unit foreign import copyObject :: forall a. MutableObject a -> Effect (FO.Object a) lookupMutableObject :: forall a. String -> MutableObject a -> Effect (Maybe a) From 97ba65ecea9d15a27662986a54563e99ac583415 Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Sat, 12 Dec 2020 12:07:39 +0800 Subject: [PATCH 5/5] Revert "Add setArgv, setExecArgv" This reverts commit f0f96c7dd80c8a85baefd639d1f200cf3ee1591f. --- src/Node/Process.js | 9 --------- src/Node/Process.purs | 11 ----------- 2 files changed, 20 deletions(-) diff --git a/src/Node/Process.js b/src/Node/Process.js index ae4545b..1834150 100644 --- a/src/Node/Process.js +++ b/src/Node/Process.js @@ -72,15 +72,6 @@ exports.copyArray = function (xs) { }; }; -exports.replaceArray = function (source) { - return function (dest) { - return function () { - dest.splice(0); - Array.prototype.push.apply(dest, source); - }; - }; -}; - exports.copyObject = function (o) { return function () { return Object.assign({}, o); diff --git a/src/Node/Process.purs b/src/Node/Process.purs index cba021f..39099d9 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -6,9 +6,7 @@ module Node.Process , onUncaughtException , onUnhandledRejection , argv - , setArgv , execArgv - , setExecArgv , execPath , chdir , cwd @@ -92,18 +90,10 @@ nextTick callback = mkEffect \_ -> process.nextTick callback argv :: Effect (Array String) argv = copyArray process.argv --- | Overwrite the array containing the command line arguments. -setArgv :: Array String -> Effect Unit -setArgv as = replaceArray as process.argv - -- | Node-specific options passed to the `node` executable. execArgv :: Effect (Array String) execArgv = copyArray process.execArgv --- | Overwrite the array containing the node-specific options. -setExecArgv :: Array String -> Effect Unit -setExecArgv as = replaceArray as process.execArgv - -- | The absolute pathname of the `node` executable that started the -- | process. execPath :: Effect String @@ -183,7 +173,6 @@ foreign import data MutableArray :: Type -> Type foreign import data MutableObject :: Type -> Type foreign import copyArray :: forall a. MutableArray a -> Effect (Array a) -foreign import replaceArray :: forall a. Array a -> MutableArray a -> Effect Unit foreign import copyObject :: forall a. MutableObject a -> Effect (FO.Object a) lookupMutableObject :: forall a. String -> MutableObject a -> Effect (Maybe a)