diff --git a/src/file.js.c b/src/file.js.c index e16708a1..984859a1 100644 --- a/src/file.js.c +++ b/src/file.js.c @@ -17,15 +17,10 @@ #include #endif -static JSValue _file_make_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); -static JSValue _file_remove_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _file_read_file_zip(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); -static JSValue _file_rename_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _file_stat(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _file_stat_zip(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); -static JSValue _file_unlink_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); -static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static double _time_spec_to_double(const uv_timespec_t* time_spec); static void _file_on_stat_complete(uv_fs_t* request); @@ -52,11 +47,6 @@ void tf_file_register(JSContext* context) const char* zip = tf_task_get_zip_path(task); JS_SetPropertyStr(context, global, "File", file); JS_SetPropertyStr(context, file, "readFile", JS_NewCFunction(context, zip ? _file_read_file_zip : _file_read_file, "readFile", 1)); - JS_SetPropertyStr(context, file, "writeFile", JS_NewCFunction(context, _file_write_file, "writeFile", 2)); - JS_SetPropertyStr(context, file, "makeDirectory", JS_NewCFunction(context, _file_make_directory, "makeDirectory", 1)); - JS_SetPropertyStr(context, file, "removeDirectory", JS_NewCFunction(context, _file_remove_directory, "removeDirectory", 1)); - JS_SetPropertyStr(context, file, "unlinkFile", JS_NewCFunction(context, _file_unlink_file, "unlinkFile", 1)); - JS_SetPropertyStr(context, file, "renameFile", JS_NewCFunction(context, _file_rename_file, "renameFile", 2)); JS_SetPropertyStr(context, file, "stat", JS_NewCFunction(context, zip ? _file_stat_zip : _file_stat, "stat", 1)); JS_FreeValue(context, global); } @@ -258,204 +248,6 @@ static JSValue _file_read_file_zip(JSContext* context, JSValueConst this_val, in return promise_value; } -static void _file_write_write_callback(uv_fs_t* req) -{ - uv_fs_req_cleanup(req); - fs_req_t* fsreq = (fs_req_t*)req; - tf_task_t* task = req->loop->data; - JSContext* context = tf_task_get_context(task); - promiseid_t promise = (promiseid_t)(intptr_t)req->data; - if (req->result >= 0) - { - tf_task_resolve_promise(task, promise, JS_NewInt64(context, req->result)); - } - else - { - tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(req->result))); - } - int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback); - if (result < 0) - { - uv_fs_req_cleanup(req); - tf_free(fsreq); - } -} - -static void _file_write_open_callback(uv_fs_t* req) -{ - fs_req_t* fsreq = (fs_req_t*)req; - uv_fs_req_cleanup(req); - tf_task_t* task = req->loop->data; - JSContext* context = tf_task_get_context(task); - promiseid_t promise = (promiseid_t)(intptr_t)req->data; - if (req->result >= 0) - { - uv_buf_t buf = { .base = fsreq->buffer, .len = fsreq->size }; - fsreq->file = req->result; - int result = uv_fs_write(req->loop, req, fsreq->file, &buf, 1, 0, _file_write_write_callback); - if (result < 0) - { - tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(result))); - result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback); - if (result < 0) - { - uv_fs_req_cleanup(req); - tf_free(fsreq); - } - } - } - else - { - tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(req->result))); - uv_fs_req_cleanup(req); - tf_free(req); - } -} - -static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - void* task = JS_GetContextOpaque(context); - const char* file_name = JS_ToCString(context, argv[0]); - - size_t size; - uint8_t* buffer = tf_util_try_get_array_buffer(context, &size, argv[1]); - bool is_array_buffer = false; - if (buffer) - { - is_array_buffer = true; - } - else - { - buffer = (uint8_t*)JS_ToCStringLen(context, &size, argv[1]); - } - - promiseid_t promise = -1; - JSValue promise_value = tf_task_allocate_promise(task, &promise); - fs_req_t* req = tf_malloc(sizeof(fs_req_t) + size); - *req = (fs_req_t) - { - .fs = - { - .data = (void*)(intptr_t)promise, - }, - .size = size, - }; - memcpy(req->buffer, buffer, size); - if (!is_array_buffer) - { - JS_FreeCString(context, (const char*)buffer); - } - - int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_CREAT | UV_FS_O_WRONLY, 0644, _file_write_open_callback); - if (result < 0) - { - tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(result))); - } - JS_FreeCString(context, file_name); - return promise_value; -} - -static void _file_async_callback(uv_fs_t* req) -{ - uv_fs_req_cleanup(req); - tf_task_t* task = req->loop->data; - JSContext* context = tf_task_get_context(task); - promiseid_t promise = (promiseid_t)(intptr_t)req->data; - if (req->result == 0) - { - tf_task_resolve_promise(task, promise, JS_NewInt32(context, req->result)); - } - else - { - tf_task_reject_promise(task, promise, JS_NewInt32(context, req->result)); - } - tf_free(req); -} - -static JSValue _file_rename_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - void* task = JS_GetContextOpaque(context); - const char* old_name = JS_ToCString(context, argv[0]); - const char* new_name = JS_ToCString(context, argv[1]); - promiseid_t promise = -1; - JSValue promise_value = tf_task_allocate_promise(task, &promise); - uv_fs_t* req = tf_malloc(sizeof(uv_fs_t)); - *req = (uv_fs_t) - { - .data = (void*)(intptr_t)promise, - }; - int result = uv_fs_rename(tf_task_get_loop(task), req, old_name, new_name,_file_async_callback); - JS_FreeCString(context, old_name); - JS_FreeCString(context, new_name); - if (result < 0) - { - tf_task_reject_promise(task, promise, JS_NewInt32(context, result)); - } - return promise_value; -} - -static JSValue _file_unlink_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - void* task = JS_GetContextOpaque(context); - const char* file_name = JS_ToCString(context, argv[0]); - promiseid_t promise = -1; - JSValue promise_value = tf_task_allocate_promise(task, &promise); - uv_fs_t* req = tf_malloc(sizeof(uv_fs_t)); - *req = (uv_fs_t) - { - .data = (void*)(intptr_t)promise, - }; - int result = uv_fs_unlink(tf_task_get_loop(task), req, file_name, _file_async_callback); - JS_FreeCString(context, file_name); - if (result < 0) - { - tf_task_reject_promise(task, promise, JS_NewInt32(context, result)); - } - return promise_value; -} - -JSValue _file_make_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - void* task = JS_GetContextOpaque(context); - const char* directory = JS_ToCString(context, argv[0]); - - promiseid_t promise = -1; - JSValue promise_value = tf_task_allocate_promise(task, &promise); - uv_fs_t* req = tf_malloc(sizeof(uv_fs_t)); - *req = (uv_fs_t) - { - .data = (void*)(intptr_t)promise, - }; - int result = uv_fs_mkdir(tf_task_get_loop(task), req, directory, 0755, _file_async_callback); - JS_FreeCString(context, directory); - if (result < 0) - { - tf_task_reject_promise(task, promise, JS_NewInt32(context, result)); - } - return promise_value; -} - -JSValue _file_remove_directory(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - void* task = JS_GetContextOpaque(context); - const char* directory = JS_ToCString(context, argv[0]); - - promiseid_t promise = -1; - JSValue promise_value = tf_task_allocate_promise(task, &promise); - uv_fs_t* req = tf_malloc(sizeof(uv_fs_t)); - *req = (uv_fs_t) - { - .data = (void*)(intptr_t)promise, - }; - int result = uv_fs_rmdir(tf_task_get_loop(task), req, directory, _file_async_callback); - JS_FreeCString(context, directory); - if (result < 0) - { - tf_task_reject_promise(task, promise, JS_NewInt32(context, result)); - } - return promise_value; -} - JSValue _file_stat(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { void* task = JS_GetContextOpaque(context); diff --git a/src/tests.c b/src/tests.c index 98e7b4d7..5eeb962f 100644 --- a/src/tests.c +++ b/src/tests.c @@ -542,36 +542,6 @@ static void _test_file(const tf_test_options_t* options) " exit(1);\n" "}).catch(function(error) {\n" " print('expected error', error);\n" - "});\n" - "File.unlinkFile('out/test/new.txt').finally(function() {\n" - " return File.removeDirectory('out/test');\n" - "}).finally(function() {\n" - " return File.makeDirectory('out/test').then(function() {\n" - " return File.writeFile('out/test/new.txt', 'hello').then(function(result) {\n" - " return File.readFile('out/test/new.txt').then(function(data) {\n" - " if (utf8Decode(data) != 'hello') {\n" - " print('READ', utf8Decode(data));\n" - " exit(1);\n" - " }\n" - " }).catch(function(error) {\n" - " print('unexpected read error', error);\n" - " exit(1);\n" - " });\n" - " }).catch(function(error) {\n" - " print('unexpected write error', error);\n" - " exit(1);\n" - " });\n" - " }).catch(function(error) {\n" - " print('unexpected make directory error', error);\n" - " exit(1);\n" - " });\n" - "}).finally(function() {\n" - " return File.renameFile('out/test/new.txt', 'out/test/renamed.txt').catch(x => print(x)).finally(function() {\n" - " return File.unlinkFile('out/test/renamed.txt').catch(x => print(x)).finally(function() {\n" - " print('removing directory');\n" - " return File.removeDirectory('out/test').catch(x => print(x));\n" - " });\n" - " });\n" "});\n"); fclose(file);