Skip to content

Question on import/package #64

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

Open
pgundlach opened this issue Mar 27, 2025 · 2 comments
Open

Question on import/package #64

pgundlach opened this issue Mar 27, 2025 · 2 comments
Labels

Comments

@pgundlach
Copy link

Not sure if this is the correct place to ask...

I try to use the import statement, but get an error:

package main

import (
	"log"

	"github.com/glycerine/zygomys/v9/zygo"
)

var str = `(import  "foo")

(assert (== foo.B "I am a Public string"))
`

func dothings() error {
	var err error
	env := zygo.NewZlisp()

	if err = env.LoadString(str); err != nil {
		return err
	}
	expr, err := env.Run()
	if err != nil {
		return err
	}
	_ = expr
	return nil
}

func main() {
	if err := dothings(); err != nil {
		log.Fatal(err)
	}
}

and the file foo in the same directory:

(package foo
    (def B "I am a Public string")
)

I also tried (package "foo" ....) like in the example file prepackage in the tests directory, but also this doesn't work. The error I get is

symbol `import` not found
@glycerine
Copy link
Owner

Hi Patrick @pgundlach,

sorry this isn't more clear -- the short answer is, you just need to call env.StandardSetup() or env.ImportPackageBuilder() first in order
to use import. I added a test to demonstrate, so it goes like this:

https://github.com/glycerine/zygomys/blob/master/zygo/import_test.go#L18

Rationale: a while back, some users requested sandboxing capability. They
wanted not all functions to be available by default, to support running in environments
where we don't want all scripts going to disk, for example.

If you take a look at the implementation of StandardSetup() (called by the zygo repl, for instance) at https://github.com/glycerine/zygomys/blob/master/zygo/repl.go#L387
you can see all the things it does. It calls env.ImportPackageBuilder()
which enables imports, for example. It also defines a bunch
of macros that you may or may not find useful. If they aren't useful,
you can just do the env.ImportPackageBuilder() to only get imports.

The idea was that sandboxes can write reduced versions of StandardSetup() to
restrict the capabilities offered to scripts.

There are also some other pre-defined subsets of functions to
make it easy to isolate scripts, to provide more or less functionality. See

https://github.com/glycerine/zygomys/blob/master/zygo/functions.go#L950 though line 1099

If you start an environment with NewZlispSandbox() instead of NewZlisp(),
for instance you get a very trimmed down set of capabilities.

Anyway, thanks for asking, and I'll leave this open as a form of documentation for others. Any other questions, just raise an another issue. Or feel free to follow up
here if anything else on imports or packages needs clarifying.

Best,
Jason

@pgundlach
Copy link
Author

Thank you very much. This works fine, and I will do more experiments with zygomys.

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

No branches or pull requests

2 participants