From 8d7179766a2233d0c0e5ed33fa96099b3767dd9a Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 14 Sep 2022 15:19:11 +0100 Subject: [PATCH] js: Port to Core::Stream --- Userland/Utilities/js.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 78ecddd36c..3d05d8df13 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -12,8 +12,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -1271,11 +1271,11 @@ static JS::ThrowCompletionOr load_ini_impl(JS::VM& vm) auto& realm = *vm.current_realm(); auto filename = TRY(vm.argument(0).to_string(vm)); - auto file = Core::File::construct(filename); - if (!file->open(Core::OpenMode::ReadOnly)) - return vm.throw_completion(String::formatted("Failed to open '{}': {}", filename, file->error_string())); + auto file_or_error = Core::Stream::File::open(filename, Core::Stream::OpenMode::Read); + if (file_or_error.is_error()) + return vm.throw_completion(String::formatted("Failed to open '{}': {}", filename, file_or_error.error())); - auto config_file = MUST(Core::ConfigFile::open(filename, file->fd())); + auto config_file = MUST(Core::ConfigFile::open(filename, file_or_error.release_value())); auto* object = JS::Object::create(realm, realm.intrinsics().object_prototype()); for (auto const& group : config_file->groups()) { auto* group_object = JS::Object::create(realm, realm.intrinsics().object_prototype()); @@ -1291,11 +1291,15 @@ static JS::ThrowCompletionOr load_ini_impl(JS::VM& vm) static JS::ThrowCompletionOr load_json_impl(JS::VM& vm) { auto filename = TRY(vm.argument(0).to_string(vm)); - auto file = Core::File::construct(filename); - if (!file->open(Core::OpenMode::ReadOnly)) - return vm.throw_completion(String::formatted("Failed to open '{}': {}", filename, file->error_string())); - auto file_contents = file->read_all(); - auto json = JsonValue::from_string(file_contents); + auto file_or_error = Core::Stream::File::open(filename, Core::Stream::OpenMode::Read); + if (file_or_error.is_error()) + return vm.throw_completion(String::formatted("Failed to open '{}': {}", filename, file_or_error.error())); + + auto file_contents_or_error = file_or_error.value()->read_all(); + if (file_contents_or_error.is_error()) + return vm.throw_completion(String::formatted("Failed to read '{}': {}", filename, file_contents_or_error.error())); + + auto json = JsonValue::from_string(file_contents_or_error.value()); if (json.is_error()) return vm.throw_completion(JS::ErrorType::JsonMalformed); return JS::JSONObject::parse_json_value(vm, json.value()); @@ -1790,8 +1794,8 @@ ErrorOr serenity_main(Main::Arguments arguments) warnln("Warning: Multiple files supplied, this will concatenate the sources and resolve modules as if it was the first file"); for (auto& path : script_paths) { - auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly)); - auto file_contents = file->read_all(); + auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read)); + auto file_contents = TRY(file->read_all()); auto source = StringView { file_contents }; if (Utf8View { file_contents }.validate()) {