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

stdlib.fmt.println not behaving like fmt.Println #352

Open
abck opened this issue Nov 27, 2021 · 3 comments
Open

stdlib.fmt.println not behaving like fmt.Println #352

abck opened this issue Nov 27, 2021 · 3 comments

Comments

@abck
Copy link

abck commented Nov 27, 2021

Hello,

just want to ask if the behavior of stdlib.fmt.println is correct.

I was expecting it to behave like the go's fmt.Println.

Minimal example:

func main() {
	script := tengo.NewScript([]byte(`fmt := import("fmt")
fmt.println(a, a)`))
	_ = script.Add("a", 1)

	moduleMap := stdlib.GetModuleMap(stdlib.AllModuleNames()...)
	script.SetImports(moduleMap)

	compiled, err := script.RunContext(context.Background())
	if err != nil {
		panic(err)
	}
	a := compiled.Get("a")
	fmt.Println(a, a)
}

Output:

11 
1 1

1st line is the output of tengo's fmt.println
2nd line is the output of go's fmt.Println

If this is not intended, it can be fixed by changing fmtPrintln in stdlib/fmt.go to

func fmtPrintln(args ...tengo.Object) (ret tengo.Object, err error) {
	printArgs, err := getPrintArgs(args...)
	if err != nil {
		return nil, err
	}
	_, _ = fmt.Println(printArgs...)
	return nil, nil
}
@misiek08
Copy link
Contributor

@d5 will you accept such patch?

@d5
Copy link
Owner

d5 commented Jan 22, 2022

This is the current code:

func fmtPrintln(args ...tengo.Object) (ret tengo.Object, err error) {
	printArgs, err := getPrintArgs(args...)
	if err != nil {
		return nil, err
	}
	printArgs = append(printArgs, "\n")
	_, _ = fmt.Print(printArgs...)
	return nil, nil
}

Honestly, I don't remember why I specifically used fmt.Print. It's been a while. @geseq any ideas?

@geseq
Copy link
Collaborator

geseq commented Jan 22, 2022

Tbh I can’t recall either but looking at the code the only reason I can think of is to avoid printing spaces between the operands.

that said, I would be happy for this to behave like standard library.

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