2021-01-02 13:10:00 -05:00
|
|
|
#include "serialize.h"
|
2022-06-04 13:04:51 -04:00
|
|
|
|
|
|
|
#include "mem.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
#include "task.h"
|
2021-10-24 11:46:30 -04:00
|
|
|
#include "taskstub.js.h"
|
2023-03-12 18:16:18 -04:00
|
|
|
#include "trace.h"
|
2021-11-03 18:15:46 -04:00
|
|
|
#include "util.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "quickjs-libc.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
typedef enum _serialize_type_t {
|
|
|
|
kUndefined,
|
|
|
|
kNull,
|
|
|
|
kUninitialized,
|
|
|
|
kBoolean,
|
|
|
|
kInt32,
|
|
|
|
kInt64,
|
|
|
|
kNumber,
|
|
|
|
kString,
|
|
|
|
kArray,
|
|
|
|
kArrayBuffer,
|
|
|
|
kObject,
|
|
|
|
kFunction,
|
|
|
|
kError,
|
|
|
|
kException,
|
|
|
|
} serialize_type_t;
|
|
|
|
|
|
|
|
typedef struct _buffer_t {
|
|
|
|
char* data;
|
|
|
|
size_t size;
|
|
|
|
size_t capacity;
|
|
|
|
} buffer_t;
|
|
|
|
|
|
|
|
static bool _serialize_store(tf_task_t* task, tf_taskstub_t* to, buffer_t* buffer, JSValue value);
|
|
|
|
static JSValue _serialize_load(tf_task_t* task, tf_taskstub_t* from, const char* buffer, size_t size);
|
|
|
|
|
|
|
|
static bool _serialize_storeInternal(tf_task_t* task, tf_taskstub_t* to, buffer_t* buffer, JSValue value, int depth);
|
|
|
|
static JSValue _serialize_loadInternal(tf_task_t* task, tf_taskstub_t* from, const char** buffer, size_t* size, int depth);
|
|
|
|
|
|
|
|
static void _serialize_writeInt8(buffer_t* buffer, int8_t value);
|
|
|
|
static void _serialize_writeInt32(buffer_t* buffer, int32_t value);
|
|
|
|
static void _serialize_writeInt64(buffer_t* buffer, int64_t value);
|
|
|
|
static void _serialize_writeDouble(buffer_t* buffer, double value);
|
|
|
|
|
|
|
|
static int8_t _serialize_readInt8(const char** buffer, size_t* size);
|
|
|
|
static int32_t _serialize_readInt32(const char** buffer, size_t* size);
|
|
|
|
static int64_t _serialize_readInt64(const char** buffer, size_t* size);
|
|
|
|
static double _serialize_readDouble(const char** buffer, size_t* size);
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_serialize_store(tf_task_t* task, tf_taskstub_t* to, void** out_buffer, size_t* out_size, JSValue value)
|
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
tf_trace_t* trace = tf_task_get_trace(task);
|
|
|
|
tf_trace_begin(trace, "tf_serialize_store");
|
2021-01-02 13:10:00 -05:00
|
|
|
buffer_t tmp = { 0 };
|
|
|
|
_serialize_store(task, to, &tmp, value);
|
2022-06-04 13:04:51 -04:00
|
|
|
tmp.data = tf_realloc(tmp.data, tmp.size);
|
2021-01-02 13:10:00 -05:00
|
|
|
*out_buffer = tmp.data;
|
|
|
|
*out_size = tmp.size;
|
2023-03-12 18:16:18 -04:00
|
|
|
tf_trace_end(trace);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue tf_serialize_load(tf_task_t* task, tf_taskstub_t* from, const char* buffer, size_t size)
|
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
tf_trace_t* trace = tf_task_get_trace(task);
|
|
|
|
tf_trace_begin(trace, "tf_serialize_load");
|
|
|
|
JSValue result = _serialize_load(task, from, buffer, size);
|
|
|
|
tf_trace_end(trace);
|
|
|
|
return result;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _buffer_append(buffer_t* buffer, const void* data, size_t size)
|
|
|
|
{
|
|
|
|
if (buffer->capacity < buffer->size + size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t new_capacity = (size + buffer->capacity) * 2;
|
2022-06-04 13:04:51 -04:00
|
|
|
buffer->data = tf_realloc(buffer->data, new_capacity);
|
2021-01-02 13:10:00 -05:00
|
|
|
buffer->capacity = new_capacity;
|
|
|
|
}
|
|
|
|
memcpy((char*)buffer->data + buffer->size, data, size);
|
|
|
|
buffer->size += size;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _serialize_writeInt8(buffer_t* buffer, int8_t value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_buffer_append(buffer, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _serialize_writeInt32(buffer_t* buffer, int32_t value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_buffer_append(buffer, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _serialize_writeInt64(buffer_t* buffer, int64_t value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_buffer_append(buffer, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _serialize_writeDouble(buffer_t* buffer, double value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_buffer_append(buffer, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _serialize_read(const char** buffer, size_t* size, void* target, size_t target_size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
assert(*size >= target_size);
|
|
|
|
memcpy(target, *buffer, target_size);
|
|
|
|
*buffer += target_size;
|
|
|
|
*size -= target_size;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static int8_t _serialize_readInt8(const char** buffer, size_t* size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int8_t result;
|
|
|
|
_serialize_read(buffer, size, &result, sizeof(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int32_t _serialize_readInt32(const char** buffer, size_t* size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int32_t result;
|
|
|
|
_serialize_read(buffer, size, &result, sizeof(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int64_t _serialize_readInt64(const char** buffer, size_t* size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int64_t result;
|
|
|
|
_serialize_read(buffer, size, &result, sizeof(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
double _serialize_readDouble(const char** buffer, size_t* size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
double result;
|
|
|
|
_serialize_read(buffer, size, &result, sizeof(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static bool _serialize_store(tf_task_t* task, tf_taskstub_t* to, buffer_t* buffer, JSValue value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return _serialize_storeInternal(task, to, buffer, value, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool _serialize_storeInternal(tf_task_t* task, tf_taskstub_t* to, buffer_t* buffer, JSValue value, int depth)
|
|
|
|
{
|
|
|
|
JSContext* context = tf_task_get_context(task);
|
|
|
|
size_t size;
|
|
|
|
size_t offset;
|
|
|
|
size_t element_size;
|
|
|
|
JSValue typed;
|
|
|
|
uint8_t* bytes;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsUndefined(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kUndefined);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (JS_IsUninitialized(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kUninitialized);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (JS_IsNull(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kNull);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (JS_IsBool(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kBoolean);
|
2023-03-12 18:16:18 -04:00
|
|
|
_serialize_writeInt8(buffer, JS_ToBool(context, value) ? 1 : 0);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (JS_IsNumber(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int64_t result = 0;
|
2023-03-12 18:16:18 -04:00
|
|
|
if (JS_ToInt64(context, &result, value) == 0)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
|
|
|
_serialize_writeInt32(buffer, kInt64);
|
|
|
|
_serialize_writeInt64(buffer, result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to store integer.\n");
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (JS_IsNumber(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
double result = 0.0;
|
2023-03-12 18:16:18 -04:00
|
|
|
if (JS_ToFloat64(context, &result, value) == 0)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
|
|
|
_serialize_writeInt32(buffer, kNumber);
|
|
|
|
_serialize_writeDouble(buffer, result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to store number.\n");
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
2022-04-17 20:24:00 -04:00
|
|
|
else if (JS_IsException(value))
|
|
|
|
{
|
|
|
|
JSValue exception = JS_GetException(context);
|
|
|
|
const char* message = JS_ToCString(context, exception);
|
|
|
|
|
|
|
|
JSValue error = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, error, "message", JS_NewString(context, message ? message : "[exception]"));
|
|
|
|
|
|
|
|
if (JS_IsError(context, exception))
|
|
|
|
{
|
|
|
|
JSValue stack = JS_GetPropertyStr(context, exception, "stack");
|
|
|
|
if (!JS_IsUndefined(stack) && !JS_IsException(stack))
|
|
|
|
{
|
|
|
|
JS_SetPropertyStr(context, error, "stack", JS_DupValue(context, stack));
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, stack);
|
|
|
|
}
|
|
|
|
_serialize_writeInt32(buffer, kException);
|
|
|
|
_serialize_storeInternal(task, to, buffer, error, depth + 1);
|
|
|
|
JS_FreeCString(context, message);
|
|
|
|
JS_FreeValue(context, error);
|
|
|
|
JS_FreeValue(context, exception);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
else if (JS_IsString(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t len = 0;
|
2023-03-12 18:16:18 -04:00
|
|
|
const char* result = JS_ToCStringLen(context, &len, value);
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kString);
|
|
|
|
_serialize_writeInt32(buffer, (int32_t)len);
|
|
|
|
_buffer_append(buffer, result, len);
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_FreeCString(context, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
2023-03-12 18:16:18 -04:00
|
|
|
else if ((bytes = tf_util_try_get_array_buffer(context, &size, value)) != 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kArrayBuffer);
|
|
|
|
_serialize_writeInt32(buffer, (int32_t)size);
|
|
|
|
_buffer_append(buffer, bytes, size);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
2023-03-12 18:16:18 -04:00
|
|
|
else if (!JS_IsException((typed = tf_util_try_get_typed_array_buffer(context, value, &offset, &size, &element_size))))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t total_size;
|
2023-03-12 18:16:18 -04:00
|
|
|
uint8_t* bytes = tf_util_try_get_array_buffer(context, &total_size, typed);
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kArrayBuffer);
|
|
|
|
_serialize_writeInt32(buffer, (int32_t)size);
|
|
|
|
_buffer_append(buffer, bytes, size);
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_FreeValue(context, typed);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
2023-03-12 18:16:18 -04:00
|
|
|
else if (JS_IsArray(context, value))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kArray);
|
2023-03-12 18:16:18 -04:00
|
|
|
int length = tf_util_get_length(context, value);
|
2022-07-09 11:13:35 -04:00
|
|
|
_serialize_writeInt32(buffer, length);
|
|
|
|
for (int i = 0; i < length; ++i)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
JSValue element = JS_GetPropertyUint32(context, value, i);
|
2022-07-09 11:13:35 -04:00
|
|
|
_serialize_storeInternal(task, to, buffer, element, depth + 1);
|
|
|
|
JS_FreeValue(context, element);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
2023-03-12 18:16:18 -04:00
|
|
|
else if (JS_IsFunction(context, value))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kFunction);
|
|
|
|
exportid_t exportId = tf_task_export_function(task, to, value);
|
|
|
|
_serialize_writeInt32(buffer, exportId);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
2023-03-12 18:16:18 -04:00
|
|
|
else if (JS_IsError(context, value))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kError);
|
|
|
|
JSPropertyEnum* ptab;
|
|
|
|
uint32_t plen;
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_GetOwnPropertyNames(context, &ptab, &plen, value, JS_GPN_STRING_MASK);
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, plen);
|
2021-10-10 17:51:38 -04:00
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
JSValue key = JS_AtomToString(context, ptab[i].atom);
|
2021-01-02 13:10:00 -05:00
|
|
|
JSPropertyDescriptor desc;
|
|
|
|
JSValue key_value = JS_NULL;
|
2023-03-12 18:16:18 -04:00
|
|
|
if (JS_GetOwnProperty(context, &desc, value, ptab[i].atom) == 1)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
key_value = desc.value;
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_FreeValue(context, desc.setter);
|
|
|
|
JS_FreeValue(context, desc.getter);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
_serialize_storeInternal(task, to, buffer, key, depth + 1);
|
|
|
|
_serialize_storeInternal(task, to, buffer, key_value, depth + 1);
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_FreeValue(context, key);
|
|
|
|
JS_FreeValue(context, key_value);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_FreeAtom(context, ptab[i].atom);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2023-03-12 18:16:18 -04:00
|
|
|
js_free(context, ptab);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (JS_IsObject(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, kObject);
|
|
|
|
JSPropertyEnum* ptab;
|
|
|
|
uint32_t plen;
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_GetOwnPropertyNames(context, &ptab, &plen, value, JS_GPN_STRING_MASK);
|
2021-01-02 13:10:00 -05:00
|
|
|
_serialize_writeInt32(buffer, plen);
|
2021-10-10 17:51:38 -04:00
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
JSValue key = JS_AtomToString(context, ptab[i].atom);
|
2021-01-02 13:10:00 -05:00
|
|
|
JSPropertyDescriptor desc;
|
|
|
|
JSValue key_value = JS_NULL;
|
2023-03-12 18:16:18 -04:00
|
|
|
if (JS_GetOwnProperty(context, &desc, value, ptab[i].atom) == 1)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
key_value = desc.value;
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_FreeValue(context, desc.setter);
|
|
|
|
JS_FreeValue(context, desc.getter);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
_serialize_storeInternal(task, to, buffer, key, depth + 1);
|
|
|
|
_serialize_storeInternal(task, to, buffer, key_value, depth + 1);
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_FreeValue(context, key);
|
|
|
|
JS_FreeValue(context, key_value);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_FreeAtom(context, ptab[i].atom);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2023-03-12 18:16:18 -04:00
|
|
|
js_free(context, ptab);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fprintf(stderr, "Unknown JSValue type: %d.\n", JS_VALUE_GET_TAG(value));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _serialize_load(tf_task_t* task, tf_taskstub_t* from, const char* buffer, size_t size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return _serialize_loadInternal(task, from, &buffer, &size, 0);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _serialize_loadInternal(tf_task_t* task, tf_taskstub_t* from, const char** buffer, size_t* size, int depth)
|
|
|
|
{
|
2022-01-22 13:50:29 -05:00
|
|
|
if (*size < sizeof(int32_t))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_UNDEFINED;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
JSContext* context = tf_task_get_context(task);
|
2021-01-02 13:10:00 -05:00
|
|
|
int32_t type = _serialize_readInt32(buffer, size);
|
|
|
|
JSValue result = JS_UNDEFINED;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
switch (type)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
case kUndefined:
|
|
|
|
result = JS_UNDEFINED;
|
|
|
|
break;
|
|
|
|
case kNull:
|
|
|
|
result = JS_NULL;
|
|
|
|
break;
|
|
|
|
case kUninitialized:
|
|
|
|
result = JS_UNINITIALIZED;
|
|
|
|
break;
|
|
|
|
case kBoolean:
|
2023-03-12 18:16:18 -04:00
|
|
|
result = JS_NewBool(context, _serialize_readInt8(buffer, size) != 0);
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
case kInt32:
|
2023-03-12 18:16:18 -04:00
|
|
|
result = JS_NewInt32(context, _serialize_readInt32(buffer, size));
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
case kInt64:
|
2023-03-12 18:16:18 -04:00
|
|
|
result = JS_NewInt64(context, _serialize_readInt64(buffer, size));
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
case kNumber:
|
2023-03-12 18:16:18 -04:00
|
|
|
result = JS_NewFloat64(context, _serialize_readDouble(buffer, size));
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
case kString:
|
|
|
|
{
|
|
|
|
int32_t length = _serialize_readInt32(buffer, size);
|
2023-03-12 18:16:18 -04:00
|
|
|
result = JS_NewStringLen(context, *buffer, length);
|
2021-01-02 13:10:00 -05:00
|
|
|
*buffer += length;
|
|
|
|
*size -= length;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kArrayBuffer:
|
|
|
|
{
|
|
|
|
int32_t length = _serialize_readInt32(buffer, size);
|
2023-03-12 18:16:18 -04:00
|
|
|
result = JS_NewArrayBufferCopy(context, (const uint8_t*)*buffer, length);
|
2021-01-02 13:10:00 -05:00
|
|
|
*buffer += length;
|
|
|
|
*size -= length;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kArray:
|
|
|
|
{
|
|
|
|
int32_t length = _serialize_readInt32(buffer, size);
|
2023-03-12 18:16:18 -04:00
|
|
|
result = JS_NewArray(context);
|
2021-10-10 17:51:38 -04:00
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
2023-03-12 18:16:18 -04:00
|
|
|
JS_SetPropertyUint32(context, result, i, _serialize_loadInternal(task, from, buffer, size, depth + 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kFunction:
|
|
|
|
{
|
|
|
|
exportid_t exportId = _serialize_readInt32(buffer, size);
|
|
|
|
result = tf_task_add_import(task, tf_taskstub_get_id(from), exportId);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kException:
|
|
|
|
{
|
|
|
|
_serialize_readInt32(buffer, size);
|
2023-03-12 18:16:18 -04:00
|
|
|
JSValue error = JS_NewError(context);
|
2021-01-02 13:10:00 -05:00
|
|
|
int32_t length = _serialize_readInt32(buffer, size);
|
2021-10-10 17:51:38 -04:00
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue key = _serialize_loadInternal(task, from, buffer, size, depth + 1);
|
|
|
|
JSValue value = _serialize_loadInternal(task, from, buffer, size, depth + 1);
|
2023-03-12 18:16:18 -04:00
|
|
|
const char* key_str = JS_ToCString(context, key);
|
|
|
|
JS_SetPropertyStr(context, error, key_str, value);
|
|
|
|
JS_FreeCString(context, key_str);
|
|
|
|
JS_FreeValue(context, key);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
result = error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kError:
|
|
|
|
case kObject:
|
|
|
|
{
|
|
|
|
int32_t length = _serialize_readInt32(buffer, size);
|
2023-03-12 18:16:18 -04:00
|
|
|
result = JS_NewObject(context);
|
2021-10-10 17:51:38 -04:00
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue key = _serialize_loadInternal(task, from, buffer, size, depth + 1);
|
|
|
|
JSValue value = _serialize_loadInternal(task, from, buffer, size, depth + 1);
|
2023-03-12 18:16:18 -04:00
|
|
|
const char* key_str = JS_ToCString(context, key);
|
|
|
|
JS_SetPropertyStr(context, result, key_str, value);
|
|
|
|
JS_FreeCString(context, key_str);
|
|
|
|
JS_FreeValue(context, key);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|