Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bhelx committed Oct 12, 2023
1 parent e5f6625 commit e94567e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,29 @@ to do this correctly. So we recommend reading out [concept doc on Host Functions

### A Simple Example

TODO: fill out
Host functions have a similar interface as exports. You just need to declare them as extern on the top of your lib.rs. You only declare the interface as it is the host's responsibility to provide the implementation:


```go
//go:wasmimport env a_python_func
func aPythonFunc(pdk.ExtismPointer) pdk.ExtismPointer
```

We should be able to call this function as a normal Go function. Note that we need to manually handle the pointer casting:

```go
//export hello_from_python
func helloFromPython() int32 {
msg := "An argument to send to Python"
mem := pdk.AllocateString(msg)
defer mem.Free()
ptr := aPythonFunc(pdk.ExtismPointer(mem.Offset()))
rmem := pdk.FindMemory(uint64(ptr))
response := string(rmem.ReadBytes())
pdk.OutputString(response)
return 0
}

```

### Testing it out
Expand All @@ -239,7 +259,7 @@ We can't really test this from the Extism CLI as something must provide the impl
write out the Python side here. Check out the [docs for Host SDKs](https://extism.org/docs/concepts/host-sdk) to implement a host function in a language of your choice.

```python
from extism import host_fn, Function, ValType
from extism import host_fn, Function, ValType, Plugin

@host_fn
def a_python_func(plugin, input_, output, _user_data):
Expand Down Expand Up @@ -271,6 +291,7 @@ functions = [
)
]

manifest = {"wasm": [{"path": "/path/to/plugin.wasm"}]}
plugin = Plugin(manifest, functions=functions)
result = plugin.call('hello_from_python')
print(result)
Expand Down
46 changes: 23 additions & 23 deletions env.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
package pdk

type extismPointer uint64
type ExtismPointer uint64

//go:wasmimport env extism_input_length
func extism_input_length() uint64

//go:wasmimport env extism_length
func extism_length(extismPointer) uint64
func extism_length(ExtismPointer) uint64

//go:wasmimport env extism_alloc
func extism_alloc(uint64) extismPointer
func extism_alloc(uint64) ExtismPointer

//go:wasmimport env extism_free
func extism_free(extismPointer)
func extism_free(ExtismPointer)

//go:wasmimport env extism_input_load_u8
func extism_input_load_u8_(extismPointer) uint32
func extism_input_load_u8_(ExtismPointer) uint32

func extism_input_load_u8(p extismPointer) uint8 {
func extism_input_load_u8(p ExtismPointer) uint8 {
return uint8(extism_input_load_u8_(p))
}

//go:wasmimport env extism_input_load_u64
func extism_input_load_u64(extismPointer) uint64
func extism_input_load_u64(ExtismPointer) uint64

//go:wasmimport env extism_output_set
func extism_output_set(extismPointer, uint64)
func extism_output_set(ExtismPointer, uint64)

//go:wasmimport env extism_error_set
func extism_error_set(extismPointer)
func extism_error_set(ExtismPointer)

//go:wasmimport env extism_config_get
func extism_config_get(extismPointer) extismPointer
func extism_config_get(ExtismPointer) ExtismPointer

//go:wasmimport env extism_var_get
func extism_var_get(extismPointer) extismPointer
func extism_var_get(ExtismPointer) ExtismPointer

//go:wasmimport env extism_var_set
func extism_var_set(extismPointer, extismPointer)
func extism_var_set(ExtismPointer, ExtismPointer)

//go:wasmimport env extism_store_u8
func extism_store_u8_(extismPointer, uint32)
func extism_store_u8(p extismPointer, v uint8) {
func extism_store_u8_(ExtismPointer, uint32)
func extism_store_u8(p ExtismPointer, v uint8) {
extism_store_u8_(p, uint32(v))
}

//go:wasmimport env extism_load_u8
func extism_load_u8_(extismPointer) uint32
func extism_load_u8(p extismPointer) uint8 {
func extism_load_u8_(ExtismPointer) uint32
func extism_load_u8(p ExtismPointer) uint8 {
return uint8(extism_load_u8_(p))
}

//go:wasmimport env extism_store_u64
func extism_store_u64(extismPointer, uint64)
func extism_store_u64(ExtismPointer, uint64)

//go:wasmimport env extism_load_u64
func extism_load_u64(extismPointer) uint64
func extism_load_u64(ExtismPointer) uint64

//go:wasmimport env extism_http_request
func extism_http_request(extismPointer, extismPointer) extismPointer
func extism_http_request(ExtismPointer, ExtismPointer) ExtismPointer

//go:wasmimport env extism_http_status_code
func extism_http_status_code() int32

//go:wasmimport env extism_log_info
func extism_log_info(extismPointer)
func extism_log_info(ExtismPointer)

//go:wasmimport env extism_log_debug
func extism_log_debug(extismPointer)
func extism_log_debug(ExtismPointer)

//go:wasmimport env extism_log_warn
func extism_log_warn(extismPointer)
func extism_log_warn(ExtismPointer)

//go:wasmimport env extism_log_error
func extism_log_error(extismPointer)
func extism_log_error(ExtismPointer)
28 changes: 17 additions & 11 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Memory struct {
offset extismPointer
offset ExtismPointer
length uint64
}

Expand All @@ -21,17 +21,17 @@ const (
LogTrace
)

func load(offset extismPointer, buf []byte) {
func load(offset ExtismPointer, buf []byte) {
length := len(buf)

for i := 0; i < length; i++ {
if length-i >= 8 {
x := extism_load_u64(offset + extismPointer(i))
x := extism_load_u64(offset + ExtismPointer(i))
binary.LittleEndian.PutUint64(buf[i:i+8], x)
i += 7
continue
}
buf[i] = extism_load_u8(offset + extismPointer(i))
buf[i] = extism_load_u8(offset + ExtismPointer(i))
}
}

Expand All @@ -41,29 +41,29 @@ func loadInput() []byte {

for i := 0; i < length; i++ {
if length-i >= 8 {
x := extism_input_load_u64(extismPointer(i))
x := extism_input_load_u64(ExtismPointer(i))
binary.LittleEndian.PutUint64(buf[i:i+8], x)
i += 7
continue
}
buf[i] = extism_input_load_u8(extismPointer(i))
buf[i] = extism_input_load_u8(ExtismPointer(i))
}

return buf
}

func store(offset extismPointer, buf []byte) {
func store(offset ExtismPointer, buf []byte) {
length := len(buf)

for i := 0; i < length; i++ {
if length-i >= 8 {
x := binary.LittleEndian.Uint64(buf[i : i+8])
extism_store_u64(offset+extismPointer(i), x)
extism_store_u64(offset+ExtismPointer(i), x)
i += 7
continue
}

extism_store_u8(offset+extismPointer(i), buf[i])
extism_store_u8(offset+ExtismPointer(i), buf[i])
}
}

Expand Down Expand Up @@ -265,6 +265,12 @@ func (r *HTTPRequest) Send() HTTPResponse {
}
}

func (m *Memory) ReadBytes() []byte {
buff := make([]byte, m.length)
m.Load(buff)
return buff
}

func (m *Memory) Load(buffer []byte) {
load(m.offset, buffer)
}
Expand All @@ -286,6 +292,6 @@ func (m *Memory) Offset() uint64 {
}

func FindMemory(offset uint64) Memory {
length := extism_length(extismPointer(offset))
return Memory{extismPointer(offset), length}
length := extism_length(ExtismPointer(offset))
return Memory{ExtismPointer(offset), length}
}

0 comments on commit e94567e

Please sign in to comment.