Skip to content

Commit 22fe4b5

Browse files
committed
Merge pull request #288 from gopherjs/fix-287
Fix internalize case for Date into time.Time struct.
2 parents 23bfde2 + fc96db6 commit 22fe4b5

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

compiler/prelude/jsmapping.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ var $internalize = function(v, t, recv) {
181181
if (t === $jsObjectPtr.elem) {
182182
$panic(new $String("cannot internalize js.Object, use *js.Object instead"));
183183
}
184+
var timePkg = $packages["time"];
185+
if (timePkg !== undefined && t === timePkg.Time) {
186+
if (!(v !== null && v !== undefined && v.constructor === Date)) {
187+
$panic(new $String("cannot internalize time.Time from " + typeof v + ", must be Date"));
188+
}
189+
return timePkg.Unix(new $Int64(0, 0), new $Int64(0, v.getTime() * 1000000));
190+
}
184191
switch (t.kind) {
185192
case $kindBool:
186193
return !!v;
@@ -270,12 +277,11 @@ var $internalize = function(v, t, recv) {
270277
case Boolean:
271278
return new $Bool(!!v);
272279
case Date:
273-
var timePkg = $packages["time"];
274280
if (timePkg === undefined) {
275281
/* time package is not present, internalize as &js.Object{Date} so it can be externalized into original Date. */
276282
return new $jsObjectPtr(v);
277283
}
278-
return new timePkg.Time(timePkg.Unix(new $Int64(0, 0), new $Int64(0, v.getTime() * 1000000)));
284+
return new timePkg.Time($internalize(v, timePkg.Time));
279285
case Function:
280286
var funcType = $funcType([$sliceType($emptyInterface)], [$jsObjectPtr], true);
281287
return new funcType($internalize(v, funcType));

js/js_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@ func TestDate(t *testing.T) {
287287
}
288288
}
289289

290+
// https://github.com/gopherjs/gopherjs/issues/287
291+
func TestInternalizeDate(t *testing.T) {
292+
var a = time.Unix(0, (123 * time.Millisecond).Nanoseconds())
293+
var b time.Time
294+
js.Global.Set("internalizeDate", func(t time.Time) { b = t })
295+
js.Global.Call("eval", "(internalizeDate(new Date(123)))")
296+
if a != b {
297+
t.Fail()
298+
}
299+
}
300+
290301
func TestEquality(t *testing.T) {
291302
if js.Global.Get("Array") != js.Global.Get("Array") || js.Global.Get("Array") == js.Global.Get("String") {
292303
t.Fail()
@@ -480,6 +491,21 @@ func TestNewArrayBuffer(t *testing.T) {
480491
}
481492
}
482493

494+
func TestInternalizeExternalizeNull(t *testing.T) {
495+
type S struct {
496+
*js.Object
497+
}
498+
r := js.Global.Call("eval", "(function(f) { return f(null); })").Invoke(func(s S) S {
499+
if s.Object != nil {
500+
t.Fail()
501+
}
502+
return s
503+
})
504+
if r != nil {
505+
t.Fail()
506+
}
507+
}
508+
483509
func TestInternalizeExternalizeUndefined(t *testing.T) {
484510
type S struct {
485511
*js.Object

0 commit comments

Comments
 (0)