From ec2d073202e9db8bb7e66e2030a906028663f49b Mon Sep 17 00:00:00 2001 From: inkeliz Date: Sun, 19 May 2024 17:17:41 +0100 Subject: [PATCH] fix: Compatible with Go 1.21+ (#2) This patch drops compatibility with Go 1.20, and adds supports for Go 1.21. Signed-off-by: inkeliz --- README.md | 16 ++++++--- build/build.go | 2 +- go.mod | 14 ++++---- go.sum | 13 +++++++ inkwasm/inkwasm_js.go | 51 ++++++++++++++++++++++++++ inkwasm/inkwasm_js.js | 7 +++- inkwasm/inkwasm_js.s | 84 ++----------------------------------------- inkwasm/wasm_js.go | 49 +++++++++++++++++++++---- inkwasm/wasm_js.js | 30 ++++++++-------- parser/binder.go | 21 +++++------ parser/parser.go | 2 +- 11 files changed, 161 insertions(+), 128 deletions(-) diff --git a/README.md b/README.md index 29cdcee..e0dace1 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,15 @@ # Golang InkWasm + **InkWasm** is faster `syscall/js` replacement, it's a package and a generator. Our goal is to be as faster and avoid unnecessary allocations. **InkWasm** initially created for [Gio](https://gioui.org/), improving the performance for [WebGL API](https://developer.mozilla.org/pt-BR/docs/Web/API/WebGL_API), in some devices and tests **InkWasm** is 2x faster than `syscall/js`, in real application it's 1.6x faster. > ⚠️ The generator changes the file of the current packages and imported packages. It is still experimental, keep a backup file and use some versioning tool (like Git). +## Golang Version + +> Go 1.20+: Current main branch +> +> Go 1.18~Go1.20: https://github.com/inkeliz/go_inkwasm/releases/tag/v0.1.20 ## Benchmark @@ -89,16 +95,16 @@ You can also call functions that belongs to a specific Object, the name must sta ``` //inkwasm:func .bufferData -func glBufferData(o inkwasm.Object, target uint, data []byte, usage uint) +func glBufferData(o inkwasm.Object, data []byte, usage uint) ``` ``` func main() { ///... - glBufferData(gl, len(data), STATIC_DRAW, data) + glBufferData(gl, data, STATIC_DRAW) } ``` -It will call `o.bufferData` (is expected that the given `o` (`inkwasm.Object`) is a WebGL Context). +It will call `o.bufferData` (is expected that the given `o`, `inkwasm.Object`, is a WebGL Context). ### Get attribute: @@ -154,9 +160,9 @@ Currently, **InkWasm** is very experimental and WebAssembly, in general, is also ## Blockers -- `CallImport` will be removed: +- ~~`CallImport` will be removed:~~ - Currently, **InkWasm** heavily rely on `CallImport`, it will be removed in the future and will be exclusive to `syscall/js` and `runtime` [golang#38248](https://github.com/golang/go/issues/38248). If that happens, the only solution is to replace `syscall/js`, maybe using `-overlay` on `cmd/go`. + ~~Currently, **InkWasm** heavily rely on `CallImport`, it will be removed in the future and will be exclusive to `syscall/js` and `runtime` [golang#38248](https://github.com/golang/go/issues/38248). If that happens, the only solution is to replace `syscall/js`, maybe using `-overlay` on `cmd/go`.~~ - `go:wasmexport` isn't (yet) supported: diff --git a/build/build.go b/build/build.go index 6a85528..cdc21dc 100644 --- a/build/build.go +++ b/build/build.go @@ -194,7 +194,7 @@ globalThis._exit_code = null; let go = undefined; (() => { - go = {argv: [], env: {}, importObject: {go: {}}}; + go = {argv: [], env: {}, importObject: {gojs: {}}}; const argv = new URLSearchParams(location.search).get("argv"); if (argv) { go["argv"] = argv.split(" "); diff --git a/go.mod b/go.mod index 049a6ce..b7b055c 100644 --- a/go.mod +++ b/go.mod @@ -1,22 +1,24 @@ module github.com/inkeliz/go_inkwasm -go 1.17 +go 1.22 require ( github.com/chromedp/cdproto v0.0.0-20220131204822-e6abebe7b8cd github.com/chromedp/chromedp v0.7.7 - golang.org/x/sync v0.0.0-20210220032951-036812b2e83c - golang.org/x/tools v0.1.9 + golang.org/x/sync v0.7.0 + golang.org/x/tools v0.21.0 ) require ( + github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 // indirect github.com/chromedp/sysutil v1.0.0 // indirect github.com/gobwas/httphead v0.1.0 // indirect github.com/gobwas/pool v0.2.1 // indirect github.com/gobwas/ws v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect - golang.org/x/mod v0.5.1 // indirect - golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + golang.org/x/mod v0.17.0 // indirect + golang.org/x/perf v0.0.0-20240510023725-bedb9135df6d // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect ) diff --git a/go.sum b/go.sum index 2c28959..a4f36ec 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 h1:xlwdaKcTNVW4PtpQb8aKA4Pjy0CdJHEqvFbAnvR5m2g= +github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794/go.mod h1:7e+I0LQFUI9AXWxOfsQROs9xPhoJtbsyWcjJqDd4KPY= github.com/chromedp/cdproto v0.0.0-20220131204822-e6abebe7b8cd h1:Ndx98cm/oI1Vwe1daFUDfkLHQ1NNvV1FWqVl7vDnGd8= github.com/chromedp/cdproto v0.0.0-20220131204822-e6abebe7b8cd/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U= github.com/chromedp/chromedp v0.7.7 h1:kRN7G7v4cGpTV2Nth218YR2VOsbpHBN3qClYCb6CBnI= @@ -21,12 +23,18 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/perf v0.0.0-20240510023725-bedb9135df6d h1:QTr4HWRb3m3+xRitZtKm9CqwiROVnNCMfL+U2P9C0Vc= +golang.org/x/perf v0.0.0-20240510023725-bedb9135df6d/go.mod h1:ipWOGiEQ0J5j74LbJ1iNKP2gTl4oge+Djuh2sTOqiRc= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -35,6 +43,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo= golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -43,7 +53,10 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8= golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= +golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw= +golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= diff --git a/inkwasm/inkwasm_js.go b/inkwasm/inkwasm_js.go index 016a8cc..cf1507b 100644 --- a/inkwasm/inkwasm_js.go +++ b/inkwasm/inkwasm_js.go @@ -12,6 +12,8 @@ func _getBasicDecoder(s string) (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__getBasicDecoder func __getBasicDecoder(s string) (_ Object) func _getSliceDecoder(f Object) (_ Object) { @@ -19,6 +21,8 @@ func _getSliceDecoder(f Object) (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__getSliceDecoder func __getSliceDecoder(f Object) (_ Object) func _newObjectFromSyscall(i uint32) (_ Object) { @@ -26,6 +30,8 @@ func _newObjectFromSyscall(i uint32) (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__newObjectFromSyscall func __newObjectFromSyscall(i uint32) (_ Object) func _getNull() (_ Object) { @@ -33,6 +39,8 @@ func _getNull() (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__getNull func __getNull() (_ Object) func _getUndefined() (_ Object) { @@ -40,6 +48,8 @@ func _getUndefined() (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__getUndefined func __getUndefined() (_ Object) func _getGlobal() (_ Object) { @@ -47,6 +57,8 @@ func _getGlobal() (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__getGlobal func __getGlobal() (_ Object) func _makeObj(p0 []int32) (_ Object) { @@ -55,12 +67,16 @@ func _makeObj(p0 []int32) (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__makeObj func __makeObj(p0 []int32) (_ Object) func _free(ref int) { __free(ref) } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__free func __free(ref int) func _call(o Object, k string, args []int32) (_ Object, _ bool) { @@ -70,6 +86,8 @@ func _call(o Object, k string, args []int32) (_ Object, _ bool) { return r0, r1 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__call func __call(o Object, k string, args []int32) (_ Object, _ bool) func _callVoid(o Object, k string, args []int32) (_ bool, ok bool) { @@ -79,6 +97,8 @@ func _callVoid(o Object, k string, args []int32) (_ bool, ok bool) { return r0, r1 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__callVoid func __callVoid(o Object, k string, args []int32) (_ bool, ok bool) func _invoke(o Object, args []int32) (_ Object, _ bool) { @@ -87,6 +107,8 @@ func _invoke(o Object, args []int32) (_ Object, _ bool) { return r0, r1 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__invoke func __invoke(o Object, args []int32) (_ Object, _ bool) func _invokeVoid(o Object, args []int32) (_ bool, ok bool) { @@ -95,6 +117,8 @@ func _invokeVoid(o Object, args []int32) (_ bool, ok bool) { return r0, r1 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__invokeVoid func __invokeVoid(o Object, args []int32) (_ bool, ok bool) func _newObj(o Object, args []int32) (_ Object, _ bool) { @@ -103,6 +127,8 @@ func _newObj(o Object, args []int32) (_ Object, _ bool) { return r0, r1 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__newObj func __newObj(o Object, args []int32) (_ Object, _ bool) func _getIndex(o Object, i int) (_ Object) { @@ -110,6 +136,8 @@ func _getIndex(o Object, i int) (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__getIndex func __getIndex(o Object, i int) (_ Object) func _getProp(o Object, k string) (_ Object) { @@ -118,6 +146,8 @@ func _getProp(o Object, k string) (_ Object) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__getProp func __getProp(o Object, k string) (_ Object) func _setProp(o Object, k string, v string) { @@ -126,13 +156,26 @@ func _setProp(o Object, k string, v string) { runtime.KeepAlive(v) } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__setProp func __setProp(o Object, k string, v string) +func _setPropObj(o Object, k string, v Object) { + __setPropObj(o, k, v) + runtime.KeepAlive(k) + +} + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__setPropObj +func __setPropObj(o Object, k string, v Object) + func _encodeString(o Object) (_ Object) { r0 := __encodeString(o) return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__encodeString func __encodeString(o Object) (_ Object) func _copyBytes(o Object, buf []byte) { @@ -140,6 +183,8 @@ func _copyBytes(o Object, buf []byte) { runtime.KeepAlive(buf) } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__copyBytes func __copyBytes(o Object, buf []byte) func _instanceOf(o Object, v Object) (_ bool) { @@ -147,6 +192,8 @@ func _instanceOf(o Object, v Object) (_ bool) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__instanceOf func __instanceOf(o Object, v Object) (_ bool) func _equal(o Object, v Object) (_ bool) { @@ -154,6 +201,8 @@ func _equal(o Object, v Object) (_ bool) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__equal func __equal(o Object, v Object) (_ bool) func _strictEqual(o Object, v Object) (_ bool) { @@ -161,4 +210,6 @@ func _strictEqual(o Object, v Object) (_ bool) { return r0 } + +//go:wasmimport gojs github.com/inkeliz/go_inkwasm/inkwasm.__strictEqual func __strictEqual(o Object, v Object) (_ bool) diff --git a/inkwasm/inkwasm_js.js b/inkwasm/inkwasm_js.js index 70ebe3d..f5e3a36 100644 --- a/inkwasm/inkwasm_js.js +++ b/inkwasm/inkwasm_js.js @@ -7,7 +7,7 @@ })(); // Code generated by INKWASM BUILD; DO NOT EDIT (() => { - Object.assign(go.importObject.go, { + Object.assign(go.importObject.gojs, { "github.com/inkeliz/go_inkwasm/inkwasm.__getBasicDecoder": (sp) => { let r = globalThis.inkwasm.Load[globalThis.inkwasm.Load.String(go, sp, 8)] @@ -133,6 +133,11 @@ }, + "github.com/inkeliz/go_inkwasm/inkwasm.__setPropObj": (sp) => { + Reflect.set(globalThis.inkwasm.Load.InkwasmObject(go, sp, 8),globalThis.inkwasm.Load.String(go, sp, 24),globalThis.inkwasm.Load.InkwasmObject(go, sp, 40)) + + }, + "github.com/inkeliz/go_inkwasm/inkwasm.__encodeString": (sp) => { let r = globalThis.inkwasm.Internal.EncodeString(globalThis.inkwasm.Load.InkwasmObject(go, sp, 8)) sp = go._inst.exports.getsp() >>> 0 diff --git a/inkwasm/inkwasm_js.s b/inkwasm/inkwasm_js.s index 7e46d67..850d7e1 100644 --- a/inkwasm/inkwasm_js.s +++ b/inkwasm/inkwasm_js.s @@ -1,170 +1,90 @@ // Code generated by INKWASM BUILD; DO NOT EDIT #include "textflag.h" -TEXT ·__getBasicDecoder(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·getBasicDecoder(SB), NOSPLIT, $0 JMP ·_getBasicDecoder(SB) RET -TEXT ·__getSliceDecoder(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·getSliceDecoder(SB), NOSPLIT, $0 JMP ·_getSliceDecoder(SB) RET -TEXT ·__newObjectFromSyscall(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·newObjectFromSyscall(SB), NOSPLIT, $0 JMP ·_newObjectFromSyscall(SB) RET -TEXT ·__getNull(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·getNull(SB), NOSPLIT, $0 JMP ·_getNull(SB) RET -TEXT ·__getUndefined(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·getUndefined(SB), NOSPLIT, $0 JMP ·_getUndefined(SB) RET -TEXT ·__getGlobal(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·getGlobal(SB), NOSPLIT, $0 JMP ·_getGlobal(SB) RET -TEXT ·__makeObj(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·makeObj(SB), NOSPLIT, $0 JMP ·_makeObj(SB) RET -TEXT ·__free(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·free(SB), NOSPLIT, $0 JMP ·_free(SB) RET -TEXT ·__call(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·call(SB), NOSPLIT, $0 JMP ·_call(SB) RET -TEXT ·__callVoid(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·callVoid(SB), NOSPLIT, $0 JMP ·_callVoid(SB) RET -TEXT ·__invoke(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·invoke(SB), NOSPLIT, $0 JMP ·_invoke(SB) RET -TEXT ·__invokeVoid(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·invokeVoid(SB), NOSPLIT, $0 JMP ·_invokeVoid(SB) RET -TEXT ·__newObj(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·newObj(SB), NOSPLIT, $0 JMP ·_newObj(SB) RET -TEXT ·__getIndex(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·getIndex(SB), NOSPLIT, $0 JMP ·_getIndex(SB) RET -TEXT ·__getProp(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·getProp(SB), NOSPLIT, $0 JMP ·_getProp(SB) RET -TEXT ·__setProp(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·setProp(SB), NOSPLIT, $0 JMP ·_setProp(SB) RET -TEXT ·__encodeString(SB), NOSPLIT, $0 - CallImport +TEXT ·setPropObj(SB), NOSPLIT, $0 + JMP ·_setPropObj(SB) RET TEXT ·encodeString(SB), NOSPLIT, $0 JMP ·_encodeString(SB) RET -TEXT ·__copyBytes(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·copyBytes(SB), NOSPLIT, $0 JMP ·_copyBytes(SB) RET -TEXT ·__instanceOf(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·instanceOf(SB), NOSPLIT, $0 JMP ·_instanceOf(SB) RET -TEXT ·__equal(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·equal(SB), NOSPLIT, $0 JMP ·_equal(SB) RET -TEXT ·__strictEqual(SB), NOSPLIT, $0 - CallImport - RET - TEXT ·strictEqual(SB), NOSPLIT, $0 JMP ·_strictEqual(SB) RET diff --git a/inkwasm/wasm_js.go b/inkwasm/wasm_js.go index 4d24c42..30d26b3 100644 --- a/inkwasm/wasm_js.go +++ b/inkwasm/wasm_js.go @@ -2,7 +2,6 @@ package inkwasm import ( "errors" - "syscall/js" "unsafe" ) @@ -18,6 +17,15 @@ type Object struct { len uint32 // len for array/string } +func init() { + if unsafe.Sizeof(Object{}) > unsafe.Sizeof(string("")) { + panic("Impossible to use Object") + } + if unsafe.Sizeof(Object{}) > unsafe.Sizeof([]byte{}) { + panic("Impossible to use Object") + } +} + // NewObject creates a new Javascript Object. // // The resulting Object must be released using Free, when @@ -35,8 +43,10 @@ func (o Object) Free() { return } switch o.typ { - case TypeFunction, TypeSymbol, TypeBigInt, TypeObject: + case TypeFunction, TypeSymbol, TypeBigInt, TypeObject, TypeString: free(*(*int)(unsafe.Pointer(&o.value))) + case TypeUndefined, TypeNull, TypeBoolean, TypeNumber: + // no-op (the value is not a reference) } } @@ -146,6 +156,11 @@ func (o Object) GetProperty(property string) Object { return getProp(o, property) } +// Get returns property of the current Object. +func (o Object) Get(property string) Object { + return getProp(o, property) +} + //inkwasm:get . func getProp(o Object, k string) Object @@ -155,9 +170,18 @@ func (o Object) SetProperty(property string, value string) { setProp(o, property, value) } +// Set defines the given property of the current Object with +// the given value. +func (o Object) Set(property string, value Object) { + setPropObj(o, property, value) +} + //inkwasm:func Reflect.set func setProp(o Object, k, v string) +//inkwasm:func Reflect.set +func setPropObj(o Object, k string, v Object) + var ( ErrInvalidType = errors.New("invalid type") ErrExecutionJS = errors.New("error while executing/calling Javascript, see console log for details") @@ -261,9 +285,21 @@ func (o Object) Length() uint32 { return o.len } +// Len is a alias for Length. +// See Length for more details. +func (o Object) Len() uint32 { + return o.len +} + func (o Object) InstanceOf(v Object) bool { - js.Value{}.Truthy() - return instanceOf(o, v) + switch v.typ { + case TypeUndefined: + return o.typ == TypeUndefined + case TypeNull: + return o.typ == TypeNull + default: + return instanceOf(o, v) + } } //inkwasm:func globalThis.inkwasm.Internal.InstanceOf @@ -280,13 +316,12 @@ func (o Object) Truthy() bool { return false case TypeBoolean: v, _ := o.Bool() - return v + return v != false case TypeNumber: v, _ := o.Float() return v != 0 case TypeString: - v, _ := o.String() - return len(v) > 0 + return o.len > 0 default: return true } diff --git a/inkwasm/wasm_js.js b/inkwasm/wasm_js.js index ff585ee..300756c 100644 --- a/inkwasm/wasm_js.js +++ b/inkwasm/wasm_js.js @@ -421,21 +421,6 @@ case "undefined": globalThis.inkwasm.Set.Uint8(go, sp, offset + 8, ObjectTypes.TypeUndefined) break; - case "object": - if (v === null) { - globalThis.inkwasm.Set.Uint8(go, sp, offset + 8, ObjectTypes.TypeNull); - } else { - globalThis.inkwasm.Set.Object(go, sp, offset, v); - globalThis.inkwasm.Set.Uint8(go, sp, offset + 8, ObjectTypes.TypeObject); - if (Array.isArray(v) || v.length !== undefined || v.byteLength !== undefined) { - let len = v.length - if (v.byteLength !== undefined) { - len = v.byteLength - } - globalThis.inkwasm.Set.Uint32(go, sp, offset + 12, len); - } - } - break; case "boolean": globalThis.inkwasm.Set.Bool(go, sp, offset, v); globalThis.inkwasm.Set.Uint8(go, sp, offset + 8, ObjectTypes.TypeBoolean); @@ -461,6 +446,21 @@ globalThis.inkwasm.Set.Object(go, sp, offset, v); globalThis.inkwasm.Set.Uint8(go, sp, offset + 8, ObjectTypes.TypeFunction); break; + case "object": + if (v === null) { + globalThis.inkwasm.Set.Uint8(go, sp, offset + 8, ObjectTypes.TypeNull); + } else { + globalThis.inkwasm.Set.Object(go, sp, offset, v); + globalThis.inkwasm.Set.Uint8(go, sp, offset + 8, ObjectTypes.TypeObject); + if (Array.isArray(v) || v.length !== undefined || v.byteLength !== undefined) { + let len = v.length + if (v.byteLength !== undefined) { + len = v.byteLength + } + globalThis.inkwasm.Set.Uint32(go, sp, offset + 12, len); + } + } + break; } } }) diff --git a/parser/binder.go b/parser/binder.go index a107244..bf92d19 100644 --- a/parser/binder.go +++ b/parser/binder.go @@ -116,14 +116,6 @@ func (b *Binder) createAssembly(_ bind.Package, info []*bind.Function) error { b.headerAssembly() for _, info := range info { - b.asm.Line() - b.asm.WriteOpen(`TEXT ·__%s(SB), NOSPLIT, $0`, info.FunctionGolang.Name) - b.asm.Line() - b.asm.Write(`CallImport`) - b.asm.Line() - b.asm.Write(`RET`) - b.asm.WriteClose("") - b.asm.Line() b.asm.Line() b.asm.WriteOpen(`TEXT ·%s(SB), NOSPLIT, $0`, info.FunctionGolang.Name) b.asm.Line() @@ -294,8 +286,17 @@ func (b *Binder) createGolang(pkg bind.Package, info []*bind.Function) error { b.golang.Line() b.golang.WriteClose("}") b.golang.Line() + + path := pkg.Path + if pkg.Name == "main" { + path = "main" + } + + b.golang.Write("//go:wasmimport gojs %s.__%s", path, info.FunctionGolang.Name) + b.golang.Line() b.golang.Write("func __%s(%s) (%s)", info.FunctionGolang.Name, params[0].ArgsString.String(), params[1].StubArgsString.String()) b.golang.Line() + b.golang.Line() } return nil @@ -368,7 +369,7 @@ func (b *Binder) createJavascript(pkg bind.Package, info []*bind.Function) error b.js.Line() b.js.WriteOpen(`(() => {`) b.js.Line() - b.js.WriteOpen(`Object.assign(go.importObject.go, {`) + b.js.WriteOpen(`Object.assign(go.importObject.gojs, {`) b.js.Line() path := pkg.Path @@ -573,11 +574,11 @@ func writeJSToGo(w *writer, r bind.Argument, sp *int, v string) error { *sp += int(r.Len) * f.Size case bind.ModeSlice: slice, _ := bind.ResultFunc[r.ArgType]["default"] - padding(sp, slice.Size) f, ok := bind.BridgeFunc[bind.ModeArray][strings.ToLower(r.SubType.Type)] if !ok { return fmt.Errorf("invalid type of slice %s", r.Type) } + padding(sp, slice.Size) w.Write(`%s(go, sp, %d, %s, %d)`, slice.JS, *sp, v, f.Size) *sp += slice.Size default: diff --git a/parser/parser.go b/parser/parser.go index aea71b8..149788d 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -162,7 +162,7 @@ type filter func(b byte) (filter, error) func (p *Parser) parseComment(s string) (b *bind.Function, err error) { const prefixLen = len("//inkwasm:") - if !strings.HasPrefix(s, "//inkwasm:") || len(s) <= prefixLen { + if len(s) <= prefixLen || !strings.HasPrefix(s, "//inkwasm:") { return b, nil }