aboutsummaryrefslogtreecommitdiff
path: root/tools/lxdrgen-mjs.awk
blob: a9a81f7f51cc6924b519f0dd0103769609ecc561 (plain)
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
# lxdrgen-mjs.awk: Javascript backend for lxdrgen.awk.
#
# Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name>
# SPDX-License-Identifier: 0BSD
#
# This backend is currently for decoding the binary format only.
# (JSON is way too expensive to process and transfer.)
#
# Import the resulting script as a Javascript module.
# Identifiers intentionally aren't prefixed.

function define_internal(name) {
	Types[name] = "internal"
}

function define_sint(size,    shortname) {
	shortname = "i" size
	define_internal(shortname)
	CodegenDeserialize[shortname] = "\t%s = r." shortname "()\n"

	print ""
	print "\t" shortname "() {"
	if (size == "64") {
		# XXX: 2^53 - 1 must be enough for anyone.  BigInts are a PITA.
		print "\t\tconst " shortname \
			" = Number(this.getBigInt" size "(this.offset))"
	} else {
		print "\t\tconst " shortname " = this.getInt" size "(this.offset)"
	}
	print "\t\tthis.offset += " (size / 8)
	print "\t\treturn " shortname
	print "\t}"
}

function define_uint(size,    shortname) {
	shortname = "u" size
	define_internal(shortname)
	CodegenDeserialize[shortname] = "\t%s = r." shortname "()\n"

	print ""
	print "\t" shortname "() {"
	if (size == "64") {
		# XXX: 2^53 - 1 must be enough for anyone.  BigInts are a PITA.
		print "\t\tconst " shortname \
			" = Number(this.getBigUint" size "(this.offset))"
	} else {
		print "\t\tconst " shortname " = this.getUint" size "(this.offset)"
	}
	print "\t\tthis.offset += " (size / 8)
	print "\t\treturn " shortname
	print "\t}"
}

function codegen_begin() {
	print "// Code generated from " FILENAME ". DO NOT EDIT."
	print ""
	print "export class Reader extends DataView {"
	print "\tconstructor() {"
	print "\t\tsuper(...arguments)"
	print "\t\tthis.offset = 0"
	print "\t\tthis.decoder = new TextDecoder('utf-8', {fatal: true})"
	print "\t}"
	print ""
	print "\tget empty() {"
	print "\t\treturn this.byteLength <= this.offset"
	print "\t}"
	print ""
	print "\trequire(len) {"
	print "\t\tif (this.byteLength - this.offset < len)"
	print "\t\t\tthrow `Premature end of data`"
	print "\t\treturn this.byteOffset + this.offset"
	print "\t}"

	define_internal("string")
	CodegenDeserialize["string"] = "\t%s = r.string()\n"

	print ""
	print "\tstring() {"
	print "\t\tconst len = this.getUint32(this.offset)"
	print "\t\tthis.offset += 4"
	print "\t\tconst array = new Uint8Array("
	print "\t\t\tthis.buffer, this.require(len), len)"
	print "\t\tthis.offset += len"
	print "\t\treturn this.decoder.decode(array)"
	print "\t}"

	define_internal("bool")
	CodegenDeserialize["bool"] = "\t%s = r.bool()\n"

	print ""
	print "\tbool() {"
	print "\t\tconst u8 = this.getUint8(this.offset)"
	print "\t\tthis.offset += 1"
	print "\t\treturn u8 != 0"
	print "\t}"

	define_sint("8")
	define_sint("16")
	define_sint("32")
	define_sint("64")
	define_uint("8")
	define_uint("16")
	define_uint("32")
	define_uint("64")

	print "}"
}

function codegen_constant(name, value) {
	print ""
	print "export const " decapitalize(snaketocamel(name)) " = " value
}

function codegen_enum_value(name, subname, value, cg) {
	append(cg, "fields", "\t" snaketocamel(subname) ": " value ",\n")
}

function codegen_enum(name, cg) {
	print ""
	print "export const " name " = Object.freeze({"
	print cg["fields"] "})"

	CodegenDeserialize[name] = "\t%s = r.i8()\n"
	for (i in cg)
		delete cg[i]
}

function codegen_struct_field(d, cg,    camel, f, deserialize) {
	camel = decapitalize(snaketocamel(d["name"]))
	f = "s." camel
	append(cg, "fields", "\t" camel "\n")

	deserialize = CodegenDeserialize[d["type"]]
	if (!d["isarray"]) {
		append(cg, "deserialize", sprintf(deserialize, f))
		return
	}

	append(cg, "deserialize",
		"\t{\n" \
		indent(sprintf(CodegenDeserialize["u32"], "const len")))
	if (d["type"] == "u8") {
		append(cg, "deserialize",
			"\t\t" f " = new Uint8Array(\n" \
			"\t\t\tr.buffer, r.require(len), len)\n" \
			"\t\tr.offset += len\n" \
			"\t}\n")
		return
	}
	if (d["type"] == "i8") {
		append(cg, "deserialize",
			"\t\t" f " = new Int8Array(\n" \
			"\t\t\tr.buffer, r.require(len), len)\n" \
			"\t\tr.offset += len\n" \
			"\t}\n")
		return
	}

	append(cg, "deserialize",
		"\t\t" f " = new Array(len)\n" \
		"\t}\n" \
		"\tfor (let i = 0; i < " f ".length; i++)\n" \
		indent(sprintf(deserialize, f "[i]")))
}

function codegen_struct_tag(d, cg) {
	append(cg, "fields", "\t" decapitalize(snaketocamel(d["name"])) "\n")
	# Do not deserialize here, that is already done by the containing union.
}

function codegen_struct(name, cg) {
	print ""
	print "export class " name " {"
	print cg["fields"] cg["methods"]
	print "\tstatic deserialize(r) {"
	print "\t\tconst s = new " name "()"
	print indent(cg["deserialize"]) "\t\treturn s"
	print "\t}"
	print "}"

	CodegenDeserialize[name] = "\t%s = " name ".deserialize(r)\n"
	for (i in cg)
		delete cg[i]
}

function codegen_union_tag(d, cg) {
	cg["tagtype"] = d["type"]
	cg["tagname"] = d["name"]
}

function codegen_union_struct(name, casename, cg, scg,     structname) {
	append(scg, "methods",
		"\n" \
		"\tconstructor() {\n" \
		"\t\tthis." decapitalize(snaketocamel(cg["tagname"])) \
			" = " cg["tagtype"] "." snaketocamel(casename) "\n" \
		"\t}\n")

	# And thus not all generated structs are present in Types.
	structname = name snaketocamel(casename)
	codegen_struct(structname, scg)

	append(cg, "deserialize",
		"\tcase " cg["tagtype"] "." snaketocamel(casename) ":\n" \
		"\t{\n" \
		indent(sprintf(CodegenDeserialize[structname], "const s")) \
		"\t\treturn s\n" \
		"\t}\n")
}

function codegen_union(name, cg,    tagvar) {
	tagvar = decapitalize(snaketocamel(cg["tagname"]))

	print ""
	print "export function deserialize" name "(r) {"
	print sprintf(CodegenDeserialize[cg["tagtype"]], "const " tagvar) \
		"\tswitch (" tagvar ") {"
	print cg["deserialize"] "\tdefault:"
	print "\t\tthrow `Unknown " cg["tagtype"] " (${tagvar})`"
	print "\t}"
	print "}"

	CodegenDeserialize[name] = "\t%s = deserialize" name "(r)\n"
	for (i in cg)
		delete cg[i]
}