I need to create an ArrayBufferView (exactly Uint8Array) on an existing ArrayBuffer. I couldn’t find the type in RemObjects.Elements.WebAssembly.DOM (there is ArrayBufferView but it’s just an empty interface) so I attempted to create it “manually” using RemObjects.Elements.System.WebAssembly.ReflectConstruct. I pass ‘Uint8Array’ string and an array with the ArrayBuffer object as the constructor arguments. The object is created but the length is different from ArrayBuffer.byteLength (it’s just a big number e.g. 5604996) and all the elements are zeros.
namespace TestModule;
type
[Export]
Program = public class
public
method Main;
begin
var len := 32;
var buf := WebAssembly.ReflectConstruct('ArrayBuffer', [len]);
assert(buf.byteLength = len); // OK
var arr := WebAssembly.ReflectConstruct('Uint8Array', [buf]);
assert(arr.length = len); // WRONG
end;
end;
end.
So the question is - how to “manually” create an object that takes another object as an argument in the constructor?
What am I missing? Or is there another approach that I failed to discover?
As I’ve mentioned before I don’t need to simply create a Uint8Array of a given size (your code), I need to create a view on an existing ArrayBuffer to read its contents. There’s no way to directly read an ArrayBuffer, you need an ArrayBufferView one of which is Uint8Array (if you need to deal with individual bytes).
Why do I need this? To read a local file. I have an <input type="file"> in my document which provides a FileList which provides a File which provides an ArrayBuffer.
Yes, this is exactly my point from the original post. I wasn’t sure if I was doing something wrong or if there is a general problem when you’re trying to call a constructor with an object as an argument. It seems to me that our tests are equivalent so it confirms the latter.