webassembly: Add support for VFS and enable VFS_POSIX.

This gets filesystem support working.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-10-28 12:58:05 +11:00
parent 54478eb9e7
commit 46bb52adf6
4 changed files with 53 additions and 1 deletions

View File

@ -37,7 +37,10 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
JSFLAGS += -s ASYNCIFY
JSFLAGS += -s EXPORTED_FUNCTIONS="['_mp_js_init', '_mp_js_init_repl', '_mp_js_do_str', '_mp_js_process_char', '_mp_hal_get_interrupt_char', '_mp_sched_keyboard_interrupt']" -s EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" -s --memory-init-file 0 --js-library library.js
JSFLAGS += -s EXPORTED_FUNCTIONS="['_mp_js_init', '_mp_js_init_repl', '_mp_js_do_str', '_mp_js_process_char', '_mp_hal_get_interrupt_char', '_mp_sched_keyboard_interrupt']"
JSFLAGS += -s EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap', 'FS']"
JSFLAGS += -s --memory-init-file 0
JSFLAGS += --js-library library.js
all: $(BUILD)/micropython.js

View File

@ -35,6 +35,8 @@
#include "py/repl.h"
#include "py/gc.h"
#include "py/mperrno.h"
#include "extmod/vfs.h"
#include "extmod/vfs_posix.h"
#include "shared/runtime/pyexec.h"
#include "emscripten.h"
@ -92,6 +94,18 @@ void mp_js_init(int heap_size) {
#endif
mp_init();
#if MICROPY_VFS_POSIX
{
// Mount the host FS at the root of our internal VFS
mp_obj_t args[2] = {
MP_OBJ_TYPE_GET_SLOT(&mp_type_vfs_posix, make_new)(&mp_type_vfs_posix, 0, 0, NULL),
MP_OBJ_NEW_QSTR(qstr_from_str("/")),
};
mp_vfs_mount(2, args, (mp_map_t *)&mp_const_empty_map);
MP_STATE_VM(vfs_cur) = MP_STATE_VM(vfs_mount_table);
}
#endif
}
void mp_js_init_repl() {
@ -109,6 +123,7 @@ void gc_collect(void) {
gc_collect_end();
}
#if !MICROPY_VFS
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
mp_raise_OSError(MP_ENOENT);
}
@ -121,6 +136,7 @@ mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs)
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
#endif
void nlr_jump_fail(void *val) {
while (1) {

View File

@ -37,6 +37,7 @@
#define MICROPY_ENABLE_COMPILER (1)
#define MICROPY_ALLOC_PATH_MAX (256)
#define MICROPY_READER_VFS (MICROPY_VFS)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_ENABLE_PYSTACK (1)
#define MICROPY_STACK_CHECK (0)
@ -48,6 +49,10 @@
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
#define MICROPY_USE_INTERNAL_ERRNO (1)
#define MICROPY_USE_INTERNAL_PRINTF (0)
#ifndef MICROPY_VFS
#define MICROPY_VFS (1)
#endif
#define MICROPY_VFS_POSIX (MICROPY_VFS)
#define MICROPY_PY_UTIME_MP_HAL (1)
#define MICROPY_PY_SYS_PLATFORM "webassembly"
#define MICROPY_PY_SYS_STDFILES (0)
@ -86,3 +91,8 @@ typedef long mp_off_t;
#define MICROPY_HW_MCU_NAME "Emscripten"
#define MP_STATE_PORT MP_STATE_VM
#if MICROPY_VFS
// _GNU_SOURCE must be defined to get definitions of DT_xxx symbols from dirent.h.
#define _GNU_SOURCE
#endif

View File

@ -37,3 +37,26 @@ mp_uint_t mp_hal_ticks_us(void);
mp_uint_t mp_hal_ticks_cpu(void);
int mp_hal_get_interrupt_char(void);
#if MICROPY_VFS_POSIX
#include <errno.h>
// This macro is used to implement PEP 475 to retry specified syscalls on EINTR
#define MP_HAL_RETRY_SYSCALL(ret, syscall, raise) \
{ \
for (;;) { \
ret = syscall; \
if (ret == -1) { \
int err = errno; \
if (err == EINTR) { \
mp_handle_pending(true); \
continue; \
} \
raise; \
} \
break; \
} \
}
#endif