forked from evanw/kiwi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiwi.sk
350 lines (290 loc) · 8.07 KB
/
kiwi.sk
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
namespace Kiwi {
var _int32 = Int32Array.new(1)
var _float32 = Float32Array.new(_int32.buffer)
class DecodeError {
const message string
}
namespace DecodeError {
def throwOutOfBounds {
throw new("Index out of bounds")
}
def throwInvalidMessage {
throw new("Attempted to parse invalid message")
}
def throwInvalidEnumValue(name string) {
throw new("Invalid value for enum \"\(name)\"")
}
def throwMissingRequiredField(name string) {
throw new("Missing required field \"\(name)\"")
}
def throwInvalidSchema {
throw new("Invalid schema")
}
}
class ByteBuffer {
var _index = 0
var _length = 0
var _data Uint8Array = null
def new {
_data = Uint8Array.new(256)
}
def new(data Uint8Array) {
_data = data
_index = 0
_length = data.length
}
def toUint8Array Uint8Array {
return _data.subarray(0, _length)
}
def readByte int {
if _index + 1 > _length {
DecodeError.throwOutOfBounds
}
return _data[_index++]
}
def readVarFloat double {
var index = _index
var data = _data
# Optimization: use a single byte to store zero
if index + 1 > _length {
DecodeError.throwOutOfBounds
}
var first = data[index]
if first == 0 {
_index = index + 1
return 0
}
# Endian-independent 32-bit read
if index + 4 > _length {
DecodeError.throwOutOfBounds
}
var bits = first | (data[index + 1] << 8) | (data[index + 2] << 16) | (data[index + 3] << 24)
_index = index + 4
# Move the exponent back into place
bits = (bits << 23) | (bits >>> 9)
# Reinterpret as a floating-point number
_int32[0] = bits
return _float32[0]
}
def readVarUint int {
var value = 0
var shift = 0
while true {
var byte = readByte
value |= (byte & 127) << shift
shift += 7
if (byte & 128) == 0 || shift >= 35 {
break
}
}
return value
}
def readVarInt int {
var value = readVarUint
return (value & 1) != 0 ? ~(value >>> 1) : value >>> 1
}
def readString string {
var result = ""
while true {
var codePoint = 0
# Decode UTF-8
var a = readByte
if a < 0xC0 {
codePoint = a
} else {
var b = readByte
if a < 0xE0 {
codePoint = ((a & 0x1F) << 6) | (b & 0x3F)
} else {
var c = readByte
if a < 0xF0 {
codePoint = ((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F)
} else {
var d = readByte
codePoint = ((a & 0x07) << 18) | ((b & 0x3F) << 12) | ((c & 0x3F) << 6) | (d & 0x3F)
}
}
}
# Strings are null-terminated
if codePoint == 0 {
break
}
# Encode unicode
result += string.fromCodePoint(codePoint)
}
return result
}
def _growBy(amount int) {
assert(amount > 0)
if _length + amount > _data.length {
var data = Uint8Array.new(_length + amount << 1)
data.set(_data)
_data = data
}
_length += amount
}
def writeByte(value int) {
assert(0 <= value && value <= 255)
var index = _length
_growBy(1)
_data[index] = value
}
def writeVarFloat(value double) {
var index = _length
# Reinterpret as an integer
_float32[0] = value
var bits = _int32[0]
# Move the exponent to the first 8 bits
bits = (bits >>> 23) | (bits << 9)
# Optimization: use a single byte to store zero and denormals (check for an exponent of 0)
if (bits & 255) == 0 {
writeByte(0)
return
}
# Endian-independent 32-bit write
_growBy(4)
var data = _data
data[index] = bits
data[index + 1] = bits >> 8
data[index + 2] = bits >> 16
data[index + 3] = bits >> 24
}
def writeVarUint(value int) {
while true {
var byte = value & 127
value >>>= 7
writeByte(value != 0 ? byte | 128 : byte)
if value == 0 {
break
}
}
}
def writeVarInt(value int) {
writeVarUint((value << 1) ^ (value >> 31))
}
def writeString(value string) {
var iterator = Unicode.StringIterator.INSTANCE.reset(value, 0)
while true {
# Decode unicode
var codePoint = iterator.nextCodePoint
if codePoint == -1 {
break
}
# Strings are null-terminated
assert(codePoint != 0)
# Encode UTF-8
if codePoint < 0x80 {
writeByte(codePoint)
} else {
if codePoint < 0x800 {
writeByte(((codePoint >> 6) & 0x1F) | 0xC0)
} else {
if codePoint < 0x10000 {
writeByte(((codePoint >> 12) & 0x0F) | 0xE0)
} else {
writeByte(((codePoint >> 18) & 0x07) | 0xF0)
writeByte(((codePoint >> 12) & 0x3F) | 0x80)
}
writeByte(((codePoint >> 6) & 0x3F) | 0x80)
}
writeByte((codePoint & 0x3F) | 0x80)
}
}
# Strings are null-terminated
writeByte(0)
}
}
class BinarySchema {
var _definitions List<Definition> = []
def parse(bb ByteBuffer) {
_definitions = []
var definitionCount = bb.readVarUint
for i in 0..definitionCount {
var name = bb.readString
var kind = bb.readByte as Kind
var fields List<Field> = []
if kind != .ENUM && kind != .STRUCT && kind != .MESSAGE {
DecodeError.throwInvalidSchema
}
for j in 0..bb.readVarUint {
var field = Field.new(bb.readString, bb.readVarInt, bb.readByte as bool, bb.readVarUint)
if field.type < TYPE_STRING || field.type >= definitionCount {
DecodeError.throwInvalidSchema
}
fields.append(field)
}
_definitions.append(Definition.new(name, kind, fields))
}
}
def findDefinition(definition string) int {
for i in 0.._definitions.count {
if _definitions[i].name == definition {
return i
}
}
# Ignore fields we're looking for in an old schema
return -1
}
def skipField(bb ByteBuffer, definition int, field int) {
if definition >= 0 && definition < _definitions.count {
for item in _definitions[definition].fields {
if item.value == field {
_skipField(bb, item)
return
}
}
}
Kiwi.DecodeError.throwInvalidMessage
}
def _skipField(bb ByteBuffer, field Field) {
for i in 0..field.isArray ? bb.readVarUint : 1 {
switch field.type {
case TYPE_BOOL, TYPE_BYTE { bb.readByte }
case TYPE_INT, TYPE_UINT { bb.readVarUint }
case TYPE_FLOAT { bb.readVarFloat }
case TYPE_STRING { while bb.readByte != 0 {} }
default {
assert(field.type >= 0 && field.type < _definitions.count)
var definition = _definitions[field.type]
switch definition.kind {
case .ENUM { bb.readVarUint }
case .STRUCT { for nested in definition.fields { _skipField(bb, nested) } }
case .MESSAGE {
while true {
var id = bb.readVarUint
if id == 0 { break }
skipField(bb, field.type, id)
}
}
default { assert(false) }
}
}
}
}
}
}
namespace BinarySchema {
const TYPE_BOOL = -1
const TYPE_BYTE = -2
const TYPE_INT = -3
const TYPE_UINT = -4
const TYPE_FLOAT = -5
const TYPE_STRING = -6
class Field {
const name string
const type int
const isArray bool
const value int
}
enum Kind {
ENUM
STRUCT
MESSAGE
}
class Definition {
const name string
const kind Kind
const fields List<Field>
}
}
}