Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit 1efe886

Browse files
committed
Add static fields with data from System.Info
This adds the static fields - arch - compiler_name - compiler_version - os
1 parent 65f47ad commit 1efe886

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ loadProg = do
2626
Documentation
2727
-------------
2828

29+
## Fields
30+
31+
### arch
32+
33+
The machine architecture on which the program is running.
34+
35+
### compiler_name
36+
37+
The Haskell implementation with which the host program was compiled.
38+
39+
### compiler_version
40+
41+
The version of `compiler_name` with which the host program was compiled.
42+
43+
### os
44+
45+
The operating system on which the program is running.
46+
47+
## Functions
48+
2949
### chdir {#system-chdir}
3050

3151
`chdir (directory)`

src/Foreign/Lua/Module/System.hs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@ module Foreign.Lua.Module.System
1717
where
1818

1919
import Control.Applicative ((<$>))
20-
import Control.Exception (IOException, catch, evaluate, try)
20+
import Control.Exception (IOException, try)
2121
import Data.Maybe (fromMaybe)
2222
import Foreign.Lua (Lua, NumResults(..), Optional (..), Peekable, Pushable,
2323
StackIndex, ToHaskellFunction)
24-
import System.IO.Error (IOError, isDoesNotExistError)
2524

25+
import qualified Data.Version
2626
import qualified Foreign.Lua as Lua
2727
import qualified System.Directory as Directory
2828
import qualified System.Environment as Env
29+
import qualified System.Info as Info
2930
import qualified System.IO.Temp as Temp
3031

3132
-- | Pushes the @text@ module to the lua stack.
3233
pushModule :: Lua NumResults
3334
pushModule = do
3435
Lua.newtable
36+
addField "arch" Info.arch
37+
addField "compiler_name" Info.compilerName
38+
addField "compiler_version" (Data.Version.versionBranch Info.compilerVersion)
39+
addField "os" Info.os
3540
addFunction "chdir" chdir
3641
addFunction "currentdir" currentdir
3742
addFunction "env" env
@@ -57,6 +62,12 @@ addPackagePreloader name modulePusher = do
5762
Lua.setfield (-2) name
5863
Lua.pop 1
5964

65+
addField :: Pushable a => String -> a -> Lua ()
66+
addField name value = do
67+
Lua.push name
68+
Lua.push value
69+
Lua.rawset (Lua.nthFromTop 3)
70+
6071
-- | Attach a function to the table at the top of the stack, using the
6172
-- given name.
6273
addFunction :: ToHaskellFunction a => String -> a -> Lua ()
@@ -66,7 +77,7 @@ addFunction name fn = do
6677
Lua.rawset (-3)
6778

6879
-- | Lua callback function
69-
newtype Callback = Callback { callbackStackIndex :: StackIndex }
80+
newtype Callback = Callback StackIndex
7081

7182
instance Peekable Callback where
7283
peek idx = do
@@ -92,7 +103,7 @@ with_tmpdir :: String -- ^ parent dir or template
92103
-> AnyValue -- ^ template or callback
93104
-> Optional Callback -- ^ callback or nil
94105
-> Lua NumResults
95-
with_tmpdir parentDir tmpl callback = do
106+
with_tmpdir parentDir tmpl callback =
96107
case fromOptional callback of
97108
Nothing -> do
98109
-- At most two args. The first arg (parent dir) has probably been

test/system-module-tests.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
--
44
local system = require 'system'
55

6+
-- Check existence static fields
7+
assert(type(system.arch) == 'string')
8+
assert(type(system.compiler_name) == 'string')
9+
assert(type(system.compiler_version) == 'table')
10+
assert(type(system.os) == 'string')
11+
612
local token = 'Banana'
713
function write_read_token (tmpdir)
814
local filename = tmpdir .. '/foo.txt'

0 commit comments

Comments
 (0)