Skip to content

Commit dfffae7

Browse files
committed
Internalize Date when time package not imported in a reasonable way.
Fixes #279 (additional findings). When time package is not imported, it's still possible to internalize a Date object (into an interface{}). Due to fallthrough being default, this resulted in Function case running when internalizing Date with no time package. That makes no sense, and means that trying to externalize the Date object back into JavaScript world would fail to produce the original Date object (also, calling methods on it would obviously fail, since it's not a real Date object). For example, consider the following code that doesn't import time package, it can now run and produce expected results: date := js.Global.Get("Date").New().Interface() js.Global.Get("console").Call("log", date)
1 parent 4c71d86 commit dfffae7

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

compiler/prelude/jsmapping.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ var $internalize = function(v, t, recv) {
273273
var timePkg = $packages["time"];
274274
if (timePkg) {
275275
return new timePkg.Time(timePkg.Unix(new $Int64(0, 0), new $Int64(0, v.getTime() * 1000000)));
276+
} else {
277+
/* time package is not present, internalize as &js.Object{Date} so it can be externalized into original Date. */
278+
return new $jsObjectPtr(v);
276279
}
277280
case Function:
278281
var funcType = $funcType([$sliceType($emptyInterface)], [$jsObjectPtr], true);

0 commit comments

Comments
 (0)