mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 10:18:15 +09:00
js: Add a loadINI() function for loading INI files :^)
This works the same way as loadJSON, except it loads INI files and always returns an object.
This commit is contained in:
parent
87da3e0004
commit
857c3501df
Notes:
sideshowbarker
2024-07-17 14:33:07 +09:00
Author: https://github.com/linusg
Commit: 857c3501df
1 changed files with 35 additions and 0 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/StandardPaths.h>
|
||||
#include <LibCore/System.h>
|
||||
|
@ -96,6 +97,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(repl_help);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_file);
|
||||
JS_DECLARE_NATIVE_FUNCTION(save_to_file);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_ini);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_json);
|
||||
JS_DECLARE_NATIVE_FUNCTION(last_value_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(print);
|
||||
|
@ -111,6 +113,7 @@ public:
|
|||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_file);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_ini);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_json);
|
||||
JS_DECLARE_NATIVE_FUNCTION(print);
|
||||
};
|
||||
|
@ -1196,6 +1199,26 @@ static JS::ThrowCompletionOr<JS::Value> load_file_impl(JS::VM& vm, JS::GlobalObj
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
static JS::ThrowCompletionOr<JS::Value> load_ini_impl(JS::VM& vm, JS::GlobalObject& global_object)
|
||||
{
|
||||
auto filename = TRY(vm.argument(0).to_string(global_object));
|
||||
auto file = Core::File::construct(filename);
|
||||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return vm.throw_completion<JS::Error>(global_object, String::formatted("Failed to open '{}': {}", filename, file->error_string()));
|
||||
|
||||
auto config_file = MUST(Core::ConfigFile::open(filename, file->fd()));
|
||||
auto* object = JS::Object::create(global_object, global_object.object_prototype());
|
||||
for (auto const& group : config_file->groups()) {
|
||||
auto* group_object = JS::Object::create(global_object, global_object.object_prototype());
|
||||
for (auto const& key : config_file->keys(group)) {
|
||||
auto entry = config_file->read_entry(group, key);
|
||||
group_object->define_direct_property(key, js_string(vm, move(entry)), JS::Attribute::Enumerable | JS::Attribute::Configurable | JS::Attribute::Writable);
|
||||
}
|
||||
object->define_direct_property(group, group_object, JS::Attribute::Enumerable | JS::Attribute::Configurable | JS::Attribute::Writable);
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
static JS::ThrowCompletionOr<JS::Value> load_json_impl(JS::VM& vm, JS::GlobalObject& global_object)
|
||||
{
|
||||
auto filename = TRY(vm.argument(0).to_string(global_object));
|
||||
|
@ -1218,6 +1241,7 @@ void ReplObject::initialize_global_object()
|
|||
define_native_function("help", repl_help, 0, attr);
|
||||
define_native_function("load", load_file, 1, attr);
|
||||
define_native_function("save", save_to_file, 1, attr);
|
||||
define_native_function("loadINI", load_ini, 1, attr);
|
||||
define_native_function("loadJSON", load_json, 1, attr);
|
||||
define_native_function("print", print, 1, attr);
|
||||
|
||||
|
@ -1274,6 +1298,11 @@ JS_DEFINE_NATIVE_FUNCTION(ReplObject::load_file)
|
|||
return load_file_impl(vm, global_object);
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ReplObject::load_ini)
|
||||
{
|
||||
return load_ini_impl(vm, global_object);
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ReplObject::load_json)
|
||||
{
|
||||
return load_json_impl(vm, global_object);
|
||||
|
@ -1291,6 +1320,7 @@ void ScriptObject::initialize_global_object()
|
|||
define_direct_property("global", this, JS::Attribute::Enumerable);
|
||||
u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
|
||||
define_native_function("load", load_file, 1, attr);
|
||||
define_native_function("loadINI", load_ini, 1, attr);
|
||||
define_native_function("loadJSON", load_json, 1, attr);
|
||||
define_native_function("print", print, 1, attr);
|
||||
}
|
||||
|
@ -1300,6 +1330,11 @@ JS_DEFINE_NATIVE_FUNCTION(ScriptObject::load_file)
|
|||
return load_file_impl(vm, global_object);
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ScriptObject::load_ini)
|
||||
{
|
||||
return load_ini_impl(vm, global_object);
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ScriptObject::load_json)
|
||||
{
|
||||
return load_json_impl(vm, global_object);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue