mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Check function status and args on entry.
This commit is contained in:
1 parent
c97511f24e
commit
aa46c5479e
4 files changed
+65
-19
No files matched your search
@@ -39,12 +39,13 @@ func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byt
|
|||||||
b.WriteString("// ---------------- Status Codes\n\n")
|
b.WriteString("// ---------------- Status Codes\n\n")
|
||||||
b.WriteString("// Status codes are non-positive integers.\n")
|
b.WriteString("// Status codes are non-positive integers.\n")
|
||||||
b.WriteString("//\n")
|
b.WriteString("//\n")
|
||||||
b.WriteString("// The least significant bit indicates a fatal status code: an error.\n")
|
b.WriteString("// The least significant bit indicates a non-recoverable status code: an error.\n")
|
||||||
b.WriteString("typedef enum {\n")
|
b.WriteString("typedef enum {\n")
|
||||||
fmt.Fprintf(b, "puffs_%s_status_ok = 0,\n", pkgName)
|
fmt.Fprintf(b, "puffs_%s_status_ok = 0,\n", pkgName)
|
||||||
fmt.Fprintf(b, "puffs_%s_error_bad_version = -1,\n", pkgName)
|
fmt.Fprintf(b, "puffs_%s_error_bad_version = -2 + 1,\n", pkgName)
|
||||||
fmt.Fprintf(b, "puffs_%s_status_short_dst = -4,\n", pkgName)
|
fmt.Fprintf(b, "puffs_%s_error_null_receiver = -4 + 1,\n", pkgName)
|
||||||
fmt.Fprintf(b, "puffs_%s_status_short_src = -6,\n", pkgName)
|
fmt.Fprintf(b, "puffs_%s_status_short_dst = -6,\n", pkgName)
|
||||||
|
fmt.Fprintf(b, "puffs_%s_status_short_src = -8,\n", pkgName)
|
||||||
fmt.Fprintf(b, "} puffs_%s_status;\n\n", pkgName)
|
fmt.Fprintf(b, "} puffs_%s_status;\n\n", pkgName)
|
||||||
|
|
||||||
b.WriteString("// ---------------- Public Structs\n\n")
|
b.WriteString("// ---------------- Public Structs\n\n")
|
||||||
@@ -181,6 +182,7 @@ func writeCtorImpls(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) er
|
|||||||
for _, ctor := range []string{"constructor", "destructor"} {
|
for _, ctor := range []string{"constructor", "destructor"} {
|
||||||
writeCtorSignature(b, pkgName, m, n, ctor)
|
writeCtorSignature(b, pkgName, m, n, ctor)
|
||||||
fmt.Fprintf(b, "{\n")
|
fmt.Fprintf(b, "{\n")
|
||||||
|
fmt.Fprintf(b, "if (!self) { return; }\n")
|
||||||
|
|
||||||
if ctor == "constructor" {
|
if ctor == "constructor" {
|
||||||
fmt.Fprintf(b, "if (puffs_version != PUFFS_VERSION) {\n")
|
fmt.Fprintf(b, "if (puffs_version != PUFFS_VERSION) {\n")
|
||||||
@@ -228,12 +230,32 @@ func writeFuncImpl(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Func) error
|
|||||||
writeFuncSignature(b, pkgName, m, n)
|
writeFuncSignature(b, pkgName, m, n)
|
||||||
b.WriteString("{\n")
|
b.WriteString("{\n")
|
||||||
|
|
||||||
if n.Suspendible() {
|
// Check the previous status and the args.
|
||||||
fmt.Fprintf(b, "puffs_%s_status ret = puffs_%s_status_ok;\n", pkgName, pkgName)
|
if n.Public() {
|
||||||
|
if n.Receiver() != 0 {
|
||||||
|
fmt.Fprintf(b, "if (!self) { return puffs_%s_error_null_receiver; }\n", pkgName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// TODO: generate the function body.
|
|
||||||
if n.Suspendible() {
|
if n.Suspendible() {
|
||||||
fmt.Fprintf(b, "return ret;\n")
|
fmt.Fprintf(b, "puffs_%s_status status = ", pkgName)
|
||||||
|
if n.Receiver() != 0 {
|
||||||
|
fmt.Fprintf(b, "self->status;\n")
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(b, "puffs_%s_status_ok;\n", pkgName)
|
||||||
|
}
|
||||||
|
} else if r := n.Receiver(); r != 0 {
|
||||||
|
// TODO: fix this.
|
||||||
|
return fmt.Errorf(`cannot convert Puffs function "%s.%s" to C`, r.String(m), n.Name().String(m))
|
||||||
|
}
|
||||||
|
if n.Public() {
|
||||||
|
fmt.Fprintf(b, "if (status & 1) { return status; }")
|
||||||
|
}
|
||||||
|
// TODO: check the args.
|
||||||
|
|
||||||
|
// TODO: generate the function body.
|
||||||
|
|
||||||
|
if n.Suspendible() {
|
||||||
|
fmt.Fprintf(b, "return status;\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
b.WriteString("}\n\n")
|
b.WriteString("}\n\n")
|
||||||
|
|||||||
+21
-7
@@ -34,12 +34,13 @@ extern "C" {
|
|||||||
|
|
||||||
// Status codes are non-positive integers.
|
// Status codes are non-positive integers.
|
||||||
//
|
//
|
||||||
// The least significant bit indicates a fatal status code: an error.
|
// The least significant bit indicates a non-recoverable status code: an error.
|
||||||
typedef enum {
|
typedef enum {
|
||||||
puffs_gif_status_ok = 0,
|
puffs_gif_status_ok = 0,
|
||||||
puffs_gif_error_bad_version = -1,
|
puffs_gif_error_bad_version = -2 + 1,
|
||||||
puffs_gif_status_short_dst = -4,
|
puffs_gif_error_null_receiver = -4 + 1,
|
||||||
puffs_gif_status_short_src = -6,
|
puffs_gif_status_short_dst = -6,
|
||||||
|
puffs_gif_status_short_src = -8,
|
||||||
} puffs_gif_status;
|
} puffs_gif_status;
|
||||||
|
|
||||||
// ---------------- Public Structs
|
// ---------------- Public Structs
|
||||||
@@ -85,6 +86,9 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self);
|
|||||||
|
|
||||||
void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self,
|
void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self,
|
||||||
uint32_t puffs_version) {
|
uint32_t puffs_version) {
|
||||||
|
if (!self) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (puffs_version != PUFFS_VERSION) {
|
if (puffs_version != PUFFS_VERSION) {
|
||||||
self->status = puffs_gif_error_bad_version;
|
self->status = puffs_gif_error_bad_version;
|
||||||
return;
|
return;
|
||||||
@@ -93,11 +97,21 @@ void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self,
|
|||||||
self->magic = PUFFS_MAGIC;
|
self->magic = PUFFS_MAGIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) {}
|
void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) {
|
||||||
|
if (!self) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------- Function Implementations
|
// ---------------- Function Implementations
|
||||||
|
|
||||||
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
|
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
|
||||||
puffs_gif_status ret = puffs_gif_status_ok;
|
if (!self) {
|
||||||
return ret;
|
return puffs_gif_error_null_receiver;
|
||||||
|
}
|
||||||
|
puffs_gif_status status = self->status;
|
||||||
|
if (status & 1) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
+5
-4
@@ -34,12 +34,13 @@ extern "C" {
|
|||||||
|
|
||||||
// Status codes are non-positive integers.
|
// Status codes are non-positive integers.
|
||||||
//
|
//
|
||||||
// The least significant bit indicates a fatal status code: an error.
|
// The least significant bit indicates a non-recoverable status code: an error.
|
||||||
typedef enum {
|
typedef enum {
|
||||||
puffs_gif_status_ok = 0,
|
puffs_gif_status_ok = 0,
|
||||||
puffs_gif_error_bad_version = -1,
|
puffs_gif_error_bad_version = -2 + 1,
|
||||||
puffs_gif_status_short_dst = -4,
|
puffs_gif_error_null_receiver = -4 + 1,
|
||||||
puffs_gif_status_short_src = -6,
|
puffs_gif_status_short_dst = -6,
|
||||||
|
puffs_gif_status_short_src = -8,
|
||||||
} puffs_gif_status;
|
} puffs_gif_status;
|
||||||
|
|
||||||
// ---------------- Public Structs
|
// ---------------- Public Structs
|
||||||
|
|||||||
@@ -17,6 +17,14 @@ Each edition should print "PASS", amongst other information, and exit(0).
|
|||||||
|
|
||||||
const char* test_filename = "gif/basic.c";
|
const char* test_filename = "gif/basic.c";
|
||||||
|
|
||||||
|
void test_null_receiver() {
|
||||||
|
puffs_gif_status status = puffs_gif_lzw_decoder_decode(NULL);
|
||||||
|
if (status != puffs_gif_error_null_receiver) {
|
||||||
|
FAIL("test_null_receiver: status: got %d, want %d", status,
|
||||||
|
puffs_gif_error_null_receiver);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void test_puffs_version_bad() {
|
void test_puffs_version_bad() {
|
||||||
puffs_gif_lzw_decoder dec;
|
puffs_gif_lzw_decoder dec;
|
||||||
puffs_gif_lzw_decoder_constructor(&dec, 0); // 0 is not PUFFS_VERSION.
|
puffs_gif_lzw_decoder_constructor(&dec, 0); // 0 is not PUFFS_VERSION.
|
||||||
@@ -48,6 +56,7 @@ cleanup0:
|
|||||||
|
|
||||||
// The empty comments forces clang-format to place one element per line.
|
// The empty comments forces clang-format to place one element per line.
|
||||||
test tests[] = {
|
test tests[] = {
|
||||||
|
test_null_receiver, //
|
||||||
test_puffs_version_bad, //
|
test_puffs_version_bad, //
|
||||||
test_puffs_version_good, //
|
test_puffs_version_good, //
|
||||||
NULL, //
|
NULL, //
|
||||||
|
|||||||
Reference in new issue
Block a user