core: core.globalSettingsDescriptions JS => C.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 31m48s

This commit is contained in:
2025-10-15 19:09:41 -04:00
parent 4bdc7ec616
commit 07605933dc
4 changed files with 107 additions and 9 deletions

View File

@@ -555,6 +555,76 @@ static JSValue _tf_ssb_getOwnerIdentities(JSContext* context, JSValueConst this_
return result;
}
typedef struct _settings_descriptions_get_t
{
const char* settings;
JSContext* context;
JSValue promise[2];
} settings_descriptions_get_t;
static void _tf_ssb_get_settings_descriptions_work(tf_ssb_t* ssb, void* user_data)
{
settings_descriptions_get_t* work = user_data;
work->settings = tf_ssb_db_get_property(ssb, "core", "settings");
}
static void _tf_ssb_get_settings_descriptions_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
settings_descriptions_get_t* work = user_data;
JSContext* context = work->context;
JSValue result = JS_NewObject(context);
JSValue settings = JS_ParseJSON(context, work->settings ? work->settings : "", work->settings ? strlen(work->settings) : 0, NULL);
const char* name;
const char* type;
tf_setting_kind_t kind;
const char* description;
for (int i = 0; tf_util_get_global_setting_by_index(i, &name, &type, &kind, &description); i++)
{
JSValue entry = JS_NewObject(context);
JS_SetPropertyStr(context, entry, "type", JS_NewString(context, type));
JS_SetPropertyStr(context, entry, "description", JS_NewString(context, description));
switch (kind)
{
case k_kind_unknown:
break;
case k_kind_bool:
JS_SetPropertyStr(context, entry, "default_value", JS_NewBool(context, tf_util_get_default_global_setting_bool(name)));
break;
case k_kind_int:
JS_SetPropertyStr(context, entry, "default_value", JS_NewInt32(context, tf_util_get_default_global_setting_int(name)));
break;
case k_kind_string:
JS_SetPropertyStr(context, entry, "default_value", JS_NewString(context, tf_util_get_default_global_setting_string(name)));
break;
}
if (JS_IsObject(settings))
{
JS_SetPropertyStr(context, entry, "value", JS_GetPropertyStr(context, settings, name));
}
JS_SetPropertyStr(context, result, name, entry);
}
JS_FreeValue(context, settings);
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
tf_util_report_error(context, error);
JS_FreeValue(context, error);
JS_FreeValue(context, result);
JS_FreeValue(context, work->promise[0]);
JS_FreeValue(context, work->promise[1]);
tf_free((void*)work->settings);
tf_free(work);
}
static JSValue _tf_ssb_globalSettingsDescriptions(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_task_t* task = tf_task_get(context);
tf_ssb_t* ssb = tf_task_get_ssb(task);
settings_descriptions_get_t* work = tf_malloc(sizeof(settings_descriptions_get_t));
*work = (settings_descriptions_get_t) { .context = context };
JSValue result = JS_NewPromiseCapability(context, work->promise);
tf_ssb_run_work(ssb, _tf_ssb_get_settings_descriptions_work, _tf_ssb_get_settings_descriptions_after_work, work);
return result;
}
typedef struct _settings_get_t
{
const char* key;
@@ -627,6 +697,7 @@ static void _tf_ssb_settings_get_after_work(tf_ssb_t* ssb, int status, void* use
JS_FreeValue(context, result);
JS_FreeValue(context, work->promise[0]);
JS_FreeValue(context, work->promise[1]);
JS_FreeCString(context, work->key);
tf_free(work);
}
@@ -673,6 +744,7 @@ static JSValue _tf_api_register_imports(JSContext* context, JSValueConst this_va
JSValue administration = JS_IsObject(permissions) ? JS_GetPropertyStr(context, permissions, "administration") : JS_UNDEFINED;
if (JS_ToBool(context, administration) > 0)
{
JS_SetPropertyStr(context, core, "globalSettingsDescriptions", JS_NewCFunction(context, _tf_ssb_globalSettingsDescriptions, "globalSettingsDescriptions", 0));
JS_SetPropertyStr(context, core, "globalSettingsGet", JS_NewCFunction(context, _tf_ssb_globalSettingsGet, "globalSettingsGet", 1));
}
JS_FreeValue(context, administration);