appwat2c

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2024 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CmdWat2c = &cli.Command{
	Hidden:    true,
	Name:      "wat2c",
	Usage:     "convert a WebAssembly text file to a C source and header",
	ArgsUsage: "<file.wat>",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "output",
			Aliases: []string{"o"},
			Usage:   "set code output file",
			Value:   "a.out.c",
		},
		&cli.StringFlag{
			Name:    "header",
			Aliases: []string{"hdr"},
			Usage:   "set header output file",
			Value:   "a.out.h",
		},
	},
	Action: func(c *cli.Context) error {
		if c.NArg() == 0 {
			fmt.Fprintf(os.Stderr, "no input file")
			os.Exit(1)
		}

		infile := c.Args().First()
		outfile := c.String("output")
		hdrfile := c.String("header")

		if outfile == "" {
			outfile = infile
			if n1, n2 := len(outfile), len(".wat"); n1 > n2 {
				if s := outfile[n1-n2:]; strings.EqualFold(s, ".wat") {
					outfile = outfile[:n1-n2]
				}
			}
			outfile += ".c"
		}
		if !strings.HasSuffix(outfile, ".c") {
			outfile += ".c"
		}

		source, err := os.ReadFile(infile)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		code, header, err := watutil.Wat2C(infile, source)
		if err != nil {
			os.WriteFile(outfile, code, 0666)
			fmt.Println(err)
			os.Exit(1)
		}

		if hdrfile != "" {
			err = os.WriteFile(hdrfile, header, 0666)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		}

		err = os.WriteFile(outfile, code, 0666)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL