Skip to content

Commit

Permalink
Access WASM module memory
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Sep 5, 2024
1 parent a249f71 commit fc91301
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ Type "help()" for more information.
16
```

## Access WASM module memory

```py
>>> import cpp
>>> cpp._memory[4096:4096+32]
bytearray(b' Blink\x00\xf0\x9f\xa4\xa9 C++ is running!\x00\n\x00\x00\x00')
```

## TODO

- [ ] Fix linking with native toolchain ABI
Expand All @@ -101,7 +109,6 @@ Type "help()" for more information.
- [x] Use functions from other modules like `time`, `machine` etc.
- [x] Implement `millis()`, run Coremark
- [x] Support memory
- [ ] Expose memory to Python
- [ ] TBD: Support globals
- [ ] Add RISC-V support: https://github.com/micropython/micropython/pull/15603
- [ ] Add AArch64 support: https://github.com/micropython/micropython/issues/4176#issuecomment-1657003511
Expand Down
7 changes: 7 additions & 0 deletions runtime/libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ size_t strlen(const char *str) {
return (s - str);
}

int strncmp(const char *_l, const char *_r, size_t n) {
const unsigned char *l=(void *)_l, *r=(void *)_r;
if (!n--) return 0;
for (; *l && *r && n && *l == *r ; l++, r++, n--);
return *l - *r;
}

void abort() {
mp_printf(&mp_plat_print, "Aborting");
for(;;) {} // Wait forever
Expand Down
20 changes: 20 additions & 0 deletions runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ static mp_obj_t loop(void) {

static MP_DEFINE_CONST_FUN_OBJ_0(loop_obj, loop);

// Module Attributes

static mp_obj_t getattr(mp_obj_t attr) {
size_t attr_len;
char* attr_str = mp_obj_str_get_data(attr, &attr_len);
if (!attr_str) {
mp_raise_msg(&mp_type_RuntimeError, "Invalid attr");
return mp_const_none;
}
if (!strncmp(attr_str, "_memory", attr_len)) {
return mp_obj_new_bytearray_by_ref(module.w2c_memory.size, (void*)(module.w2c_memory.data));
} else {
mp_raise_msg(&mp_type_RuntimeError, "Unknown attr");
return mp_const_none;
}
}

static MP_DEFINE_CONST_FUN_OBJ_1(getattr_obj, getattr);

// Imported Functions

void w2c_wiring_stopWdt(struct w2c_wiring* wiring) {
Expand Down Expand Up @@ -96,6 +115,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
// Make the function available in the module's namespace
mp_store_global(MP_QSTR_setup, MP_OBJ_FROM_PTR(&setup_obj));
mp_store_global(MP_QSTR_loop, MP_OBJ_FROM_PTR(&loop_obj));
mp_store_global(MP_QSTR___getattr__, MP_OBJ_FROM_PTR(&getattr_obj));

// This must be last, it restores the globals dict
MP_DYNRUNTIME_INIT_EXIT
Expand Down
Binary file modified test/virgil.wasm
Binary file not shown.

0 comments on commit fc91301

Please sign in to comment.