-
Notifications
You must be signed in to change notification settings - Fork 110
[k2] add support argc/argv superglobals in k2-cli #1295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please, considering using |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2025 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string_view> | ||
|
|
||
| namespace kphp::compiler_interface { | ||
|
|
||
| std::string_view get_main_file_name() noexcept; | ||
|
|
||
| } // namespace kphp::compiler_interface |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| #include "runtime-light/state/component-state.h" | ||
|
|
||
| #include <cctype> | ||
| #include <cstring> | ||
| #include <iterator> | ||
| #include <string_view> | ||
|
|
@@ -13,6 +14,7 @@ | |
| #include "runtime-common/core/runtime-core.h" | ||
| #include "runtime-common/core/utils/kphp-assert-core.h" | ||
| #include "runtime-common/stdlib/serialization/json-functions.h" | ||
| #include "runtime-light/core/compiler-info/compiler_interface.h" | ||
| #include "runtime-light/k2-platform/k2-api.h" | ||
|
|
||
| void ComponentState::parse_env() noexcept { | ||
|
|
@@ -54,6 +56,40 @@ void ComponentState::parse_runtime_config_arg(std::string_view value_view) noexc | |
| } | ||
| } | ||
|
|
||
| void ComponentState::parse_command_line_arg(std::string_view value_view) noexcept { | ||
| if (value_view.empty()) [[unlikely]] { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to have |
||
| php_warning("command line argument is empty"); | ||
| return; | ||
| } | ||
|
|
||
| if (!command_line_argv.empty()) [[unlikely]] { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check should be placed at the top of the function |
||
| php_warning("command line argument support no more one usage"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weird message, I'd suggest |
||
| return; | ||
| } | ||
|
|
||
| const auto& main_file_view{kphp::compiler_interface::get_main_file_name()}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think reference is redundant here |
||
| command_line_argv.push_back(string(main_file_view.data(), main_file_view.size())); | ||
|
|
||
| bool in_quote{}; | ||
| string current_arg{}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like an error. You need to set special reference counter for each reference counted object |
||
| for (char letter : value_view) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not actually |
||
| if (std::isspace(letter) && !in_quote && !current_arg.empty()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks overcomplicated. Let's use standard string view functions instead |
||
| command_line_argv.push_back(std::move(current_arg)); | ||
| current_arg = string(); | ||
| } else if (letter == '"') { | ||
| in_quote = !in_quote; | ||
| } else if (letter == '\'') { | ||
| php_warning("in command line arg supported only \" quote"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think you added an extra space |
||
| } else { | ||
| current_arg.push_back(letter); | ||
| } | ||
| } | ||
|
|
||
| if (!current_arg.empty()) { | ||
| command_line_argv.push_back(std::move(current_arg)); | ||
| } | ||
| } | ||
|
|
||
| void ComponentState::parse_args() noexcept { | ||
| for (auto i = 0; i < argc; ++i) { | ||
| const auto [arg_key, arg_value]{k2::arg_fetch(i)}; | ||
|
|
@@ -64,10 +100,13 @@ void ComponentState::parse_args() noexcept { | |
| parse_ini_arg(key_view, value_view); | ||
| } else if (key_view == RUNTIME_CONFIG_ARG) { | ||
| parse_runtime_config_arg(value_view); | ||
| } else if (key_view == COMMAND_LINE_ARG) { | ||
| parse_command_line_arg(value_view); | ||
| } else { | ||
| php_warning("unknown argument: %s", key_view.data()); | ||
| } | ||
| } | ||
| command_line_argv.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| runtime_config.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| ini_opts.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ struct ComponentState final : private vk::not_copyable { | |
| const uint32_t argc; | ||
| const uint32_t envc; | ||
| mixed runtime_config; | ||
| array<string> command_line_argv; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| array<string> ini_opts; | ||
| array<mixed> env; | ||
|
|
||
|
|
@@ -43,6 +44,7 @@ struct ComponentState final : private vk::not_copyable { | |
|
|
||
| private: | ||
| static constexpr std::string_view INI_ARG_PREFIX = "ini "; | ||
| static constexpr std::string_view COMMAND_LINE_ARG = "command-line"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's define it as |
||
| static constexpr std::string_view RUNTIME_CONFIG_ARG = "runtime-config"; | ||
| static constexpr auto INIT_COMPONENT_ALLOCATOR_SIZE = static_cast<size_t>(512U * 1024U); // 512KB | ||
|
|
||
|
|
@@ -53,4 +55,6 @@ struct ComponentState final : private vk::not_copyable { | |
| void parse_ini_arg(std::string_view, std::string_view) noexcept; | ||
|
|
||
| void parse_runtime_config_arg(std::string_view) noexcept; | ||
|
|
||
| void parse_command_line_arg(std::string_view) noexcept; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's make this function
constexprThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clang++gives error with message thatinline function ... is not defined