Skip to content

Commit

Permalink
Update to moonc v0.1.20250121+7fc3467ab
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Lewis <[email protected]>
  • Loading branch information
gmlewis committed Jan 24, 2025
1 parent 6d2de2f commit 87982a4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ The code has been updated to support compiler:

```bash
$ moon version --all
moon 0.1.20241216 (b57ed1c 2024-12-16) ~/.moon/bin/moon
moonc v0.1.20241216+68e710374 ~/.moon/bin/moonc
moonrun 0.1.20241216 (b57ed1c 2024-12-16) ~/.moon/bin/moonrun
moon 0.1.20250121 (a825806 2025-01-21) ~/.moon/bin/moon
moonc v0.1.20250121+7fc3467ab ~/.moon/bin/moonc
moonrun 0.1.20250121 (a825806 2025-01-21) ~/.moon/bin/moonrun
```

Use `moonup` to manage `moon` compiler versions:
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extism/moonbit-pdk",
"version": "0.46.0",
"version": "0.47.0",
"deps": {},
"readme": "README.md",
"repository": "https://github.com/extism/moonbit-pdk",
Expand Down
10 changes: 5 additions & 5 deletions pdk/config/config.mbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
///| `Config` provides methods to get "config" data from the host.
pub(all) struct Config {}

///| `Config::get_memory` returns a "config" Memory block from the host that is keyed by `key`.
///| `get_memory` returns a "config" Memory block from the host that is keyed by `key`.
/// Note that no processing is performed on this block of memory.
pub fn Config::get_memory(key : String) -> @host.Memory? {
pub fn get_memory(key : String) -> @host.Memory? {
let key_mem = @host.allocate_string(key)
let offset = @extism.config_get(key_mem.offset)
key_mem.free()
Expand All @@ -17,11 +17,11 @@ pub fn Config::get_memory(key : String) -> @host.Memory? {
Some({ offset, length })
}

///| `Config::get` returns a "config" String from the host that is keyed by `key`.
///| `get` returns a "config" String from the host that is keyed by `key`.
/// Note that the Extism host strings are UTF-8 and therefore the returned
/// String is encoded as UTF-16 in compliance with MoonBit Strings.
pub fn Config::get(key : String) -> String? {
match Config::get_memory(key) {
pub fn get(key : String) -> String? {
match get_memory(key) {
Some(mem) => {
let s = mem.to_string()
mem.free()
Expand Down
9 changes: 4 additions & 5 deletions pdk/host/host.mbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
///| `input` returns a sequence of (unprocessed) bytes from the host.
pub fn input() -> Bytes {
let length = @extism.input_length().to_int()
let value = Bytes::new(length)
for i = 0; i < length; i = i + 1 {
value[i] = @extism.input_load_u8(i.to_int64())
}
value
let value = FixedArray::makei(length, fn(i) {
@extism.input_load_u8(i.to_int64())
})
Bytes::from_fixedarray(value)
}

///| `input_string` returns a (UTF-16) String from the host.
Expand Down
30 changes: 14 additions & 16 deletions pdk/host/memory.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ pub fn free(self : Memory) -> Unit {
@extism.free(self.offset)
}

///| `Memory::allocate` allocates an uninitialized (determined by host)
///| `allocate` allocates an uninitialized (determined by host)
/// area of shared memory on the host.
pub fn Memory::allocate(length : Int64) -> Memory {
pub fn allocate(length : Int64) -> Memory {
{ offset: @extism.alloc(length), length }
}

///| `Memory::allocate_bytes` allocates and initializes host memory
///| `allocate_bytes` allocates and initializes host memory
/// with the provided (unprocessed) bytes.
pub fn Memory::allocate_bytes(bytes : Bytes) -> Memory {
pub fn allocate_bytes(bytes : Bytes) -> Memory {
let length = bytes.length().to_int64()
let offset = @extism.alloc(length)
for i = 0L; i < length; i = i + 1L {
for i in 0L..<length {
@extism.store_u8(offset + i, bytes[i.to_int()])
}
{ offset, length }
Expand All @@ -38,16 +38,16 @@ pub fn output_memory(self : Memory) -> Unit {
@extism.output_set(self.offset, self.length)
}

///| `Memory::allocate_string` allocates and initializes a UTF-8 string
///| `allocate_string` allocates and initializes a UTF-8 string
/// in host memory that is converted from this UTF-16 MoonBit String.
pub fn Memory::allocate_string(s : String) -> Memory {
pub fn allocate_string(s : String) -> Memory {
@pdk.ToUtf8::to_utf8(s) |> allocate_bytes()
}

///| `Memory::allocate_json_value` allocates and initializes a UTF-8 string
///| `allocate_json_value` allocates and initializes a UTF-8 string
/// in host memory that is converted from this `Json`.
pub fn Memory::allocate_json_value(j : Json) -> Memory {
j.stringify() |> Memory::allocate_string()
pub fn allocate_json_value(j : Json) -> Memory {
j.stringify() |> allocate_string()
}

///| `to_string` reads and converts the UTF-8 string residing in the host memory
Expand All @@ -69,10 +69,8 @@ pub fn to_int(self : Memory) -> Int {
///| `to_bytes` reads the (unprocessed) bytes residing in the host memory
/// to a MoonBit Bytes.
pub fn to_bytes(self : Memory) -> Bytes {
let bytes = Bytes::new(self.length.to_int())
for i = 0L; i < self.length; i = i + 1L {
let byte = @extism.load_u8(self.offset + i)
bytes[i.to_int()] = byte
}
bytes
let bytes = FixedArray::makei(self.length.to_int(), fn(i) {
@extism.load_u8(self.offset + i.to_int64())
})
Bytes::from_fixedarray(bytes)
}
10 changes: 4 additions & 6 deletions pdk/var/var.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ pub fn set_bytes(key : String, value : Bytes) -> Unit {
///| `set_int` sets the host's "var" Int associated with the provided `key`.
pub fn set_int(key : String, value : Int) -> Unit {
let key_mem = @host.allocate_string(key)
let bytes = Bytes::new(4)
bytes[0] = value.land(255).to_byte()
bytes[1] = (value >> 8).land(255).to_byte()
bytes[2] = (value >> 16).land(255).to_byte()
bytes[3] = (value >> 24).land(255).to_byte()
let val_mem = @host.allocate_bytes(bytes)
let bytes = FixedArray::makei(4, fn(i) {
(value >> (i * 8)).land(255).to_byte()
})
let val_mem = @host.allocate_bytes(Bytes::from_fixedarray(bytes))
@extism.var_set(key_mem.offset, val_mem.offset)
key_mem.free()
val_mem.free()
Expand Down

0 comments on commit 87982a4

Please sign in to comment.