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

Problem sending string concatenated with variable #29

Closed
Rbsann opened this issue Sep 10, 2018 · 5 comments
Closed

Problem sending string concatenated with variable #29

Rbsann opened this issue Sep 10, 2018 · 5 comments

Comments

@Rbsann
Copy link

Rbsann commented Sep 10, 2018

Hello all,

I am trying to run a ts (compiled with Assemblyscript) file that implements a simple function, it logs on the terminal a string concatenated with a variable (that is also a string). The code is imported through Perlin and runs in a Golang Vm.

Here is a Go code chunk that is handling the generated wasm,;-the string is reconstructed ffollowing AssemblyScript documentation on how strings are allocated in the memory(https://github.com/AssemblyScript/assemblyscript/wiki/Memory-Layout-&-Management):


ptr := int(uint32(vm.GetCurrentFrame().Locals[0]))
msgLen := int(uint32(vm.Memory[ptr]))
msg := vm.Memory[(ptr + 4):(ptr + 4 + 2*msgLen)]
fmt.Println(ptr)`

This is a ts code that when compiled works fine :

export function app_main():string{
	let a = "Hello";
	r.consoleLog(a);
	return '0';
}

Here is another ts code that also runs without any problem:

export function app_main():string{
		r.consoleLog('World');
		return '0';
}

But here is a code that not only does not work but also returns an error:

export function app_main():string{
		let a = 'World';
		r.consoleLog("Hello"+a);
		return '0';
}

And here is the error that Life returns:

Resolve func: env abort
panic: unknown field: abort

It is important to emphasize that this error does not appear in any other instances.

So, anyone has a workaround for this issue??

@MaxGraey
Copy link

MaxGraey commented Sep 10, 2018

AssemblyScript always import abort function from host. So you got this errors because life can't execute wasm file. You should implement abort function on Go side and import via "env" scope. Or you could compile AssemblyScript with additional cli argument --abort="" which allow avoid external import for abort.

@Rbsann
Copy link
Author

Rbsann commented Sep 10, 2018

That actually doesnt solve the problem, just create a new one. If i implement 'abort' function, here is what i get:
--- Begin stack trace ---
<2> [5]
<1> [9]
<0> [10]
--- End stack trace ---
panic: wasm: unreachable executed
goroutine 1 [running]:
main.main()
/home/rbsann/.gvm/pkgsets/go1.11/global/src/golang.org/x/vgo/vendor/life/main.go:216 +0x5f8

And here is the ts code that is being compiled:

var v = 'teste';
var numberr = "2";
r.consoleLog(numberr);
r.consoleLog("v");
r.consoleLog("1"+numberr);
return 'something';

2 and v are printed and then the code reaches that error.

@losfair
Copy link
Contributor

losfair commented Sep 14, 2018

What do you mean by "this error does not appear in any other instances"?

If you mean this error only appears in life but not other WebAssembly runtimes, can you post the .wasm file that causes the error?

@losfair
Copy link
Contributor

losfair commented Oct 2, 2018

Closing due to inactivity.

@losfair losfair closed this as completed Oct 2, 2018
@iwasaki-kenta
Copy link
Contributor

Hello,

Tried it out just now and it appears to work just fine.

index.ts:

import "allocator/arena";

declare namespace env {
    function log(offset: usize, len: usize): void
}

function log(str: string): void {
    env.log(str.toUTF8(), str.lengthUTF8);
}

export function main(): void {
    let v = 'teste';
    let number = "2";

    log(v);
    log(number);
    log("1" + number);
}

main.go:

package main

import (
	"errors"
	"github.com/perlin-network/life/exec"
	"io/ioutil"
	"fmt"
)

func check(err error) {
	if err != nil {
		panic(err)
	}
}

var _ exec.ImportResolver = (*resolver)(nil)

type resolver struct {}

func (resolver) ResolveFunc(module, field string) exec.FunctionImport {
	switch module {
	case "env":
		switch field {
		case "abort":
			return func(vm *exec.VirtualMachine) int64 {
				return 0
			}
		case "log":
			return func(vm *exec.VirtualMachine) int64 {
				frame := vm.GetCurrentFrame()

				msgPtr := int(uint32(frame.Locals[0]))
				msgLen := int(uint32(frame.Locals[1]))
				msg := string(vm.Memory[msgPtr : msgPtr+msgLen])

				fmt.Println(msg)

				return 0
			}
		default:
			panic(fmt.Errorf("unknown import resolved: %s", field))
		}
	}

	panic(fmt.Errorf("unknown module: %s", module))
}

func (resolver) ResolveGlobal(module, field string) int64 {
	panic("implement me")
}

func main() {
	code, err := ioutil.ReadFile("build/optimized.wasm")
	check(err)

	vm, err := exec.NewVirtualMachine(code, exec.VMConfig{}, new(resolver), nil)
	check(err)

	entrypoint, exists := vm.GetFunctionExport("main")
	if !exists {
		check(errors.New("failed to find entry point"))
	}

	_, err = vm.Run(entrypoint)
	check(err)
}

image

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

No branches or pull requests

4 participants