v1.0.0
This commit is contained in:
+41
-7
@@ -16,10 +16,31 @@ func GenerateTypeScript(messages []parser.Message, namespace string) ([]byte, er
|
||||
func GenerateTypeScriptSchema(schema parser.Schema, namespace string) ([]byte, error) {
|
||||
messages := schema.Messages
|
||||
var b strings.Builder
|
||||
needsLengthGuards := schemaNeedsLengthGuards(messages)
|
||||
needsQuantGuards := schemaNeedsQuantRangeGuards(messages)
|
||||
|
||||
b.WriteString("// <auto-generated> arpack </auto-generated>\n")
|
||||
b.WriteString("// Code generated by arpack. DO NOT EDIT.\n\n")
|
||||
|
||||
if needsLengthGuards {
|
||||
b.WriteString("const arpackTextEncoder = new TextEncoder();\n")
|
||||
b.WriteString("const arpackTextDecoder = new TextDecoder();\n\n")
|
||||
b.WriteString("function arpackEnsureUint16Length(length: number, context: string): number {\n")
|
||||
b.WriteString(" if (length > 65535) {\n")
|
||||
b.WriteString(" throw new RangeError(\"arpack: \" + context + \" exceeds uint16 limit\");\n")
|
||||
b.WriteString(" }\n")
|
||||
b.WriteString(" return length;\n")
|
||||
b.WriteString("}\n\n")
|
||||
}
|
||||
|
||||
if needsQuantGuards {
|
||||
b.WriteString("function arpackEnsureQuantizedRange(value: number, min: number, max: number, context: string): void {\n")
|
||||
b.WriteString(" if (Number.isNaN(value) || value < min || value > max) {\n")
|
||||
b.WriteString(" throw new RangeError(\"arpack: quantized value out of range for \" + context);\n")
|
||||
b.WriteString(" }\n")
|
||||
b.WriteString("}\n\n")
|
||||
}
|
||||
|
||||
enumNames := make(map[string]struct{}, len(schema.Enums))
|
||||
for _, enum := range schema.Enums {
|
||||
enumNames[enum.Name] = struct{}{}
|
||||
@@ -148,7 +169,10 @@ func writeTSSerializeField(b *strings.Builder, recv string, f parser.Field, inde
|
||||
}
|
||||
fmt.Fprintf(b, "%s}\n", indent)
|
||||
case parser.KindSlice:
|
||||
fmt.Fprintf(b, "%sview.setUint16(pos, %s.length, true);\n", indent, access)
|
||||
lenVar := "_len" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sconst %s = arpackEnsureUint16Length(%s.length, %q);\n",
|
||||
indent, lenVar, access, lengthContext(f))
|
||||
fmt.Fprintf(b, "%sview.setUint16(pos, %s, true);\n", indent, lenVar)
|
||||
fmt.Fprintf(b, "%spos += 2;\n", indent)
|
||||
iVar := "_i" + f.Name
|
||||
fmt.Fprintf(b, "%sfor (const %s of %s) {\n", indent, iVar, access)
|
||||
@@ -222,8 +246,11 @@ func writeTSSerializePrimitiveElement(b *strings.Builder, access string, f parse
|
||||
fmt.Fprintf(b, "%spos += 8;\n", indent)
|
||||
case parser.KindString:
|
||||
lenVar := "_slen" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sconst %s = new TextEncoder().encode(%s);\n", indent, lenVar, valueExpr)
|
||||
fmt.Fprintf(b, "%sview.setUint16(pos, %s.length, true);\n", indent, lenVar)
|
||||
guardVar := "_slenChecked" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sconst %s = arpackTextEncoder.encode(%s);\n", indent, lenVar, valueExpr)
|
||||
fmt.Fprintf(b, "%sconst %s = arpackEnsureUint16Length(%s.length, %q);\n",
|
||||
indent, guardVar, lenVar, lengthContext(f))
|
||||
fmt.Fprintf(b, "%sview.setUint16(pos, %s, true);\n", indent, guardVar)
|
||||
fmt.Fprintf(b, "%spos += 2;\n", indent)
|
||||
fmt.Fprintf(b, "%snew Uint8Array(view.buffer, pos, %s.length).set(%s);\n", indent, lenVar, lenVar)
|
||||
fmt.Fprintf(b, "%spos += %s.length;\n", indent, lenVar)
|
||||
@@ -273,8 +300,11 @@ func writeTSSerializePrimitive(b *strings.Builder, access string, f parser.Field
|
||||
fmt.Fprintf(b, "%spos += 8;\n", indent)
|
||||
case parser.KindString:
|
||||
lenVar := "_slen" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sconst %s = new TextEncoder().encode(%s);\n", indent, lenVar, valueExpr)
|
||||
fmt.Fprintf(b, "%sview.setUint16(pos, %s.length, true);\n", indent, lenVar)
|
||||
guardVar := "_slenChecked" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sconst %s = arpackTextEncoder.encode(%s);\n", indent, lenVar, valueExpr)
|
||||
fmt.Fprintf(b, "%sconst %s = arpackEnsureUint16Length(%s.length, %q);\n",
|
||||
indent, guardVar, lenVar, lengthContext(f))
|
||||
fmt.Fprintf(b, "%sview.setUint16(pos, %s, true);\n", indent, guardVar)
|
||||
fmt.Fprintf(b, "%spos += 2;\n", indent)
|
||||
fmt.Fprintf(b, "%snew Uint8Array(view.buffer, pos, %s.length).set(%s);\n", indent, lenVar, lenVar)
|
||||
fmt.Fprintf(b, "%spos += %s.length;\n", indent, lenVar)
|
||||
@@ -286,6 +316,8 @@ func writeTSSerializeQuant(b *strings.Builder, access string, f parser.Field, in
|
||||
q := f.Quant
|
||||
maxUint := q.MaxUint()
|
||||
varName := "_q" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sarpackEnsureQuantizedRange(%s, %g, %g, %q);\n",
|
||||
indent, access, q.Min, q.Max, quantContext(f))
|
||||
if q.Bits == 8 {
|
||||
fmt.Fprintf(b, "%sconst %s = Math.trunc((%s - (%g)) / (%g - (%g)) * %g);\n",
|
||||
indent, varName, access, q.Min, q.Max, q.Min, maxUint)
|
||||
@@ -304,6 +336,8 @@ func writeTSSerializeQuantElement(b *strings.Builder, access string, f parser.Fi
|
||||
q := f.Quant
|
||||
maxUint := q.MaxUint()
|
||||
varName := "_q" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sarpackEnsureQuantizedRange(%s, %g, %g, %q);\n",
|
||||
indent, access, q.Min, q.Max, quantContext(f))
|
||||
if q.Bits == 8 {
|
||||
fmt.Fprintf(b, "%sconst %s = Math.trunc((%s - (%g)) / (%g - (%g)) * %g);\n",
|
||||
indent, varName, access, q.Min, q.Max, q.Min, maxUint)
|
||||
@@ -438,7 +472,7 @@ func writeTSDeserializePrimitiveElement(b *strings.Builder, access string, f par
|
||||
lenVar := "_slen" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sconst %s = view.getUint16(pos, true);\n", indent, lenVar)
|
||||
fmt.Fprintf(b, "%spos += 2;\n", indent)
|
||||
expr := fmt.Sprintf("new TextDecoder().decode(new Uint8Array(view.buffer, pos, %s))", lenVar)
|
||||
expr := fmt.Sprintf("arpackTextDecoder.decode(new Uint8Array(view.buffer, pos, %s))", lenVar)
|
||||
fmt.Fprintf(b, "%s%s = %s;\n", indent, access, tsDeserializeValueExpr(expr, f, enumNames))
|
||||
fmt.Fprintf(b, "%spos += %s;\n", indent, lenVar)
|
||||
}
|
||||
@@ -499,7 +533,7 @@ func writeTSDeserializePrimitive(b *strings.Builder, access string, f parser.Fie
|
||||
lenVar := "_slen" + sanitizeVarName(access)
|
||||
fmt.Fprintf(b, "%sconst %s = view.getUint16(pos, true);\n", indent, lenVar)
|
||||
fmt.Fprintf(b, "%spos += 2;\n", indent)
|
||||
expr := fmt.Sprintf("new TextDecoder().decode(new Uint8Array(view.buffer, pos, %s))", lenVar)
|
||||
expr := fmt.Sprintf("arpackTextDecoder.decode(new Uint8Array(view.buffer, pos, %s))", lenVar)
|
||||
fmt.Fprintf(b, "%s%s = %s;\n", indent, access, tsDeserializeValueExpr(expr, f, enumNames))
|
||||
fmt.Fprintf(b, "%spos += %s;\n", indent, lenVar)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user