forked from cory/tildefriends
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:
72
src/ssb.js.c
72
src/ssb.js.c
@ -4,6 +4,7 @@
|
||||
#include "ssb.db.h"
|
||||
#include "ssb.h"
|
||||
#include "task.h"
|
||||
#include "util.js.h"
|
||||
|
||||
#include <malloc.h>
|
||||
#include <sodium/crypto_hash_sha256.h>
|
||||
@ -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");
|
||||
}
|
||||
|
Reference in New Issue
Block a user