2021-11-03 18:15:46 -04:00
|
|
|
#include "util.js.h"
|
|
|
|
|
|
|
|
#include "task.h"
|
2021-12-27 15:49:07 -05:00
|
|
|
#include "trace.h"
|
2021-11-03 18:15:46 -04:00
|
|
|
|
2021-11-03 18:28:25 -04:00
|
|
|
#include "quickjs-libc.h"
|
|
|
|
|
2021-12-27 15:49:07 -05:00
|
|
|
#include <uv.h>
|
|
|
|
|
2022-06-04 11:59:58 -04:00
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
2021-12-27 15:49:07 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2022-06-03 23:01:12 -04:00
|
|
|
static int64_t s_uv_malloc_size;
|
2022-06-04 11:59:58 -04:00
|
|
|
static int64_t s_tls_malloc_size;
|
2022-06-03 23:01:12 -04:00
|
|
|
|
2022-06-04 11:59:58 -04:00
|
|
|
static void* _tf_alloc(int64_t* total, size_t size)
|
2022-06-03 23:01:12 -04:00
|
|
|
{
|
|
|
|
void* ptr = malloc(size + sizeof(size_t));
|
|
|
|
if (ptr)
|
|
|
|
{
|
2022-06-04 11:59:58 -04:00
|
|
|
__atomic_add_fetch(total, size, __ATOMIC_RELAXED);
|
2022-06-03 23:01:12 -04:00
|
|
|
memcpy(ptr, &size, sizeof(size_t));
|
|
|
|
return (void*)((intptr_t)ptr + sizeof(size_t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-04 11:59:58 -04:00
|
|
|
static void* _tf_realloc(int64_t* total, void* ptr, size_t size)
|
2022-06-03 23:01:12 -04:00
|
|
|
{
|
|
|
|
void* old_ptr = ptr ? (void*)((intptr_t)ptr - sizeof(size_t)) : NULL;
|
|
|
|
size_t old_size = 0;
|
|
|
|
if (old_ptr)
|
|
|
|
{
|
|
|
|
memcpy(&old_size, old_ptr, sizeof(size_t));
|
|
|
|
}
|
|
|
|
void* new_ptr = realloc(old_ptr, size + sizeof(size_t));
|
|
|
|
if (new_ptr)
|
|
|
|
{
|
2022-06-04 11:59:58 -04:00
|
|
|
__atomic_add_fetch(total, (int64_t)size - (int64_t)old_size, __ATOMIC_RELAXED);
|
2022-06-03 23:01:12 -04:00
|
|
|
memcpy(new_ptr, &size, sizeof(size_t));
|
|
|
|
return (void*)((intptr_t)new_ptr + sizeof(size_t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-04 11:59:58 -04:00
|
|
|
__atomic_sub_fetch(total, old_size, __ATOMIC_RELAXED);
|
2022-06-03 23:01:12 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-04 11:59:58 -04:00
|
|
|
static void _tf_free(int64_t* total, void* ptr)
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
void* old_ptr = (void*)((intptr_t)ptr - sizeof(size_t));
|
|
|
|
size_t size = 0;
|
|
|
|
memcpy(&size, old_ptr, sizeof(size_t));
|
|
|
|
__atomic_sub_fetch(total, size, __ATOMIC_RELAXED);
|
|
|
|
free(old_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* _tf_uv_alloc(size_t size)
|
|
|
|
{
|
|
|
|
return _tf_alloc(&s_uv_malloc_size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* _tf_uv_realloc(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
return _tf_realloc(&s_uv_malloc_size, ptr, size);
|
|
|
|
}
|
|
|
|
|
2022-06-03 23:01:12 -04:00
|
|
|
static void* _tf_uv_calloc(size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
void* ptr = calloc(1, nmemb * size + sizeof(size_t));
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
size_t total_size = nmemb * size;
|
|
|
|
__atomic_add_fetch(&s_uv_malloc_size, total_size, __ATOMIC_RELAXED);
|
|
|
|
memcpy(ptr, &total_size, sizeof(size_t));
|
|
|
|
return (void*)((intptr_t)ptr + sizeof(size_t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_uv_free(void* ptr)
|
|
|
|
{
|
2022-06-04 11:59:58 -04:00
|
|
|
_tf_free(&s_uv_malloc_size, ptr);
|
2022-06-03 23:01:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tf_util_replace_uv_allocator()
|
|
|
|
{
|
|
|
|
uv_replace_allocator(_tf_uv_alloc, _tf_uv_realloc, _tf_uv_calloc, _tf_uv_free);
|
|
|
|
}
|
|
|
|
|
2022-06-04 11:59:58 -04:00
|
|
|
size_t tf_util_get_uv_malloc_size()
|
2022-06-03 23:01:12 -04:00
|
|
|
{
|
|
|
|
return s_uv_malloc_size;
|
|
|
|
}
|
|
|
|
|
2022-06-04 11:59:58 -04:00
|
|
|
void* _tf_tls_alloc(size_t size, const char* file, int line)
|
|
|
|
{
|
|
|
|
return _tf_alloc(&s_tls_malloc_size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* _tf_tls_realloc(void* ptr, size_t size, const char* file, int line)
|
|
|
|
{
|
|
|
|
return _tf_realloc(&s_tls_malloc_size, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _tf_tls_free(void* ptr, const char* file, int line)
|
|
|
|
{
|
|
|
|
_tf_free(&s_tls_malloc_size, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_util_replace_tls_allocator()
|
|
|
|
{
|
|
|
|
CRYPTO_set_mem_functions(_tf_tls_alloc, _tf_tls_realloc, _tf_tls_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t tf_util_get_tls_malloc_size()
|
|
|
|
{
|
|
|
|
return s_tls_malloc_size;
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:15:54 -05:00
|
|
|
static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
size_t length = 0;
|
|
|
|
const char* value = JS_ToCStringLen(context, &length, argv[0]);
|
|
|
|
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (const uint8_t*)value, length);
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
JSValue typedArray = JS_CallConstructor(context, constructor, 1, &arrayBuffer);
|
|
|
|
JS_FreeValue(context, constructor);
|
|
|
|
JS_FreeValue(context, arrayBuffer);
|
|
|
|
JS_FreeCString(context, value);
|
|
|
|
return typedArray;
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:15:46 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:28:25 -04:00
|
|
|
uint8_t* tf_util_try_get_array_buffer(JSContext* context, size_t* psize, JSValueConst obj)
|
2021-11-03 18:15:46 -04:00
|
|
|
{
|
2021-11-03 18:28:25 -04:00
|
|
|
uint8_t* result = JS_GetArrayBuffer(context, psize, obj);
|
|
|
|
JS_FreeValue(context, JS_GetException(context));
|
2021-11-03 18:15:46 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:28:25 -04:00
|
|
|
JSValue tf_util_try_get_typed_array_buffer(JSContext* context, JSValueConst obj, size_t* pbyte_offset, size_t* pbyte_length, size_t* pbytes_per_element)
|
2021-11-03 18:15:46 -04:00
|
|
|
{
|
2021-11-03 18:28:25 -04:00
|
|
|
JSValue result = JS_GetTypedArrayBuffer(context, obj, pbyte_offset, pbyte_length, pbytes_per_element);
|
|
|
|
JS_FreeValue(context, JS_GetException(context));
|
2021-11-03 18:15:46 -04:00
|
|
|
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));
|
2022-02-13 17:39:22 -05:00
|
|
|
tf_task_print(task, argc, argv);
|
2021-11-03 18:15:46 -04:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-27 16:48:16 -05:00
|
|
|
bool tf_util_report_error(JSContext* context, JSValue value)
|
2021-11-03 18:28:25 -04:00
|
|
|
{
|
2021-12-27 16:48:16 -05:00
|
|
|
bool is_error = false;
|
2021-11-03 18:28:25 -04:00
|
|
|
if (JS_IsError(context, value))
|
|
|
|
{
|
|
|
|
const char* string = JS_ToCString(context, value);
|
|
|
|
printf("ERROR: %s\n", string);
|
|
|
|
JS_FreeCString(context, string);
|
|
|
|
|
|
|
|
JSValue stack = JS_GetPropertyStr(context, value, "stack");
|
|
|
|
if (!JS_IsUndefined(stack))
|
|
|
|
{
|
|
|
|
const char* stack_str = JS_ToCString(context, stack);
|
|
|
|
printf("%s\n", stack_str);
|
|
|
|
JS_FreeCString(context, stack_str);
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, stack);
|
|
|
|
|
|
|
|
tf_task_t* task = tf_task_get(context);
|
|
|
|
if (task)
|
|
|
|
{
|
|
|
|
tf_task_send_error_to_parent(task, value);
|
|
|
|
}
|
2021-12-27 16:48:16 -05:00
|
|
|
is_error = true;
|
2021-11-03 18:28:25 -04:00
|
|
|
}
|
|
|
|
else if (JS_IsException(value))
|
|
|
|
{
|
|
|
|
tf_task_t* task = tf_task_get(context);
|
|
|
|
if (task)
|
|
|
|
{
|
2022-04-17 20:24:00 -04:00
|
|
|
tf_task_send_error_to_parent(task, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
js_std_dump_error(context);
|
2021-11-03 18:28:25 -04:00
|
|
|
}
|
2021-12-27 16:48:16 -05:00
|
|
|
is_error = true;
|
2021-11-03 18:28:25 -04:00
|
|
|
}
|
2021-12-27 16:48:16 -05:00
|
|
|
return is_error;
|
2021-11-03 18:28:25 -04:00
|
|
|
}
|
|
|
|
|
2021-12-27 15:49:07 -05:00
|
|
|
typedef struct _timeout_t {
|
|
|
|
tf_task_t* _task;
|
|
|
|
JSValue _callback;
|
|
|
|
} timeout_t;
|
|
|
|
|
|
|
|
static void _handle_closed(uv_handle_t* handle)
|
|
|
|
{
|
|
|
|
free(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _util_timeoutCallback(uv_timer_t* handle)
|
|
|
|
{
|
|
|
|
timeout_t* timeout = handle->data;
|
|
|
|
tf_trace_begin(tf_task_get_trace(timeout->_task), "_util_timeoutCallback");
|
|
|
|
JSContext* context = tf_task_get_context(timeout->_task);
|
|
|
|
JSValue result = JS_Call(
|
|
|
|
context,
|
|
|
|
timeout->_callback,
|
|
|
|
JS_NULL,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
tf_util_report_error(context, result);
|
|
|
|
JS_FreeValue(context, result);
|
|
|
|
tf_trace_end(tf_task_get_trace(timeout->_task));
|
|
|
|
free(timeout);
|
|
|
|
uv_close((uv_handle_t*)handle, _handle_closed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _util_setTimeout(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_task_t* task = JS_GetContextOpaque(context);
|
|
|
|
|
|
|
|
timeout_t* timeout = malloc(sizeof(timeout_t));
|
|
|
|
*timeout = (timeout_t)
|
|
|
|
{
|
|
|
|
._task = task,
|
|
|
|
._callback = JS_DupValue(context, argv[0]),
|
|
|
|
};
|
|
|
|
|
|
|
|
uv_timer_t* timer = malloc(sizeof(uv_timer_t));
|
|
|
|
memset(timer, 0, sizeof(uv_timer_t));
|
|
|
|
uv_timer_init(tf_task_get_loop(task), timer);
|
|
|
|
timer->data = timeout;
|
|
|
|
|
|
|
|
int64_t duration;
|
|
|
|
JS_ToInt64(context, &duration, argv[1]);
|
|
|
|
uv_timer_start(timer, _util_timeoutCallback, duration, 0);
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:15:46 -04:00
|
|
|
void tf_util_register(JSContext* context)
|
|
|
|
{
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JS_SetPropertyStr(context, global, "utf8Decode", JS_NewCFunction(context, _util_utf8_decode, "utf8Decode", 1));
|
2022-01-26 20:15:54 -05:00
|
|
|
JS_SetPropertyStr(context, global, "utf8Encode", JS_NewCFunction(context, _util_utf8_encode, "utf8Encode", 1));
|
2021-11-03 18:15:46 -04:00
|
|
|
JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1));
|
2021-12-27 15:49:07 -05:00
|
|
|
JS_SetPropertyStr(context, global, "setTimeout", JS_NewCFunction(context, _util_setTimeout, "setTimeout", 2));
|
2021-11-03 18:15:46 -04:00
|
|
|
JS_FreeValue(context, global);
|
|
|
|
}
|