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

Execute function instead of whole script #319

Closed
vulpivia opened this issue Nov 6, 2019 · 6 comments
Closed

Execute function instead of whole script #319

vulpivia opened this issue Nov 6, 2019 · 6 comments

Comments

@vulpivia
Copy link

vulpivia commented Nov 6, 2019

Is it possible to execute a specific function in a script instead of the entire script? Something like vm.ExecuteFunction with the name of the function as a parameter would be nice.

@MichaelS11
Copy link
Contributor

I think there are other ways to solve this issue. For example, first run in the script with all the functions, then can run a script with just the wanted function. Could also look into using modules.

@vulpivia
Copy link
Author

vulpivia commented Nov 6, 2019

I'm not sure I understand. I can run a whole script, but how would I then run the wanted function only?

Modules look basically like classes to me. Unless I can interact with them from outside Anko, I'm not sure that modules would help me.

@MichaelS11
Copy link
Contributor

Split the script into two part. First run in the part with all the functions. Then run a script with just the function you want to call.

@vulpivia
Copy link
Author

vulpivia commented Nov 6, 2019

Are the variables shared between the two parts?

Basically I have a function (or in this case script) that gets called in regular intervals and with each execution of the script, all variables are reset. My thought was that if I just call a function and not the whole script, I can have variables outside of that function that don't reset between calls.

@MichaelS11
Copy link
Contributor

MichaelS11 commented Nov 6, 2019

Any script can be run in any env, the env is where the script runs. Can keep running scripts as long as you want in the same env.

package main

import (
	"fmt"
	"log"

	"github.com/mattn/anko/env"
	"github.com/mattn/anko/vm"
)

func main() {
	e := env.NewEnv()

	err := e.Define("println", fmt.Println)
	if err != nil {
		log.Fatalf("Define error: %v\n", err)
	}

	script := `
a = 1
	`

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	script = `
a = a + 1
println(a)
`

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

}

Output:

2
3
4

@vulpivia
Copy link
Author

vulpivia commented Nov 6, 2019

Okay, that solves this issue. Thanks!

@vulpivia vulpivia closed this as completed Nov 6, 2019
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

2 participants