Skip to content

Commit 5ac0f1f

Browse files
committed
Initial commit
1 parent 4223b8c commit 5ac0f1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+9919
-2
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Steven Massey, Volodymyr Shymanskyy
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
# wasm3-arduino
2-
Wasm3 library for Arduino, PlatformIO, Particle
1+
# Wasm3
2+
3+
This library can be used with `Arduino`, `PlatformIO` and `Particle`.
4+
5+
Please note that this library requires at least ~64Kb Flash and ~10Kb RAM even for minimal functionality.
6+
7+
### License
8+
This project is released under The MIT License (MIT)

examples/Blink/Blink.ino

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* Wasm3 - high performance WebAssembly interpreter written in C.
3+
* Copyright © 2020 Volodymyr Shymanskyy, Steven Massey.
4+
* All rights reserved.
5+
*/
6+
7+
#include <wasm3.h>
8+
9+
// You can redefine the default LED pin here, if needed
10+
//#define LED_BUILTIN D7
11+
12+
#ifndef LED_BUILTIN
13+
#define LED_BUILTIN 13
14+
#endif
15+
16+
/*
17+
* WebAssembly app
18+
*
19+
* This is essentially a simple "Blink" sketch, compiled to WebAssembly
20+
* The file was generated from ./wasm_apps/cpp
21+
* You can also find examples for Rust, TinyGO
22+
*/
23+
24+
unsigned char app_wasm[] = {
25+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x11, 0x04, 0x60,
26+
0x02, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x00, 0x60, 0x01, 0x7f, 0x00, 0x60,
27+
0x00, 0x01, 0x7f, 0x02, 0x4e, 0x04, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69,
28+
0x6e, 0x6f, 0x09, 0x67, 0x65, 0x74, 0x50, 0x69, 0x6e, 0x4c, 0x45, 0x44,
29+
0x00, 0x03, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x07, 0x70,
30+
0x69, 0x6e, 0x4d, 0x6f, 0x64, 0x65, 0x00, 0x00, 0x07, 0x61, 0x72, 0x64,
31+
0x75, 0x69, 0x6e, 0x6f, 0x0c, 0x64, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c,
32+
0x57, 0x72, 0x69, 0x74, 0x65, 0x00, 0x00, 0x07, 0x61, 0x72, 0x64, 0x75,
33+
0x69, 0x6e, 0x6f, 0x05, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x00, 0x02, 0x03,
34+
0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x13, 0x02, 0x06,
35+
0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x06, 0x5f, 0x73, 0x74,
36+
0x61, 0x72, 0x74, 0x00, 0x04, 0x0a, 0x3a, 0x01, 0x38, 0x01, 0x01, 0x7f,
37+
0x41, 0x80, 0x08, 0x10, 0x00, 0x22, 0x00, 0x36, 0x02, 0x00, 0x20, 0x00,
38+
0x41, 0x01, 0x10, 0x01, 0x03, 0x40, 0x41, 0x80, 0x08, 0x28, 0x02, 0x00,
39+
0x41, 0x01, 0x10, 0x02, 0x41, 0xe4, 0x00, 0x10, 0x03, 0x41, 0x80, 0x08,
40+
0x28, 0x02, 0x00, 0x41, 0x00, 0x10, 0x02, 0x41, 0x84, 0x07, 0x10, 0x03,
41+
0x0c, 0x00, 0x0b, 0x00, 0x0b
42+
};
43+
unsigned int app_wasm_len = 197;
44+
45+
/*
46+
* API bindings
47+
*
48+
* Note: each RawFunction should complete with one of these calls:
49+
* m3ApiReturn(val) - Returns a value
50+
* m3ApiSuccess() - Returns void (and no traps)
51+
* m3ApiTrap(trap) - Returns a trap
52+
*/
53+
54+
#include <m3_api_defs.h>
55+
#include <m3_env.h>
56+
57+
m3ApiRawFunction(m3_arduino_millis)
58+
{
59+
m3ApiReturnType (uint32_t)
60+
61+
m3ApiReturn(millis());
62+
}
63+
64+
m3ApiRawFunction(m3_arduino_delay)
65+
{
66+
m3ApiGetArg (uint32_t, ms)
67+
68+
// You can also trace API calls
69+
//Serial.print("api: delay "); Serial.println(ms);
70+
71+
delay(ms);
72+
73+
m3ApiSuccess();
74+
}
75+
76+
// This maps pin modes from arduino_wasm_api.h
77+
// to actual platform-specific values
78+
uint8_t mapPinMode(uint8_t mode)
79+
{
80+
switch(mode) {
81+
case (0): return INPUT;
82+
case (1): return OUTPUT;
83+
case (2): return INPUT_PULLUP;
84+
}
85+
return INPUT;
86+
}
87+
88+
m3ApiRawFunction(m3_arduino_pinMode)
89+
{
90+
m3ApiGetArg (uint32_t, pin)
91+
m3ApiGetArg (uint32_t, mode)
92+
93+
#if !defined(PARTICLE)
94+
typedef uint8_t PinMode;
95+
#endif
96+
pinMode(pin, (PinMode)mapPinMode(mode));
97+
98+
m3ApiSuccess();
99+
}
100+
101+
m3ApiRawFunction(m3_arduino_digitalWrite)
102+
{
103+
m3ApiGetArg (uint32_t, pin)
104+
m3ApiGetArg (uint32_t, value)
105+
106+
digitalWrite(pin, value);
107+
108+
m3ApiSuccess();
109+
}
110+
111+
// This is a convenience function
112+
m3ApiRawFunction(m3_arduino_getPinLED)
113+
{
114+
m3ApiReturnType (uint32_t)
115+
116+
m3ApiReturn(LED_BUILTIN);
117+
}
118+
119+
// Dummy, for TinyGO
120+
m3ApiRawFunction(m3_dummy)
121+
{
122+
m3ApiSuccess();
123+
}
124+
125+
M3Result m3_LinkArduino (IM3Runtime runtime)
126+
{
127+
IM3Module module = runtime->modules;
128+
const char* arduino = "arduino";
129+
130+
m3_LinkRawFunction (module, arduino, "millis", "i()", &m3_arduino_millis);
131+
m3_LinkRawFunction (module, arduino, "delay", "v(i)", &m3_arduino_delay);
132+
m3_LinkRawFunction (module, arduino, "pinMode", "v(ii)", &m3_arduino_pinMode);
133+
m3_LinkRawFunction (module, arduino, "digitalWrite", "v(ii)", &m3_arduino_digitalWrite);
134+
135+
m3_LinkRawFunction (module, arduino, "getPinLED", "i()", &m3_arduino_getPinLED);
136+
137+
m3_LinkRawFunction (module, "env", "io_get_stdout", "i()", &m3_dummy);
138+
139+
return m3Err_none;
140+
}
141+
142+
/*
143+
* Engine start, liftoff!
144+
*/
145+
146+
#define FATAL(func, msg) { Serial.print("Fatal: " func " "); Serial.println(msg); return; }
147+
148+
void wasm_task(void*)
149+
{
150+
M3Result result = m3Err_none;
151+
152+
IM3Environment env = m3_NewEnvironment ();
153+
if (!env) FATAL("m3_NewEnvironment", "failed");
154+
155+
IM3Runtime runtime = m3_NewRuntime (env, 1024, NULL);
156+
if (!runtime) FATAL("m3_NewRuntime", "failed");
157+
158+
IM3Module module;
159+
result = m3_ParseModule (env, &module, app_wasm, app_wasm_len-1);
160+
if (result) FATAL("m3_ParseModule", result);
161+
162+
result = m3_LoadModule (runtime, module);
163+
if (result) FATAL("m3_LoadModule", result);
164+
165+
result = m3_LinkArduino (runtime);
166+
if (result) FATAL("m3_LinkArduino", result);
167+
168+
IM3Function f;
169+
result = m3_FindFunction (&f, runtime, "_start");
170+
if (result) FATAL("m3_FindFunction", result);
171+
172+
Serial.println("Running WebAssembly...\n");
173+
174+
const char* i_argv[1] = { NULL };
175+
result = m3_CallWithArgs (f, 0, i_argv);
176+
177+
if (result) FATAL("m3_CallWithArgs", result);
178+
179+
// Should not arrive here
180+
}
181+
182+
void setup()
183+
{
184+
Serial.begin(115200);
185+
delay(100);
186+
187+
Serial.println("\nWasm3 v" M3_VERSION ", build " __DATE__ " " __TIME__);
188+
189+
#ifdef ESP32
190+
// On ESP32, we can launch in a separate thread
191+
xTaskCreate(&wasm_task, "wasm3", 32768, NULL, 5, NULL);
192+
#else
193+
wasm_task(NULL);
194+
#endif
195+
}
196+
197+
void loop()
198+
{
199+
delay(100);
200+
}

keywords.txt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#######################################
2+
# Data types (KEYWORD1)
3+
#######################################
4+
M3Result KEYWORD1
5+
IM3Environment KEYWORD1
6+
IM3Runtime KEYWORD1
7+
IM3Module KEYWORD1
8+
m3ApiRawFunction KEYWORD1
9+
10+
#######################################
11+
# Methods and Functions (KEYWORD2)
12+
#######################################
13+
wasm3 KEYWORD2
14+
m3_NewEnvironment KEYWORD2
15+
m3_NewRuntime KEYWORD2
16+
m3_ParseModule KEYWORD2
17+
m3_LoadModule KEYWORD2
18+
m3_FindFunction KEYWORD2
19+
m3_CallWithArgs KEYWORD2
20+
m3_LinkRawFunction KEYWORD2
21+
22+
#######################################
23+
# Literals (LITERAL1)
24+
#######################################
25+
I32 LITERAL1
26+
I64 LITERAL1
27+
F32 LITERAL1
28+
F64 LITERAL1

library.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "Wasm3",
3+
"version": "0.4.2",
4+
"description": "The fastest WebAssembly interpreter. It allows you to run WASM files directly on a wide range of devices, including microcontrollers, routers, smartphones and of course within browsers.",
5+
"keywords": "esp32, esp8266, wasm, webassembly, interpreter, iot, edge computing",
6+
"authors": [
7+
{
8+
"name": "Volodymyr Shymanskyy",
9+
"url": "https://github.com/vshymanskyy",
10+
"maintainer": true
11+
},
12+
{
13+
"name": "Steven Massey",
14+
"url": "https://github.com/soundandform",
15+
}
16+
],
17+
"repository":
18+
{
19+
"type": "git",
20+
"url": "https://github.com/wasm3/wasm3.git"
21+
},
22+
"homepage": "https://github.com/wasm3/wasm3",
23+
"frameworks": "*",
24+
"platforms": "*",
25+
"examples": "examples/*/*.ino"
26+
}
27+

library.properties

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name=Wasm3
2+
version=0.4.2
3+
author=Volodymyr Shymanskyy <[email protected]>, Steven Massey <[email protected]>
4+
license=MIT
5+
maintainer=Volodymyr Shymanskyy <[email protected]>
6+
sentence=The fastest WebAssembly interpreter.
7+
paragraph=It allows you to run WASM files directly on a wide range of devices, including microcontrollers, smartphones, routers, and of course within browsers.
8+
category=Other
9+
url=https://github.com/wasm3/wasm3
10+
repository=https://github.com/wasm3/wasm3.git
11+
architectures=*
12+
includes=wasm3.h

src/m3_api_defs.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// m3_api_defs.h
3+
//
4+
// Created by Volodymyr Shymanskyy on 12/20/19.
5+
// Copyright © 2019 Volodymyr Shymanskyy. All rights reserved.
6+
//
7+
8+
#ifndef m3_api_defs_h
9+
#define m3_api_defs_h
10+
11+
#include "m3_core.h"
12+
13+
// TODO: perform bounds checks
14+
#define m3ApiOffsetToPtr(offset) (void*)((u8*)_mem + (u32)(offset))
15+
#define m3ApiPtrToOffset(ptr) (u32)((u8*)ptr - (u8*)_mem)
16+
17+
#define m3ApiReturnType(TYPE) TYPE* raw_return = ((TYPE*) (_sp));
18+
#define m3ApiGetArg(TYPE, NAME) TYPE NAME = * ((TYPE *) (_sp++));
19+
#define m3ApiGetArgMem(TYPE, NAME) TYPE NAME = (TYPE)m3ApiOffsetToPtr(* ((u32 *) (_sp++)));
20+
21+
#define m3ApiRawFunction(NAME) const void * NAME (IM3Runtime runtime, uint64_t * _sp, void * _mem)
22+
#define m3ApiReturn(VALUE) { *raw_return = (VALUE); return m3Err_none; }
23+
#define m3ApiTrap(VALUE) { return VALUE; }
24+
#define m3ApiSuccess() { return m3Err_none; }
25+
26+
#endif // m3_api_defs_h

0 commit comments

Comments
 (0)