Skip to content

[Web] Add memory64 option to setup the foundations of wasm64 #105670

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion platform/web/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_tools(env: "SConsEnvironment"):


def get_opts():
from SCons.Variables import BoolVariable
from SCons.Variables import BoolVariable, EnumVariable

return [
("initial_memory", "Initial WASM memory (in MiB)", 32),
Expand All @@ -57,6 +57,13 @@ def get_opts():
"Use Emscripten PROXY_TO_PTHREAD option to run the main application code to a separate thread",
False,
),
EnumVariable(
"memory64",
"Set Emscripten MEMORY64 mode",
"wasm32",
["wasm32", "wasm64", "wasm32+64bitptrs"],
{"no": "wasm32", "yes": "wasm64", "0": "wasm32", "1": "wasm64", "2": "wasm32+64bitptrs"},
),
]


Expand Down Expand Up @@ -303,3 +310,14 @@ def configure(env: "SConsEnvironment"):
# This workaround creates a closure that prevents the garbage collector from freeing the WebGL context.
# We also only use WebGL2, and changing context version is not widely supported anyway.
env.Append(LINKFLAGS=["-sGL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0"])

# Set 32-bit or 64-bit pointers.
if env["memory64"] == "wasm32":
memory64_value = 0
elif env["memory64"] == "wasm64":
memory64_value = 1
else:
# "wasm32+64bitptrs"
memory64_value = 2
env.Append(CCFLAGS=[f"-sMEMORY64={memory64_value}"])
env.Append(LINKFLAGS=[f"-sMEMORY64={memory64_value}"])
2 changes: 2 additions & 0 deletions platform/web/os_web.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class OS_Web : public OS_Unix {
// Override return type to make writing static callbacks less tedious.
static OS_Web *get_singleton();

_FORCE_INLINE_ bool is_memory64() const { return sizeof(size_t) == sizeof(uint64_t); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true or false with wasm32+64bitptrs?

Copy link
Member Author

@adamscott adamscott Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true or false with wasm32+64bitptrs?

This is true. The runtime is wasm32, but the pointer sizes are 64-bit wide. The previous WASM_MEMORY64_ENABLED define was applied to wasm32+64bitptrs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true or false with wasm32+64bitptrs?

This is true. The runtime is wasm32, but the pointer sizes are 64-bit wide. The previous WASM_MEMORY64_ENABLED define was applied to wasm32+64bitptrs.

This is why I specified the option as +64bitptrs instead of choosing a simpler, but way more cryptic wasm32+64 (which would imply wasm32+wasm64, which is not the case).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm. Makes me reconsider the use of actual name of the function is_memory64(), as it may be misleading. Technically, wasm32+64bitptrs isn't truly MEMORY64.


bool pwa_needs_update() const { return pwa_is_waiting; }
Error pwa_update();
void force_fs_sync();
Expand Down