package org.shedlang.compiler.backends.wasm; import org.shedlang.compiler.backends.targets.WasmTarget; import org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormat; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmCall; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmInstruction; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFunction; import org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatTesting; import org.shedlang.compiler.patio.PatioCompilationUnit; import org.shedlang.compiler.patio.PatioExampleTests; import org.shedlang.compiler.runtime.ShedRuntime; import org.shedlang.compiler.testing.ExecutionResult; import org.shedlang.compiler.testing.TestProcessRunner; import org.shedlang.compiler.util.Lists; import org.shedlang.compiler.util.P; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; public abstract class PatioToWasmTests extends PatioExampleTests { @Override protected ExecutionResult executeCompilationUnit(PatioCompilationUnit patio) throws IOException, InterruptedException { var wasmModule = PatioToWasm.generateCompilationUnit(patio, wasmTarget()); wasmModule = WasmBackend.generateNativeRuntime(wasmModule); var body = P.vector(new WasmCall(new WasmFuncName(FUNCTION_NAME))); wasmModule = wasmModule.addFunction(new WasmFunction( "_start", List.of(), List.of(), List.of(), new WasmExpression(body) )); Files.createDirectories(Path.of("tests-tmp")); var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "PatioToWasmTests"); var objectPath = workingDirectory.resolve("main.o"); try (var outputStream = new FileOutputStream(objectPath.toFile())) { WasmBinaryFormat.serializeModule(wasmModule, outputStream); } var executablePath = workingDirectory.resolve("main.wasm"); var runtime = ShedRuntime.defaultRuntime(wasmTarget().targetName()); WasmBackend.forTarget(wasmTarget()).link( List.of( objectPath, runtime.dir().resolve("libpatio_test.a") ), executablePath, ShedRuntime.defaultRuntime(wasmTarget().targetName()) ); WasmBinaryFormatTesting.validate(executablePath, wasmTarget()); return TestProcessRunner.runCaptureOutput(Lists.concat( WasmTesting.patioTestBinaryRunner(wasmTarget()), List.of( executablePath.toString() ) )); } protected abstract WasmTarget wasmTarget(); }