Differences From
Artifact [500f446f0a]:
134 134 -- generic type constructors --
135 135 -------------------------------
136 136
137 137 function G.int(bits,signed)
138 138 local bytes = math.ceil(bits / 8)
139 139 local max = 2 ^ bits
140 140 local spoint = math.floor(max/2)
141 - return {
142 - sz = bytes;
143 - name = string.format("%sint<%s>",
141 + local name = string.format("%sint<%s>",
144 142 signed and 's' or 'u', bits
145 143 );
144 + return {
145 + name = name;
146 + sz = bytes;
146 147 enc = function(obj)
148 + report('encoding %s value=%s', name, dump(obj))
147 149 obj = obj or 0
148 150 local val = math.abs(obj)
149 151 local str = ''
150 152 if signed then
151 153 local max = math.floor(max / 2)
152 154 if (obj > max) or (obj < (0-(max+1))) then
153 - return m.err.domain end
155 + return error('domain error') end
154 156 if obj < 0 then val = val + spoint end
155 157 -- e.g. for 8bit: 0x80 == -1; 0xFF = -128
156 158 else
157 - if val > max then return m.err.domain end
159 + if val > max then error('domain error') end
158 160 end
159 161 for i=1,bytes do
160 162 local n = math.fmod(val, 0x100)
161 163 str = str .. string.char(n)
162 164 val = math.floor(val / 0x100)
163 165 end
164 166 return str
................................................................................
198 200 def = ...
199 201 end
200 202 name = 'struct' .. (name and ':' .. name or '');
201 203 report('defining struct name=%q fields=%s', name, dump(def))
202 204 return {
203 205 name = name;
204 206 enc = function(obj)
207 + report('encoding struct name=%q vals=%s', name, dump(obj))
205 208 local enc = m.streamEncoder()
206 209 local n = 0
207 210 for k,ty in pairs(def) do n=n+1
208 211 if obj[k] == nil then error('missing key '..dump(k)..' for type '..ty.name) end
209 212 local encoded = ty.enc(obj[k])
210 213 enc.push(T.u8.enc(#k), size.enc(#encoded), k, encoded)
211 214 end