Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang support using Yaegi #393

Closed
wants to merge 4 commits into from
Closed

Golang support using Yaegi #393

wants to merge 4 commits into from

Conversation

mliezun
Copy link

@mliezun mliezun commented Aug 24, 2021

Issue 359

  • Implemented golang support using yaegi
  • Golang code used to generate yaegi.wasm:
package main

import (
	"bytes"
	"syscall/js"

	"github.com/traefik/yaegi/interp"
	"github.com/traefik/yaegi/stdlib"
)

func main() {
	idle := make(chan int)

	js.Global().Set("evalGo", js.FuncOf(evalGo))

	<-idle
}

func evalGo(this js.Value, inputs []js.Value) interface{} {
	stdout := bytes.NewBuffer(nil)
	stderr := bytes.NewBuffer(nil)

	i := interp.New(interp.Options{
		Stdout: stdout,
		Stderr: stderr,
	})

	i.Use(stdlib.Symbols)

	var errMessage string
	_, err := i.Eval(inputs[0].String())
	if err != nil {
		errMessage = err.Error()
	}

	return map[string]interface{}{
		"stdout": stdout.String(),
		"stderr": stderr.String(),
		"error":  errMessage,
	}
}
  • Compiled with go 1.16: $ GOOS=js GOARCH=wasm go build -o yaegi.wasm main.go
  • wasm_exec.js is a standard file got from $ cp "$(go env GOROOT)/misc/wasm/wasm_exec.js"
  • Added scripts-src-local and wasm-src-local for local dev testing. (usage of these function should be changed before accepting PR)

@mliezun mliezun marked this pull request as ready for review August 26, 2021 20:17
@viebel
Copy link
Owner

viebel commented Aug 29, 2021

Very good job @mliezun !
I cleaned up a bit the code in #394 and merged it to master.
Thank you.

@viebel viebel closed this Aug 29, 2021
@mliezun
Copy link
Author

mliezun commented Aug 30, 2021

Great!
I found the blog post in Lobsters. Thank you for mentioning my contribution ✌️

@viebel
Copy link
Owner

viebel commented Aug 30, 2021

My pleasure @mliezun
Some people asked about the implementation details. Could you describe the process for compiling yaegi into WASM?

@mliezun
Copy link
Author

mliezun commented Aug 30, 2021

Hi @viebel
The explanation is in the first comment of this PR. You have the go code used and the command used to generate yaegi.wasm just underneath it.
If you never used go before it's easier to use go modules:

  • Create new folder called yaegi-js
  • Initialize module inside folder $ go mod init yaegi-js
  • Copy go code to a new file called main.go
  • Compile with $ GOOS=js GOARCH=wasm go build -o yaegi.wasm main.go
    Let me know if you have any trouble running that or you need a more in depth explanation. Im happy to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants