-
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.
add host function exception and handler that allows host function to …
…stop the vm gracefully
- Loading branch information
1 parent
104302b
commit ae25d17
Showing
10 changed files
with
101 additions
and
1 deletion.
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
59 changes: 59 additions & 0 deletions
59
...src/commonTest/kotlin/io/github/charlietap/chasm/integration/HostFunctionExceptionTest.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,59 @@ | ||
package io.github.charlietap.chasm.integration | ||
|
||
import io.github.charlietap.chasm.embedding.error.ChasmError | ||
import io.github.charlietap.chasm.embedding.fixture.publicFunctionType | ||
import io.github.charlietap.chasm.embedding.fixture.publicImport | ||
import io.github.charlietap.chasm.embedding.fixture.publicStore | ||
import io.github.charlietap.chasm.embedding.function | ||
import io.github.charlietap.chasm.embedding.shapes.ChasmResult | ||
import io.github.charlietap.chasm.embedding.shapes.HostFunction | ||
import io.github.charlietap.chasm.embedding.shapes.ValueType | ||
import io.github.charlietap.chasm.executor.runtime.error.InvocationError | ||
import io.github.charlietap.chasm.fixture.executor.runtime.store | ||
import io.github.charlietap.chasm.host.HostFunctionException | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class HostFunctionExceptionTest { | ||
|
||
@Test | ||
fun `can run a host function that throws an exception and return a chasm error`() { | ||
|
||
val store = publicStore(store()) | ||
|
||
val functionType = publicFunctionType( | ||
emptyList(), | ||
listOf(ValueType.Number.I32), | ||
) | ||
val reason = "Fail gracefully" | ||
val exception = HostFunctionException(reason) | ||
val hostFunction: HostFunction = { | ||
throw exception | ||
} | ||
val functionExternal = function(store, functionType, hostFunction) | ||
val functionImport = publicImport( | ||
"env", | ||
"host_function", | ||
functionExternal, | ||
) | ||
val result = testRunner( | ||
fileName = "host_function.wasm", | ||
fileDirectory = FILE_DIR, | ||
functionName = "call_host_function", | ||
store = store, | ||
imports = listOf( | ||
functionImport, | ||
), | ||
) | ||
|
||
val expected = ChasmError.ExecutionError( | ||
InvocationError.HostFunctionError(reason).toString(), | ||
) | ||
|
||
assertEquals(ChasmResult.Error(expected), result) | ||
} | ||
|
||
companion object { | ||
private const val FILE_DIR = "src/commonTest/resources/integration/" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(module | ||
(import "env" "host_function" (func $host_function (result i32))) | ||
(func $call_host_function (result i32) call $host_function) | ||
(export "call_host_function" (func $call_host_function)) | ||
) |
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
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
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
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,21 @@ | ||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
alias(libs.plugins.kotlin.multiplatform) | ||
|
||
alias(libs.plugins.conventions.kmp) | ||
alias(libs.plugins.conventions.linting) | ||
alias(libs.plugins.conventions.publishing) | ||
} | ||
|
||
configure<PublishingConventionsExtension> { | ||
name = "host" | ||
description = "host system interface" | ||
} | ||
|
||
tasks.withType<KotlinCompile>().configureEach { | ||
compilerOptions { | ||
jvmTarget = JvmTarget.fromTarget(libs.versions.java.bytecode.version.get()) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
host/src/commonMain/kotlin/io/github/charlietap/chasm/host/HostFunctionException.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,3 @@ | ||
package io.github.charlietap.chasm.host | ||
|
||
class HostFunctionException(val reason: String) : Exception() |
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