From e6d69aabc2daf3a6e3d0379b7b04c8dc4ed21c50 Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Fri, 11 Dec 2020 12:34:06 +0800 Subject: [PATCH 1/3] Add mutable references for argv, execArgv and env --- bower.json | 4 +++- src/Node/Process.purs | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/bower.json b/bower.json index a75c17d..9626072 100644 --- a/bower.json +++ b/bower.json @@ -12,12 +12,14 @@ "output" ], "dependencies": { + "purescript-arrays": "master", "purescript-effect": "master", "purescript-foreign-object": "master", "purescript-maybe": "master", "purescript-node-streams": "master", "purescript-posix-types": "master", "purescript-unsafe-coerce": "master", - "purescript-prelude": "master" + "purescript-prelude": "master", + "purescript-st": "master" } } diff --git a/src/Node/Process.purs b/src/Node/Process.purs index e20e31c..cc0a250 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -6,11 +6,14 @@ module Node.Process , onUncaughtException , onUnhandledRejection , argv + , argv' , execArgv + , execArgv' , execPath , chdir , cwd , getEnv + , getEnv' , lookupEnv , setEnv , unsetEnv @@ -27,6 +30,10 @@ module Node.Process import Prelude +import Control.Monad.ST.Global (Global) +import Control.Monad.ST.Global as STGlobal +import Data.Array.ST (STArray) +import Data.Array.ST as STArray import Data.Maybe (Maybe) import Data.Posix (Pid) import Data.Posix.Signal (Signal) @@ -34,6 +41,8 @@ import Data.Posix.Signal as Signal import Effect (Effect) import Effect.Exception (Error) import Foreign.Object as FO +import Foreign.Object.ST (STObject) +import Foreign.Object.ST as STObject import Node.Platform (Platform) import Node.Platform as Platform import Node.Stream (Readable, Writable) @@ -86,15 +95,22 @@ 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 = mkEffect \_ -> process.argv +argv = STGlobal.toEffect (STArray.freeze argv') --- | Node-specific options passed to the `node` executable. Be aware that --- | this can change over the course of the program. +-- | Get the mutable array containing the command line arguments. +argv' :: STArray Global String +argv' = process.argv + +-- | Node-specific options passed to the `node` executable. execArgv :: Effect (Array String) -execArgv = mkEffect \_ -> process.execArgv +execArgv = STGlobal.toEffect (STArray.freeze execArgv') + +-- | Mutable array of node-specific options passed to the +-- | `node` executable. +execArgv' :: STArray Global String +execArgv' = process.execArgv -- | The absolute pathname of the `node` executable that started the -- | process. @@ -109,13 +125,16 @@ foreign import chdir :: String -> Effect Unit cwd :: Effect String cwd = process.cwd --- | Get a copy of the current environment. +-- | Get the current environment. getEnv :: Effect (FO.Object String) -getEnv = mkEffect \_ -> process.env +getEnv = STGlobal.toEffect (FO.freezeST getEnv') + +getEnv' :: STObject Global String +getEnv' = process.env -- | Lookup a particular environment variable. lookupEnv :: String -> Effect (Maybe String) -lookupEnv k = FO.lookup k <$> getEnv +lookupEnv k = STGlobal.toEffect (STObject.peek k getEnv') -- | Set an environment variable. foreign import setEnv :: String -> String -> Effect Unit From a0770029832557c744df049607de8f33584fdaeb Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Fri, 11 Dec 2020 12:48:41 +0800 Subject: [PATCH 2/3] Rename getEnv' to env. Add docs. --- src/Node/Process.purs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Node/Process.purs b/src/Node/Process.purs index cc0a250..6455f06 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -13,7 +13,7 @@ module Node.Process , chdir , cwd , getEnv - , getEnv' + , env , lookupEnv , setEnv , unsetEnv @@ -125,16 +125,17 @@ foreign import chdir :: String -> Effect Unit cwd :: Effect String cwd = process.cwd --- | Get the current environment. +-- | Get a copy of the current environment. getEnv :: Effect (FO.Object String) -getEnv = STGlobal.toEffect (FO.freezeST getEnv') +getEnv = STGlobal.toEffect (FO.freezeST env) -getEnv' :: STObject Global String -getEnv' = process.env +-- | Mutable reference to the current environment. +env :: STObject Global String +env = process.env -- | Lookup a particular environment variable. lookupEnv :: String -> Effect (Maybe String) -lookupEnv k = STGlobal.toEffect (STObject.peek k getEnv') +lookupEnv k = STGlobal.toEffect (STObject.peek k env) -- | Set an environment variable. foreign import setEnv :: String -> String -> Effect Unit From 38cfec18efd1b029b2925221995cbde7e209ebc1 Mon Sep 17 00:00:00 2001 From: Jan Vincent Liwanag Date: Fri, 11 Dec 2020 22:31:36 +0800 Subject: [PATCH 3/3] Change setEnv, unsetEnv to use st instead of ffi --- src/Node/Process.js | 14 -------------- src/Node/Process.purs | 6 ++++-- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/Node/Process.js b/src/Node/Process.js index 27b3de7..df0531e 100644 --- a/src/Node/Process.js +++ b/src/Node/Process.js @@ -46,20 +46,6 @@ exports.chdir = function (dir) { }; }; -exports.setEnv = function (var_) { - return function (val) { - return function () { - process.env[var_] = val; - }; - }; -}; - -exports.unsetEnv = function (var_) { - return function () { - delete process.env[var_]; - }; -}; - exports.exit = function (code) { return function () { process.exit(code); diff --git a/src/Node/Process.purs b/src/Node/Process.purs index 6455f06..00cbc45 100644 --- a/src/Node/Process.purs +++ b/src/Node/Process.purs @@ -138,11 +138,13 @@ lookupEnv :: String -> Effect (Maybe String) lookupEnv k = STGlobal.toEffect (STObject.peek k env) -- | Set an environment variable. -foreign import setEnv :: String -> String -> Effect Unit +setEnv :: String -> String -> Effect Unit +setEnv k v = void $ STGlobal.toEffect (STObject.poke k v env) -- | Delete an environment variable. -- | Use case: to hide secret environment variable from child processes. -foreign import unsetEnv :: String -> Effect Unit +unsetEnv :: String -> Effect Unit +unsetEnv k = void $ STGlobal.toEffect (STObject.delete k env) pid :: Pid pid = process.pid