-
Notifications
You must be signed in to change notification settings - Fork 570
Date to time.time causes Uncaught Error: cannot internalize time.Time #287
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
Comments
This looks like a bug in package main
import (
"fmt"
"time"
"github.com/gopherjs/gopherjs/js"
)
func Foo(x string) {
fmt.Println(x)
}
func Bar(x []int) {
fmt.Println(x)
}
func Baz(x time.Time) {
fmt.Println(x)
}
func main() {
js.Global.Set("Foo", Foo)
js.Global.Set("Bar", Bar)
js.Global.Set("Baz", Baz)
} <script type="text/javascript">
Foo("hello");
var x = new Int32Array(3);
x[0] = 1;
x[1] = 2;
x[2] = 3;
Bar(x);
Baz(new Date());
</script> Output: I've tried a few various types, and it seems to only have a problem with I've confirmed this is not related to #279 (because adding Looking at the generated JavaScript for the above sample, it looks odd. I'd expect to see sliceType = $sliceType($emptyInterface);
funcType = $funcType([$String], [], false);
sliceType$1 = $sliceType($Int);
funcType$1 = $funcType([sliceType$1], [], false);
funcType$2 = $funcType([time.Time], [], false);
...
main = function() {
var $ptr;
$global.Foo = $externalize(Foo, funcType);
$global.Bar = $externalize(Bar, funcType$1);
$global.Baz = $externalize(Baz, funcType$2);
}; But that might be intended and the problem could be elsewhere. Edit: Yeah, that part might be fine. The problem might be in |
It seems the following patch for I'm not sure if this is the right solution/approach, but it seems to work, and it seems symmetrical ( diff --git a/compiler/prelude/jsmapping.go b/compiler/prelude/jsmapping.go
index 38874da..8566a20 100644
--- a/compiler/prelude/jsmapping.go
+++ b/compiler/prelude/jsmapping.go
@@ -313,6 +313,13 @@ var $internalize = function(v, t, recv) {
}
return s;
case $kindStruct:
+ if (v !== null && v !== undefined && v.constructor === Date) {
+ var timePkg = $packages["time"];
+ if (timePkg !== undefined && t === timePkg.Time) {
+ return timePkg.Unix(new $Int64(0, 0), new $Int64(0, v.getTime() * 1000000));
+ }
+ }
+
var noJsObject = {};
var searchJsObject = function(t) {
if (t === $jsObjectPtr) { Here's the output of my sample program after applying this patch: |
Test case for #287, currently failing.
Yes, it is right that we should handle this case. My opinion:
|
That sounds good, I'll make those changes in the PR. |
Can you elaborate on this? How/why would |
I was just thinking about the timestamp 0. Maybe it would be better to also panic in these cases and make sure that you can use |
Test case for #287, currently failing.
I've looked into that, and it's not quite true. There's nothing fundamentally different about unmarshaling into a The type
Unmarshalling t := (*time.Time)(nil)
fmt.Println(t.IsZero())
// panic: runtime error: invalid memory address or nil pointer dereference Can you please elaborate on what you mean by "timestamp 0"? When does it occur in JavaScript and how is it normally used? In what situation would you want to try to internalize a null into a |
Attempt to follow suggestion in #287 (comment). Not quite sure it's an improvement at this time, but maybe it just needs more work? No handling of null or undefined values instead of Date. Also no support for *time.Time at this time.
This sounded like a good idea when I first heard it, but I tried it now and I'm not very happy with how it's working out so far. Maybe you'll have ideas on how to improve it, or maybe you'll agree it's not an improvement. Well, I'm still unsure... #293 - Please take a look and review. For one, I don't like that the Also, the |
I would not worry about performance, this is really insignificant. You can remove line 280. Then it looks quite good to me. |
Nice idea, done in 81cc5d8. |
Refactor based on suggestion at #287 (comment). Panic on null or undefined values instead of Date, no support for *time.Time. Remove unneeded declaration. The same variable is already declared in parent scope.
All right, I've cleaned it up and added that refactor commit to #288. |
According to the docs (if I'm reading them correctly) I can expose a method to Javascript and call it using MakeWrapper. GopherJS will take care of the conversion for the arguments within the exposed methods. (http://godoc.org/github.com/gopherjs/gopherjs/js#MakeWrapper)
However I can't seem to get a date object converted in to time.Time
Playground link: http://www.gopherjs.org/playground/#/JEcteZCXUP
When I call dog.New(new Date()) I get
Uncaught Error: cannot internalize time.Time
Am I missing something here?
The text was updated successfully, but these errors were encountered: