about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--mu.arc24
-rw-r--r--mu.arc.t18
2 files changed, 31 insertions, 11 deletions
diff --git a/mu.arc b/mu.arc
index 366895bd..47d50cea 100644
--- a/mu.arc
+++ b/mu.arc
@@ -203,21 +203,19 @@
         (err "can't take len of non-array @operand")))
 
 (def array-ref-addr (operand idx)
-;?   (prn "aref addr: @operand @idx")
   (assert typeinfo.operand!array)
-  (assert (< -1 idx (array-len operand)))
+  (unless (< -1 idx (array-len operand))
+    (die "aref-addr: out of bounds index @idx for @operand of size @array-len.operand"))
   (withs (elem  typeinfo.operand!elem
           offset  (+ 1 (* idx sz.elem)))
     (+ v.operand offset)))
 
 (def array-ref (operand idx)
-;?   (prn "aref: @operand @idx")
   (assert typeinfo.operand!array)
-  (assert (< -1 idx (array-len operand)))
-;?   (prn "aref2: @operand @idx")
+  (unless (< -1 idx (array-len operand))
+    (die "aref: out of bounds index @idx for @operand of size @array-len.operand"))
   (withs (elem  typeinfo.operand!elem
           offset  (+ 1 (* idx sz.elem)))
-;?     (prn "aref3: @elem @v.operand @offset")
     (m `(,(+ v.operand offset) ,elem))))
 
 ; data structure: routine
@@ -269,9 +267,10 @@
   (let (oargs _ _)  (parse-instr ((body routine 1) (pc routine 1)))
     oargs))
 
-(= running-routines* (queue))
-(= completed-routines* (queue))
-(= routine* nil)
+(on-init
+  (= running-routines* (queue))
+  (= completed-routines* (queue))
+  (= routine* nil))
 
 (def run fn-names
   (ret result 0
@@ -287,6 +286,11 @@
         (enq routine* running-routines*)
         (enq-limit routine* completed-routines*)))))
 
+(def die (msg)
+  (= rep.routine*!error msg)
+  (= rep.routine*!stack-trace rep.routine*!call-stack)
+  (wipe rep.routine*!call-stack))
+
 ($:require "charterm/main.rkt")
 
 (def run-for-time-slice (time-slice)
@@ -493,6 +497,8 @@
                         (err "no such op @op"))
                       (continue))
                 )
+              (when rep.routine*!error
+                (return time-slice))
               ; opcode generated some value, stored in 'tmp'
               ; copy to output args
 ;?               (prn "store: " tmp " " oarg)
diff --git a/mu.arc.t b/mu.arc.t
index 52327ef2..8fbbf7ad 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -519,8 +519,6 @@
 (if (~iso memory* (obj 1 2  2 23 3 nil  4 24 5 t  6 1  7 4))
   (prn "F - 'index-address' returns addresses of indices of arrays"))
 
-; todo: test that out-of-bounds access throws an error
-
 ; Array values know their length. Record lengths are saved in the types table.
 
 (reset)
@@ -1340,6 +1338,22 @@
 ; Eventually we want the right stack-management primitives to build delimited
 ; continuations in mu.
 
+; Routines can throw errors.
+(reset)
+(new-trace "array-bounds-check")
+(add-fns
+  '((main
+      ((1 integer) <- copy (2 literal))
+      ((2 integer) <- copy (23 literal))
+      ((3 integer) <- copy (24 literal))
+      ((4 integer) <- index (1 integer-array) (2 literal)))))
+;? (set dump-trace*)
+(run 'main)
+;? (prn memory*)
+(let last-routine (deq completed-routines*)
+  (if (no rep.last-routine!error)
+    (prn "F - 'index' throws an error if out of bounds")))
+
 ; ---
 
 (reset)
'Blame the previous revision' href='/akkartik/view.love/blame/source_select.lua?id=4121613fc678d71acaa6213300dac3c321530589'>^
73fefa7 ^




73fefa7 ^
637e28f ^
55f5c2d ^
637e28f ^
73fefa7 ^
73fefa7 ^






f2299cb ^

73fefa7 ^














































007b965 ^
73fefa7 ^

























007b965 ^
73fefa7 ^








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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167