Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python/py2wasm PoC #20009

Draft
wants to merge 2 commits into
base: cjp/wasm-poc
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/daml-lf/interpreter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ da_scala_library(

da_scala_test_suite(
name = "tests",
size = "small",
size = "large",
srcs = glob(
["src/test/**/*.scala"],
exclude = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import com.dylibso.chicory.runtime.{
Instance => WasmInstance,
Module => WasmModule,
}
import com.dylibso.chicory.wasm.types.{Value => WasmValue, ValueType => WasmValueType}
import com.google.protobuf.ByteString

import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.jdk.CollectionConverters._

/*
Notes:
Expand Down Expand Up @@ -390,22 +392,126 @@ final class WasmRunner(
finish
}

private val wasiClockTimeGet: WasmHostFunction = new WasmHostFunction(
(_: WasmInstance, _: Array[WasmValue]) => {
logger.error("wasi host function can not be called: clock_time_get")
Array(WasmValue.i32(0))
},
"wasi_snapshot_preview1",
"clock_time_get",
List(WasmValueType.I32, WasmValueType.I64, WasmValueType.I32).asJava,
WasmValueResultType.toList.asJava,
)

private val wasiEnvironSizesGet: WasmHostFunction = new WasmHostFunction(
(_: WasmInstance, _: Array[WasmValue]) => {
logger.error("wasi host function can not be called: environ_sizes_get")
Array(WasmValue.i32(0))
},
"wasi_snapshot_preview1",
"environ_sizes_get",
List(WasmValueType.I32, WasmValueType.I32).asJava,
WasmValueResultType.toList.asJava,
)

private val wasiArgsSizesGet: WasmHostFunction = new WasmHostFunction(
(_: WasmInstance, _: Array[WasmValue]) => {
logger.error("wasi host function can not be called: args_sizes_get")
Array(WasmValue.i32(0))
},
"wasi_snapshot_preview1",
"args_sizes_get",
List(WasmValueType.I32, WasmValueType.I32).asJava,
WasmValueResultType.toList.asJava,
)

private val wasiArgsGet: WasmHostFunction = new WasmHostFunction(
(_: WasmInstance, _: Array[WasmValue]) => {
logger.error("wasi host function can not be called: args_get")
Array(WasmValue.i32(0))
},
"wasi_snapshot_preview1",
"args_get",
List(WasmValueType.I32, WasmValueType.I32).asJava,
WasmValueResultType.toList.asJava,
)

private val wasiRandomGet: WasmHostFunction = new WasmHostFunction(
(_: WasmInstance, _: Array[WasmValue]) => {
logger.error("wasi host function can not be called: random_get")
Array(WasmValue.i32(0))
},
"wasi_snapshot_preview1",
"random_get",
List(WasmValueType.I32, WasmValueType.I32).asJava,
WasmValueResultType.toList.asJava,
)

private val wasiFdPrestatGet: WasmHostFunction = new WasmHostFunction(
(_: WasmInstance, _: Array[WasmValue]) => {
logger.error("wasi host function can not be called: fd_prestat_get")
Array(WasmValue.i32(0))
},
"wasi_snapshot_preview1",
"fd_prestat_get",
List(WasmValueType.I32, WasmValueType.I32).asJava,
WasmValueResultType.toList.asJava,
)

private val wasiFdPrestatDirName: WasmHostFunction = new WasmHostFunction(
(instance: WasmInstance, _: Array[WasmValue]) => {
logger.error("wasi host function can not be called: fd_prestat_dir_name")
copyByteString(ByteString.copyFromUtf8("/"))(instance)
},
"wasi_snapshot_preview1",
"fd_prestat_dir_name",
List(WasmValueType.I32, WasmValueType.I32, WasmValueType.I32).asJava,
WasmValueResultType.toList.asJava,
)

private val wasiProcExit: WasmHostFunction = new WasmHostFunction(
(_: WasmInstance, _: Array[WasmValue]) => {
logger.error("wasi host function can not be called: proc_exit")
Array(WasmValue.i32(0))
},
"wasi_snapshot_preview1",
"proc_exit",
List(WasmValueType.I32).asJava,
List.empty.asJava,
)

private val wasiHostFunctions: Array[WasmHostFunction] = Array(
wasiClockTimeGet,
wasiEnvironSizesGet,
wasiArgsSizesGet,
wasiArgsGet,
wasiRandomGet,
wasiFdPrestatGet,
wasiFdPrestatDirName,
wasiProcExit,
)

private def PureWasmInstance(): WasmInstance = {
val imports = new WasmHostImports(
Array[WasmHostFunction](
logFunc,
PureWasmHostFunctions.createContractFunc,
PureWasmHostFunctions.fetchContractArgFunc,
PureWasmHostFunctions.exerciseChoiceFunc,
)
) ++ wasiHostFunctions
)

WasmModule.builder(wasmExpr.module.toByteArray).withHostImports(imports).build().instantiate()
}

private def UpdateWasmInstance(): WasmInstance = {
val imports = new WasmHostImports(
Array[WasmHostFunction](logFunc, createContractFunc, fetchContractArgFunc, exerciseChoiceFunc)
Array[WasmHostFunction](
logFunc,
createContractFunc,
fetchContractArgFunc,
exerciseChoiceFunc,
) ++ wasiHostFunctions
)

WasmModule.builder(wasmExpr.module.toByteArray).withHostImports(imports).build().instantiate()
Expand Down
Binary file not shown.
Loading