UInt8Array library?

In JavaScript, there exists a type, Uint8Array. This type allows you to store a array of some size filled with bytes. This provides a memory-compact way to store raw binary data rather than arrays. I would like a library (along with a extension JS file for the JS part, in Snap!) that allows you to create and modify these arrays.

Lets take one use case; a emulator, specifically ROM/RAM for it. Normally most architectures use a big area filled with bytes, of course; you know this. Now in Snap! you might want to describe this with a list that defaults to zeros spanning across the data size you need. But this is inefficient for many reasons:

  • It has to be a Snap! list. This is a small wrapper, yet it does add more memory.
  • Even under Snap!'s List class the JS array itself is meant for dynamic adding/removing, not a raw array (like in C where you do something like type array[size];)
  • Each element is a whole float (not just a single byte!)

So for this use case, a Uint8Array would be very VERY useful! It would also allow you to save the memory or such. Now there is, of course; way more use cases. But this one is a important one. With Uint8Array you would also be able to read/write random binary formats (kind of a niche use case, but might be nice for loading formats Snap! doesn’t support).

Now how would you implement this? Of course in Snap! it would be a seperate extension JS file that create extensions, and the library to complement that. But for now, I made this little proof-of-concept (might be buggy and doesnt have all the features I want, but it works):

Hope this gets added to Snap!, sometime (probably not during the Snap! v12 lifecycle, but maybe in the future Snap! v13??) :~)

I think you’d have a better chance trying for v12.4 or something than v13.0. The time before a major update has Jens running around like a chicken with his head cut off; he’d probably more amenable to this at a relaxed time.

Not arguing against this feature, but if hardware emulation is the use case for it, you should bear in mind that modern computers don’t actually read memory a byte at a time, even if their instruction set pretends to. The CPU↔Memory bus is typically several words wide, so that the processor can fill an entire cache row at once. But I guess you can emulate the instruction set without emulating the memory architecture.

I accidentally went to the UInt8Array documentation.

Whoa, what a way to express this!

Yeah, but there’s still some lower-end CPUs or such that read bytes (and would probably be better to emulate on Snap! of all things), e.g. 6502, or some other 8-bit processors.

Update: I have now changed the JS blocks sprinkled about with one init block, that uses SnapExtensions; so it can more easily be added (if it ever will).