mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Add PUFFS_VERSION.
This commit is contained in:
1 parent
cc9aeb63c1
commit
a9bb43229a
6 files changed
+101
-20
No files matched your search
@@ -29,10 +29,14 @@ func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byt
|
||||
b.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
|
||||
|
||||
b.WriteString("// ---------------- Status Codes\n\n")
|
||||
b.WriteString("// Status codes are non-positive integers.\n")
|
||||
b.WriteString("//\n")
|
||||
b.WriteString("// The least significant bit indicates a fatal status code: an error.\n")
|
||||
b.WriteString("typedef enum {\n")
|
||||
fmt.Fprintf(b, "puffs_%s_status_ok = 0,\n", pkgName)
|
||||
fmt.Fprintf(b, "puffs_%s_status_short_dst = -1,\n", pkgName)
|
||||
fmt.Fprintf(b, "puffs_%s_status_short_src = -2,\n", pkgName)
|
||||
fmt.Fprintf(b, "puffs_%s_error_bad_version = -1,\n", pkgName)
|
||||
fmt.Fprintf(b, "puffs_%s_status_short_dst = -4,\n", pkgName)
|
||||
fmt.Fprintf(b, "puffs_%s_status_short_src = -6,\n", pkgName)
|
||||
fmt.Fprintf(b, "} puffs_%s_status;\n\n", pkgName)
|
||||
|
||||
b.WriteString("// ---------------- Structs\n\n")
|
||||
@@ -92,14 +96,22 @@ func writeStruct(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeCtorSignature(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct, ctor string) {
|
||||
structName := n.Name().String(m)
|
||||
fmt.Fprintf(b, "void puffs_%s_%s_%s(puffs_%s_%s *self", pkgName, structName, ctor, pkgName, structName)
|
||||
if ctor == "constructor" {
|
||||
fmt.Fprintf(b, ", uint32_t puffs_version")
|
||||
}
|
||||
fmt.Fprintf(b, ")")
|
||||
}
|
||||
|
||||
func writeCtorPrototypes(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error {
|
||||
if !n.Suspendible() {
|
||||
return nil
|
||||
}
|
||||
structName := n.Name().String(m)
|
||||
for _, ctor := range []string{"constructor", "destructor"} {
|
||||
fmt.Fprintf(b, "void puffs_%s_%s_%s(puffs_%s_%s *self);\n\n",
|
||||
pkgName, structName, ctor, pkgName, structName)
|
||||
writeCtorSignature(b, pkgName, m, n, ctor)
|
||||
b.WriteString(";\n\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -108,14 +120,20 @@ func writeCtorImpls(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) er
|
||||
if !n.Suspendible() {
|
||||
return nil
|
||||
}
|
||||
structName := n.Name().String(m)
|
||||
for _, ctor := range []string{"constructor", "destructor"} {
|
||||
fmt.Fprintf(b, "void puffs_%s_%s_%s(puffs_%s_%s *self) {\n",
|
||||
pkgName, structName, ctor, pkgName, structName)
|
||||
writeCtorSignature(b, pkgName, m, n, ctor)
|
||||
fmt.Fprintf(b, "{\n")
|
||||
|
||||
if ctor == "constructor" {
|
||||
fmt.Fprintf(b, "if (puffs_version != PUFFS_VERSION) {\n")
|
||||
fmt.Fprintf(b, "self->status = puffs_%s_error_bad_version;\n", pkgName)
|
||||
fmt.Fprintf(b, "return;\n")
|
||||
fmt.Fprintf(b, "}\n")
|
||||
|
||||
b.WriteString("memset(self, 0, sizeof(*self));\n")
|
||||
// TODO: set any non-zero default values.
|
||||
}
|
||||
|
||||
// TODO: call any ctor/dtors on sub-structures.
|
||||
b.WriteString("}\n\n")
|
||||
}
|
||||
|
||||
@@ -6,5 +6,5 @@
|
||||
package cgen
|
||||
|
||||
const preamble = "" +
|
||||
"// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n" +
|
||||
"// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in preamble.h.\n#define PUFFS_VERSION (0x00001)\n" +
|
||||
""
|
||||
@@ -14,3 +14,14 @@
|
||||
#if __WORDSIZE < 32
|
||||
#error "Puffs requires a word size of at least 32 bits"
|
||||
#endif
|
||||
|
||||
// PUFFS_VERSION is the major.minor version number as a uint32. The major
|
||||
// number is the high 16 bits. The minor number is the low 16 bits.
|
||||
//
|
||||
// The intention is to bump the version number at least on every API / ABI
|
||||
// backwards incompatible change.
|
||||
//
|
||||
// For now, the API and ABI are simply unstable and can change at any time.
|
||||
//
|
||||
// TODO: don't hard code this in preamble.h.
|
||||
#define PUFFS_VERSION (0x00001)
|
||||
Reference in new issue
Block a user