From fde7fb4270c86b5ec9ef313daa23b5ae5d63cf68 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 3 Nov 2021 22:15:46 +0000 Subject: [PATCH] Create a util.js.{h,c} from some common things. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3681 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/ssb.js | 12 +++---- src/file.js.c | 3 +- src/serialize.c | 7 ++-- src/socket.js.c | 7 ++-- src/ssb.js.c | 72 ++++----------------------------------- src/task.c | 85 +++++----------------------------------------- src/task.h | 1 + src/util.js.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ src/util.js.h | 8 +++++ 9 files changed, 129 insertions(+), 155 deletions(-) create mode 100644 src/util.js.c create mode 100644 src/util.js.h diff --git a/core/ssb.js b/core/ssb.js index 22e04942..79e60fc1 100644 --- a/core/ssb.js +++ b/core/ssb.js @@ -52,11 +52,11 @@ var g_database = new Database('core'); async function test_following() { try { - debug_print("I AM", await ssb.whoami()); + print("I AM", await ssb.whoami()); var result = await followingDeep(g_database, [await ssb.whoami()], 1); - debug_print("following ", JSON.stringify(result)); + print("following ", JSON.stringify(result)); } catch (e) { - debug_print("DOH", e, e.stack); + print("DOH", e, e.stack); } } @@ -91,7 +91,7 @@ ssb.registerConnectionsChanged(function(change, connection) { g_wants_requests[connection.id].send_json(out_message); } } else { - debug_print("blobs.get", id); + print("blobs.get", id); var received_bytes = 0; var expected_bytes = message.message[id]; var buffer = new Uint8Array(expected_bytes); @@ -114,10 +114,10 @@ ssb.registerConnectionsChanged(function(change, connection) { } }); } else if (change == 'remove') { - debug_print('REMOVE', connection.id); + print('REMOVE', connection.id); delete g_wants_requests[connection.id]; } else { - debug_print('CHANGE', change); + print('CHANGE', change); } }); diff --git a/src/file.js.c b/src/file.js.c index a1dc7a14..4578bcf6 100644 --- a/src/file.js.c +++ b/src/file.js.c @@ -1,6 +1,7 @@ #include "file.js.h" #include "task.h" +#include "util.js.h" #include #include @@ -177,7 +178,7 @@ static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int a const char* file_name = JS_ToCString(context, argv[0]); size_t size; - uint8_t* buffer = tf_try_get_array_buffer(context, &size, argv[1]); + uint8_t* buffer = tf_util_try_get_array_buffer(context, &size, argv[1]); bool is_array_buffer = false; if (buffer) { diff --git a/src/serialize.c b/src/serialize.c index 9529f7c2..671a2aa9 100644 --- a/src/serialize.c +++ b/src/serialize.c @@ -1,6 +1,7 @@ #include "serialize.h" #include "task.h" #include "taskstub.js.h" +#include "util.js.h" #include #include @@ -195,16 +196,16 @@ static bool _serialize_storeInternal(tf_task_t* task, tf_taskstub_t* to, buffer_ _buffer_append(buffer, result, len); JS_FreeCString(tf_task_get_context(task), result); } - else if ((bytes = tf_try_get_array_buffer(tf_task_get_context(task), &size, value)) != 0) + else if ((bytes = tf_util_try_get_array_buffer(tf_task_get_context(task), &size, value)) != 0) { _serialize_writeInt32(buffer, kArrayBuffer); _serialize_writeInt32(buffer, (int32_t)size); _buffer_append(buffer, bytes, size); } - else if (!JS_IsException((typed = tf_try_get_typed_array_buffer(tf_task_get_context(task), value, &offset, &size, &element_size)))) + else if (!JS_IsException((typed = tf_util_try_get_typed_array_buffer(tf_task_get_context(task), value, &offset, &size, &element_size)))) { size_t total_size; - uint8_t* bytes = tf_try_get_array_buffer(tf_task_get_context(task), &total_size, typed); + uint8_t* bytes = tf_util_try_get_array_buffer(tf_task_get_context(task), &total_size, typed); _serialize_writeInt32(buffer, kArrayBuffer); _serialize_writeInt32(buffer, (int32_t)size); _buffer_append(buffer, bytes, size); diff --git a/src/socket.js.c b/src/socket.js.c index c898e570..2461955e 100644 --- a/src/socket.js.c +++ b/src/socket.js.c @@ -2,6 +2,7 @@ #include "task.h" #include "tls.h" #include "tlscontext.js.h" +#include "util.js.h" #include #include @@ -773,7 +774,7 @@ int _socket_writeBytes(socket_t* socket, promiseid_t promise, int (*callback)(so result = callback(socket, promise, stringValue, length); JS_FreeCString(context, stringValue); } - else if ((array = tf_try_get_array_buffer(context, &length, value)) != 0) + else if ((array = tf_util_try_get_array_buffer(context, &length, value)) != 0) { result = callback(socket, promise, (const char*)array, length); } @@ -781,9 +782,9 @@ int _socket_writeBytes(socket_t* socket, promiseid_t promise, int (*callback)(so { size_t offset; size_t element_size; - JSValue buffer = tf_try_get_typed_array_buffer(context, value, &offset, &length, &element_size); + JSValue buffer = tf_util_try_get_typed_array_buffer(context, value, &offset, &length, &element_size); size_t size; - if ((array = tf_try_get_array_buffer(context, &size, buffer)) != 0) + if ((array = tf_util_try_get_array_buffer(context, &size, buffer)) != 0) { result = callback(socket, promise, (const char*)array, length); } diff --git a/src/ssb.js.c b/src/ssb.js.c index 206183ce..5bf99cea 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -4,6 +4,7 @@ #include "ssb.db.h" #include "ssb.h" #include "task.h" +#include "util.js.h" #include #include @@ -90,7 +91,7 @@ static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int } JS_FreeCString(context, text); } - else if ((blob = tf_try_get_array_buffer(context, &size, argv[0])) != 0) + else if ((blob = tf_util_try_get_array_buffer(context, &size, argv[0])) != 0) { if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id))) { @@ -101,10 +102,10 @@ static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int { size_t offset; size_t element_size; - JSValue buffer = tf_try_get_typed_array_buffer(context, argv[0], &offset, &size, &element_size); + JSValue buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &size, &element_size); if (!JS_IsException(buffer)) { - blob = tf_try_get_array_buffer(context, &size, buffer); + blob = tf_util_try_get_array_buffer(context, &size, buffer); if (blob) { if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id))) @@ -430,7 +431,7 @@ static JSValue _tf_ssb_rpc_send_binary(JSContext* context, JSValueConst this_val JS_FreeValue(context, request_val); size_t size; - uint8_t* message = tf_try_get_array_buffer(context, &size, argv[0]); + uint8_t* message = tf_util_try_get_array_buffer(context, &size, argv[0]); if (message) { tf_ssb_connection_rpc_send( @@ -659,65 +660,6 @@ void tf_ssb_run_file(JSContext* context, const char* file_name) free(source); } -JSValue _print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - for (int i = 0; i < argc; ++i) - { - if (JS_IsNull(argv[i])) - { - printf(" null"); - } - else - { - const char* value = JS_ToCString(context, argv[i]); - printf(" %s", value); - JS_FreeCString(context, value); - } - } - printf("\n"); - return JS_NULL; -} - -static JSValue _utf8Decode(JSContext* context, uint8_t* data, size_t length) -{ - return JS_NewStringLen(context, (const char*)data, length); -} - -static JSValue _utf8_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - JSValue result = JS_NULL; - size_t length; - if (JS_IsString(argv[0])) - { - result = JS_DupValue(context, argv[0]); - } - else - { - uint8_t* array = tf_try_get_array_buffer(context, &length, argv[0]); - if (array) - { - result = _utf8Decode(context, array, length); - } - else - { - size_t offset; - size_t element_size; - JSValue buffer = tf_try_get_typed_array_buffer(context, argv[0], &offset, &length, &element_size); - size_t size; - if (!JS_IsException(buffer)) - { - array = tf_try_get_array_buffer(context, &size, buffer); - if (array) - { - result = _utf8Decode(context, array, size); - } - } - JS_FreeValue(context, buffer); - } - } - return result; -} - void tf_ssb_register(JSContext* context, tf_ssb_t* ssb) { JS_NewClassID(&_tf_ssb_classId); @@ -753,11 +695,9 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb) JS_SetPropertyStr(context, object, "registerBlobWantAdded", JS_NewCFunction(context, _tf_ssb_register_blob_want_added, "registerBlobWantAdded", 1)); JS_SetPropertyStr(context, object, "registerConnectionsChanged", JS_NewCFunction(context, _tf_ssb_register_connections_changed, "registerConnectionsChanged", 1)); - JS_SetPropertyStr(context, global, "debug_print", JS_NewCFunction(context, _print, "debug_print", 2)); - JS_SetPropertyStr(context, global, "debug_utf8Decode", JS_NewCFunction(context, _utf8_decode, "debug_utf8Decode", 1)); - JS_FreeValue(context, global); + tf_util_register(context); tf_database_register(context, tf_ssb_get_db(ssb)); tf_ssb_run_file(context, "core/ssb.js"); } diff --git a/src/task.c b/src/task.c index af0cb8f6..f0ade915 100644 --- a/src/task.c +++ b/src/task.c @@ -11,6 +11,7 @@ #include "taskstub.js.h" #include "tlscontext.js.h" #include "trace.h" +#include "util.js.h" #include #include @@ -127,9 +128,7 @@ static bool _export_record_release(export_record_t* export) } static JSValue _tf_task_version(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); -static JSValue _tf_task_utf8Decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_get_parent(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); -static JSValue _tf_task_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_trace(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); @@ -307,27 +306,6 @@ static const char* _task_loadFile(const char* fileName) return result; } -JSValue _tf_task_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - tf_task_t* task = JS_GetContextOpaque(context); - printf("Task[%p:%s]>", task, task->_scriptName); - for (int i = 0; i < argc; ++i) - { - if (JS_IsNull(argv[i])) - { - printf(" null"); - } - else - { - const char* value = JS_ToCString(task->_context, argv[i]); - printf(" %s", value); - JS_FreeCString(task->_context, value); - } - } - printf("\n"); - return JS_NULL; -} - typedef struct _timeout_t { tf_task_t* _task; JSValue _callback; @@ -865,7 +843,7 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length, JS_FreeValue(to->_context, name_val); JS_FreeValue(to->_context, value); - JSValue utf8 = _tf_task_utf8Decode(to->_context, JS_NULL, 1, &source); + JSValue utf8 = tf_util_utf8_decode(to->_context, source); const char* source_str = JS_ToCString(to->_context, utf8); JS_FreeValue(to->_context, utf8); _tf_task_executeSource(to, source_str, name); @@ -1058,7 +1036,7 @@ JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int const char* name = JS_ToCString(context, argv[0]); JSValue value = JS_GetPropertyStr(context, task->_loadedFiles, name); size_t length; - uint8_t* array = tf_try_get_array_buffer(context, &length, value); + uint8_t* array = tf_util_try_get_array_buffer(context, &length, value); if (array) { char* source = malloc(length + 1); @@ -1099,41 +1077,6 @@ JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int return JS_UNDEFINED; } -static JSValue _tf_task_utf8Decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - JSValue result = JS_NULL; - size_t length; - if (JS_IsString(argv[0])) - { - result = JS_DupValue(context, argv[0]); - } - else - { - uint8_t* array = tf_try_get_array_buffer(context, &length, argv[0]); - if (array) - { - result = JS_NewStringLen(context, (const char*)array, length); - } - else - { - size_t offset; - size_t element_size; - JSValue buffer = tf_try_get_typed_array_buffer(context, argv[0], &offset, &length, &element_size); - size_t size; - if (!JS_IsException(buffer)) - { - array = tf_try_get_array_buffer(context, &size, buffer); - if (array) - { - result = JS_NewStringLen(context, (const char*)array, size); - } - } - JS_FreeValue(context, buffer); - } - } - return result; -} - uv_loop_t* tf_task_get_loop(tf_task_t* task) { return task->_loop; @@ -1380,8 +1323,7 @@ void tf_task_activate(tf_task_t* task) } tf_bcrypt_register(context); - JS_SetPropertyStr(context, global, "utf8Decode", JS_NewCFunction(context, _tf_task_utf8Decode, "utf8Decode", 1)); - JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _tf_task_print, "print", 0)); + tf_util_register(context); JS_SetPropertyStr(context, global, "exit", JS_NewCFunction(context, _tf_task_exit, "exit", 1)); JS_SetPropertyStr(context, global, "version", JS_NewCFunction(context, _tf_task_version, "version", 0)); JS_SetPropertyStr(context, global, "setTimeout", JS_NewCFunction(context, _task_setTimeout, "setTimeout", 2)); @@ -1523,20 +1465,6 @@ void tf_task_send_promise_message(tf_task_t* from, tf_taskstub_t* to, tf_task_me _tf_task_sendPromiseMessage(from, to, type, promise, payload); } -JSValue tf_try_get_typed_array_buffer(JSContext *ctx, JSValueConst obj, size_t *pbyte_offset, size_t *pbyte_length, size_t *pbytes_per_element) -{ - JSValue result = JS_GetTypedArrayBuffer(ctx, obj, pbyte_offset, pbyte_length, pbytes_per_element); - JS_FreeValue(ctx, JS_GetException(ctx)); - return result; -} - -uint8_t *tf_try_get_array_buffer(JSContext *ctx, size_t *psize, JSValueConst obj) -{ - uint8_t* result = JS_GetArrayBuffer(ctx, psize, obj); - JS_FreeValue(ctx, JS_GetException(ctx)); - return result; -} - void tf_task_set_ssb_port(tf_task_t* task, int port) { task->_ssb_port = port; @@ -1561,3 +1489,8 @@ void tf_task_set_secrets_path(tf_task_t* task, const char* secrets_path) { task->_secrets_path = secrets_path; } + +const char* tf_task_get_name(tf_task_t* task) +{ + return task->_scriptName; +} diff --git a/src/task.h b/src/task.h index 92c59721..65e753f4 100644 --- a/src/task.h +++ b/src/task.h @@ -51,6 +51,7 @@ JSValue tf_task_add_import(tf_task_t* task, taskid_t stub_id, exportid_t export_ uv_loop_t* tf_task_get_loop(tf_task_t* task); tf_task_t* tf_task_get(JSContext* context); void tf_task_run_jobs(tf_task_t* task); +const char* tf_task_get_name(tf_task_t* task); promiseid_t tf_task_allocate_promise(tf_task_t* task); void tf_task_reject_promise(tf_task_t* task, promiseid_t promise, JSValue error); diff --git a/src/util.js.c b/src/util.js.c new file mode 100644 index 00000000..aa8e110a --- /dev/null +++ b/src/util.js.c @@ -0,0 +1,89 @@ +#include "util.js.h" + +#include "task.h" + +static JSValue _util_utf8_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) +{ + JSValue result = JS_NULL; + size_t length; + if (JS_IsString(argv[0])) + { + result = JS_DupValue(context, argv[0]); + } + else + { + uint8_t* array = tf_util_try_get_array_buffer(context, &length, argv[0]); + if (array) + { + result = JS_NewStringLen(context, (const char*)array, length); + } + else + { + size_t offset; + size_t element_size; + JSValue buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &length, &element_size); + size_t size; + if (!JS_IsException(buffer)) + { + array = tf_util_try_get_array_buffer(context, &size, buffer); + if (array) + { + result = JS_NewStringLen(context, (const char*)array, size); + } + } + JS_FreeValue(context, buffer); + } + } + return result; +} + +JSValue tf_util_utf8_decode(JSContext* context, JSValue value) +{ + return _util_utf8_decode(context, JS_NULL, 1, &value); +} + +uint8_t* tf_util_try_get_array_buffer(JSContext *ctx, size_t *psize, JSValueConst obj) +{ + uint8_t* result = JS_GetArrayBuffer(ctx, psize, obj); + JS_FreeValue(ctx, JS_GetException(ctx)); + return result; +} + +JSValue tf_util_try_get_typed_array_buffer(JSContext *ctx, JSValueConst obj, size_t *pbyte_offset, size_t *pbyte_length, size_t *pbytes_per_element) +{ + JSValue result = JS_GetTypedArrayBuffer(ctx, obj, pbyte_offset, pbyte_length, pbytes_per_element); + JS_FreeValue(ctx, JS_GetException(ctx)); + return result; +} + +JSValue _util_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) +{ + tf_task_t* task = JS_GetContextOpaque(context); + if (task) + { + printf("Task[%p:%s]>", task, tf_task_get_name(task)); + } + for (int i = 0; i < argc; ++i) + { + if (JS_IsNull(argv[i])) + { + printf(" null"); + } + else + { + const char* value = JS_ToCString(context, argv[i]); + printf(" %s", value); + JS_FreeCString(context, value); + } + } + printf("\n"); + return JS_NULL; +} + +void tf_util_register(JSContext* context) +{ + JSValue global = JS_GetGlobalObject(context); + JS_SetPropertyStr(context, global, "utf8Decode", JS_NewCFunction(context, _util_utf8_decode, "utf8Decode", 1)); + JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1)); + JS_FreeValue(context, global); +} diff --git a/src/util.js.h b/src/util.js.h new file mode 100644 index 00000000..b588fe5e --- /dev/null +++ b/src/util.js.h @@ -0,0 +1,8 @@ +#pragma once + +#include "quickjs.h" + +void tf_util_register(JSContext* context); +JSValue tf_util_utf8_decode(JSContext* context, JSValue value); +uint8_t* tf_util_try_get_array_buffer(JSContext *ctx, size_t *psize, JSValueConst obj); +JSValue tf_util_try_get_typed_array_buffer(JSContext *ctx, JSValueConst obj, size_t *pbyte_offset, size_t *pbyte_length, size_t *pbytes_per_element);