From e6f5fd7f53f7d669225c50385f0e3f012b499990 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 16 Jun 2017 14:54:51 +1000 Subject: [PATCH] Generate C code for "var x T = foo?()". --- cmd/puffs-gen-c/main.go | 6 +++++- gen/c/std/gif.c | 23 +++++++++++++++++++++++ std/gif/decode_gif.puffs | 9 +++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index c80b73bf..d5760b50 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -883,7 +883,11 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error { case a.KVar: n := n.Var() - // TODO: consider suspendible calls. + if v := n.Value(); v != nil { + if err := g.writeSuspendibles(v, depth); err != nil { + return err + } + } g.printf("%s%s = ", vPrefix, n.Name().String(g.tm)) if v := n.Value(); v != nil { if err := g.writeExpr(v, replaceCallSuspendibles, parenthesesMandatory, 0); err != nil { diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 884b06e4..287c2c55 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -285,6 +285,8 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, goto cleanup0; } + uint8_t v_c; + if (a_src->ri >= a_src->wi) { status = a_src->closed ? puffs_gif_error_unexpected_eof : puffs_gif_status_short_read; @@ -313,6 +315,27 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, status = puffs_gif_error_bad_gif_header; goto cleanup0; } + if (a_src->ri >= a_src->wi) { + status = a_src->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + goto cleanup0; + } + uint8_t t_4 = a_src->ptr[a_src->ri++]; + v_c = t_4; + if ((v_c != 55) && (v_c != 57)) { + status = puffs_gif_error_bad_gif_header; + goto cleanup0; + } + if (a_src->ri >= a_src->wi) { + status = a_src->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + goto cleanup0; + } + uint8_t t_5 = a_src->ptr[a_src->ri++]; + if (t_5 != 97) { + status = puffs_gif_error_bad_gif_header; + goto cleanup0; + } cleanup0: self->private_impl.status = status; diff --git a/std/gif/decode_gif.puffs b/std/gif/decode_gif.puffs index d162e484..d7c9a488 100644 --- a/std/gif/decode_gif.puffs +++ b/std/gif/decode_gif.puffs @@ -1,6 +1,8 @@ // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. +// The GIF spec is https://www.w3.org/Graphics/GIF/spec-gif89a.txt + pub error "bad GIF header" pub struct decoder?( @@ -13,4 +15,11 @@ pub func decoder.decode?(dst ptr buf1, src ptr buf1)() { (in.src.read_u8?() != 0x46) or (in.src.read_u8?() != 0x38) { return error "bad GIF header" } + var c u8 = in.src.read_u8?() + if (c != 0x37) and (c != 0x39) { + return error "bad GIF header" + } + if in.src.read_u8?() != 0x61 { + return error "bad GIF header" + } }