|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "unsafe" |
| 5 | +) |
| 6 | + |
| 7 | +// === |
| 8 | +// functions for the protocol |
| 9 | + |
| 10 | +//go:wasmimport typst_env wasm_minimal_protocol_write_args_to_buffer |
| 11 | +func write_args_to_buffer(ptr int32) |
| 12 | + |
| 13 | +func WriteArgsToBuffer(argBuf []byte) { |
| 14 | + ptr := int32(uintptr(unsafe.Pointer(unsafe.SliceData(argBuf)))) |
| 15 | + write_args_to_buffer(ptr) |
| 16 | +} |
| 17 | + |
| 18 | +//go:wasmimport typst_env wasm_minimal_protocol_send_result_to_host |
| 19 | +func send_result_to_host(ptr, size int32) |
| 20 | + |
| 21 | +func SendResultToHost(resBuf []byte) { |
| 22 | + size := int32(len(resBuf)) |
| 23 | + ptr := int32(uintptr(unsafe.Pointer(unsafe.SliceData(resBuf)))) |
| 24 | + send_result_to_host(ptr, size) |
| 25 | +} |
| 26 | + |
| 27 | +// === |
| 28 | + |
| 29 | +// main is required for the `wasip1` target, even if it isn't used. |
| 30 | +func main() {} |
| 31 | + |
| 32 | +//go:export hello |
| 33 | +func hello() int32 { |
| 34 | + const message = "Hello from wasm!!!" |
| 35 | + SendResultToHost([]byte(message)) |
| 36 | + return 0 |
| 37 | +} |
| 38 | + |
| 39 | +//go:export double_it |
| 40 | +func doubleIt(arg1Len int32) int32 { |
| 41 | + buf := make([]byte, arg1Len*2) |
| 42 | + WriteArgsToBuffer(buf) |
| 43 | + |
| 44 | + copy(buf[arg1Len:], buf[:arg1Len]) |
| 45 | + SendResultToHost(buf) |
| 46 | + return 0 |
| 47 | +} |
| 48 | + |
| 49 | +//go:export concatenate |
| 50 | +func concatenate(arg1Len, arg2Len int32) int32 { |
| 51 | + buf := make([]byte, arg1Len+arg2Len+1) |
| 52 | + WriteArgsToBuffer(buf) |
| 53 | + |
| 54 | + copy(buf[arg1Len+1:], buf[arg1Len:]) |
| 55 | + buf[arg1Len] = '*' |
| 56 | + SendResultToHost(buf) |
| 57 | + return 0 |
| 58 | +} |
| 59 | + |
| 60 | +//go:export shuffle |
| 61 | +func shuffle(arg1Len, arg2Len, arg3Len int) int32 { |
| 62 | + argBuf := make([]byte, arg1Len+arg2Len+arg3Len) |
| 63 | + arg1 := argBuf[:arg1Len] |
| 64 | + arg2 := argBuf[arg1Len : arg1Len+arg2Len] |
| 65 | + arg3 := argBuf[arg1Len+arg2Len:] |
| 66 | + WriteArgsToBuffer(argBuf) |
| 67 | + |
| 68 | + resBuf := make([]byte, 0, arg1Len+arg2Len+arg3Len+2) |
| 69 | + resBuf = append(resBuf, arg3...) |
| 70 | + resBuf = append(resBuf, '-') |
| 71 | + resBuf = append(resBuf, arg1...) |
| 72 | + resBuf = append(resBuf, '-') |
| 73 | + resBuf = append(resBuf, arg2...) |
| 74 | + SendResultToHost(resBuf) |
| 75 | + return 0 |
| 76 | +} |
| 77 | + |
| 78 | +//go:export returns_ok |
| 79 | +func returnsOk() int32 { |
| 80 | + const message = "This is an `Ok`" |
| 81 | + SendResultToHost([]byte(message)) |
| 82 | + return 0 |
| 83 | +} |
| 84 | + |
| 85 | +//go:export returns_err |
| 86 | +func returnsErr() int32 { |
| 87 | + const message = "This is an `Err`" |
| 88 | + SendResultToHost([]byte(message)) |
| 89 | + return 1 |
| 90 | +} |
| 91 | + |
| 92 | +//go:export will_panic |
| 93 | +func willPanic() int32 { |
| 94 | + panic("Panicking, this message will not be seen...") |
| 95 | +} |
0 commit comments