diff --git a/_examples/arduino/Makefile b/_examples/arduino/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..a6d35f4b2b601379da5888e13596858643ed639e --- /dev/null +++ b/_examples/arduino/Makefile @@ -0,0 +1,7 @@ +default: + go run ../../main.go build -target=arduino app.wa + wat2wasm a.out.wat -o app.wasm + xxd -i app.wasm > app.wasm.h + +clean: + -rm *.h *.wasm *.out* diff --git a/_examples/arduino/app.wa b/_examples/arduino/app.wa index ddb6cb4a6b0d84d97e9fb75535cc3175684357ee..54cc8c6efe6cec28b0647d06c91c2876138b145e 100644 --- a/_examples/arduino/app.wa +++ b/_examples/arduino/app.wa @@ -2,20 +2,15 @@ import "syscall/arduino" +var LED = arduino.GetPinLED() + fn init() { arduino.PinMode(LED, 1) - - arduino.Println("Wa is running 😎") - arduino.Println("Greeting: " + GetGreeting()) + arduino.Println("凹语言(Wa)/Arduino is running ...") } fn main() { - LED := arduino.GetPinLED() - for { - time := arduino.Millis() - arduino.Println(time) - arduino.DigitalWrite(LED, arduino.HIGH) arduino.Delay(100) arduino.DigitalWrite(LED, arduino.LOW) diff --git a/_examples/arduino/app.wasm.h b/_examples/arduino/app.wasm.h new file mode 100644 index 0000000000000000000000000000000000000000..6e49be84132a7ce2a1b12eca40920828ec76c86e --- /dev/null +++ b/_examples/arduino/app.wasm.h @@ -0,0 +1,129 @@ +unsigned char app_wasm[] = { + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x2a, 0x08, 0x60, + 0x01, 0x7f, 0x00, 0x60, 0x02, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x01, 0x7f, + 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, 0x01, + 0x7f, 0x60, 0x01, 0x7f, 0x02, 0x7f, 0x7f, 0x60, 0x00, 0x00, 0x60, 0x03, + 0x7f, 0x7f, 0x7f, 0x00, 0x02, 0x6f, 0x06, 0x07, 0x61, 0x72, 0x64, 0x75, + 0x69, 0x6e, 0x6f, 0x0c, 0x64, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c, 0x57, + 0x72, 0x69, 0x74, 0x65, 0x00, 0x01, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, + 0x6e, 0x6f, 0x09, 0x67, 0x65, 0x74, 0x50, 0x69, 0x6e, 0x4c, 0x45, 0x44, + 0x00, 0x02, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x07, 0x70, + 0x69, 0x6e, 0x4d, 0x6f, 0x64, 0x65, 0x00, 0x01, 0x07, 0x61, 0x72, 0x64, + 0x75, 0x69, 0x6e, 0x6f, 0x05, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x00, 0x00, + 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x06, 0x6d, 0x69, 0x6c, + 0x6c, 0x69, 0x73, 0x00, 0x02, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, + 0x6f, 0x05, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x00, 0x01, 0x03, 0x13, 0x12, + 0x03, 0x00, 0x04, 0x05, 0x03, 0x00, 0x06, 0x07, 0x06, 0x07, 0x06, 0x00, + 0x00, 0x01, 0x06, 0x06, 0x06, 0x06, 0x04, 0x04, 0x01, 0x70, 0x00, 0x01, + 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x43, 0x0d, 0x7f, 0x01, 0x41, 0x80, + 0x10, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, + 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, + 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, + 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, + 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, + 0x00, 0x0b, 0x07, 0xdc, 0x01, 0x0c, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, + 0x79, 0x02, 0x00, 0x17, 0x73, 0x79, 0x73, 0x63, 0x61, 0x6c, 0x6c, 0x24, + 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x50, 0x72, 0x69, 0x6e, + 0x74, 0x6c, 0x6e, 0x00, 0x0d, 0x14, 0x73, 0x79, 0x73, 0x63, 0x61, 0x6c, + 0x6c, 0x24, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x69, 0x6e, + 0x69, 0x74, 0x00, 0x0e, 0x15, 0x73, 0x79, 0x73, 0x63, 0x61, 0x6c, 0x6c, + 0x24, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x50, 0x72, 0x69, + 0x6e, 0x74, 0x00, 0x0f, 0x0c, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, + 0x2e, 0x69, 0x6e, 0x69, 0x74, 0x00, 0x10, 0x13, 0x24, 0x72, 0x75, 0x6e, + 0x74, 0x69, 0x6d, 0x65, 0x2e, 0x77, 0x61, 0x50, 0x72, 0x69, 0x6e, 0x74, + 0x49, 0x33, 0x32, 0x00, 0x11, 0x14, 0x24, 0x72, 0x75, 0x6e, 0x74, 0x69, + 0x6d, 0x65, 0x2e, 0x77, 0x61, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x52, 0x75, + 0x6e, 0x65, 0x00, 0x12, 0x0f, 0x24, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, + 0x65, 0x2e, 0x77, 0x61, 0x50, 0x75, 0x74, 0x73, 0x00, 0x13, 0x0d, 0x5f, + 0x5f, 0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x5f, 0x2e, 0x69, 0x6e, 0x69, 0x74, + 0x00, 0x14, 0x0f, 0x5f, 0x5f, 0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x5f, 0x2e, + 0x69, 0x6e, 0x69, 0x74, 0x23, 0x31, 0x00, 0x15, 0x0d, 0x5f, 0x5f, 0x6d, + 0x61, 0x69, 0x6e, 0x5f, 0x5f, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x16, + 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x17, 0x0a, 0xc1, 0x07, + 0x12, 0x0b, 0x00, 0x23, 0x00, 0x23, 0x00, 0x20, 0x00, 0x6a, 0x24, 0x00, + 0x0b, 0x02, 0x00, 0x0b, 0x25, 0x00, 0x20, 0x00, 0x20, 0x00, 0x04, 0x40, + 0x20, 0x00, 0x41, 0x01, 0x36, 0x00, 0x00, 0x20, 0x00, 0x20, 0x01, 0x36, + 0x00, 0x04, 0x20, 0x00, 0x20, 0x02, 0x36, 0x00, 0x08, 0x20, 0x00, 0x20, + 0x03, 0x36, 0x00, 0x0c, 0x0b, 0x0b, 0x06, 0x00, 0x20, 0x00, 0x20, 0x00, + 0x0b, 0x16, 0x00, 0x20, 0x00, 0x20, 0x00, 0x04, 0x40, 0x20, 0x00, 0x20, + 0x00, 0x28, 0x00, 0x00, 0x41, 0x01, 0x6a, 0x36, 0x00, 0x00, 0x0b, 0x0b, + 0x6d, 0x01, 0x05, 0x7f, 0x20, 0x00, 0x41, 0x00, 0x46, 0x04, 0x40, 0x0f, + 0x0b, 0x20, 0x00, 0x28, 0x00, 0x00, 0x41, 0x01, 0x6b, 0x21, 0x01, 0x20, + 0x01, 0x04, 0x40, 0x20, 0x00, 0x20, 0x01, 0x36, 0x00, 0x00, 0x05, 0x20, + 0x00, 0x28, 0x00, 0x08, 0x21, 0x03, 0x20, 0x03, 0x04, 0x40, 0x20, 0x00, + 0x28, 0x00, 0x04, 0x21, 0x02, 0x20, 0x02, 0x04, 0x40, 0x20, 0x00, 0x28, + 0x00, 0x0c, 0x21, 0x04, 0x20, 0x00, 0x41, 0x10, 0x6a, 0x21, 0x05, 0x03, + 0x40, 0x20, 0x05, 0x20, 0x03, 0x11, 0x00, 0x00, 0x20, 0x02, 0x41, 0x01, + 0x6b, 0x21, 0x02, 0x20, 0x02, 0x04, 0x40, 0x20, 0x05, 0x20, 0x04, 0x6a, + 0x21, 0x05, 0x0c, 0x01, 0x0b, 0x0b, 0x0b, 0x0b, 0x20, 0x00, 0x10, 0x07, + 0x0b, 0x0b, 0x2c, 0x00, 0x41, 0x04, 0x10, 0x06, 0x24, 0x04, 0x41, 0x04, + 0x10, 0x06, 0x24, 0x05, 0x41, 0x04, 0x10, 0x06, 0x24, 0x07, 0x41, 0x04, + 0x10, 0x06, 0x24, 0x08, 0x41, 0x04, 0x10, 0x06, 0x24, 0x09, 0x41, 0x04, + 0x10, 0x06, 0x24, 0x0a, 0x41, 0x0c, 0x10, 0x06, 0x24, 0x0c, 0x0b, 0x32, + 0x01, 0x02, 0x7f, 0x41, 0x00, 0x21, 0x03, 0x41, 0x00, 0x21, 0x04, 0x02, + 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x03, 0x0e, 0x01, 0x00, + 0x00, 0x0b, 0x41, 0x00, 0x21, 0x04, 0x20, 0x01, 0x20, 0x02, 0x10, 0x13, + 0x41, 0x0a, 0x10, 0x12, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x20, 0x00, 0x10, + 0x0b, 0x0b, 0x6f, 0x01, 0x03, 0x7f, 0x41, 0x00, 0x21, 0x00, 0x41, 0x00, + 0x21, 0x01, 0x41, 0x00, 0x21, 0x02, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, + 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x00, 0x0e, 0x03, 0x00, 0x01, + 0x02, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x01, 0x23, 0x06, 0x21, 0x02, 0x20, + 0x02, 0x04, 0x40, 0x0c, 0x02, 0x05, 0x0c, 0x01, 0x0b, 0x0b, 0x41, 0x01, + 0x21, 0x01, 0x41, 0x01, 0x24, 0x06, 0x23, 0x07, 0x41, 0x00, 0x36, 0x00, + 0x00, 0x23, 0x09, 0x41, 0x01, 0x36, 0x00, 0x00, 0x23, 0x08, 0x41, 0x00, + 0x36, 0x00, 0x00, 0x23, 0x0a, 0x41, 0x01, 0x36, 0x00, 0x00, 0x23, 0x05, + 0x41, 0x02, 0x36, 0x00, 0x00, 0x0c, 0x00, 0x0b, 0x41, 0x02, 0x21, 0x01, + 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b, 0x2e, 0x01, 0x02, 0x7f, 0x41, 0x00, + 0x21, 0x03, 0x41, 0x00, 0x21, 0x04, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, + 0x02, 0x40, 0x20, 0x03, 0x0e, 0x01, 0x00, 0x00, 0x0b, 0x41, 0x00, 0x21, + 0x04, 0x20, 0x01, 0x20, 0x02, 0x10, 0x13, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, + 0x20, 0x00, 0x10, 0x0b, 0x0b, 0x6c, 0x01, 0x03, 0x7f, 0x41, 0x00, 0x21, + 0x00, 0x41, 0x00, 0x21, 0x01, 0x41, 0x00, 0x21, 0x02, 0x02, 0x40, 0x03, + 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x00, 0x0e, + 0x03, 0x00, 0x01, 0x02, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x01, 0x23, 0x0b, + 0x21, 0x02, 0x20, 0x02, 0x04, 0x40, 0x0c, 0x02, 0x05, 0x0c, 0x01, 0x0b, + 0x0b, 0x41, 0x01, 0x21, 0x01, 0x41, 0x01, 0x24, 0x0b, 0x10, 0x0e, 0x23, + 0x0c, 0x41, 0x00, 0x10, 0x0a, 0x23, 0x0c, 0x28, 0x00, 0x00, 0x10, 0x0b, + 0x36, 0x00, 0x00, 0x23, 0x0c, 0x41, 0x08, 0x36, 0x00, 0x04, 0x23, 0x0c, + 0x41, 0x07, 0x36, 0x00, 0x08, 0x0c, 0x00, 0x0b, 0x41, 0x02, 0x21, 0x01, + 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b, 0x24, 0x01, 0x02, 0x7f, 0x41, 0x00, + 0x21, 0x01, 0x41, 0x00, 0x21, 0x02, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, + 0x02, 0x40, 0x20, 0x01, 0x0e, 0x01, 0x00, 0x00, 0x0b, 0x41, 0x00, 0x21, + 0x02, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b, 0x24, 0x01, 0x02, 0x7f, 0x41, + 0x00, 0x21, 0x01, 0x41, 0x00, 0x21, 0x02, 0x02, 0x40, 0x03, 0x40, 0x02, + 0x40, 0x02, 0x40, 0x20, 0x01, 0x0e, 0x01, 0x00, 0x00, 0x0b, 0x41, 0x00, + 0x21, 0x02, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b, 0x2a, 0x01, 0x02, 0x7f, + 0x41, 0x00, 0x21, 0x02, 0x41, 0x00, 0x21, 0x03, 0x02, 0x40, 0x03, 0x40, + 0x02, 0x40, 0x02, 0x40, 0x20, 0x02, 0x0e, 0x01, 0x00, 0x00, 0x0b, 0x41, + 0x00, 0x21, 0x03, 0x20, 0x00, 0x20, 0x01, 0x10, 0x05, 0x0c, 0x02, 0x0b, + 0x0b, 0x0b, 0x0b, 0x5f, 0x01, 0x04, 0x7f, 0x41, 0x00, 0x21, 0x00, 0x41, + 0x00, 0x21, 0x01, 0x41, 0x00, 0x21, 0x02, 0x41, 0x00, 0x21, 0x03, 0x02, + 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, + 0x00, 0x0e, 0x03, 0x00, 0x01, 0x02, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x01, + 0x23, 0x03, 0x21, 0x02, 0x20, 0x02, 0x04, 0x40, 0x0c, 0x02, 0x05, 0x0c, + 0x01, 0x0b, 0x0b, 0x41, 0x01, 0x21, 0x01, 0x41, 0x01, 0x24, 0x03, 0x10, + 0x0e, 0x10, 0x01, 0x21, 0x03, 0x23, 0x04, 0x20, 0x03, 0x36, 0x00, 0x00, + 0x10, 0x15, 0x0c, 0x00, 0x0b, 0x41, 0x02, 0x21, 0x01, 0x0c, 0x02, 0x0b, + 0x0b, 0x0b, 0x0b, 0x3f, 0x01, 0x03, 0x7f, 0x41, 0x00, 0x21, 0x00, 0x41, + 0x00, 0x21, 0x01, 0x41, 0x00, 0x21, 0x02, 0x02, 0x40, 0x03, 0x40, 0x02, + 0x40, 0x02, 0x40, 0x20, 0x00, 0x0e, 0x01, 0x00, 0x00, 0x0b, 0x41, 0x00, + 0x21, 0x01, 0x23, 0x04, 0x28, 0x00, 0x00, 0x21, 0x02, 0x20, 0x02, 0x41, + 0x01, 0x10, 0x02, 0x41, 0x00, 0x10, 0x0a, 0x41, 0x0f, 0x41, 0x24, 0x10, + 0x0d, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b, 0x74, 0x01, 0x06, 0x7f, 0x41, + 0x00, 0x21, 0x00, 0x41, 0x00, 0x21, 0x01, 0x41, 0x00, 0x21, 0x02, 0x41, + 0x00, 0x21, 0x03, 0x41, 0x00, 0x21, 0x04, 0x41, 0x00, 0x21, 0x05, 0x02, + 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x00, 0x0e, + 0x02, 0x00, 0x01, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x01, 0x0c, 0x00, 0x0b, + 0x41, 0x01, 0x21, 0x01, 0x23, 0x04, 0x28, 0x00, 0x00, 0x21, 0x02, 0x23, + 0x09, 0x28, 0x00, 0x00, 0x21, 0x03, 0x20, 0x02, 0x20, 0x03, 0x10, 0x00, + 0x41, 0xe4, 0x00, 0x10, 0x03, 0x23, 0x04, 0x28, 0x00, 0x00, 0x21, 0x04, + 0x23, 0x07, 0x28, 0x00, 0x00, 0x21, 0x05, 0x20, 0x04, 0x20, 0x05, 0x10, + 0x00, 0x41, 0x84, 0x07, 0x10, 0x03, 0x41, 0x01, 0x21, 0x00, 0x0c, 0x01, + 0x0b, 0x0b, 0x0b, 0x0b, 0x08, 0x00, 0x10, 0x0c, 0x10, 0x14, 0x10, 0x16, + 0x0b, 0x0b, 0x39, 0x01, 0x00, 0x41, 0x00, 0x0b, 0x33, 0x24, 0x24, 0x77, + 0x61, 0x64, 0x73, 0x24, 0x24, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, + 0xe5, 0x87, 0xb9, 0xe8, 0xaf, 0xad, 0xe8, 0xa8, 0x80, 0x28, 0x57, 0x61, + 0x29, 0x2f, 0x41, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x20, 0x69, 0x73, + 0x20, 0x72, 0x75, 0x6e, 0x6e, 0x69, 0x6e, 0x67, 0x20, 0x2e, 0x2e, 0x2e +}; +unsigned int app_wasm_len = 1512; diff --git a/_examples/arduino/arduino.ino b/_examples/arduino/arduino.ino new file mode 100644 index 0000000000000000000000000000000000000000..0ba60e4a3ae6ce3705d51fa4a1227299067dde12 --- /dev/null +++ b/_examples/arduino/arduino.ino @@ -0,0 +1,209 @@ +/* + * Wasm3 - high performance WebAssembly interpreter written in C. + * Copyright © 2020 Volodymyr Shymanskyy, Steven Massey. + * All rights reserved. + */ + +#include +#include + +/* + * Configuration + */ + +// Redefine the default LED pin here, if needed +#ifndef LED_PIN +#define LED_PIN LED_BUILTIN +#endif + +#define WASM_STACK_SLOTS 1024 +#define NATIVE_STACK_SIZE (32*1024) + +// For (most) devices that cannot allocate a 64KiB wasm page +#define WASM_MEMORY_LIMIT 8096 + +/* + * WebAssembly app + * + * This is essentially a simple "Blink" sketch, compiled to WebAssembly + * You can build a wasm binary using C/C++, Rust, AssemblyScript, TinyGo, ... + * See https://github.com/wasm3/wasm3-arduino/tree/master/wasm_apps for details + */ + +#include "app.wasm.h" + +/* + * API bindings + * + * Note: each RawFunction should complete with one of these calls: + * m3ApiReturn(val) - Returns a value + * m3ApiSuccess() - Returns void (and no traps) + * m3ApiTrap(trap) - Returns a trap + */ + +m3ApiRawFunction(m3_arduino_millis) +{ + m3ApiReturnType (uint32_t) + + m3ApiReturn(millis()); +} + +m3ApiRawFunction(m3_arduino_delay) +{ + m3ApiGetArg (uint32_t, ms) + + // You can also trace API calls + //Serial.print("api: delay "); Serial.println(ms); + + delay(ms); + + m3ApiSuccess(); +} + +// This maps pin modes from arduino_wasm_api.h +// to actual platform-specific values +uint8_t mapPinMode(uint8_t mode) +{ + switch(mode) { + case 0: return INPUT; + case 1: return OUTPUT; + case 2: return INPUT_PULLUP; + } + return INPUT; +} + +m3ApiRawFunction(m3_arduino_pinMode) +{ + m3ApiGetArg (uint32_t, pin) + m3ApiGetArg (uint32_t, mode) + +#if !defined(PARTICLE) + typedef uint8_t PinMode; +#endif + pinMode(pin, (PinMode)mapPinMode(mode)); + + m3ApiSuccess(); +} + +m3ApiRawFunction(m3_arduino_digitalWrite) +{ + m3ApiGetArg (uint32_t, pin) + m3ApiGetArg (uint32_t, value) + + digitalWrite(pin, value); + + m3ApiSuccess(); +} + +m3ApiRawFunction(m3_arduino_getPinLED) +{ + m3ApiReturnType (uint32_t) + + m3ApiReturn(LED_PIN); +} + +m3ApiRawFunction(m3_arduino_print) +{ + m3ApiGetArgMem (const uint8_t *, buf) + m3ApiGetArg (uint32_t, len) + + Serial.write(buf, len); + m3ApiSuccess(); +} + +M3Result LinkArduino (IM3Runtime runtime) +{ + IM3Module module = runtime->modules; + const char* arduino = "arduino"; + + m3_LinkRawFunction (module, arduino, "millis", "i()", &m3_arduino_millis); + m3_LinkRawFunction (module, arduino, "delay", "v(i)", &m3_arduino_delay); + m3_LinkRawFunction (module, arduino, "pinMode", "v(ii)", &m3_arduino_pinMode); + m3_LinkRawFunction (module, arduino, "digitalWrite", "v(ii)", &m3_arduino_digitalWrite); + + // Test functions + m3_LinkRawFunction (module, arduino, "getPinLED", "i()", &m3_arduino_getPinLED); + m3_LinkRawFunction (module, arduino, "print", "v(*i)", &m3_arduino_print); + + return m3Err_none; +} + +/* + * Engine start, liftoff! + */ + +#define FATAL(func, msg) { Serial.print("Fatal: " func " "); Serial.println(msg); return; } + +void wasm_task(void*) +{ + M3Result result = m3Err_none; + + IM3Environment env = m3_NewEnvironment (); + if (!env) FATAL("NewEnvironment", "failed"); + + IM3Runtime runtime = m3_NewRuntime (env, WASM_STACK_SLOTS, NULL); + if (!runtime) FATAL("NewRuntime", "failed"); + +#ifdef WASM_MEMORY_LIMIT + runtime->memoryLimit = WASM_MEMORY_LIMIT; +#endif + + IM3Module module; + result = m3_ParseModule (env, &module, app_wasm, app_wasm_len); + if (result) FATAL("ParseModule", result); + + result = m3_LoadModule (runtime, module); + if (result) FATAL("LoadModule", result); + + result = LinkArduino (runtime); + if (result) FATAL("LinkArduino", result); + + IM3Function f; + result = m3_FindFunction (&f, runtime, "_start"); + if (result) FATAL("FindFunction", result); + + Serial.println("Running WebAssembly..."); + result = m3_CallV (f); + + // Should not arrive here + + if (result) { + M3ErrorInfo info; + m3_GetErrorInfo (runtime, &info); + Serial.print("Error44: "); + Serial.print(result); + Serial.print(" ("); + Serial.print(info.message); + Serial.println(")"); + if (info.file && strlen(info.file) && info.line) { + Serial.print("At "); + Serial.print(info.file); + Serial.print(":"); + Serial.println(info.line); + } + } +} + +void setup() +{ + Serial.begin(115200); + delay(100); + + // Wait for serial port to connect + // Needed for native USB port only + while(!Serial) {} + + Serial.println("\nWasm3 v" M3_VERSION " (" M3_ARCH "), build " __DATE__ " " __TIME__); + +#ifdef ESP32 + // On ESP32, we can launch in a separate thread + xTaskCreate(&wasm_task, "wasm3", NATIVE_STACK_SIZE, NULL, 5, NULL); +#else + wasm_task(NULL); +#endif +} + +void loop() +{ + delay(100); +}