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

Instance defined with VmModules is not a new instance #315

Open
houxul opened this issue Jul 22, 2019 · 24 comments
Open

Instance defined with VmModules is not a new instance #315

houxul opened this issue Jul 22, 2019 · 24 comments

Comments

@houxul
Copy link

houxul commented Jul 22, 2019

Anko code

module User{
	_age = -1
	_name = "undefined"

	func SetAge(age) {
		_age = age
	}

	func SetName(name) {
		_name = name
	}
}

data = [
	{
		"age": 10,
		"name" : "jack"
	},
	{
		"age": 0,
		"name" : ""
	},
]

for item in data {
	user = User
	if item["age"] != 0 {
		user.SetAge(item["age"])
	}

	if item["name"] != "" {
		user.SetName(item["name"])
	}
	
	println(user._age)
	println(user._name)
}

Expected output

10
jack
-1
undefined

Actual output

10
jack
10
jack
@MichaelS11
Copy link
Contributor

Looks like a bug to me. Another example:

package main

import (
	"fmt"
	"log"

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

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

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

	script := `
module rectangle {
	_length = 1
	_width = 1
	
	func setLength (length) {
		if length <= 0 {
			return
		}
		_length = length
	}
	
	func setWidth (width) {
		if width <= 0 {
			return
		}
		_width = width
	}
	
	func area () {
		return _length * _width
	}
	
	func perimeter () {
		return 2 * (_length + _width)
	}
 }

var rectangle1 = rectangle

rectangle1.setLength(4)
rectangle1.setWidth(5)

var rectangle2 = rectangle

rectangle2.setLength(2)
rectangle2.setWidth(4)

println(rectangle1.area())
println(rectangle1.perimeter())

println(rectangle2.area())
println(rectangle2.perimeter())
`

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

Output:

8
12
8
12

Expected output:

20
18
8
12

@MichaelS11
Copy link
Contributor

@mattn

So for var rectangle1 = rectangle the right side is an IdentExpr, which means it is basically calling env.get("rectangle"). This is setting rectangle1 to an *Env which is the same *Env that rectangle points to.
Here is the line of code that is doing that setting:

anko/vm/env.go

Line 277 in adf6aee

e.env[k] = v

I know this can be fixed by creatinga copy of the env, but not sure if that is the correct approach or where that code should go. Any thoughts @mattn ?

@mattn
Copy link
Owner

mattn commented Jul 24, 2019

I don't look code yet but it seems correct behavior.

@MichaelS11
Copy link
Contributor

@mattn

I think there would be a difference between rectangle1 = rectangle and var rectangle1 = rectangle but they do the same thing, copy the pointer instead of copying the whole module.

@traetox
Copy link

traetox commented Dec 2, 2019

Is there a work around that would allow for instantiating a new module?

Module "types" don't work with new or make.

@mattn
Copy link
Owner

mattn commented Dec 3, 2019

Most of users not use module. So we can change specification of module, I think.

@MichaelS11
Copy link
Contributor

I am going to look into this more, see if there is something not too hard to make this work.

@traetox
Copy link

traetox commented Dec 3, 2019

Even if we could figure out how to hook the new() function to allow for copying that environment, that would be cool too. I don't want to advocate for changing the language spec if at all possible.

@MichaelS11
Copy link
Contributor

The new spec is looking for a type, so would like to look at other options.

So having a module named a, like:
module a { b = 1 }
Here are some thoughts for a copy of a to c:

  1. module c = a
  2. var c = a
  3. c := a

Please let me know if you have any other ideas and your thoughts about the above.

@traetox
Copy link

traetox commented Dec 4, 2019 via email

@MichaelS11
Copy link
Contributor

@mattn What you think?

@mattn
Copy link
Owner

mattn commented Dec 4, 2019

On my first design, module meant namespace not class. So I did not expect that module can be instance with new keyword. But most of users does not use module and they seems to expect classes. We may have to add classes.

@MichaelS11
Copy link
Contributor

So rename module to class? Have some details on what you would like?

@mattn
Copy link
Owner

mattn commented Dec 4, 2019

Let's add new code for class and make "module" deprecated.

Go's reflect does not have APIs to make struct dynamically. So adding class on anko will be useful.

Class has methods and self-keyword, and definition of variables.

@MichaelS11
Copy link
Contributor

Yes it does. https://golang.org/pkg/reflect/#StructOf

Sounds like we just need to support struct better and that should work instead of classes.

@mattn
Copy link
Owner

mattn commented Dec 4, 2019

Yes, few years ago, I worked in progress for struct. But some APIs did not exists in that time.

158d91c

@MichaelS11
Copy link
Contributor

Ok, so deprecate modules and add better support for structs.

@MichaelS11
Copy link
Contributor

@mattn What about this PR #322 for now?

@MichaelS11
Copy link
Contributor

@traetox Please test

@traetox
Copy link

traetox commented Dec 5, 2019 via email

@MichaelS11
Copy link
Contributor

@traetox All good?

@traetox
Copy link

traetox commented Dec 15, 2019

doesn't look like the PR fixed it.

Here is a test script:

#!anko

module Foo {
  var x = "testing"
  func bar1() {
    return x
  }
  func bar2(z) {
     x = z
  }
}

var t1 = Foo
var t2 = Foo
t1.bar2("A")
t2.bar2("B")
println(t1.bar1())
println(t2.bar1())
println(Foo.bar1())

Using anko version 0.15 i got the following output

B
B
B

Expected output:

A
B
testing

@MichaelS11
Copy link
Contributor

So t1 and t2 are new instances of Foo however the functions still point to the orginal variable x. I will think about it some more but I don't think there are any easy fixes for modules.

Will probably need to work on better struct support so can do Go struct methods. In partiluar, need to add struct creation.

Note that struct methods are supported to some extent, can look at Sort for an example:
https://godoc.org/github.com/mattn/anko/vm#example-package--VmSort

So for now if you define the struct using env define, it might do what you are looking for.

@traetox
Copy link

traetox commented Dec 16, 2019

ok, that seems entirely fair. I would say you could close this as "working as intended"

Thank you for your time! We will poke a bit as we can on the code base and see if we can get some PRs that make sense.

Thank you!

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