Skip to content

Commit

Permalink
lift module information out of result and expose values on module emb…
Browse files Browse the repository at this point in the history
…edding
  • Loading branch information
CharlieTap committed Nov 8, 2024
1 parent fe78117 commit 6acc197
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,7 +13,7 @@ import io.github.charlietap.chasm.embedding.transform.Mapper

fun moduleInfo(
module: Module,
): ChasmResult<ModuleInfo, ChasmError.ExecutionError> = moduleInfo(
): ModuleInfo = moduleInfo(
module = module,
importDescriptorMapper = ImportDescriptorMapper.instance,
exportDescriptorMapper = ExportDescriptorMapper(module.module),
Expand All @@ -25,7 +23,7 @@ internal fun moduleInfo(
module: Module,
importDescriptorMapper: Mapper<Import.Descriptor, ExternalType>,
exportDescriptorMapper: Mapper<Export.Descriptor, ExternalType>,
): ChasmResult<ModuleInfo, ChasmError.ExecutionError> {
): ModuleInfo {

val internalModule = module.module

Expand All @@ -39,5 +37,5 @@ internal fun moduleInfo(
ExportDefinition(export.name.name, type)
}

return ChasmResult.Success(ModuleInfo(imports, exports))
return ModuleInfo(imports, exports)
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -109,7 +109,7 @@ class ModuleInfoTest {
),
)

assertEquals(ChasmResult.Success(expected), actual)
assertEquals(expected, actual)
}

companion object {
Expand Down

0 comments on commit 6acc197

Please sign in to comment.