Continuing to chip away at moving ssb.js to C. This time, following.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4096 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-12-31 21:44:48 +00:00
parent a9f6593979
commit 120ed36552
4 changed files with 167 additions and 50 deletions

View File

@ -966,6 +966,45 @@ static JSValue _tf_ssb_connectionSendJson(JSContext* context, JSValueConst this_
return JS_NewInt32(context, request_number);
}
static JSValue _tf_ssb_followingDeep(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
int depth = 2;
if (!JS_IsArray(context, argv[0]))
{
return JS_ThrowTypeError(context, "Expected argument 1 to be an array of ids.");
}
if (JS_ToInt32(context, &depth, argv[1]))
{
return JS_ThrowTypeError(context, "Could not convert argument 2 to integer.");
}
int count = tf_util_get_length(context, argv[0]);
const char** ids = tf_malloc(count * sizeof(char*));
for (int i = 0; i < count; i++)
{
JSValue id = JS_GetPropertyUint32(context, argv[0], i);
ids[i] = JS_ToCString(context, id);
JS_FreeValue(context, id);
}
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
const char** following_deep = tf_ssb_db_following_deep(ssb, ids, count, depth);
JSValue result = JS_NewArray(context);
int index = 0;
for (const char** id = following_deep; *id; id++)
{
JS_SetPropertyUint32(context, result, index++, JS_NewString(context, *id));
}
tf_free(following_deep);
for (int i = 0; i < count; i++)
{
JS_FreeCString(context, ids[i]);
}
tf_free(ids);
return result;
}
void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
{
JS_NewClassID(&_tf_ssb_classId);
@ -1005,6 +1044,7 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0));
JS_SetPropertyStr(context, object, "connect", JS_NewCFunction(context, _tf_ssb_connect, "connect", 1));
JS_SetPropertyStr(context, object, "createTunnel", JS_NewCFunction(context, _tf_ssb_createTunnel, "createTunnel", 3));
JS_SetPropertyStr(context, object, "followingDeep", JS_NewCFunction(context, _tf_ssb_followingDeep, "followingDeep", 2));
/* Should be trusted only. */
JS_SetPropertyStr(context, object, "addRpc", JS_NewCFunction(context, _tf_ssb_add_rpc, "addRpc", 2));