-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use tinygo instead of go to create smaller wasm binary
This commit refactors the wasm and JavaScript parts to make use of TinyGo for creating even smaller wasm binaries. It also introduces another wasm-opt pass that shaves off ~500K. The API has changed: Previously the exported `createLinter()` function was synchronous and returned an async function to lint a single file. Now the `createLinter()` function is asynchronous and the returned linter function is synchronous. ```js import { createLinter } from "actionlint"; const linter = await createLinter(); const results = linter("on: psuh", "borked.yml"); ``` BREAKING CHANGE: The `createLinter()` function is now asynchronous and resolves to a function that synchronously invokes the actionlint binary.
- Loading branch information
1 parent
1987395
commit bb9571e
Showing
15 changed files
with
189 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
main.wasm | ||
wasm_exec.js | ||
tmp.wasm | ||
node_modules/ | ||
types/ | ||
types/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
.github/ | ||
.vscode/ | ||
node_modules/ | ||
go.mod | ||
go.sum | ||
globals.d.ts | ||
main.go | ||
Makefile | ||
renovate.json | ||
test.mjs | ||
tmp.wasm | ||
tsconfig.json | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
"GOARCH": "wasm" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
build: wasm_exec.js main.wasm types/node.d.mts | ||
build: main.wasm types/node.d.mts | ||
|
||
wasm_exec.js: | ||
cp $$(go env GOROOT)/misc/wasm/wasm_exec.js wasm_exec.tmp.js | ||
echo "// @ts-nocheck" > wasm_exec.js | ||
cat wasm_exec.tmp.js >> wasm_exec.js | ||
rm wasm_exec.tmp.js | ||
main.wasm: tmp.wasm | ||
wasm-opt -c -O4 -o main.wasm tmp.wasm | ||
|
||
main.wasm: main.go go.mod | ||
GOOS=js GOARCH=wasm go build -o main.wasm | ||
tmp.wasm: main.go go.mod | ||
tinygo build -o tmp.wasm -target wasm -panic trap -opt z main.go | ||
|
||
types/node.d.mts: *.cjs *.mjs *.d.ts *.json yarn.lock | ||
$$(yarn bin)/tsc -p . | ||
|
||
clean: | ||
rm main.wasm | ||
rm wasm_exec.js | ||
rm tmp.wasm | ||
rm -rf types | ||
|
||
.PHONY: build clean | ||
.PHONY: build clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"container/list" | ||
"io/ioutil" | ||
"reflect" | ||
"syscall/js" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/rhysd/actionlint" | ||
) | ||
|
||
func toMap(input interface{}) map[string]interface{} { | ||
out := make(map[string]interface{}) | ||
value := reflect.ValueOf(input) | ||
if value.Kind() == reflect.Ptr { | ||
value = value.Elem() | ||
} | ||
var Memory = list.New() | ||
var OutputString = []byte{} | ||
|
||
for i := 0; i < value.NumField(); i++ { | ||
out[value.Type().Field(i).Name] = value.Field(i).Interface() | ||
} | ||
type block struct { | ||
ptr *[]byte | ||
value []byte | ||
} | ||
|
||
//export WasmAlloc | ||
func WasmAlloc(size int) *[]byte { | ||
slice := make([]byte, size) | ||
block := block{ | ||
ptr: &slice, | ||
value: slice, | ||
} | ||
Memory.PushBack(block) | ||
|
||
return out | ||
return block.ptr | ||
} | ||
|
||
func runActionlint(source string, filePath string) (interface{}, error) { | ||
//export WasmFree | ||
func WasmFree(ptr *[]byte) { | ||
for e := Memory.Front(); e != nil; e = e.Next() { | ||
block := e.Value.(block) | ||
if block.ptr == ptr { | ||
Memory.Remove(e) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func serialize(errors []*actionlint.Error, target *[]byte) { | ||
*target = []byte("[") | ||
|
||
for i, err := range errors { | ||
*target = append(*target, `{ | ||
"file":"`+err.Filepath+`", | ||
"line":`+strconv.FormatInt(int64(err.Line), 10)+`, | ||
"column":`+strconv.FormatInt(int64(err.Column), 10)+`, | ||
"message":"`+strings.ReplaceAll(err.Message, `"`, `\"`)+`", | ||
"kind":"`+strings.ReplaceAll(err.Kind, `"`, `\"`)+`" | ||
}`...) | ||
|
||
if i < len(errors)-1 { | ||
*target = append(*target, ',') | ||
} | ||
} | ||
|
||
*target = append(*target, ']', 0) | ||
} | ||
|
||
//export RunActionlint | ||
func RunActionlint(input []byte, path []byte) *byte { | ||
opts := actionlint.LinterOptions{} | ||
|
||
linter, err := actionlint.NewLinter(ioutil.Discard, &opts) | ||
if err != nil { | ||
return nil, err | ||
OutputString = []byte(err.Error()) | ||
return &OutputString[0] | ||
} | ||
|
||
errs, err := linter.Lint(filePath, []byte(source), nil) | ||
errs, err := linter.Lint(string(path), input, nil) | ||
if err != nil { | ||
return nil, err | ||
OutputString = []byte(err.Error()) | ||
return &OutputString[0] | ||
} | ||
|
||
ret := make([]interface{}, 0, len(errs)) | ||
for _, err := range errs { | ||
ret = append(ret, toMap(*err)) | ||
} | ||
serialize(errs, &OutputString) | ||
|
||
return ret, nil | ||
return &OutputString[0] | ||
} | ||
|
||
func main() { | ||
js.Global().Set("runActionlint", js.FuncOf(func(this js.Value, args []js.Value) interface{} { | ||
result, err := runActionlint(args[0].String(), args[1].String()) | ||
return js.Global().Get("Array").New(result, err) | ||
})) | ||
|
||
js.Global().Call("actionlintInitialized") | ||
|
||
select {} | ||
} | ||
func main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.