diff --git a/cmd/internal/cgen/cgen.go b/cmd/internal/cgen/cgen.go index 1ef0a82d..9f68d8bc 100644 --- a/cmd/internal/cgen/cgen.go +++ b/cmd/internal/cgen/cgen.go @@ -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 are non-positive integers.\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") 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_status_short_dst = -4,\n", pkgName) - fmt.Fprintf(b, "puffs_%s_status_short_src = -6,\n", pkgName) + fmt.Fprintf(b, "puffs_%s_error_bad_version = -2 + 1,\n", pkgName) + fmt.Fprintf(b, "puffs_%s_error_null_receiver = -4 + 1,\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) 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"} { writeCtorSignature(b, pkgName, m, n, ctor) fmt.Fprintf(b, "{\n") + fmt.Fprintf(b, "if (!self) { return; }\n") if ctor == "constructor" { 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) b.WriteString("{\n") - if n.Suspendible() { - fmt.Fprintf(b, "puffs_%s_status ret = puffs_%s_status_ok;\n", pkgName, pkgName) + // Check the previous status and the args. + 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() { - 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") diff --git a/gen/c/gif.c b/gen/c/gif.c index c2dbd013..e0e76d2a 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -34,12 +34,13 @@ extern "C" { // 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 { puffs_gif_status_ok = 0, - puffs_gif_error_bad_version = -1, - puffs_gif_status_short_dst = -4, - puffs_gif_status_short_src = -6, + puffs_gif_error_bad_version = -2 + 1, + puffs_gif_error_null_receiver = -4 + 1, + puffs_gif_status_short_dst = -6, + puffs_gif_status_short_src = -8, } puffs_gif_status; // ---------------- 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, uint32_t puffs_version) { + if (!self) { + return; + } if (puffs_version != PUFFS_VERSION) { self->status = puffs_gif_error_bad_version; return; @@ -93,11 +97,21 @@ void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self, 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 puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) { - puffs_gif_status ret = puffs_gif_status_ok; - return ret; + if (!self) { + return puffs_gif_error_null_receiver; + } + puffs_gif_status status = self->status; + if (status & 1) { + return status; + } + return status; } diff --git a/gen/h/gif.h b/gen/h/gif.h index a29a0442..2420a4be 100644 --- a/gen/h/gif.h +++ b/gen/h/gif.h @@ -34,12 +34,13 @@ extern "C" { // 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 { puffs_gif_status_ok = 0, - puffs_gif_error_bad_version = -1, - puffs_gif_status_short_dst = -4, - puffs_gif_status_short_src = -6, + puffs_gif_error_bad_version = -2 + 1, + puffs_gif_error_null_receiver = -4 + 1, + puffs_gif_status_short_dst = -6, + puffs_gif_status_short_src = -8, } puffs_gif_status; // ---------------- Public Structs diff --git a/test/c/gif/basic.c b/test/c/gif/basic.c index 66794c93..f151621e 100644 --- a/test/c/gif/basic.c +++ b/test/c/gif/basic.c @@ -17,6 +17,14 @@ Each edition should print "PASS", amongst other information, and exit(0). 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() { puffs_gif_lzw_decoder dec; 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. test tests[] = { + test_null_receiver, // test_puffs_version_bad, // test_puffs_version_good, // NULL, //