-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate.go
52 lines (45 loc) · 1.06 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2020 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"hash/crc64"
"log"
"os"
"os/exec"
)
func main() {
wat2wasm := os.Getenv("WAT2WASM")
if wat2wasm == "" {
wat2wasm = "wat2wasm"
}
cmd := exec.Command(wat2wasm, "-o", "/dev/stdout", "library.wat")
cmd.Stderr = os.Stderr
wasm, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
checksum := crc64.Checksum(wasm, crc64.MakeTable(crc64.ECMA))
f, err := os.Create("library.go")
if err != nil {
log.Fatal(err)
}
defer f.Close()
fmt.Fprintln(f, "// Code generated by internal/cmd/abi-library. DO NOT EDIT.")
fmt.Fprintln(f)
fmt.Fprintln(f, "package abi")
fmt.Fprintln(f)
fmt.Fprintf(f, "const LibraryChecksum uint64 = 0x%016x\n", checksum)
fmt.Fprintln(f)
fmt.Fprint(f, "var libraryWASM = [...]byte{")
for i, b := range wasm {
if i%12 == 0 {
fmt.Fprintf(f, "\n\t")
} else {
fmt.Fprintf(f, " ")
}
fmt.Fprintf(f, "0x%02x,", b)
}
fmt.Fprintln(f, "\n}")
}