Skip to content

Add mutable references for argv, execArgv and env without ST #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 15, 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
12 changes: 12 additions & 0 deletions src/Node/Process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};
26 changes: 18 additions & 8 deletions src/Node/Process.purs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@ 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 = 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 = mkEffect \_ -> process.execArgv
execArgv = copyArray process.execArgv

-- | The absolute pathname of the `node` executable that started the
-- | process.
Expand All @@ -111,11 +109,11 @@ 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)
lookupEnv k = FO.lookup k <$> getEnv
lookupEnv k = lookupMutableObject k process.env

-- | Set an environment variable.
foreign import setEnv :: String -> String -> Effect Unit
Expand Down Expand Up @@ -168,3 +166,15 @@ 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)

lookupMutableObject :: forall a. String -> MutableObject a -> Effect (Maybe a)
lookupMutableObject k o =
mkEffect \_ -> FO.lookup k (unsafeCoerce o)