diff --git a/src/main/java/wasm/disassembly/modules/indices/TypeIdx.java b/src/main/java/wasm/disassembly/modules/indices/TypeIdx.java index f7c8b89..4b0856e 100644 --- a/src/main/java/wasm/disassembly/modules/indices/TypeIdx.java +++ b/src/main/java/wasm/disassembly/modules/indices/TypeIdx.java @@ -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); + } } diff --git a/src/main/java/wasm/misc/Function.java b/src/main/java/wasm/misc/Function.java index 2a9db64..d1245d0 100644 --- a/src/main/java/wasm/misc/Function.java +++ b/src/main/java/wasm/misc/Function.java @@ -17,41 +17,26 @@ import java.util.List; public class Function { private FuncType funcType = null; - private List locals = null; + private List locals = null; private Expression code = null; - public Function(FuncType funcType, List locals, Expression code) { + public Function(FuncType funcType, List 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 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 getLocals() { + public List getLocals() { return locals; } - public void setLocals(List locals) { + public List getLocalsFloored() { + List 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) { this.locals = locals; }