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.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 e20e31c..00cbc45 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 + , env , 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. @@ -111,18 +127,24 @@ cwd = process.cwd -- | Get a copy of the current environment. getEnv :: Effect (FO.Object String) -getEnv = mkEffect \_ -> process.env +getEnv = STGlobal.toEffect (FO.freezeST 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 = FO.lookup k <$> getEnv +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