Skip to content

Are empty/nil slices externalized correctly? #399

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

Closed
dmitshur opened this issue Feb 14, 2016 · 5 comments
Closed

Are empty/nil slices externalized correctly? #399

dmitshur opened this issue Feb 14, 2016 · 5 comments

Comments

@dmitshur
Copy link
Member

I know that (interface{})(nil) gets externalized as a JavaScript null, and js.Undefined gets externalized as JavaScript undefined.

I also know that, according to the conversion table, []byte slices get externalized as Uint8Array. But other slices, e.g., []string, get externalized as Array.

However, it seems that it's not always the case. Empty []byte slices seemingly get externalized as JavaScript Array rather than Uint8Array.

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", (interface{})(nil))'
null

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", js.Undefined)'
undefined

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", []byte{1, 2, 3})'
Uint8Array { '0': 1, '1': 2, '2': 3 }

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", []byte{})'
[]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", ([]byte)(nil))'
[]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", []string{"hi", "bye"})'
[ 'hi', 'bye' ]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", []string{})'
[]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", ([]string)(nil))'
[]

Is this intentional by design, or an accidental oversight?

I've run into this as part of goxjs/gl#18.

@dmitshur
Copy link
Member Author

dmitshur commented Feb 14, 2016

The reason I ask is because I would expect js.Global.Get("console").Call("log", []byte{}) to get externalized as an empty Uint8Array:

Uint8Array {}

Not an empty Array.

Or is there a reason why numeric slices with 0 elements are treated differently than numeric slices with 1 or more elements?

@dmitshur
Copy link
Member Author

dmitshur commented Apr 17, 2017

Is this intentional by design, or an accidental oversight?

I'm guessing this was an accidental oversight, given that #597 (/cc @flimzy) looks like similar/identical issue, and it was resolved by #599.

Running the same code as above with latest GopherJS produces different results, that look more in line with what's expected:

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", (interface{})(nil))'
null

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", js.Undefined)'
undefined

$  goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", []byte{1, 2, 3})'
Uint8Array [ 1, 2, 3 ]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", []byte{})'
Uint8Array [  ]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", ([]byte)(nil))'
Uint8Array [  ]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", []string{"hi", "bye"})'
[ 'hi', 'bye' ]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", []string{})'
[]

$ goexec -quiet -compiler=gopherjs 'js.Global.Get("console").Call("log", ([]string)(nil))'
[]

@dmitshur
Copy link
Member Author

I'm guessing this was an accidental oversight, given that #597 (/cc @flimzy) looks like similar/identical issue, and it was resolved by #599.

Since no one has disputed my guess above so far, I'm going to take that to be the reality.

Closing as duplicate of #597.

@myitcv
Copy link
Member

myitcv commented Jul 27, 2017

@shurcooL apologies I missed this for some reason.

I'll make sure I look at this in #617 (comment)

@dmitshur
Copy link
Member Author

4. Ensure we address #399

@myitcv, #597 (the issue this is a duplicate of) has already been resolved by #599.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants