Workarounds for passing 64bit from C to JS

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Workarounds for passing 64bit from C to JS

Postby leryss » Mon Mar 11, 2024 2:16 pm

From my understanding JS objects are stored in doubles and we only get something like 2^53 maximum ints. This is fine for most cases. But i have a very specific case where i need 64bit integers.
My first workaround was to send the int64s as an object/array with 2 ints. This works well but it feels very hacky, are there any other alternatives that you would recommend ? I was thinking a BigInt but im unsure how to efficiently create BigInt instances in C++.
leryss
Newbie
 
Posts: 3
Joined: Mon Mar 11, 2024 2:10 pm

Re: Workarounds for passing 64bit from C to JS

Postby magreenblatt » Tue Mar 12, 2024 6:39 pm

My first workaround was to send the int64s as an object/array with 2 ints.

Why does it need to be an object/array? Are you using it as a function return value? You could also wrap it in a BigInt on the JS side and extract the 32-bit high/low value when required.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Workarounds for passing 64bit from C to JS

Postby leryss » Sat Mar 16, 2024 11:18 am

How do i wrap it in a BigInt there is nothing such as CefV8Value::CreateBigInt. I cant find anything related to this, the only way I found to do this is to write a function that returns a BigInt and call it from C
leryss
Newbie
 
Posts: 3
Joined: Mon Mar 11, 2024 2:10 pm

Re: Workarounds for passing 64bit from C to JS

Postby magreenblatt » Sat Mar 16, 2024 11:51 am

Where (C or JS) does the 64-bit integer come from, and where (C or JS) do you want to use it?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Workarounds for passing 64bit from C to JS

Postby leryss » Sat Mar 16, 2024 1:52 pm

everything is on the render process, basically i modify the global V8 context in OnContextCreated and add some of my generated V8 bindings (i generate externally a bunch of CefV8Handlers for all my bindings).
when i call my bindings from JS the serialization of args is not done in JS but rather in C++ in the render process

all the primitive types have reader/writes from and to CefV8Values. you can see an example below of what i currently have for int64
Code: Select all
   
     inline CefRefPtr<CefV8Value> write_t_int64(const int64_t val) {
        auto obj = CefV8Value::CreateObject(nullptr, nullptr);
        obj->SetValue("l", CefV8Value::CreateUInt(val & 0xFFFFFFFF), V8_PROPERTY_ATTRIBUTE_NONE);
        obj->SetValue("h", CefV8Value::CreateUInt(val >> 32), V8_PROPERTY_ATTRIBUTE_NONE);
       
        return obj;
    }


what i dont understand is how can i modify this code to return a BigInt
leryss
Newbie
 
Posts: 3
Joined: Mon Mar 11, 2024 2:10 pm

Re: Workarounds for passing 64bit from C to JS

Postby magreenblatt » Sat Mar 16, 2024 2:16 pm

You could return the int64 value as a string and then pass it to the BitInt constructor in JavaScript. For example: https://developer.mozilla.org/en-US/doc ... escription. That would also work for values greater than 64-bit.

If you must return a BigInt directly from C++ (vs returning a string) then you could use CefV8Context::Eval to create it from C++ code.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 208 guests