Zaplib / zaplib

⚡ Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly.

Home Page:https://zaplib.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better representation of typed arrays in the browser console

janpaul123 opened this issue · comments

@disambiguator "Would be cool if we could clean up our monkey patching such that normal TypedArrays don't get represented as ZapTypedArrays, not sure how though. i.e."

image

Maybe this still works? https://stackoverflow.com/a/38652208

Or maybe we can create it not as an ES6 class but in the old prototype style so we can give it a custom name?

@disambiguator: "I dont want to try right this moment but also curious what would happen if we did.."

diff --git a/zaplib/web/zap_buffer.ts b/zaplib/web/zap_buffer.ts
index 9f5ce3f..b0a54b0 100644
--- a/zaplib/web/zap_buffer.ts
+++ b/zaplib/web/zap_buffer.ts
@@ -81,7 +81,7 @@ function zapBufferExtends(cls: any) {
         super(...args);
         this.__zaplibBuffer = buffer;
       } else {
-        super(...args);
+        cls(...args);
       }
     }

I did some investigation on this on Friday. A few problems:

If I do the above replacement such that our else path just returns the original TypedArray implementation, we get errors in getZapParamType

zaplib/zaplib/web/common.ts

Lines 378 to 389 in 74c0058

export const getZapParamType = (
array: ZapArray,
readonly: boolean
): ZapParamType => {
if (array instanceof Uint8Array) {
return readonly ? ZapParamType.ReadOnlyU8Buffer : ZapParamType.U8Buffer;
} else if (array instanceof Float32Array) {
return readonly ? ZapParamType.ReadOnlyF32Buffer : ZapParamType.F32Buffer;
} else {
throw new Error("Invalid array type");
}
};

because the array is no longer an instanceof our overridden array type, because of this line:

return class ZapTypedArray extends cls {

doesn't seem to correctly cls. For example:
image

Right but that makes sense right? In that else clause it's not backed by a ZapBuffer so it isn't a zap array any more? So maybe calls to getZapParamType should be preceded with isZapBuffer (aren't they already?) since that should return false in that case.

Not necessarily. getZapParamType is also used by createBuffer / createReadOnlyBuffer to determine what kind of param should be created from a normal TypedArray.

Oh I see I understand now. Hm, that is strange. I wonder what happens if we use a traditional constructor, not an ES6 class. I suspect that that will work with return cls(...args).