-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* enable linking testsuite tests and tighten typing on linked imports
- Loading branch information
1 parent
a4275d4
commit addbd22
Showing
20 changed files
with
292 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
* text eol=lf | ||
*.bat text eol=crlf | ||
*.jar binary | ||
*.wasm binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/FunctionImportMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import io.github.charlietap.chasm.ast.module.Import | ||
import io.github.charlietap.chasm.ast.module.Module | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
|
||
internal typealias FunctionImportMatcher = (Store, Module, Import.Descriptor.Function, ExternalValue.Function) -> Result<Boolean, ModuleTrapError> |
27 changes: 27 additions & 0 deletions
27
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/FunctionImportMatcherImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import com.github.michaelbull.result.binding | ||
import io.github.charlietap.chasm.ast.module.Module | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.ext.function | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.executor.type.ext.definedType | ||
import io.github.charlietap.chasm.executor.type.ext.functionType | ||
import io.github.charlietap.chasm.ast.module.Import as ModuleImport | ||
|
||
internal fun FunctionImportMatcherImpl( | ||
store: Store, | ||
module: Module, | ||
descriptor: ModuleImport.Descriptor.Function, | ||
import: ExternalValue.Function, | ||
): Result<Boolean, ModuleTrapError> = binding { | ||
val actualFunction = store.function(import.address).bind() | ||
val actualFunctionType = actualFunction.functionType() | ||
|
||
val requiredImportType = module.types[descriptor.typeIndex.idx.toInt()] | ||
val requiredFunctionType = requiredImportType.recursiveType.definedType().functionType() | ||
|
||
requiredFunctionType == actualFunctionType | ||
} |
9 changes: 9 additions & 0 deletions
9
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/GlobalImportMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import io.github.charlietap.chasm.ast.module.Import | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
|
||
internal typealias GlobalImportMatcher = (Store, Import.Descriptor.Global, ExternalValue.Global) -> Result<Boolean, ModuleTrapError> |
22 changes: 22 additions & 0 deletions
22
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/GlobalImportMatcherImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import com.github.michaelbull.result.binding | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.ext.global | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.ast.module.Import as ModuleImport | ||
|
||
internal fun GlobalImportMatcherImpl( | ||
store: Store, | ||
descriptor: ModuleImport.Descriptor.Global, | ||
import: ExternalValue.Global, | ||
): Result<Boolean, ModuleTrapError> = binding { | ||
val actualGlobal = store.global(import.address).bind() | ||
val actualGlobalType = actualGlobal.type | ||
|
||
val requiredGlobalType = descriptor.type | ||
|
||
requiredGlobalType == actualGlobalType | ||
} |
10 changes: 10 additions & 0 deletions
10
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/ImportDescriptorMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import io.github.charlietap.chasm.ast.module.Module | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.ast.module.Import as ModuleImport | ||
|
||
internal typealias ImportDescriptorMatcher = (Store, Module, ModuleImport.Descriptor, ExternalValue) -> Result<Boolean, ModuleTrapError> |
53 changes: 53 additions & 0 deletions
53
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/ImportDescriptorMatcherImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import com.github.michaelbull.result.binding | ||
import io.github.charlietap.chasm.ast.module.Module | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.ast.module.Import as ModuleImport | ||
|
||
internal fun ImportDescriptorMatcherImpl( | ||
store: Store, | ||
module: Module, | ||
descriptor: ModuleImport.Descriptor, | ||
externalValue: ExternalValue, | ||
): Result<Boolean, ModuleTrapError> = | ||
ImportDescriptorMatcherImpl( | ||
store = store, | ||
module = module, | ||
descriptor = descriptor, | ||
externalValue = externalValue, | ||
functionImportMatcher = ::FunctionImportMatcherImpl, | ||
tableImportMatcher = ::TableImportMatcherImpl, | ||
memoryImportMatcher = ::MemoryImportMatcherImpl, | ||
globalImportMatcher = ::GlobalImportMatcherImpl, | ||
) | ||
|
||
internal fun ImportDescriptorMatcherImpl( | ||
store: Store, | ||
module: Module, | ||
descriptor: ModuleImport.Descriptor, | ||
externalValue: ExternalValue, | ||
functionImportMatcher: FunctionImportMatcher, | ||
tableImportMatcher: TableImportMatcher, | ||
memoryImportMatcher: MemoryImportMatcher, | ||
globalImportMatcher: GlobalImportMatcher, | ||
): Result<Boolean, ModuleTrapError> = binding { | ||
when { | ||
descriptor is ModuleImport.Descriptor.Function && externalValue is ExternalValue.Function -> { | ||
functionImportMatcher(store, module, descriptor, externalValue).bind() | ||
} | ||
descriptor is ModuleImport.Descriptor.Table && externalValue is ExternalValue.Table -> { | ||
tableImportMatcher(store, descriptor, externalValue).bind() | ||
} | ||
descriptor is ModuleImport.Descriptor.Memory && externalValue is ExternalValue.Memory -> { | ||
memoryImportMatcher(store, descriptor, externalValue).bind() | ||
} | ||
descriptor is ModuleImport.Descriptor.Global && externalValue is ExternalValue.Global -> { | ||
globalImportMatcher(store, descriptor, externalValue).bind() | ||
} | ||
else -> false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 21 additions & 18 deletions
39
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/ImportMatcherImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Err | ||
import com.github.michaelbull.result.Ok | ||
import com.github.michaelbull.result.Result | ||
import com.github.michaelbull.result.binding | ||
import io.github.charlietap.chasm.ast.module.Module | ||
import io.github.charlietap.chasm.executor.runtime.error.InstantiationError | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.ast.module.Import as ModuleImport | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
|
||
internal fun ImportMatcherImpl( | ||
store: Store, | ||
module: Module, | ||
imports: List<Import>, | ||
): Result<List<ExternalValue>, InstantiationError> { | ||
return module.imports.map { moduleImport -> | ||
): Result<List<ExternalValue>, ModuleTrapError> = | ||
ImportMatcherImpl( | ||
store = store, | ||
module = module, | ||
imports = imports, | ||
descriptorMatcher = ::ImportDescriptorMatcherImpl, | ||
) | ||
|
||
internal fun ImportMatcherImpl( | ||
store: Store, | ||
module: Module, | ||
imports: List<Import>, | ||
descriptorMatcher: ImportDescriptorMatcher, | ||
): Result<List<ExternalValue>, ModuleTrapError> = binding { | ||
module.imports.map { moduleImport -> | ||
imports.firstOrNull { import -> | ||
moduleImport.moduleName.name == import.moduleName && | ||
moduleImport.entityName.name == import.entityName && | ||
descriptorMatcher(moduleImport.descriptor, import.value) | ||
}?.value ?: return Err(InstantiationError.MissingImport) | ||
}.let(::Ok) | ||
} | ||
|
||
private fun descriptorMatcher( | ||
descriptor: ModuleImport.Descriptor, | ||
externalValue: ExternalValue, | ||
): Boolean { | ||
return when (descriptor) { | ||
is ModuleImport.Descriptor.Function -> externalValue is ExternalValue.Function | ||
is ModuleImport.Descriptor.Table -> externalValue is ExternalValue.Table | ||
is ModuleImport.Descriptor.Memory -> externalValue is ExternalValue.Memory | ||
is ModuleImport.Descriptor.Global -> externalValue is ExternalValue.Global | ||
descriptorMatcher(store, module, moduleImport.descriptor, import.value).bind() | ||
}?.value ?: Err(InstantiationError.MissingImport).bind() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/LimitsMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import io.github.charlietap.chasm.ast.type.Limits | ||
|
||
internal typealias LimitsMatcher = (Limits, Limits) -> Boolean |
18 changes: 18 additions & 0 deletions
18
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/LimitsMatcherImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import io.github.charlietap.chasm.ast.type.Limits | ||
|
||
internal fun LimitsMatcherImpl( | ||
first: Limits, | ||
second: Limits, | ||
): Boolean { | ||
return if (first.min >= second.min) { | ||
if (second.max == null) { | ||
true | ||
} else { | ||
first.max != null && first.max!! <= second.max!! | ||
} | ||
} else { | ||
false | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/MemoryImportMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import io.github.charlietap.chasm.ast.module.Import | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
|
||
internal typealias MemoryImportMatcher = (Store, Import.Descriptor.Memory, ExternalValue.Memory) -> Result<Boolean, ModuleTrapError> |
35 changes: 35 additions & 0 deletions
35
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/MemoryImportMatcherImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import com.github.michaelbull.result.binding | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.ext.memory | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.ast.module.Import as ModuleImport | ||
|
||
internal fun MemoryImportMatcherImpl( | ||
store: Store, | ||
descriptor: ModuleImport.Descriptor.Memory, | ||
import: ExternalValue.Memory, | ||
): Result<Boolean, ModuleTrapError> = | ||
MemoryImportMatcherImpl( | ||
store = store, | ||
descriptor = descriptor, | ||
import = import, | ||
limitsMatcher = ::LimitsMatcherImpl, | ||
) | ||
|
||
internal fun MemoryImportMatcherImpl( | ||
store: Store, | ||
descriptor: ModuleImport.Descriptor.Memory, | ||
import: ExternalValue.Memory, | ||
limitsMatcher: LimitsMatcher, | ||
): Result<Boolean, ModuleTrapError> = binding { | ||
val actualMemory = store.memory(import.address).bind() | ||
val actualMemoryType = actualMemory.type | ||
|
||
val requiredMemoryType = descriptor.type | ||
|
||
limitsMatcher(actualMemoryType.limits, requiredMemoryType.limits) | ||
} |
9 changes: 9 additions & 0 deletions
9
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/TableImportMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import io.github.charlietap.chasm.ast.module.Import | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
|
||
internal typealias TableImportMatcher = (Store, Import.Descriptor.Table, ExternalValue.Table) -> Result<Boolean, ModuleTrapError> |
36 changes: 36 additions & 0 deletions
36
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/import/TableImportMatcherImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.github.charlietap.chasm.import | ||
|
||
import com.github.michaelbull.result.Result | ||
import com.github.michaelbull.result.binding | ||
import io.github.charlietap.chasm.executor.runtime.error.ModuleTrapError | ||
import io.github.charlietap.chasm.executor.runtime.ext.table | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.ast.module.Import as ModuleImport | ||
|
||
internal fun TableImportMatcherImpl( | ||
store: Store, | ||
descriptor: ModuleImport.Descriptor.Table, | ||
import: ExternalValue.Table, | ||
): Result<Boolean, ModuleTrapError> = | ||
TableImportMatcherImpl( | ||
store = store, | ||
descriptor = descriptor, | ||
import = import, | ||
limitsMatcher = ::LimitsMatcherImpl, | ||
) | ||
|
||
internal fun TableImportMatcherImpl( | ||
store: Store, | ||
descriptor: ModuleImport.Descriptor.Table, | ||
import: ExternalValue.Table, | ||
limitsMatcher: LimitsMatcher, | ||
): Result<Boolean, ModuleTrapError> = binding { | ||
val actualTable = store.table(import.address).bind() | ||
val actualTableType = actualTable.type | ||
|
||
val requiredTableType = descriptor.type | ||
|
||
requiredTableType.referenceType == actualTableType.referenceType && | ||
limitsMatcher(actualTableType.limits, requiredTableType.limits) | ||
} |
22 changes: 18 additions & 4 deletions
22
...monTest/kotlin/io/github/charlietap/chasm/script/command/AssertUnlinkableCommandRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
package io.github.charlietap.chasm.script.command | ||
|
||
import io.github.charlietap.chasm.embedding.instance | ||
import io.github.charlietap.chasm.embedding.module | ||
import io.github.charlietap.chasm.flatMap | ||
import io.github.charlietap.chasm.fold | ||
import io.github.charlietap.chasm.script.ScriptContext | ||
import io.github.charlietap.chasm.script.ext.readBytesFromPath | ||
import io.github.charlietap.sweet.lib.command.AssertUnlinkableCommand | ||
|
||
typealias AssertUnlinkableCommandRunner = (AssertUnlinkableCommand) -> CommandResult | ||
typealias AssertUnlinkableCommandRunner = (ScriptContext, AssertUnlinkableCommand) -> CommandResult | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
fun AssertUnlinkableCommandRunner( | ||
context: ScriptContext, | ||
command: AssertUnlinkableCommand, | ||
): CommandResult { | ||
println("ignoring AssertUnlinkableCommand") | ||
return CommandResult.Success | ||
val moduleFilePath = context.binaryDirectory + "/" + command.filename | ||
val bytes = moduleFilePath.readBytesFromPath() | ||
|
||
return module(bytes).flatMap { module -> | ||
instance(context.store, module, context.imports) | ||
}.fold({ _ -> | ||
CommandResult.Failure(command, "unlinkable module was instantiated when it should have failed") | ||
}) { | ||
CommandResult.Success | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters