Add Emscripten/WebAssembly support#175
Open
key2 wants to merge 1 commit into
Open
Conversation
Minimal changes to make the library compile and run correctly when targeting WebAssembly with Emscripten (GS_LIN/_LINUX platform macros, DONT_USE_XERCES_AS_XMLLIB, mvrxchange excluded): - UUID.cpp: Emscripten provides no libuuid; generate random (v4) UUIDs from std::random_device instead. - FilingWrapper.cpp: getpwuid() returns NULL under Emscripten; resolve the app data path from $HOME with a fallback to /home/web_user (the default MEMFS home directory). - XmlFileHelper: wasm32 defines size_t as unsigned long, which is a distinct type from Uint32 (unsigned int) even though both are 32 bit, unlike the other 32-bit targets the IS64BIT guard was written for. Compile the Uint32 overloads of ConvertInteger under Emscripten as well, otherwise calls with Uint32 arguments are ambiguous.
|
Thanks for opening this pull request! Please check out our contributing guidelines. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This makes libMVRgdtf compile and run correctly as WebAssembly with Emscripten, so the reference implementation can be used directly in browsers (and Node.js) with 100% file compatibility — e.g. for web-based GDTF/MVR viewers and visualizers — instead of re-implementing the formats in JS.
The library builds with the
GS_LIN/_LINUXplatform macros,DONT_USE_XERCES_AS_XMLLIB(bundled tinyxml2/rapidxml) and withoutBUILD_MVR_XCHANGE(raw TCP/mDNS is not available in a browser sandbox). Only three small platform issues needed changes:Changes
UUID.cpp— Emscripten has no libuuid and no/proc. Under__EMSCRIPTEN__, generate random (version 4) UUIDs usingstd::random_device, which maps to a secure entropy source in Emscripten.Wrapper/FilingWrapper.cpp—getpwuid()returnsNULLunder Emscripten, soGetFolderAppDataPathdereferenced a null pointer and produced an invalid path (which broke the working-folder extraction of GDTF/MVR archives). Use$HOMEwith a fallback to/home/web_user, the default MEMFS home directory.XmlFileHelper.{h,cpp}— wasm32 definessize_tasunsigned long, which is a distinct type fromUint32(unsigned int) even though both are 32-bit — unlike the 32-bit targets theIS64BITguard was written for. Compile theUint32overloads ofConvertIntegerunder Emscripten as well; otherwise calls withUint32arguments (e.g.GdtfMap::OnPrintToFile,GdtfConnector::OnReadFromNode) are ambiguous and fail to compile.All changes are guarded by
__EMSCRIPTEN__and do not affect any existing platform.Testing
Built with Emscripten 6.0.3 (
-fwasm-exceptions, embind bindings on top) and verified in Node.js and headless Chrome:GetParsingErrorCount(), identical results to the native Linux buildNote: for a fully working WASM build, the pending fixes #169 (root node name) and #170 (3DS/SVG buffer null check) are also needed, but they are independent of this PR.