-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.carp
More file actions
42 lines (33 loc) · 1.64 KB
/
Copy patherrors.carp
File metadata and controls
42 lines (33 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(load "https://github.com/carpentry-org/lua@0.2.0")
(Lua.setup "lua")
; demonstrating error handling across the API
(defn main []
(Lua.with-lua-do
(Lua.libs lua)
(match (Luax.do-in lua "x = 1 + 1")
(Result.Success _) (IO.println "valid code: ok")
(Result.Error e) (IO.errorln &e))
(match (Luax.do-in lua "x = nil + 1")
(Result.Success _) (IO.println "should not reach here")
(Result.Error e) (IO.println &(fmt "caught: %s" &e)))
(match (Luax.call-fn lua no-such-function Lua.get-int)
(Result.Success _) (IO.println "should not reach here")
(Result.Error e) (IO.println &(fmt "caught: %s" &e)))
(ignore (Lua.do-string lua (cstr "function boom() error('kaboom!') end")))
(match (Luax.call-fn lua boom Lua.get-int)
(Result.Success _) (IO.println "should not reach here")
(Result.Error e) (IO.println &(fmt "caught: %s" &e)))
(match (Lua.eval-file lua "nonexistent.lua")
(Result.Success _) (IO.println "should not reach here")
(Result.Error e) (IO.println &(fmt "caught: %s" &e)))
(Lua.push-int lua 42)
(match (Luax.maybe-get-string lua -1)
(Maybe.Just _) (IO.println "should not reach here")
(Maybe.Nothing) (IO.println "type mismatch: got Nothing (expected)"))
(match (Luax.maybe-get-int lua -1)
(Maybe.Just n) (IO.println &(fmt "correct type: %d" n))
(Maybe.Nothing) (IO.println "should not reach here"))
(Lua.pop lua 1)
(Luax.set-int-global lua "present" 1)
(IO.println &(fmt "present exists: %s" &(str (Lua.global-exists? lua "present"))))
(IO.println &(fmt "absent exists: %s" &(str (Lua.global-exists? lua "absent"))))))