Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
Add some explains to the function 'RegisterVars'
Browse files Browse the repository at this point in the history
  • Loading branch information
haifenghuang committed Jun 4, 2018
1 parent 906d2da commit 676c06a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/monkey/eval/goobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ func GoValueToObject(obj interface{}) Object {

func RegisterVars(name string, vars map[string]interface{}) {
for k, v := range vars {
//Note: Here we do not convert GoValue to Object using 'GoValueToObject()' function.
// If we do, then we may get the result which we do not expect. e.g.
//
// let hours, _ = gtime.ParseDuration("10h")
// gfmt.Println(hours) //Output: 5577006791947779410
//
// The expected result should be '10h0m0s', but we get '5577006791947779410'.
// The reason is that 'ParseDuration' returns a `Duration` type, the `Duration`
// type's kind is 'reflect.Int64', if we use 'GoValueToObject()' function to
// convert `Duration` type to 'Integer' type, then when we print the result, it will
// print 5577006791947779410. For it to work as expected, we need to keep the `Duration` as it is
// and make no conversion.
//SetGlobalObj(name + "." + k, GoValueToObject(v))
SetGlobalObj(name + "." + k, NewGoObject(v))
}
Expand Down

0 comments on commit 676c06a

Please sign in to comment.