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

[question] How can I register native type into script? #441

Open
ckcfcc opened this issue Feb 22, 2024 · 1 comment
Open

[question] How can I register native type into script? #441

ckcfcc opened this issue Feb 22, 2024 · 1 comment

Comments

@ckcfcc
Copy link

ckcfcc commented Feb 22, 2024

I want to register my own object MyPos in go code, a struct like this.

golang code:

type MyPos struct {
	X, Y int
}

And provide a newMO() function to return this object. I hope that the members of this object can be accessed in the script, but I do not want to register this object in the script, for example:

script code:

	mp := import(mypos)

	pos1 := mp.newMO()

	pos1.X = 1000
	pos1.Y = 20
	...

Please tell me how to do this. Thx!

type MyPos struct {
	X, Y int
}

func newMyPos(args ...tengo.Object) (ret tengo.Object, err error) {
	myPos := &MyPos{}
	return ... // ??? how can I return myPos
}

var PosModule = &tengo.BuiltinModule{
	Attrs: map[string]tengo.Object{
		"newMO": &tengo.UserFunction{
			Name:  "newMO",
			Value: newMyPos,
		},
	},
}
@ckcfcc
Copy link
Author

ckcfcc commented Feb 23, 2024

The previous problem has been solved.

But a new problem has arisen. The values of members in the native object cannot be changed in the script. What method can be used to solve this problem?

native module code:

type MyPos struct {
	X, Y int
}

func (mp *MyPos) GetTengoObject() tengo.Object {
	return &tengo.Map{
		Value: map[string]tengo.Object{
			"x": &tengo.Int{Value: int64(mp.X)},
			"y": &tengo.Int{Value: int64(mp.Y)},
			"print": &tengo.UserFunction{
				Name: "print",
				Value: func(args ...tengo.Object) (ret tengo.Object, err error) {
					mp.Print()
					return tengo.UndefinedValue, nil
				},
			},
		},
	}
}

func (mp *MyPos) Print() {
	log.Printf("[native] X:%d Y:%d", mp.X, mp.Y)
}

func newMyPos(args ...tengo.Object) (ret tengo.Object, err error) {
	myPos := &MyPos{X: 1, Y: 2}
	return myPos.GetTengoObject(), nil
}

var PosModule = &tengo.BuiltinModule{
	Attrs: map[string]tengo.Object{
		"newMP": &tengo.UserFunction{
			Name:  "new",
			Value: newMyPos,
		},
	},
}

script code:

md := import("myModule")

p1 := md.newMP()
p1.x += 123
p1.y = 234
log("x:%d y:%d", p1.x, p1.y) // output script object field value
p1.y -= 11
p1.print() // output native object field value

output:
2024/02/23 11:48:49 [scp] x:124 y:234
2024/02/23 11:48:49 [native] X:1 Y:2 // why hasn't it changed?

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

1 participant