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
This commit is contained in:
2021-11-03 22:15:46 +00:00
parent 03a2367532
commit fde7fb4270
9 changed files with 129 additions and 155 deletions

View File

@ -11,6 +11,7 @@
#include "taskstub.js.h"
#include "tlscontext.js.h"
#include "trace.h"
#include "util.js.h"
#include <assert.h>
#include <string.h>
@ -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;
}