mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibMain: Add a new library for more ergonomic userspace entry functions
By linking with LibMain, your program no longer needs to provide main(). Instead, execution begins in this function: ErrorOr<int> serenity_main(Main::Arguments); This allows programs that link with LibMain to use TRY() already in their entry function, without having to do manual ErrorOr unwrapping. This is very experimental, but it seems like a nice idea so let's try it out. :^)
This commit is contained in:
parent
317ceb0ee2
commit
d3cf68a540
Notes:
sideshowbarker
2024-07-18 00:52:49 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/d3cf68a5407
4 changed files with 57 additions and 0 deletions
|
@ -30,6 +30,7 @@ add_subdirectory(LibJS)
|
|||
add_subdirectory(LibKeyboard)
|
||||
add_subdirectory(LibLine)
|
||||
add_subdirectory(LibM)
|
||||
add_subdirectory(LibMain)
|
||||
add_subdirectory(LibMarkdown)
|
||||
add_subdirectory(LibPCIDB)
|
||||
add_subdirectory(LibPDF)
|
||||
|
|
6
Userland/Libraries/LibMain/CMakeLists.txt
Normal file
6
Userland/Libraries/LibMain/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
set(SOURCES
|
||||
Main.cpp
|
||||
)
|
||||
|
||||
serenity_lib(LibMain main)
|
||||
target_link_libraries(LibMain LibC)
|
29
Userland/Libraries/LibMain/Main.cpp
Normal file
29
Userland/Libraries/LibMain/Main.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Format.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Vector<StringView> arguments;
|
||||
arguments.ensure_capacity(argc);
|
||||
for (int i = 0; i < argc; ++i)
|
||||
arguments.unchecked_append(argv[i]);
|
||||
|
||||
auto result = serenity_main({
|
||||
.argc = argc,
|
||||
.argv = argv,
|
||||
.arguments = arguments.span(),
|
||||
});
|
||||
if (result.is_error()) {
|
||||
warnln("Runtime error: {}", result.error());
|
||||
return 1;
|
||||
}
|
||||
return result.value();
|
||||
}
|
21
Userland/Libraries/LibMain/Main.h
Normal file
21
Userland/Libraries/LibMain/Main.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
|
||||
namespace Main {
|
||||
|
||||
struct Arguments {
|
||||
int argc {};
|
||||
char** argv {};
|
||||
Span<StringView> arguments;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments);
|
Loading…
Reference in a new issue