Skip to content
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

add few builtins in runtime-light #1225

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions builtin-functions/kphp-light/server.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ function setcookie ($name ::: string, $value ::: string, $expire_or_options :::

function setrawcookie ($name ::: string, $value ::: string, $expire_or_options ::: int = 0, $path ::: string = '', $domain ::: string = '', $secure ::: bool = false, $http_only ::: bool = false): void;

function headers_list () ::: string[];

function ip2long ($ip ::: string) ::: int | false;

function ip2ulong ($ip ::: string) ::: string | false;

function long2ip ($ip ::: int) ::: string;

function memory_get_usage ($real_usage ::: bool = false) ::: int;

function memory_get_peak_usage ($real_usage ::: bool = false) ::: int;
2 changes: 2 additions & 0 deletions builtin-functions/kphp-light/system.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
function php_uname($mode ::: string = "a"): string;

function posix_getpid(): int;

function iconv ($input_encoding ::: string, $output_encoding ::: string, $input_str ::: string) ::: string | false;
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ function get_kphp_cluster_name(): string;
/** @kphp-extern-func-info generate-stub */
function get_webserver_stats() ::: tuple(int, int, int, int);

/** @kphp-extern-func-info generate-stub */
function iconv ($input_encoding ::: string, $output_encoding ::: string, $input_str ::: string) ::: string | false;


/** @kphp-extern-func-info generate-stub */
function ini_set ($s ::: string, $v ::: string) ::: bool;
Expand Down Expand Up @@ -41,10 +38,6 @@ function inet_pton ($address ::: string) ::: string | false;
function kphp_get_runtime_config() ::: mixed;


/** @kphp-extern-func-info generate-stub */
function memory_get_usage ($real_usage ::: bool = false) ::: int;
/** @kphp-extern-func-info generate-stub */
function memory_get_peak_usage ($real_usage ::: bool = false) ::: int;
/** @kphp-extern-func-info generate-stub */
function memory_get_total_usage() ::: int;
/** @kphp-extern-func-info generate-stub */
Expand Down Expand Up @@ -85,8 +78,6 @@ function debug_backtrace() ::: string[][];

function estimate_memory_usage($value ::: any) ::: int;

/** @kphp-extern-func-info generate-stub */
function headers_list () ::: string[];
/** @kphp-extern-func-info generate-stub */
function send_http_103_early_hints($headers ::: string[]) ::: void;
/** @kphp-extern-func-info generate-stub */
Expand Down
5 changes: 2 additions & 3 deletions builtin-functions/kphp-light/vkext.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function vk_whitespace_pack ($str ::: string, $html_opt ::: bool = false) ::: st

function vk_sp_full_simplify ($str ::: string) ::: string;

function vk_json_encode_safe ($v ::: mixed) ::: string;

// ===== UNSUPPORTED =====

/** @kphp-extern-func-info generate-stub */
Expand All @@ -29,6 +31,3 @@ function vk_stats_hll_count($hll ::: string) ::: float | false;

/** @kphp-extern-func-info generate-stub */
function vk_flex ($name ::: string, $case_name ::: string, $sex ::: int, $type ::: string, $lang_id ::: int = 0) ::: string;

/** @kphp-extern-func-info can_throw generate-stub */
function vk_json_encode_safe ($v ::: mixed) ::: string;
12 changes: 12 additions & 0 deletions runtime-light/k2-platform/k2-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,16 @@ inline int32_t tcp_connect(uint64_t *socket_d, const char *host, size_t host_len
return k2_tcp_connect(socket_d, host, host_len);
}

inline int32_t iconv_open(void **iconv_cd, const char *tocode, const char *fromcode) noexcept {
return k2_iconv_open(iconv_cd, tocode, fromcode);
}

inline void iconv_close(void *iconv_cd) noexcept {
k2_iconv_close(iconv_cd);
}

inline int32_t iconv(size_t *result, void *iconv_cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) noexcept {
return k2_iconv(result, iconv_cd, inbuf, inbytesleft, outbuf, outbytesleft);
}

} // namespace k2
34 changes: 34 additions & 0 deletions runtime-light/k2-platform/k2-header.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,40 @@ uint32_t k2_env_value_len(uint32_t env_num);
*/
void k2_env_fetch(uint32_t env_num, char *key, char *value);

// ---- libc analogues, designed to work instance-local ----

/**
* Works instance-local.
* Does not return previous locale.
* @return: `0` on success, `errno != 0` otherwise
*/
int32_t k2_uselocale(int32_t category, const char *locale);

/**
* Works instance-local.
* Synonym for libc `nl_langinfo(NL_LOCALE_NAME(category))`
*
* The returned string is read-only and will be valid until k2_uselocale is called;
*
* Note: LC_ALL is not supported for now :(
*
* @return: current locale name.
* if `category` is invalid, as empty string returned.
*/
char *k2_current_locale_name(int32_t category);

/**
* @return: `0` on success, `errno != 0` otherwise
*/
int32_t k2_iconv_open(void **iconv_cd, const char *tocode, const char *fromcode);

void k2_iconv_close(void *iconv_cd);

/**
* @return: `0` on success, `errno != 0` otherwise
*/
int32_t k2_iconv(size_t *result, void *iconv_cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);

#ifdef __cplusplus
}
#endif
Expand Down
21 changes: 21 additions & 0 deletions runtime-light/stdlib/memory/memory-usage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2025 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstdint>

#include "runtime-common/core/allocator/runtime-allocator.h"
apolyakov marked this conversation as resolved.
Show resolved Hide resolved

inline int64_t f$memory_get_peak_usage(bool real_usage = false) noexcept {
if (real_usage) {
return static_cast<int64_t>(RuntimeAllocator::get().memory_resource.get_memory_stats().max_real_memory_used);
} else {
return static_cast<int64_t>(RuntimeAllocator::get().memory_resource.get_memory_stats().max_memory_used);
}
}

inline int64_t f$memory_get_usage([[maybe_unused]] bool real_usage = false) noexcept {
return static_cast<int64_t>(RuntimeAllocator::get().memory_resource.get_memory_stats().memory_used);
}
25 changes: 24 additions & 1 deletion runtime-light/stdlib/serialization/json-functions.h
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
// todo:k2 implement string f$vk_json_encode_safe(const T &v, bool simple_encode = true) noexcept
// Compiler for PHP (aka KPHP)
// Copyright (c) 2025 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include "runtime-common/core/runtime-core.h"
#include "runtime-common/stdlib/serialization/json-functions.h"

template<class T>
string f$vk_json_encode_safe(const T &v, bool simple_encode = true) noexcept {
auto &rt_ctx{RuntimeContext::get()};
rt_ctx.static_SB.clean();
rt_ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_ON;
impl_::JsonEncoder(0, simple_encode).encode(v, RuntimeContext::get().static_SB);
if (rt_ctx.sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED) [[unlikely]] {
rt_ctx.static_SB.clean();
rt_ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF;
php_critical_error("vk_json_encode_safe tried to throw exception but it unsupported in runtime light");
return {};
}
rt_ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF;
return rt_ctx.static_SB.str();
}
16 changes: 16 additions & 0 deletions runtime-light/stdlib/server/http-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cstdint>
#include <string_view>
#include <utility>

#include "runtime-common/core/runtime-core.h"
#include "runtime-common/stdlib/server/url-functions.h"
Expand All @@ -32,3 +33,18 @@ inline void f$setcookie(const string &name, const string &value = {}, int64_t ex
bool secure = false, bool http_only = false) noexcept {
f$setrawcookie(name, f$urlencode(value), expire_or_options, path, domain, secure, http_only);
}

inline array<string> f$headers_list() noexcept {
const auto &headers{HttpServerInstanceState::get().headers()};
constexpr std::string_view header_separator{": "};

array<string> list{array_size{static_cast<int64_t>(headers.size()), true}};
for (const auto &[header_name, header_value] : headers) {
list.push_back(string{static_cast<string::size_type>(header_name.size() + header_value.size() + header_separator.size()), true}
.append(header_name.c_str())
.append(header_separator.data())
.append(header_value.c_str()));
}

return list;
}
1 change: 1 addition & 0 deletions runtime-light/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ prepend(
string/regex-functions.cpp
string/regex-state.cpp
string/string-state.cpp
system/system-functions.cpp
system/system-state.cpp
file/file-system-functions.cpp
file/file-system-state.cpp
Expand Down
41 changes: 41 additions & 0 deletions runtime-light/stdlib/system/system-functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2025 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include <cstddef>
#include <iconv.h>
#include <memory>

#include "common/containers/final_action.h"
#include "runtime-common/core/utils/kphp-assert-core.h"
#include "runtime-light/k2-platform/k2-api.h"
#include "runtime-light/stdlib/system/system-functions.h"

Optional<string> f$iconv(const string &input_encoding, const string &output_encoding, const string &input_str) noexcept {
iconv_t cd{};
if (k2::iconv_open(std::addressof(cd), output_encoding.c_str(), input_encoding.c_str()) != k2::errno_ok) [[unlikely]] {
php_warning(R"(unsupported iconv from "%s" to "%s")", input_encoding.c_str(), output_encoding.c_str());
return false;
}

vk::final_action finalizer{[&cd]() { k2::iconv_close(cd); }};

for (int mul = 4; mul <= 16; mul *= 4) {
string output_str{mul * input_str.size(), false};

size_t input_len{input_str.size()};
size_t output_len{output_str.size()};
char *input_buf{const_cast<char *>(input_str.c_str())};
char *output_buf{output_str.buffer()};
size_t res{};
if (k2::iconv(std::addressof(res), cd, std::addressof(input_buf), std::addressof(input_len), std::addressof(output_buf), std::addressof(output_len))
== k2::errno_ok) {
output_str.shrink(static_cast<string::size_type>(output_buf - output_str.c_str()));
return output_str;
}

k2::iconv(std::addressof(res), cd, nullptr, nullptr, nullptr, nullptr);
}

return false;
}
2 changes: 2 additions & 0 deletions runtime-light/stdlib/system/system-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ inline string f$php_uname(const string &mode = string{1, 'a'}) noexcept {
return image_st.uname_info_a;
}
}

Optional<string> f$iconv(const string &input_encoding, const string &output_encoding, const string &input_str) noexcept;
2 changes: 1 addition & 1 deletion tests/phpt/json/31_russian_cp1251.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok k2_skip
@ok
<?php
// important! this file is saved as cp1251
require_once 'kphp_tester_include.php';
Expand Down
Loading