Skip to content

Commit

Permalink
JS: Add storage copy() move() mkdir()
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Apr 5, 2024
1 parent 309889e commit 5f80b90
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
3 changes: 3 additions & 0 deletions applications/system/js_app/examples/apps/Scripts/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ storage.remove(path);
print("Done")

// There's also:
// storage.copy(old_path, new_path);
// storage.move(old_path, new_path);
// storage.mkdir(path);
// storage.virtualInit(path);
// storage.virtualMount();
// storage.virtualQuit();
65 changes: 57 additions & 8 deletions applications/system/js_app/modules/js_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ static bool check_arg_count(struct mjs* mjs, size_t count) {
return true;
}

static bool get_path_arg(struct mjs* mjs, const char** path) {
mjs_val_t path_obj = mjs_arg(mjs, 0);
static bool get_path_arg(struct mjs* mjs, const char** path, size_t index) {
mjs_val_t path_obj = mjs_arg(mjs, index);
if(!mjs_is_string(path_obj)) {
ret_bad_args(mjs, "Path must be a string");
return false;
Expand All @@ -51,7 +51,7 @@ static void js_storage_read(struct mjs* mjs) {
JsStorageInst* storage = get_this_ctx(mjs);

const char* path;
if(!get_path_arg(mjs, &path)) return;
if(!get_path_arg(mjs, &path, 0)) return;

File* file = storage_file_alloc(storage->api);
do {
Expand Down Expand Up @@ -93,7 +93,7 @@ static void js_storage_write(struct mjs* mjs) {
JsStorageInst* storage = get_this_ctx(mjs);

const char* path;
if(!get_path_arg(mjs, &path)) return;
if(!get_path_arg(mjs, &path, 0)) return;

mjs_val_t data_arg = mjs_arg(mjs, 1);
if(!mjs_is_typed_array(data_arg) && !mjs_is_string(data_arg)) {
Expand Down Expand Up @@ -137,7 +137,7 @@ static void js_storage_append(struct mjs* mjs) {
if(!check_arg_count(mjs, 2)) return;

const char* path;
if(!get_path_arg(mjs, &path)) return;
if(!get_path_arg(mjs, &path, 0)) return;

mjs_val_t data_arg = mjs_arg(mjs, 1);
if(!mjs_is_typed_array(data_arg) && !mjs_is_string(data_arg)) {
Expand Down Expand Up @@ -170,7 +170,7 @@ static void js_storage_exists(struct mjs* mjs) {
if(!check_arg_count(mjs, 1)) return;

const char* path;
if(!get_path_arg(mjs, &path)) return;
if(!get_path_arg(mjs, &path, 0)) return;

mjs_return(mjs, mjs_mk_boolean(mjs, storage_common_exists(storage->api, path)));
}
Expand All @@ -180,17 +180,63 @@ static void js_storage_remove(struct mjs* mjs) {
if(!check_arg_count(mjs, 1)) return;

const char* path;
if(!get_path_arg(mjs, &path)) return;
if(!get_path_arg(mjs, &path, 0)) return;

mjs_return(mjs, mjs_mk_boolean(mjs, storage_simply_remove(storage->api, path)));
}

static void js_storage_copy(struct mjs* mjs) {
JsStorageInst* storage = get_this_ctx(mjs);
if(!check_arg_count(mjs, 2)) return;

const char* old_path;
if(!get_path_arg(mjs, &old_path, 0)) return;

const char* new_path;
if(!get_path_arg(mjs, &new_path, 1)) return;

FS_Error error = storage_common_copy(storage->api, old_path, new_path);
if(error == FSE_OK) {
mjs_return(mjs, MJS_UNDEFINED);
} else {
ret_int_err(mjs, storage_error_get_desc(error));
}
}

static void js_storage_move(struct mjs* mjs) {
JsStorageInst* storage = get_this_ctx(mjs);
if(!check_arg_count(mjs, 2)) return;

const char* old_path;
if(!get_path_arg(mjs, &old_path, 0)) return;

const char* new_path;
if(!get_path_arg(mjs, &new_path, 1)) return;

FS_Error error = storage_common_rename(storage->api, old_path, new_path);
if(error == FSE_OK) {
mjs_return(mjs, MJS_UNDEFINED);
} else {
ret_int_err(mjs, storage_error_get_desc(error));
}
}

static void js_storage_mkdir(struct mjs* mjs) {
JsStorageInst* storage = get_this_ctx(mjs);
if(!check_arg_count(mjs, 1)) return;

const char* path;
if(!get_path_arg(mjs, &path, 0)) return;

mjs_return(mjs, mjs_mk_boolean(mjs, storage_simply_mkdir(storage->api, path)));
}

static void js_storage_virtual_init(struct mjs* mjs) {
JsStorageInst* storage = get_this_ctx(mjs);
if(!check_arg_count(mjs, 1)) return;

const char* path;
if(!get_path_arg(mjs, &path)) return;
if(!get_path_arg(mjs, &path, 0)) return;

if(storage->virtual) {
ret_int_err(mjs, "Virtual already setup");
Expand Down Expand Up @@ -259,6 +305,9 @@ static void* js_storage_create(struct mjs* mjs, mjs_val_t* object) {
mjs_set(mjs, storage_obj, "append", ~0, MJS_MK_FN(js_storage_append));
mjs_set(mjs, storage_obj, "exists", ~0, MJS_MK_FN(js_storage_exists));
mjs_set(mjs, storage_obj, "remove", ~0, MJS_MK_FN(js_storage_remove));
mjs_set(mjs, storage_obj, "copy", ~0, MJS_MK_FN(js_storage_copy));
mjs_set(mjs, storage_obj, "move", ~0, MJS_MK_FN(js_storage_move));
mjs_set(mjs, storage_obj, "mkdir", ~0, MJS_MK_FN(js_storage_mkdir));
mjs_set(mjs, storage_obj, "virtualInit", ~0, MJS_MK_FN(js_storage_virtual_init));
mjs_set(mjs, storage_obj, "virtualMount", ~0, MJS_MK_FN(js_storage_virtual_mount));
mjs_set(mjs, storage_obj, "virtualQuit", ~0, MJS_MK_FN(js_storage_virtual_quit));
Expand Down

0 comments on commit 5f80b90

Please sign in to comment.