+ support writing imports from a different module in the wasm binary object writer

This commit is contained in:
Nikolay Nikolov 2021-09-26 05:36:51 +03:00
parent 238d09d875
commit bd6bbb6cf4

View File

@ -39,6 +39,7 @@ interface
owbase; owbase;
type type
TWasmObjSymbolExtraData = class;
{ TWasmObjSymbol } { TWasmObjSymbol }
@ -47,6 +48,7 @@ interface
FuncIndex: Integer; FuncIndex: Integer;
SymbolIndex: Integer; SymbolIndex: Integer;
AliasOf: string; AliasOf: string;
ExtraData: TWasmObjSymbolExtraData;
constructor create(AList:TFPHashObjectList;const AName:string); constructor create(AList:TFPHashObjectList;const AName:string);
function ImportOrFuncIndex: Integer; function ImportOrFuncIndex: Integer;
function IsAlias: Boolean; function IsAlias: Boolean;
@ -282,6 +284,7 @@ implementation
FuncIndex:=-1; FuncIndex:=-1;
SymbolIndex:=-1; SymbolIndex:=-1;
AliasOf:=''; AliasOf:='';
ExtraData:=nil;
end; end;
function TWasmObjSymbol.ImportOrFuncIndex: Integer; function TWasmObjSymbol.ImportOrFuncIndex: Integer;
@ -1089,6 +1092,10 @@ implementation
begin begin
objsym.ImportIndex:=NextImportFunctionIndex; objsym.ImportIndex:=NextImportFunctionIndex;
Inc(NextImportFunctionIndex); Inc(NextImportFunctionIndex);
objsym.ExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
if objsym.ExtraData.ImportModule<>'' then
WriteName(FWasmSections[wsiImport],objsym.ExtraData.ImportModule)
else
WriteName(FWasmSections[wsiImport],'env'); WriteName(FWasmSections[wsiImport],'env');
WriteName(FWasmSections[wsiImport],objsym.Name); WriteName(FWasmSections[wsiImport],objsym.Name);
WriteByte(FWasmSections[wsiImport],$00); { func } WriteByte(FWasmSections[wsiImport],$00); { func }
@ -1124,8 +1131,17 @@ implementation
objsym.SymbolIndex:=FWasmSymbolTableEntriesCount; objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
Inc(FWasmSymbolTableEntriesCount); Inc(FWasmSymbolTableEntriesCount);
WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION)); WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
if objsym.ExtraData.ImportModule<>'' then
begin
WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED or WASM_SYM_EXPLICIT_NAME);
WriteUleb(FWasmSymbolTable,objsym.ImportIndex);
WriteName(FWasmSymbolTable,objsym.Name);
end
else
begin
WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED); WriteUleb(FWasmSymbolTable,WASM_SYM_UNDEFINED);
WriteUleb(FWasmSymbolTable,objsym.ImportIndex); WriteUleb(FWasmSymbolTable,objsym.ImportIndex);
end;
end end
else if objsym.typ=AT_FUNCTION then else if objsym.typ=AT_FUNCTION then
begin begin