Open
Description
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.
Metadata
Metadata
Assignees
Labels
No labels
Activity
krmbn0576 commentedon Dec 21, 2023
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 commentedon Dec 21, 2023
I think what you need is JavaScript's ArrayBuffer <-> Ruby String (ASCII_8BIT) conversion or just add some methods to
JS::Object
to behave likeIO
. 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 commentedon Dec 21, 2023
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.
ArrayBuffer#to_s
and return data converted to ASCII_8BITOf course this is destructive, but no one will spare the old behavior since the current
to_s
always returns[object ArrayBuffer]
and is useless.JS.fetch
function and convert to ASCII_8BIT if to_rstr: trueHere is how to use it.
kateinoigakukun commentedon Dec 22, 2023
Thank you for your idea. I'll continue exploring ways including yours.
kateinoigakukun commentedon Dec 22, 2023
It seems
String
class hasString#b
method to convert the self content to ASCII-8BIT string.JS::Object#b
might be a considerable option?krmbn0576 commentedon Dec 22, 2023
It might not be bad.
However,
String#b
is probably justString#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.ArrayBuffer#to_s
and return ASCII-8BIT data #384