如何在 emscripten 的 EM_JS 方法中访问 js window 对象?

How to access js window object in emscripten's EM_JS method?

基本上我想在 EM_JS 方法中访问 window.location 以从 c++ 调用此 javascript 方法。

我这样试过

EM_JS(const char*, getlocation, (), {
    let location = window.location;
    let length = lengthBytesUTF8(location) + 1;
    let str = _malloc(length);
    stringToUTF8(location, str, length);
    return str;
});

但是得到

ReferenceError: window is not defined

或者有没有其他方法可以在使用 emscripten 编译的 c++ 代码中获取 window.location?但是我不想使用 js 将 window.location 的值传递给 c++ api 使用 ccall 或 cwrap.

题中上面的代码刚刚生效,只是我需要等到main加载后才能调用这个方法,看来。