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

Port arraybuffer to ruby #342

Open
krmbn0576 opened this issue Dec 15, 2023 · 6 comments · May be fixed by #384
Open

Port arraybuffer to ruby #342

krmbn0576 opened this issue Dec 15, 2023 · 6 comments · May be fixed by #384

Comments

@krmbn0576
Copy link
Contributor

require "js"

response = JS.global.fetch("<dumped buffer's url>").await
puts response[:status]

arraybuffer = response.arrayBuffer.await

# Expected goal:
#   Marshal.load(arraybuffer)

some byte arrays need to port ruby (typically generated by Marshal.dump), but no fast way

# Correct but too slow way:
uint8array = JS.global[:Uint8Array].new(arraybuffer)
Marshal.load(uint8array.to_a.map(&:to_i).pack("C*")) # Especially the slow JS::Object#to_i on each byte.
@krmbn0576
Copy link
Contributor Author

Hi @kateinoigakukun , what is the status of this issue?
Is more detailed information required to understand the issue?
Or did you understand the issue, but decided it was not important?
Is my contribution to the issue welcome or not?

@kateinoigakukun
Copy link
Member

I think what you need is JavaScript's ArrayBuffer <-> Ruby String (ASCII_8BIT) conversion or just add some methods to JS::Object to behave like IO. But I'm still not sure what kind of design is good to be Rubyish.

If you have any particular idea or implementation, I'm open to discussing it.

@krmbn0576
Copy link
Contributor Author

Hmmm certainly worth thinking about.
I'm not a Rubyist, so it's not a brilliant idea by any means, but I've thought of two ways.

  1. specialize ArrayBuffer#to_s and return data converted to ASCII_8BIT

Of course this is destructive, but no one will spare the old behavior since the current to_s always returns [object ArrayBuffer] and is useless.

  1. create a new JS.fetch function and convert to ASCII_8BIT if to_rstr: true

Here is how to use it.

ascii_8bit = JS.fetch("<url>", to_rstr: true).await

@kateinoigakukun
Copy link
Member

Thank you for your idea. I'll continue exploring ways including yours.

@kateinoigakukun
Copy link
Member

It seems String class has String#b method to convert the self content to ASCII-8BIT string. JS::Object#b might be a considerable option?

@krmbn0576
Copy link
Contributor Author

It might not be bad.
However, String#b is probably just String#force_encoding("ASCII-8BIT") internally.
Note that the nature of this issue is conversion, which is more like String#encode, so the nuance may be a bit different.

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

Successfully merging a pull request may close this issue.

2 participants