-
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.
public api and host functions dsl (#25)
- Loading branch information
1 parent
690bd95
commit adad58c
Showing
147 changed files
with
2,220 additions
and
697 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
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
25 changes: 25 additions & 0 deletions
25
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/Exports.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,25 @@ | ||
package io.github.charlietap.chasm.embedding | ||
|
||
import io.github.charlietap.chasm.embedding.shapes.Export | ||
import io.github.charlietap.chasm.embedding.shapes.Importable | ||
import io.github.charlietap.chasm.embedding.shapes.Instance | ||
import io.github.charlietap.chasm.embedding.transform.BidirectionalMapper | ||
import io.github.charlietap.chasm.embedding.transform.ImportableMapper | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
|
||
fun exports( | ||
instance: Instance, | ||
): List<Export> = exports( | ||
instance = instance, | ||
importableMapper = ImportableMapper, | ||
) | ||
|
||
internal fun exports( | ||
instance: Instance, | ||
importableMapper: BidirectionalMapper<Importable, ExternalValue>, | ||
): List<Export> = instance.instance.exports.map { exportInstance -> | ||
Export( | ||
exportInstance.name.name, | ||
importableMapper.bimap(exportInstance.value), | ||
) | ||
} |
24 changes: 19 additions & 5 deletions
24
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/Function.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,26 +1,40 @@ | ||
package io.github.charlietap.chasm.embedding | ||
|
||
import io.github.charlietap.chasm.ast.type.FunctionType | ||
import io.github.charlietap.chasm.embedding.shapes.Function | ||
import io.github.charlietap.chasm.embedding.shapes.FunctionType | ||
import io.github.charlietap.chasm.embedding.shapes.HostFunction | ||
import io.github.charlietap.chasm.embedding.shapes.Store | ||
import io.github.charlietap.chasm.embedding.transform.FunctionTypeMapper | ||
import io.github.charlietap.chasm.embedding.transform.HostFunctionMapper | ||
import io.github.charlietap.chasm.embedding.transform.Mapper | ||
import io.github.charlietap.chasm.executor.instantiator.allocation.function.HostFunctionAllocator | ||
import io.github.charlietap.chasm.executor.instantiator.allocation.function.HostFunctionAllocatorImpl | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.instance.HostFunction | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.ast.type.FunctionType as InternalFunctionType | ||
import io.github.charlietap.chasm.executor.runtime.instance.HostFunction as InternalHostFunction | ||
|
||
fun function( | ||
store: Store, | ||
type: FunctionType, | ||
function: HostFunction, | ||
): ExternalValue.Function = function( | ||
): Function = function( | ||
store = store, | ||
type = type, | ||
function = function, | ||
allocator = ::HostFunctionAllocatorImpl, | ||
functionTypeMapper = FunctionTypeMapper.instance, | ||
hostFunctionMapper = HostFunctionMapper.instance, | ||
) | ||
|
||
internal fun function( | ||
store: Store, | ||
type: FunctionType, | ||
function: HostFunction, | ||
allocator: HostFunctionAllocator, | ||
): ExternalValue.Function = ExternalValue.Function(allocator(store, type, function)) | ||
functionTypeMapper: Mapper<FunctionType, InternalFunctionType>, | ||
hostFunctionMapper: Mapper<HostFunction, InternalHostFunction>, | ||
): Function { | ||
val functionType = functionTypeMapper.map(type) | ||
val hostFunction = hostFunctionMapper.map(function) | ||
return Function(ExternalValue.Function(allocator(store.store, functionType, hostFunction))) | ||
} |
26 changes: 20 additions & 6 deletions
26
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/Global.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,26 +1,40 @@ | ||
package io.github.charlietap.chasm.embedding | ||
|
||
import io.github.charlietap.chasm.ast.type.GlobalType | ||
import io.github.charlietap.chasm.embedding.shapes.Global | ||
import io.github.charlietap.chasm.embedding.shapes.GlobalType | ||
import io.github.charlietap.chasm.embedding.shapes.Store | ||
import io.github.charlietap.chasm.embedding.shapes.Value | ||
import io.github.charlietap.chasm.embedding.transform.GlobalTypeMapper | ||
import io.github.charlietap.chasm.embedding.transform.Mapper | ||
import io.github.charlietap.chasm.embedding.transform.ValueMapper | ||
import io.github.charlietap.chasm.executor.instantiator.allocation.global.GlobalAllocator | ||
import io.github.charlietap.chasm.executor.instantiator.allocation.global.GlobalAllocatorImpl | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.executor.runtime.value.ExecutionValue | ||
import io.github.charlietap.chasm.ast.type.GlobalType as InternalGlobalType | ||
|
||
fun global( | ||
store: Store, | ||
type: GlobalType, | ||
value: ExecutionValue, | ||
): ExternalValue.Global = global( | ||
value: Value, | ||
): Global = global( | ||
store = store, | ||
type = type, | ||
value = value, | ||
allocator = ::GlobalAllocatorImpl, | ||
globalTypeMapper = GlobalTypeMapper.instance, | ||
valueMapper = ValueMapper.instance, | ||
) | ||
|
||
internal fun global( | ||
store: Store, | ||
type: GlobalType, | ||
value: ExecutionValue, | ||
value: Value, | ||
allocator: GlobalAllocator, | ||
): ExternalValue.Global = ExternalValue.Global(allocator(store, type, value)) | ||
globalTypeMapper: Mapper<GlobalType, InternalGlobalType>, | ||
valueMapper: Mapper<Value, ExecutionValue>, | ||
): Global { | ||
val globalType = globalTypeMapper.map(type) | ||
val executionValue = valueMapper.map(value) | ||
return Global(ExternalValue.Global(allocator(store.store, globalType, executionValue))) | ||
} |
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
37 changes: 22 additions & 15 deletions
37
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/Invoke.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,49 +1,56 @@ | ||
package io.github.charlietap.chasm.embedding | ||
|
||
import com.github.michaelbull.result.fold | ||
import com.github.michaelbull.result.map | ||
import com.github.michaelbull.result.mapError | ||
import io.github.charlietap.chasm.ChasmResult | ||
import io.github.charlietap.chasm.ChasmResult.Error | ||
import io.github.charlietap.chasm.ChasmResult.Success | ||
import io.github.charlietap.chasm.error.ChasmError | ||
import io.github.charlietap.chasm.embedding.error.ChasmError | ||
import io.github.charlietap.chasm.embedding.shapes.ChasmResult | ||
import io.github.charlietap.chasm.embedding.shapes.ChasmResult.Error | ||
import io.github.charlietap.chasm.embedding.shapes.ChasmResult.Success | ||
import io.github.charlietap.chasm.embedding.shapes.Instance | ||
import io.github.charlietap.chasm.embedding.shapes.Store | ||
import io.github.charlietap.chasm.embedding.shapes.Value | ||
import io.github.charlietap.chasm.embedding.transform.BidirectionalMapper | ||
import io.github.charlietap.chasm.embedding.transform.ValueMapper | ||
import io.github.charlietap.chasm.executor.invoker.FunctionInvoker | ||
import io.github.charlietap.chasm.executor.invoker.FunctionInvokerImpl | ||
import io.github.charlietap.chasm.executor.runtime.error.InvocationError | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.instance.ModuleInstance | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.executor.runtime.value.ExecutionValue | ||
|
||
fun invoke( | ||
store: Store, | ||
instance: ModuleInstance, | ||
instance: Instance, | ||
name: String, | ||
args: List<ExecutionValue> = emptyList(), | ||
): ChasmResult<List<ExecutionValue>, ChasmError.ExecutionError> = invoke( | ||
args: List<Value> = emptyList(), | ||
): ChasmResult<List<Value>, ChasmError.ExecutionError> = invoke( | ||
store = store, | ||
instance = instance, | ||
name = name, | ||
args = args, | ||
invoker = ::FunctionInvokerImpl, | ||
valueMapper = ValueMapper.instance, | ||
) | ||
|
||
internal fun invoke( | ||
store: Store, | ||
instance: ModuleInstance, | ||
instance: Instance, | ||
name: String, | ||
args: List<ExecutionValue>, | ||
args: List<Value>, | ||
invoker: FunctionInvoker, | ||
): ChasmResult<List<ExecutionValue>, ChasmError.ExecutionError> { | ||
valueMapper: BidirectionalMapper<Value, ExecutionValue>, | ||
): ChasmResult<List<Value>, ChasmError.ExecutionError> { | ||
|
||
val extern = instance.exports.firstOrNull { export -> | ||
val extern = instance.instance.exports.firstOrNull { export -> | ||
export.name.name == name | ||
}?.value | ||
|
||
val address = (extern as? ExternalValue.Function)?.address ?: return Error( | ||
ChasmError.ExecutionError(InvocationError.FunctionNotFound(name)), | ||
) | ||
val arguments = args.map(valueMapper::map) | ||
|
||
return invoker(store, address, args) | ||
return invoker(store.store, address, arguments) | ||
.map { values -> values.map(valueMapper::bimap) } | ||
.mapError(ChasmError::ExecutionError) | ||
.fold(::Success, ::Error) | ||
} |
17 changes: 13 additions & 4 deletions
17
chasm/src/commonMain/kotlin/io/github/charlietap/chasm/embedding/Memory.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,22 +1,31 @@ | ||
package io.github.charlietap.chasm.embedding | ||
|
||
import io.github.charlietap.chasm.ast.type.MemoryType | ||
import io.github.charlietap.chasm.embedding.shapes.Memory | ||
import io.github.charlietap.chasm.embedding.shapes.MemoryType | ||
import io.github.charlietap.chasm.embedding.shapes.Store | ||
import io.github.charlietap.chasm.embedding.transform.Mapper | ||
import io.github.charlietap.chasm.embedding.transform.MemoryTypeMapper | ||
import io.github.charlietap.chasm.executor.instantiator.allocation.memory.MemoryAllocator | ||
import io.github.charlietap.chasm.executor.instantiator.allocation.memory.MemoryAllocatorImpl | ||
import io.github.charlietap.chasm.executor.runtime.instance.ExternalValue | ||
import io.github.charlietap.chasm.executor.runtime.store.Store | ||
import io.github.charlietap.chasm.ast.type.MemoryType as InternalMemoryType | ||
|
||
fun memory( | ||
store: Store, | ||
type: MemoryType, | ||
): ExternalValue.Memory = memory( | ||
): Memory = memory( | ||
store = store, | ||
type = type, | ||
allocator = ::MemoryAllocatorImpl, | ||
memoryTypeMapper = MemoryTypeMapper.instance, | ||
) | ||
|
||
internal fun memory( | ||
store: Store, | ||
type: MemoryType, | ||
allocator: MemoryAllocator, | ||
): ExternalValue.Memory = ExternalValue.Memory(allocator(store, type)) | ||
memoryTypeMapper: Mapper<MemoryType, InternalMemoryType>, | ||
): Memory { | ||
val memoryType = memoryTypeMapper.map(type) | ||
return Memory(ExternalValue.Memory(allocator(store.store, memoryType))) | ||
} |
Oops, something went wrong.