From 8cbc2116162764479edeec78e4b2b7c41447b643 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 22 Aug 2024 12:42:12 +0100 Subject: [PATCH] Meta: Make embed_as_string_view.py produce Strings instead This is only used for CSS style sheets. One case wants it as a String, and the others don't care, but will in future also want to have the source as a String. --- Meta/CMake/code_generators.cmake | 7 +++---- Meta/CMake/libweb_generators.cmake | 8 ++++---- Meta/{embed_as_string_view.py => embed_as_string.py} | 10 +++++----- .../{embed_as_string_view.gni => embed_as_string.gni} | 10 +++++----- Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn | 10 +++++----- .../secondary/Userland/Libraries/LibWebView/BUILD.gn | 4 ++-- Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 8 ++++---- Userland/Libraries/LibWebView/CMakeLists.txt | 2 +- Userland/Libraries/LibWebView/ViewImplementation.cpp | 4 ++-- 9 files changed, 31 insertions(+), 32 deletions(-) rename Meta/{embed_as_string_view.py => embed_as_string.py} (79%) rename Meta/gn/build/{embed_as_string_view.gni => embed_as_string.gni} (85%) diff --git a/Meta/CMake/code_generators.cmake b/Meta/CMake/code_generators.cmake index ad9e8382fb8..236efa0c877 100644 --- a/Meta/CMake/code_generators.cmake +++ b/Meta/CMake/code_generators.cmake @@ -2,7 +2,7 @@ # Functions for generating sources using host tools # -function(embed_as_string_view name source_file output source_variable_name) +function(embed_as_string name source_file output source_variable_name) cmake_parse_arguments(PARSE_ARGV 4 EMBED_STRING_VIEW "" "NAMESPACE" "") set(namespace_arg "") if (EMBED_STRING_VIEW_NAMESPACE) @@ -11,11 +11,11 @@ function(embed_as_string_view name source_file output source_variable_name) find_package(Python3 REQUIRED COMPONENTS Interpreter) add_custom_command( OUTPUT "${output}" - COMMAND "${Python3_EXECUTABLE}" "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string_view.py" "${source_file}" -o "${output}.tmp" -n "${source_variable_name}" ${namespace_arg} + COMMAND "${Python3_EXECUTABLE}" "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string.py" "${source_file}" -o "${output}.tmp" -n "${source_variable_name}" ${namespace_arg} COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${output}.tmp" "${output}" COMMAND "${CMAKE_COMMAND}" -E remove "${output}.tmp" VERBATIM - DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string_view.py" + DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string.py" MAIN_DEPENDENCY "${source_file}" ) @@ -78,4 +78,3 @@ function(compile_ipc source output) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${output} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${current_source_dir_relative}" OPTIONAL) endif() endfunction() - diff --git a/Meta/CMake/libweb_generators.cmake b/Meta/CMake/libweb_generators.cmake index f874ce5c542..f08c98e5935 100644 --- a/Meta/CMake/libweb_generators.cmake +++ b/Meta/CMake/libweb_generators.cmake @@ -64,7 +64,7 @@ function (generate_css_implementation) arguments -j "${LIBWEB_INPUT_FOLDER}/CSS/Keywords.json" ) - embed_as_string_view( + embed_as_string( "DefaultStyleSheetSource.cpp" "${LIBWEB_INPUT_FOLDER}/CSS/Default.css" "CSS/DefaultStyleSheetSource.cpp" @@ -72,7 +72,7 @@ function (generate_css_implementation) NAMESPACE "Web::CSS" ) - embed_as_string_view( + embed_as_string( "QuirksModeStyleSheetSource.cpp" "${LIBWEB_INPUT_FOLDER}/CSS/QuirksMode.css" "CSS/QuirksModeStyleSheetSource.cpp" @@ -80,7 +80,7 @@ function (generate_css_implementation) NAMESPACE "Web::CSS" ) - embed_as_string_view( + embed_as_string( "MathMLStyleSheetSource.cpp" "${LIBWEB_INPUT_FOLDER}/MathML/Default.css" "MathML/MathMLStyleSheetSource.cpp" @@ -88,7 +88,7 @@ function (generate_css_implementation) NAMESPACE "Web::CSS" ) - embed_as_string_view( + embed_as_string( "SVGStyleSheetSource.cpp" "${LIBWEB_INPUT_FOLDER}/SVG/Default.css" "SVG/SVGStyleSheetSource.cpp" diff --git a/Meta/embed_as_string_view.py b/Meta/embed_as_string.py similarity index 79% rename from Meta/embed_as_string_view.py rename to Meta/embed_as_string.py index a9bd7a16e14..f20d40d7b7d 100644 --- a/Meta/embed_as_string_view.py +++ b/Meta/embed_as_string.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 r""" - Embeds a file into a StringView, a la #embed from C++23 + Embeds a file into a String, a la #embed from C++23 """ import argparse @@ -21,15 +21,15 @@ def main(): args = parser.parse_args() with open(args.output, 'w') as f: - f.write("#include \n") + f.write("#include \n") if args.namespace: f.write(f"namespace {args.namespace} {{\n") - f.write(f"extern StringView {args.variable_name};\n") - f.write(f"StringView {args.variable_name} = R\"~~~(") + f.write(f"extern String {args.variable_name};\n") + f.write(f"String {args.variable_name} = R\"~~~(") with open(args.input, 'r') as input: for line in input.readlines(): f.write(f"{line}") - f.write(")~~~\"sv;\n") + f.write(")~~~\"_string;\n") if args.namespace: f.write("}\n") diff --git a/Meta/gn/build/embed_as_string_view.gni b/Meta/gn/build/embed_as_string.gni similarity index 85% rename from Meta/gn/build/embed_as_string_view.gni rename to Meta/gn/build/embed_as_string.gni index eab79dbadc9..70301e00af1 100644 --- a/Meta/gn/build/embed_as_string_view.gni +++ b/Meta/gn/build/embed_as_string.gni @@ -1,6 +1,6 @@ -# This file introduces a template for calling embed_as_string_view.py. +# This file introduces a template for calling embed_as_string.py. # -# embed_as_string_view behaves like C++23 #embed, converting an input file into +# embed_as_string behaves like C++23 #embed, converting an input file into # an AK::StringView literal rather than a C-string literal. The literal will # be placed into a global variable, optionally with a namespace wrapping it. # @@ -19,21 +19,21 @@ # # Example use: # -# embed_as_string_view("embed_my_file") { +# embed_as_string("embed_my_file") { # input = "MyFile.txt" # output = "$root_gen_dir/MyDirectory/MyFile.cpp" # variable_name = "my_file_contents" # namespace = "My::NS" # } -template("embed_as_string_view") { +template("embed_as_string") { assert(defined(invoker.input), "must set 'input' in $target_name") assert(defined(invoker.output), "must set 'output' in $target_name") assert(defined(invoker.variable_name), "must set 'variable_name' in $target_name") action(target_name) { - script = "//Meta/embed_as_string_view.py" + script = "//Meta/embed_as_string.py" sources = [ invoker.input ] outputs = [ invoker.output ] diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn index 3e75c6f1606..c3a80b1fd6c 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn @@ -1,5 +1,5 @@ import("//Meta/gn/build/compiled_action.gni") -import("//Meta/gn/build/embed_as_string_view.gni") +import("//Meta/gn/build/embed_as_string.gni") import("generate_idl_bindings.gni") import("idl_files.gni") @@ -220,28 +220,28 @@ compiled_action("generate_css_keyword") { ] } -embed_as_string_view("generate_default_stylesheet_source") { +embed_as_string("generate_default_stylesheet_source") { input = "CSS/Default.css" output = "$target_gen_dir/CSS/DefaultStyleSheetSource.cpp" variable_name = "default_stylesheet_source" namespace = "Web::CSS" } -embed_as_string_view("generate_mathml_stylesheet_source") { +embed_as_string("generate_mathml_stylesheet_source") { input = "MathML/Default.css" output = "$target_gen_dir/MathML/MathMLStyleSheetSource.cpp" variable_name = "mathml_stylesheet_source" namespace = "Web::CSS" } -embed_as_string_view("generate_svg_stylesheet_source") { +embed_as_string("generate_svg_stylesheet_source") { input = "SVG/Default.css" output = "$target_gen_dir/SVG/SVGStyleSheetSource.cpp" variable_name = "svg_stylesheet_source" namespace = "Web::CSS" } -embed_as_string_view("generate_quirks_mode_stylesheet_source") { +embed_as_string("generate_quirks_mode_stylesheet_source") { input = "CSS/QuirksMode.css" output = "$target_gen_dir/CSS/QuirksModeStyleSheetSource.cpp" variable_name = "quirks_mode_stylesheet_source" diff --git a/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn index 8bf5e478354..250a03d1380 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn @@ -1,7 +1,7 @@ import("//Meta/gn/build/compiled_action.gni") import("//Meta/gn/build/download_cache.gni") import("//Meta/gn/build/download_file.gni") -import("//Meta/gn/build/embed_as_string_view.gni") +import("//Meta/gn/build/embed_as_string.gni") declare_args() { # If true, Download public suffix list from GitHub. @@ -106,7 +106,7 @@ compiled_action("UIProcessServerEndpoint") { ] } -embed_as_string_view("generate_native_stylesheet_source") { +embed_as_string("generate_native_stylesheet_source") { input = "Native.css" output = "$target_gen_dir/NativeStyleSheetSource.cpp" variable_name = "native_stylesheet_source" diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 39ec485acbe..3495ad3974b 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -218,7 +218,7 @@ static CSSStyleSheet& default_stylesheet(DOM::Document const& document) { static JS::Handle sheet; if (!sheet.cell()) { - extern StringView default_stylesheet_source; + extern String default_stylesheet_source; sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), default_stylesheet_source)); } return *sheet; @@ -228,7 +228,7 @@ static CSSStyleSheet& quirks_mode_stylesheet(DOM::Document const& document) { static JS::Handle sheet; if (!sheet.cell()) { - extern StringView quirks_mode_stylesheet_source; + extern String quirks_mode_stylesheet_source; sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), quirks_mode_stylesheet_source)); } return *sheet; @@ -238,7 +238,7 @@ static CSSStyleSheet& mathml_stylesheet(DOM::Document const& document) { static JS::Handle sheet; if (!sheet.cell()) { - extern StringView mathml_stylesheet_source; + extern String mathml_stylesheet_source; sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), mathml_stylesheet_source)); } return *sheet; @@ -248,7 +248,7 @@ static CSSStyleSheet& svg_stylesheet(DOM::Document const& document) { static JS::Handle sheet; if (!sheet.cell()) { - extern StringView svg_stylesheet_source; + extern String svg_stylesheet_source; sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), svg_stylesheet_source)); } return *sheet; diff --git a/Userland/Libraries/LibWebView/CMakeLists.txt b/Userland/Libraries/LibWebView/CMakeLists.txt index f0ffbe30f0b..38f5056e7cf 100644 --- a/Userland/Libraries/LibWebView/CMakeLists.txt +++ b/Userland/Libraries/LibWebView/CMakeLists.txt @@ -23,7 +23,7 @@ set(SOURCES set(GENERATED_SOURCES ${CURRENT_LIB_GENERATED}) -embed_as_string_view( +embed_as_string( "NativeStyleSheetSource.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Native.css" "NativeStyleSheetSource.cpp" diff --git a/Userland/Libraries/LibWebView/ViewImplementation.cpp b/Userland/Libraries/LibWebView/ViewImplementation.cpp index 72d3f1ee304..ce321232a7c 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.cpp +++ b/Userland/Libraries/LibWebView/ViewImplementation.cpp @@ -592,8 +592,8 @@ void ViewImplementation::set_user_style_sheet(String source) void ViewImplementation::use_native_user_style_sheet() { - extern StringView native_stylesheet_source; - set_user_style_sheet(MUST(String::from_utf8(native_stylesheet_source))); + extern String native_stylesheet_source; + set_user_style_sheet(native_stylesheet_source); } void ViewImplementation::enable_inspector_prototype()