Skip to content
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

[]byte/string parameter conversion oddity #51

Closed
liclac opened this issue Nov 28, 2017 · 4 comments
Closed

[]byte/string parameter conversion oddity #51

liclac opened this issue Nov 28, 2017 · 4 comments

Comments

@liclac
Copy link

liclac commented Nov 28, 2017

func TestByteToString(t *testing.T) {
	rt := goja.New()
	rt.Set("v", []byte("hi"))
	rt.Set("fn", func(v string) {
		assert.Equal(t, "hi", v)
	})
	_, err := rt.RunString(`fn(v)`)
	assert.NoError(t, err)
}

Expected "hi", got "104,105". Looks like mangling of arrays that could use a special case for []byte? The opposite direction appears to work just fine.

@dop251
Copy link
Owner

dop251 commented Nov 29, 2017

I'm not sure []byte should be converted to string. Not only they are distinct data types, but one is immutable, the other is not. This means the data will be copied during the conversion which is inconsistent with how it normally works.

I had an idea of converting []byte into ArrayBuffer which is more suitable, but never really had the time to implement it.

@liclac
Copy link
Author

liclac commented Nov 29, 2017

Hm, good point. The dilemma we're facing with k6 is that we need some functions to accept string literals, but we also need to be able to handle binary data (POST-ing images being the big one that comes up) in some kind of sane fashion.

If I were to contribute an ArrayBuffer implementation, how would I go about doing it?

@dop251
Copy link
Owner

dop251 commented Nov 30, 2017

Make sure it passes all relevant tc39 tests and raise a PR. However you could consider wrapping []byte into a custom type before passing it to ToValue(). You could then define methods on that type and use them in JS. It all depends on what you want to do with it...

@dop251
Copy link
Owner

dop251 commented Dec 14, 2017

I'm closing this, if you do implement ArrayBuffer, please raise a PR

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

No branches or pull requests

2 participants