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 2 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
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;
13 changes: 13 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,17 @@ 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
19 changes: 19 additions & 0 deletions runtime-light/stdlib/memory/memory-usage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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/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);
}
27 changes: 26 additions & 1 deletion runtime-light/stdlib/serialization/json-functions.h
apolyakov marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
// 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 &ctx = RuntimeContext::get();
ctx.static_SB.clean();
ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_ON;
impl_::JsonEncoder(0, simple_encode).encode(v, RuntimeContext::get().static_SB);
if (unlikely(ctx.sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) {
ctx.static_SB.clean();
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 {};
}
ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF;
return ctx.static_SB.str();
}
13 changes: 13 additions & 0 deletions runtime-light/stdlib/server/http-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,16 @@ void f$setrawcookie(const string &name, const string &value, int64_t expire_or_o
}
kphp::http::header({static_SB_spare.c_str(), static_SB_spare.size()}, false, kphp::http::status::NO_STATUS);
}

array<string> f$headers_list() noexcept {
apolyakov marked this conversation as resolved.
Show resolved Hide resolved
auto &http_server_instance_st{HttpServerInstanceState::get()};
apolyakov marked this conversation as resolved.
Show resolved Hide resolved
const auto &headers{http_server_instance_st.headers()};

array<string> list;
apolyakov marked this conversation as resolved.
Show resolved Hide resolved
for (const auto & header : headers) {
string value{(header.first + ": " + header.second).c_str()};
apolyakov marked this conversation as resolved.
Show resolved Hide resolved
list.push_back(std::move(value));
}

return list;
}
2 changes: 2 additions & 0 deletions runtime-light/stdlib/server/http-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ 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);
}

array<string> f$headers_list() noexcept;
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
37 changes: 37 additions & 0 deletions runtime-light/stdlib/system/system-functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2025 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include <iconv.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(&cd, output_encoding.c_str(), input_encoding.c_str()) != k2::errno_ok) {
apolyakov marked this conversation as resolved.
Show resolved Hide resolved
php_warning("unsupported iconv from \"%s\" to \"%s\"", input_encoding.c_str(), output_encoding.c_str());
return false;
}

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(&res, cd, &input_buf, &input_len, &output_buf, &output_len) == k2::errno_ok) {
output_str.shrink(static_cast<string::size_type>(output_buf - output_str.c_str()));
k2::iconv_close(cd);
return output_str;
}

k2::iconv(&res, cd, nullptr, nullptr, nullptr, nullptr);
}

k2::iconv_close(cd);
apolyakov marked this conversation as resolved.
Show resolved Hide resolved

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