-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (72 loc) · 1.98 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"time"
"github.com/aligator/goslice"
"github.com/aligator/goslice/data"
"github.com/dev/go/goslice/wasm"
"github.com/dev/go/gowasm"
flag "github.com/spf13/pflag"
)
// NewWebGoSlice provides a GoSlice with all built in implementations optimized for webassembly.
// For this it contains a custom reader and writer because file access is otherwise not easily possible.
// Also a custom logger is used which writes to the provided writer to avoid writing to stdout.
func newWebGoSlice(options data.Options, onFinished func(gcode string), writer io.Writer) *goslice.GoSlice {
options.GoSlice.Logger = log.New(writer, "", 0)
s := goslice.NewGoSlice(options)
s.Reader = wasm.Reader{}
s.Writer = wasm.Writer{
OnWrite: onFinished,
}
return s
}
type callbackWriter struct {
OnWrite func(p []byte) (n int, err error)
}
func (w callbackWriter) Write(p []byte) (n int, err error) {
return w.OnWrite(p)
}
type GoSliceRunner struct{}
func (w GoSliceRunner) Run(c gowasm.Channels) {
stdoutWriter := callbackWriter{
OnWrite: func(p []byte) (n int, err error) {
c.Stdout <- string(p)
time.Sleep(100 * time.Millisecond)
return len(p), nil
},
}
// A bit of a hack to get help output.
flag.CommandLine.SetOutput(stdoutWriter)
printHelp := flag.BoolP("help", "h", false, "print this help")
o := data.ParseFlags()
if *printHelp {
_, _ = fmt.Fprintf(stdoutWriter, "Usage of goslice: goslice STL_FILE [flags]\n")
flag.Usage()
os.Exit(0)
}
if o.GoSlice.PrintVersion {
c.Stdout <- "webassembly version"
c.ResultCh <- nil
return
}
if o.GoSlice.InputFilePath == "" {
c.ErrCh <- errors.New("the STL_FILE path has to be specified (e.g. 'goslice gopher.stl')\n")
return
}
p := newWebGoSlice(o, func(gcode string) {
c.ResultCh <- gcode
return
}, stdoutWriter)
err := p.Process()
if err != nil {
c.ErrCh <- errors.New("the STL_FILE path has to be specified\n")
return
}
}
func main() {
gowasm.Run(GoSliceRunner{})
}