Generate the `writeString` helper function.
(&self, tokens: &mut Tokens<Go>)
| 38 | |
| 39 | /// Generate the `writeString` helper function. |
| 40 | fn generate_write_string(&self, tokens: &mut Tokens<Go>) { |
| 41 | // Add writeString helper function for interface string returns |
| 42 | quote_in! { *tokens => |
| 43 | $(comment(&[ |
| 44 | "writeString will put a Go string into the Wasm memory following the Component", |
| 45 | "Model calling conventions, such as allocating memory with the realloc function", |
| 46 | ])) |
| 47 | func writeString( |
| 48 | ctx $CONTEXT_CONTEXT, |
| 49 | s string, |
| 50 | memory $WAZERO_API_MEMORY, |
| 51 | realloc api.Function, |
| 52 | ) (uint64, uint64, error) { |
| 53 | if len(s) == 0 { |
| 54 | return 1, 0, nil |
| 55 | } |
| 56 | |
| 57 | results, err := realloc.Call(ctx, 0, 0, 1, uint64(len(s))) |
| 58 | if err != nil { |
| 59 | return 1, 0, err |
| 60 | } |
| 61 | ptr := results[0] |
| 62 | ok := memory.Write(uint32(ptr), []byte(s)) |
| 63 | if !ok { |
| 64 | return 1, 0, $ERRORS_NEW("failed to write string to wasm memory") |
| 65 | } |
| 66 | return uint64(ptr), uint64(len(s)), nil |
| 67 | } |
| 68 | $['\n'] |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | /// Generate the Factory struct, constructor, and methods. |
| 73 | fn generate_factory(&self, tokens: &mut Tokens<Go>) { |
no outgoing calls