Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTF8ToString does not work when compiling for 64bit #23325

Closed
ptc-jpharvey opened this issue Jan 7, 2025 · 5 comments
Closed

UTF8ToString does not work when compiling for 64bit #23325

ptc-jpharvey opened this issue Jan 7, 2025 · 5 comments

Comments

@ptc-jpharvey
Copy link

Please include the following in your bug report:

Version of emscripten/emsdk:
3.1.72

Failing command line in full:
em++ -o test.html -sMEMORY64=1 -g -fexceptions test.cpp

At runtime the method says it can only take a number not a bigint
test.js:550
Aborted(Assertion failed: UTF8ToString expects a number (got bigint))
test.js:568
Uncaught RuntimeError: Aborted(Assertion failed: UTF8ToString expects a number (got bigint))
at abort (test.js:568:11)
at assert (test.js:286:5)
at UTF8ToString (test.js:1080:7)
at SetCanvasAsEventTarget (test.js:922:60)
at invoke_vj (test.js:1592:37)
at test.wasm.main (test.wasm:0xded)
at test.js:1697:49
at test.js:620:12
at callMain (test.js:2045:15)
at doRun (test.js:2095:23)

If bigint is allowed through or assertions are disabled then the code fails in UTF8ArrayToString due to mixing types (bigint & number) etc.

#include <stdio.h>

#include "emscripten.h"
#include <string>
EM_JS(void, SetCanvasAsEventTarget, (const char* canvasName),
    {
        var canvas = UTF8ToString(canvasName);
    }
);

int
main(int argc, char **argv)
{
    printf("Hello\n");
    std::string blah("foo");
    SetCanvasAsEventTarget(blah.c_str());
}

@kripken
Copy link
Member

kripken commented Jan 7, 2025

This is expected atm, hence that assertion on it:

assert(typeof ptr == 'number', `UTF8ToString expects a number (got ${typeof ptr})`);

However, I agree that this is confusing: in wasm64, a pointer is a bigint, and it would be nice for functions that receive pointers to handle that. @sbc100 what do you think?

@kripken
Copy link
Member

kripken commented Jan 7, 2025

@ptc-jpharvey for now you can work around this with UTF8ToString(Number(canvasName))

@sbc100
Copy link
Collaborator

sbc100 commented Jan 7, 2025

The way emscripten works today is that it converts pointers to int64 (number) so the JS code can assume that pointers are int53 numbers. However we currently an automatic way to do this conversion for EM_JS function.

IIRC this is because we dont have any way to distinguish between pointers and non-pointer EM_JS arguments.

So for EM_JS and EM_ASM one currently needs to do this conversion to int64 yourself.

We should probably have more docs on how to do this..

@sbc100
Copy link
Collaborator

sbc100 commented Jan 7, 2025

Here is the issue: #16975. Maybe we can close this as a dup of that?

@kripken
Copy link
Member

kripken commented Jan 7, 2025

Sure. I'll comment there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants