Skip to content

Commit 552e99a

Browse files
authored
Add mutable references for argv, execArgv and env without ST (#26)
1 parent bff6cc2 commit 552e99a

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/Node/Process.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ exports.exit = function (code) {
6565
process.exit(code);
6666
};
6767
};
68+
69+
exports.copyArray = function (xs) {
70+
return function () {
71+
return xs.slice();
72+
};
73+
};
74+
75+
exports.copyObject = function (o) {
76+
return function () {
77+
return Object.assign({}, o);
78+
};
79+
};

src/Node/Process.purs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,13 @@ onSignal sig = onSignalImpl (Signal.toString sig)
8686
nextTick :: Effect Unit -> Effect Unit
8787
nextTick callback = mkEffect \_ -> process.nextTick callback
8888

89-
-- | Get an array containing the command line arguments. Be aware
90-
-- | that this can change over the course of the program.
89+
-- | Get an array containing the command line arguments.
9190
argv :: Effect (Array String)
92-
argv = mkEffect \_ -> process.argv
91+
argv = copyArray process.argv
9392

94-
-- | Node-specific options passed to the `node` executable. Be aware that
95-
-- | this can change over the course of the program.
93+
-- | Node-specific options passed to the `node` executable.
9694
execArgv :: Effect (Array String)
97-
execArgv = mkEffect \_ -> process.execArgv
95+
execArgv = copyArray process.execArgv
9896

9997
-- | The absolute pathname of the `node` executable that started the
10098
-- | process.
@@ -111,11 +109,11 @@ cwd = process.cwd
111109

112110
-- | Get a copy of the current environment.
113111
getEnv :: Effect (FO.Object String)
114-
getEnv = mkEffect \_ -> process.env
112+
getEnv = copyObject process.env
115113

116114
-- | Lookup a particular environment variable.
117115
lookupEnv :: String -> Effect (Maybe String)
118-
lookupEnv k = FO.lookup k <$> getEnv
116+
lookupEnv k = lookupMutableObject k process.env
119117

120118
-- | Set an environment variable.
121119
foreign import setEnv :: String -> String -> Effect Unit
@@ -168,3 +166,15 @@ stderrIsTTY = process.stderr.isTTY
168166
-- | Get the Node.js version.
169167
version :: String
170168
version = process.version
169+
170+
-- Utils
171+
172+
foreign import data MutableArray :: Type -> Type
173+
foreign import data MutableObject :: Type -> Type
174+
175+
foreign import copyArray :: forall a. MutableArray a -> Effect (Array a)
176+
foreign import copyObject :: forall a. MutableObject a -> Effect (FO.Object a)
177+
178+
lookupMutableObject :: forall a. String -> MutableObject a -> Effect (Maybe a)
179+
lookupMutableObject k o =
180+
mkEffect \_ -> FO.lookup k (unsafeCoerce o)

0 commit comments

Comments
 (0)