Closed JounQin closed 2 years ago
Thanks for the nudge; this has been in the back of my mind. GopherJS is being maintained again, so we're no longer blocked by that.
I've been thinking that the JS layer should be made more generic so it can be compiled to both JS and WASM. Right now, it's a bit too tightly coupled with GopherJS.
Hi @mvdan,
Can we do something to assist you in this task? I would also need the new version of the lib.
Thank you for your lib!
Thanks for the offer! If someone wants to take a stab at exposing this Go library via JS or Wasm, by all means go ahead. I think the current design I went with wasn't the right approach, and if/when I find enough time to try again, I'd probably start from scratch. It is on my radar, but I'm a bit busy with other projects right at this moment. Perhaps someone else taking this on would be for the best, given that I am by no means an expert at either JS or Wasm.
I try to use the Wasm compiler but I did not succeed to go far. I don't have enough knowledge to succeed. It seems that Wasm compiler does not seem to be adapted to do back and forth between the JS and Go since everything needs to be wrapped and converted into primitive types. It is probably better to write the API in JS and then call the Wasm for parsing the bash and output a JSON file that can be processed in JS.
Here is my naive attempt to use wasm https://github.com/rx-ts/sh-syntax/blob/main/main.go#L208
The parse function is basically working as intended to parse a shell script string to AST by converting syntax.File
to map[string]interface {}
, but I can't get it work for print
, because I have no idea how to convert a js object into map[string]interface {}
and then syntax.File
because there are many private fields inside.
Unlike solution like gopherjs
, js and wasm can't share all data structure easily like @tdurieux said.
Here's my attempt to use latest gopherjs
with makeFullWrapper
support: https://github.com/mvdan/sh/pull/840.
Hope they help.
If you guys have any other good ideas, I'll try to follow, although I'm not a go expert actually. 😂
@mvdan Do you have any advice for the WASM module?
It's time to admit I won't have time to properly do this. And by that I mean one of these two options:
1) Figure out how to publish a newer javascript module with a newer version of Go, GopherJS, and the shell module. 2) Figure out how to publish a new javascript module using Go's webassembly support (perhaps with tinygo to keep the size down).
If anyone plans to work on either of those and succeeds, I am more than happy to deprecate https://www.npmjs.com/package/mvdan-sh in favor of it, or hand it over if that would be easier. I am also of course happy to give my opinions, reviews, advice from the Go side, etc. I just don't have the experience or time to commit to this :smiling_face_with_tear:
@mvdan
For the first option, see https://github.com/mvdan/sh/pull/840
For the second option, see https://github.com/rx-ts/sh-syntax
Thanks, I've now merged #840, and will look into a JS release.
I hadn't noticed you had already published sh-syntax as an NPM module that uses WASM. What are the pros and cons compared to the GopherJS version, do you think? I can see it's larger, where presumably most of the size comes from GOOS=js GOARCH=wasm go build
. Have you tried tinygo to reduce the size?
I also imagine the wasm version is faster, but that really depends on how good Go/TinyGo are at compiling wasm versus GopherJS with JS.
Longer term I imagine WASM will be the best bet, also because it should be possible to use the shell APIs without needing to use JS at all. Think like a Rust or Python program using a WASM runtime to run the parser, for example.
One more thought: I see you have some glue code to convert the AST nodes to map
, so that you can use js.ValueOf
and expose them as javascript maps. I have something extremely similar in https://github.com/mvdan/sh/blob/839f7b77f6487282e1d50943429a1f3f4f68ac76/cmd/shfmt/json.go for shfmt -tojson
, so perhaps I could expose that as an API. I think what's going to work best for both JS and pure Wasm is to have the Go APIs in Wasm parse shell into a JSON-encoded AST string, as described in https://github.com/mvdan/sh/issues/760#issuecomment-1087300095. Then, the "print" form would be the opposite: take a JSON-encoded AST string, and produce shell as a string.
I've now published v0.10.0 - note that the unpacked size has jumped from 1.85MiB to 2.3 MiB.
I've published v0.10.1 with JS minification, which brings us back down to 1.5MiB. It would be nice to also bring down the size of the sh-syntax package with Wasm.
Closing for now, as the "version stuck behind" problem is now fixed. More than happy to deprecate the GopherJS pacakge if we agree that https://github.com/rx-ts/sh-syntax is better.
It seems the js module is outdated for a long time.
related https://github.com/rx-ts/prettier/issues/133
lolaus.sh
```sh #!/usr/bin/env bash set -euo pipefail DEBUG=${DEBUG:-} help() { cat <