[Lazarus] FPC and WebAssembly

Kostas Michalopoulos badsectoracula at gmail.com
Sat Dec 25 17:01:03 CET 2021


Hello,

> I'm glad to anncounce that FPC now covers working with webassembly in the
> browser.

Sounds great. I did a quick compile for the wasi target and seemed to 
work fine with a simple helloworld-style test. So i decided to try and 
port a game i made last year[0] - initially made for an MS-DOS game jam, 
but added a bunch of ports later and wanted to make a web-based one too.

I decided to go with a similar approach to the one i used for Android, 
with the game compiled into a library (or a wasm file for here) and a 
"stub" that loads the library and passes events, etc to it.

However no matter what i try, it always seems to fail when (i guess) 
tries to link or assemble the first unit (the unit doesn't do anything, 
just defines an entity class).

By default it gets this error which happens after it compiles everything 
(SndEmit.o is the object file for the unit, it is just the first unit it 
tries to link/compile):

wasm-ld: error: ./Engine/SndEmit.o: invalid relocation function index
Error: Error while linking

If i pass -Awabt to use the wasa assembler (thinking that there might be 
something wrong with the object files), it then has the error:

SndEmit.pas(171) Fatal: Internal error 2019093001

(The unit is 171 lines long)

Also there are a bunch of "steam read error" errors, often after 
"assembling someunitname" (though not all "assembling someunitname" have 
such an error after them).

If i pass -Allvm-mc to use LLVM's assembler, i get a bunch of errors 
from a different unit (Misc):

Compiling ./Engine/Misc.pas
Assembling misc
./Engine/Misc.wat:5082:3: error: symbol 
MISC$_$TSTRINGKEYVALUESET_$__$$_INDEXOFKEY$ANSISTRING$$LONGINT missing 
.functype
                 call 
MISC$_$TSTRINGKEYVALUESET_$__$$_INDEXOFKEY$ANSISTRING$$LONGINT
                 ^
./Engine/Misc.wat:5412:3: error: symbol 
MISC$_$TSTRINGKEYVALUESET_$__$$_INDEXOFKEY$ANSISTRING$$LONGINT missing 
.functype
                 call 
MISC$_$TSTRINGKEYVALUESET_$__$$_INDEXOFKEY$ANSISTRING$$LONGINT
                 ^
./Engine/Misc.wat:5606:3: error: symbol 
MISC$_$TSTRINGKEYVALUESET_$__$$_INDEXOFKEY$ANSISTRING$$LONGINT missing 
.functype
                 call 
MISC$_$TSTRINGKEYVALUESET_$__$$_INDEXOFKEY$ANSISTRING$$LONGINT
                 ^
./Engine/Misc.wat:6023:3: error: 3 superfluous return values
                 end_function
                 ^
./Engine/Misc.wat:6272:3: error: 1 superfluous return values
                 end_function
                 ^
./Engine/Misc.wat:6496:3: error: 1 superfluous return values
                 end_function
                 ^
./Engine/Misc.wat:6681:3: error: 1 superfluous return values
                 end_function
                 ^
Misc.pas(150) Error: Error while assembling exitcode 1

Note that i have LLVM 13 installed but also tried removing it and 
installing LLVM 11 and didn't make any difference. Also FWIW i'm calling 
the ppcrosswasm32 executable directly and have it installed in 
~/Apps/fpc, though it shouldn't make a difference (and the simple hello 
world code compiled anyway so the compiler setup should be fine).

Is there anything i am missing/not setting up correctly (e.g. i didn't 
tried with the "embedded" target - i tried to build FPC with it first 
but got some errors so i didn't explore further and went with "wasi" 
instead that seemed to work)? AFAIK i do not use any of the 
functionality that isn't implemented in the Roadmap page in the wiki.

Kostas

[0] https://bad-sector.itch.io/post-apocalyptic-petra


More information about the lazarus mailing list