Source Code (main.hike)
Hike Language
package main
// Query available heap size (KB) from the JavaScript host
extern func requestHeapSizeKB() int
// Memory allocator provided by runtime
extern func malloc(size int) *byte
// UI update callback to the JavaScript host
extern func displayResult(sum int, mul int)
func compute(a int, b int) (int, int) {
return a + b, a * b
}
func main() int {
// 1. Query available size (KB) from JS host
heapKB := requestHeapSizeKB()
// 2. Allocate memory configured by host
heapBuffer := malloc(heapKB * 1024)
// 3. Offload work to background thread and await with <-
sum, mul := <-Async(func() (int, int) {
return compute(15, 3)
})
// 4. Update results in browser UI
displayResult(sum, mul)
return 0
}