1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:48:11 +00:00

js: Use Core::Stream::File in place of POSIX in write_to_file()

This commit is contained in:
Evan Smal 2023-02-03 22:15:47 -05:00 committed by Linus Groh
parent f871dc097b
commit d18a7ca2dc

View file

@ -25,13 +25,10 @@
#include <LibLine/Editor.h> #include <LibLine/Editor.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
#include <LibTextCodec/Decoder.h> #include <LibTextCodec/Decoder.h>
#include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdio.h>
#include <unistd.h>
RefPtr<JS::VM> g_vm; RefPtr<JS::VM> g_vm;
Vector<DeprecatedString> g_repl_statements; Vector<String> g_repl_statements;
JS::Handle<JS::Value> g_last_value = JS::make_handle(JS::js_undefined()); JS::Handle<JS::Value> g_last_value = JS::make_handle(JS::js_undefined());
class ReplObject final : public JS::GlobalObject { class ReplObject final : public JS::GlobalObject {
@ -185,30 +182,20 @@ static ErrorOr<String> read_next_piece()
return piece.to_string(); return piece.to_string();
} }
static bool write_to_file(DeprecatedString const& path) static ErrorOr<void> write_to_file(String const& path)
{ {
int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666); auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Write, 0666));
for (size_t i = 0; i < g_repl_statements.size(); i++) { for (size_t i = 0; i < g_repl_statements.size(); i++) {
auto line = g_repl_statements[i]; auto line = g_repl_statements[i].bytes();
if (line.length() && i != g_repl_statements.size() - 1) { if (line.size() > 0 && i != g_repl_statements.size() - 1) {
ssize_t nwritten = write(fd, line.characters(), line.length()); TRY(file->write(line));
if (nwritten < 0) {
close(fd);
return false;
}
} }
if (i != g_repl_statements.size() - 1) { if (i != g_repl_statements.size() - 1) {
char ch = '\n'; TRY(file->write_value('\n'));
ssize_t nwritten = write(fd, &ch, 1);
if (nwritten != 1) {
perror("write");
close(fd);
return false;
}
} }
} }
close(fd); file->close();
return true; return {};
} }
static ErrorOr<bool> parse_and_run(JS::Interpreter& interpreter, StringView source, StringView source_name) static ErrorOr<bool> parse_and_run(JS::Interpreter& interpreter, StringView source, StringView source_name)
@ -421,8 +408,8 @@ JS_DEFINE_NATIVE_FUNCTION(ReplObject::save_to_file)
{ {
if (!vm.argument_count()) if (!vm.argument_count())
return JS::Value(false); return JS::Value(false);
DeprecatedString save_path = vm.argument(0).to_string_without_side_effects(); auto const save_path = TRY(vm.argument(0).to_string(vm));
if (write_to_file(save_path)) { if (!write_to_file(save_path).is_error()) {
return JS::Value(true); return JS::Value(true);
} }
return JS::Value(false); return JS::Value(false);
@ -509,7 +496,7 @@ static ErrorOr<void> repl(JS::Interpreter& interpreter)
if (Utf8View { piece }.trim(JS::whitespace_characters).is_empty()) if (Utf8View { piece }.trim(JS::whitespace_characters).is_empty())
continue; continue;
g_repl_statements.append(piece.to_deprecated_string()); g_repl_statements.append(piece);
TRY(parse_and_run(interpreter, piece, "REPL"sv)); TRY(parse_and_run(interpreter, piece, "REPL"sv));
} }
return {}; return {};