From 6acc19765b8aebb9d9000a63373248490ee2ac46 Mon Sep 17 00:00:00 2001 From: CharlieTap Date: Fri, 8 Nov 2024 19:17:40 +0000 Subject: [PATCH] lift module information out of result and expose values on module embedding --- .../charlietap/chasm/embedding/ModuleInfo.kt | 8 +++----- .../charlietap/chasm/embedding/shapes/Module.kt | 17 ++++++++++++++++- .../chasm/integration/ModuleInfoTest.kt | 10 +++++----- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/ModuleInfo.kt b/chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/ModuleInfo.kt index 4954c5f5..8b8a48cc 100644 --- a/chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/ModuleInfo.kt +++ b/chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/ModuleInfo.kt @@ -2,8 +2,6 @@ package io.github.charlietap.chasm.embedding import io.github.charlietap.chasm.ast.module.Export import io.github.charlietap.chasm.ast.module.Import -import io.github.charlietap.chasm.embedding.error.ChasmError -import io.github.charlietap.chasm.embedding.shapes.ChasmResult import io.github.charlietap.chasm.embedding.shapes.ExportDefinition import io.github.charlietap.chasm.embedding.shapes.ExternalType import io.github.charlietap.chasm.embedding.shapes.ImportDefinition @@ -15,7 +13,7 @@ import io.github.charlietap.chasm.embedding.transform.Mapper fun moduleInfo( module: Module, -): ChasmResult = moduleInfo( +): ModuleInfo = moduleInfo( module = module, importDescriptorMapper = ImportDescriptorMapper.instance, exportDescriptorMapper = ExportDescriptorMapper(module.module), @@ -25,7 +23,7 @@ internal fun moduleInfo( module: Module, importDescriptorMapper: Mapper, exportDescriptorMapper: Mapper, -): ChasmResult { +): ModuleInfo { val internalModule = module.module @@ -39,5 +37,5 @@ internal fun moduleInfo( ExportDefinition(export.name.name, type) } - return ChasmResult.Success(ModuleInfo(imports, exports)) + return ModuleInfo(imports, exports) } diff --git a/chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/shapes/Module.kt b/chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/shapes/Module.kt index b59badd9..907787c4 100644 --- a/chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/shapes/Module.kt +++ b/chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/shapes/Module.kt @@ -1,5 +1,20 @@ package io.github.charlietap.chasm.embedding.shapes +import io.github.charlietap.chasm.embedding.moduleInfo import io.github.charlietap.chasm.ast.module.Module as InternalModule -class Module internal constructor(internal val module: InternalModule) +class Module internal constructor( + internal val module: InternalModule, +) { + private val info by lazy { + moduleInfo(this) + } + + val imports by lazy { + info.imports + } + + val exports by lazy { + info.exports + } +} diff --git a/chasm/src/commonTest/kotlin/io/github/charlietap/chasm/integration/ModuleInfoTest.kt b/chasm/src/commonTest/kotlin/io/github/charlietap/chasm/integration/ModuleInfoTest.kt index 0f707b11..113485dd 100644 --- a/chasm/src/commonTest/kotlin/io/github/charlietap/chasm/integration/ModuleInfoTest.kt +++ b/chasm/src/commonTest/kotlin/io/github/charlietap/chasm/integration/ModuleInfoTest.kt @@ -4,7 +4,6 @@ import com.goncalossilva.resources.Resource import io.github.charlietap.chasm.decoder.FakeSourceReader import io.github.charlietap.chasm.embedding.module import io.github.charlietap.chasm.embedding.moduleInfo -import io.github.charlietap.chasm.embedding.shapes.ChasmResult import io.github.charlietap.chasm.embedding.shapes.ExportDefinition import io.github.charlietap.chasm.embedding.shapes.ExternalType import io.github.charlietap.chasm.embedding.shapes.FunctionType @@ -18,7 +17,8 @@ import io.github.charlietap.chasm.embedding.shapes.Mutability import io.github.charlietap.chasm.embedding.shapes.TableType import io.github.charlietap.chasm.embedding.shapes.TagType import io.github.charlietap.chasm.embedding.shapes.ValueType -import io.github.charlietap.chasm.embedding.shapes.flatMap +import io.github.charlietap.chasm.embedding.shapes.getOrNull +import io.github.charlietap.chasm.embedding.shapes.map import kotlin.test.Test import kotlin.test.assertEquals @@ -53,9 +53,9 @@ class ModuleInfoTest { val byteStream = Resource(FILE_DIR + "module_info.wasm").readBytes() val reader = FakeSourceReader(byteStream) - val actual = module(reader).flatMap { module -> + val actual = module(reader).map { module -> moduleInfo(module) - } + }.getOrNull() val expected = ModuleInfo( imports = listOf( @@ -109,7 +109,7 @@ class ModuleInfoTest { ), ) - assertEquals(ChasmResult.Success(expected), actual) + assertEquals(expected, actual) } companion object {