flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

Flexbuffers arithmetic missing BigInt/Number conversion [JS/TS, [email protected]]

Open jcmoore opened this issue 2 years ago • 9 comments

Invoking

toObject(
  new Uint8Array([116,0,118,0,98,108,117,114,0,102,111,99,117,115,0,116,77,111,110,111,0,116,78,111,119,0,116,90,111,110,101,0,117,114,108,0,0,0,6,35,31,26,21,17,12,0,0,0,9,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,222,33,222,34,162,142,120,66,216,193,35,234,136,1,0,0,44,1,0,0,0,0,0,0,75,0,0,0,0,0,0,0,4,4,15,7,5,20,2,127,126,2,1,2,3,61,4,39,4,36,1]).buffer
);

causes Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions around here.

Looks like type coercions are already happening elsewhere, so I'd guess another coercion would not hurt, but I haven't grokked the how and why of indirect numbers sufficiently to know for sure.


Edit 1: somewhat related, toObject(new Uint8Array([2,0,0,0,0,0,0,0,255,255,255,255,255,255,255,255,0,64,129,162,176,142,120,66,4,15,18,43,1])) reveals a few other as number type assertions that should probably be substituted for Number(value) coercions.

Example buffers are coming from my investigation of #8010

jcmoore avatar Jun 23 '23 22:06 jcmoore

I'm experimenting in the browser and edge function and experience the same error. Using random not too complex object:

import { encode, toObject } from 'flatbuffers/js/flexbuffers';
toObject(encode(randomObject).buffer);

I get exactly the same error: Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions

flexchar avatar Nov 24 '23 10:11 flexchar

I figured I had some long floats and long integers such as Unix timestamp. Max 12 digits. This helper function to stringify numbers solved the issue.

// Stringify all numbers
function prepare(obj: any): any {
    for (const key in obj) {
        if (typeof obj[key] === 'number') {
            obj[key] = obj[key].toString();
        } else if (typeof obj[key] === 'object') {
            // Recursively prepare objects
            obj[key] = prepare(obj[key]);
        }
    }
    return obj;
}

flexchar avatar Nov 24 '23 11:11 flexchar

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

github-actions[bot] avatar May 24 '24 20:05 github-actions[bot]

not-stale

flexchar avatar May 25 '24 07:05 flexchar