This commit is contained in:
sirjonasxx 2020-12-23 14:52:20 +01:00
parent fa41bb85e1
commit 9264d9292d
2 changed files with 26 additions and 25 deletions

View File

@ -33,4 +33,9 @@ public class TypeIdx extends WASMOpCode {
public void setX(long x) {
this.x = x;
}
@Override
public boolean equals(Object obj) {
return (obj instanceof TypeIdx && ((TypeIdx)obj).getX() == x);
}
}

View File

@ -17,41 +17,26 @@ import java.util.List;
public class Function {
private FuncType funcType = null;
private List<ValType> locals = null;
private List<Locals> locals = null;
private Expression code = null;
public Function(FuncType funcType, List<ValType> locals, Expression code) {
public Function(FuncType funcType, List<Locals> locals, Expression code) {
this.funcType = funcType;
this.locals = locals;
this.code = code;
}
public Function(Module module, int funcId) {
if (funcId < 0) return;
TypeIdx typeIdx = module.getFunctionSection().getTypeIdxVector().get(funcId);
funcType = module.getTypeSection().getByTypeIdx(typeIdx);
Func code = module.getCodeSection().getCodesEntries().get(funcId).getCode();
this.code = code.getExpression();
locals = new ArrayList<>();
List<Locals> localss = code.getLocalss();
for (Locals loc : localss) {
for (int i = 0; i < loc.getAmount(); i++) {
locals.add(loc.getValType());
}
}
}
public Function(Module module, FuncIdx funcIdx) {
this(module, (int)(funcIdx.getX()) - module.getImportSection().getTotalFuncImports());
funcType = module.getTypeSection().getByFuncIdx(funcIdx);
Func code = module.getCodeSection().getByIdx(funcIdx);
this.code = code.getExpression();
locals = code.getLocalss();
}
public FuncIdx addToModule(Module module) {
TypeIdx typeIdx = module.getTypeSection().getTypeIdxForFuncType(funcType);
ValType[] valTypeArr = locals.toArray(new ValType[0]);
Func func = new Func(valTypeArr, code);
Func func = new Func(locals, code);
module.getFunctionSection().getTypeIdxVector().add(typeIdx);
module.getCodeSection().getCodesEntries().add(new Code(func));
@ -70,11 +55,22 @@ public class Function {
this.funcType = funcType;
}
public List<ValType> getLocals() {
public List<Locals> getLocals() {
return locals;
}
public void setLocals(List<ValType> locals) {
public List<ValType> getLocalsFloored() {
List<ValType> result = new ArrayList<>();
for (Locals loc : locals) {
for (int i = 0; i < loc.getAmount(); i++) {
result.add(loc.getValType());
}
}
return result;
}
public void setLocals(List<Locals> locals) {
this.locals = locals;
}