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

[k2] vkext builtins #1168

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions builtin-functions/kphp-light/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ require_once __DIR__ . '/file.txt';
require_once __DIR__ . '/hash.txt';
require_once __DIR__ . '/job-workers.txt';
require_once __DIR__ . '/rpc.txt';
require_once __DIR__ . '/serialize.txt';
require_once __DIR__ . '/string.txt';
require_once __DIR__ . '/server.txt';
require_once __DIR__ . '/kphp-toggles.txt';
require_once __DIR__ . '/kphp_internal.txt';
require_once __DIR__ . '/time.txt';
require_once __DIR__ . '/vkext.txt';

/** defined in runtime-core.h **/
function likely ($x ::: bool) ::: bool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,11 @@
<?php

/** @kphp-extern-func-info generate-stub */
function serialize($v ::: mixed) ::: string;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function unserialize ($v ::: string) ::: mixed;

/** @kphp-extern-func-info generate-stub */
function msgpack_serialize($v ::: mixed) ::: string | null;
/** @kphp-extern-func-info generate-stub */
function msgpack_deserialize($v ::: string) ::: mixed;
/** @kphp-extern-func-info can_throw generate-stub */
function msgpack_serialize_safe($v ::: mixed) ::: string;
/** @kphp-extern-func-info can_throw generate-stub */
function msgpack_deserialize_safe($v ::: string) ::: mixed;

function instance_serialize(object $instance) ::: string | null;
/** @kphp-extern-func-info can_throw */
function instance_serialize_safe(object $instance) ::: string;
/** @kphp-extern-func-info cpp_template_call */
function instance_deserialize($serialized ::: string, $to_type ::: string) ::: instance<^2>;
/** @kphp-extern-func-info cpp_template_call can_throw */
function instance_deserialize_safe($serialized ::: string, $to_type ::: string) ::: instance<^2>;

define('JSON_UNESCAPED_UNICODE', 1);
define('JSON_FORCE_OBJECT', 16);
define('JSON_PRETTY_PRINT', 128); // TODO: add actual support
define('JSON_PARTIAL_OUTPUT_ON_ERROR', 512);
define('JSON_PRESERVE_ZERO_FRACTION', 1024);

/** @kphp-generate-stub-class */
class JsonEncoder {
const rename_policy = 'none';
const visibility_policy = 'all';
Expand All @@ -52,4 +26,3 @@ class JsonEncoder {
/** @kphp-extern-func-info cpp_template_call */
static function from_json_impl(string $encoder_tag, string $json, string $class_name) ::: instance<^3>;
}

3 changes: 1 addition & 2 deletions builtin-functions/kphp-light/unsupported-functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ require_once __DIR__ . '/unsupported/math.txt';
require_once __DIR__ . '/unsupported/memcache.txt';
require_once __DIR__ . '/unsupported/misc.txt';
require_once __DIR__ . '/unsupported/regex.txt';
require_once __DIR__ . '/unsupported/serialize.txt';
require_once __DIR__ . '/unsupported/unsupported-serialize.txt';
require_once __DIR__ . '/unsupported/spl.txt';
require_once __DIR__ . '/unsupported/uberh3.txt';
require_once __DIR__ . '/unsupported/vkext.txt';
require_once __DIR__ . '/unsupported/unsupported-server.txt';
27 changes: 27 additions & 0 deletions builtin-functions/kphp-light/unsupported/unsupported-serialize.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/** @kphp-extern-func-info generate-stub */
function serialize($v ::: mixed) ::: string;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function unserialize ($v ::: string) ::: mixed;

/** @kphp-extern-func-info generate-stub */
function msgpack_serialize($v ::: mixed) ::: string | null;
/** @kphp-extern-func-info generate-stub */
function msgpack_deserialize($v ::: string) ::: mixed;
/** @kphp-extern-func-info can_throw generate-stub */
function msgpack_serialize_safe($v ::: mixed) ::: string;
/** @kphp-extern-func-info can_throw generate-stub */
function msgpack_deserialize_safe($v ::: string) ::: mixed;

function instance_serialize(object $instance) ::: string | null;
/** @kphp-extern-func-info can_throw */
function instance_serialize_safe(object $instance) ::: string;
/** @kphp-extern-func-info cpp_template_call */
function instance_deserialize($serialized ::: string, $to_type ::: string) ::: instance<^2>;
/** @kphp-extern-func-info cpp_template_call can_throw */
function instance_deserialize_safe($serialized ::: string, $to_type ::: string) ::: instance<^2>;

25 changes: 0 additions & 25 deletions builtin-functions/kphp-light/unsupported/vkext.txt

This file was deleted.

33 changes: 33 additions & 0 deletions builtin-functions/kphp-light/vkext.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

function vk_utf8_to_win ($text ::: string, $max_len ::: int = 0, $exit_on_error ::: bool = false) ::: string;
function vk_win_to_utf8 ($text ::: string, $escape ::: bool = true) ::: string;
function cp1251 ($utf8_string ::: string) ::: string;

function vk_flex ($name ::: string, $case_name ::: string, $sex ::: int, $type ::: string, $lang_id ::: int = 0) ::: string;

function vk_json_encode ($v ::: mixed) ::: string | false;
/** @kphp-extern-func-info can_throw */
function vk_json_encode_safe ($v ::: mixed) ::: string;

function vk_whitespace_pack ($str ::: string, $html_opt ::: bool = false) ::: string;

function vk_sp_simplify ($str ::: string) ::: string;
function vk_sp_full_simplify ($str ::: string) ::: string;
function vk_sp_deunicode ($str ::: string) ::: string;
function vk_sp_to_lower ($str ::: string) ::: string;
function vk_sp_to_upper ($str ::: string) ::: string;
function vk_sp_to_sort ($str ::: string) ::: string;
function vk_sp_remove_repeats ($str ::: string) ::: string;
function vk_sp_to_cyrillic ($str ::: string) ::: string;
function vk_sp_words_only ($str ::: string) ::: string;

function vk_stats_hll_merge($str ::: mixed) ::: string | false;
function vk_stats_hll_count($hll ::: string) ::: float | false;
function vk_stats_hll_create($a ::: array = array(), $size ::: int = 256) ::: string | false;
function vk_stats_hll_add($hll ::: string, $a ::: array) ::: string | false;
function vk_stats_hll_pack($hll ::: string) ::: string | false;
function vk_stats_hll_unpack($hll ::: string) ::: string | false;
function vk_stats_hll_is_packed($hll ::: string) ::: bool;


5 changes: 0 additions & 5 deletions common/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ prepend(COMMON_MAIN_SOURCES ${COMMON_DIR}/
crypto/aes256-${CMAKE_SYSTEM_PROCESSOR}.cpp

fast-backtrace.cpp
string-processing.cpp
kphp-tasks-lease/lease-worker-mode.cpp)

prepend(COMMON_KFS_SOURCES ${COMMON_DIR}/kfs/
Expand Down Expand Up @@ -54,8 +53,4 @@ set(COMMON_ALL_SOURCES
${COMMON_TL_SOURCES}
${COMMON_UCONTEXT_SOURCES})

if(COMPILER_CLANG)
set_source_files_properties(${COMMON_DIR}/string-processing.cpp PROPERTIES COMPILE_FLAGS -Wno-invalid-source-encoding)
endif()

vk_add_library(common_src OBJECT ${COMMON_ALL_SOURCES})
2 changes: 1 addition & 1 deletion compiler/make/objs-to-k2-component-target.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Objs2K2ComponentTarget : public Target {
ss << deps.back()->get_name() << " ";
}
// add vendored statically linking libs
std::vector<std::string> libs = split(RUNTIME_LINK_LIBS);
std::vector<std::string> libs = split(RUNTIME_LINK_LIBS, ';');
std::for_each(libs.cbegin(), libs.cend(), [&ss](const auto &lib) noexcept { ss << lib << " "; });
return ss.str();
}
Expand Down
13 changes: 10 additions & 3 deletions runtime-common/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
prepend(STDLIB_STRING stdlib/string/ string-functions.cpp
mbstring-functions.cpp)
prepend(STDLIB_STRING stdlib/string/ json-functions.cpp
json-writer.cpp
mbstring-functions.cpp
string-functions.cpp)
prepend(STDLIB_SERVER stdlib/server/ url-functions.cpp)
prepend(STDLIB_VKEXT stdlib/vkext/ vkext.cpp vkext_stats.cpp string-processing.cpp)

set(STDLIB_SRC ${STDLIB_STRING} ${STDLIB_SERVER})
if(COMPILER_CLANG)
set_source_files_properties(${RUNTIME_COMMON_DIR}/stdlib/vkext/string-processing.cpp PROPERTIES COMPILE_FLAGS -Wno-invalid-source-encoding)
endif()

set(STDLIB_SRC ${STDLIB_STRING} ${STDLIB_SERVER} ${STDLIB_VKEXT})
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

#pragma once

#include <string_view>
#include "runtime/context/runtime-context.h"

#include "runtime-common/core/runtime-core.h"
#include "runtime/json-functions.h"
#include "runtime/json-processor-utils.h"
#include "runtime-common/stdlib/string/json-functions.h"
#include "runtime-common/stdlib/string/json-processor-utils.h"
#include "runtime-common/stdlib/string/string-context.h"

template<class Tag>
class FromJsonVisitor {
Expand Down Expand Up @@ -91,13 +92,13 @@ class FromJsonVisitor {
}

void do_set(JsonRawString &value, const mixed &json) noexcept {
kphp_runtime_context.static_SB.clean();
if (!impl_::JsonEncoder{0, false, get_json_obj_magic_key()}.encode(json)) {
RuntimeContext::get().static_SB.clean();
if (!impl_::JsonEncoder{0, false, get_json_obj_magic_key()}.encode(json, RuntimeContext::get().static_SB)) {
error_.append("failed to decode @kphp-json raw_string field ");
error_.append(json_path_.to_string());
return;
}
value.str = kphp_runtime_context.static_SB.str();
value.str = RuntimeContext::get().static_SB.str();
}

template<class T>
Expand Down Expand Up @@ -169,14 +170,14 @@ class_instance<I> from_json_impl(const mixed &json, JsonPath &json_path) noexcep
FromJsonVisitor<Tag> visitor{json, impl_::IsJsonFlattenClass<I>::value, json_path};
instance.get()->accept(visitor);
if (visitor.has_error()) {
JsonEncoderError::msg.append(visitor.get_error());
StringLibContext::get().last_json_processor_error.append(visitor.get_error());
return {};
}
}
if constexpr (impl_::HasClassWakeupMethod<I>::value) {
instance.get()->wakeup(instance);
}
return JsonEncoderError::msg.empty() ? instance : class_instance<I>{};
return StringLibContext::get().last_json_processor_error.empty() ? instance : class_instance<I>{};
}

template<class Tag>
Expand All @@ -196,18 +197,19 @@ void FromJsonVisitor<Tag>::do_set(class_instance<I> &klass, const mixed &json) n

template<class ClassName, class Tag>
ClassName f$JsonEncoder$$from_json_impl(Tag /*tag*/, const string &json_string, const string &/*class_mame*/) noexcept {
JsonEncoderError::msg = {};
auto &msg = StringLibContext::get().last_json_processor_error;
msg = {};

auto [json, success] = json_decode(json_string, FromJsonVisitor<Tag>::get_json_obj_magic_key());

if (!success) {
JsonEncoderError::msg.append(json_string.empty() ? "provided empty json string" : "failed to parse json string");
msg.append(json_string.empty() ? "provided empty json string" : "failed to parse json string");
return {};
}
if constexpr (!impl_::IsJsonFlattenClass<typename ClassName::ClassType>::value) {
if (!json.is_array() || json.as_array().is_vector()) {
JsonEncoderError::msg.append("root element of json string must be an object type, got ");
JsonEncoderError::msg.append(json.get_type_c_str());
msg.append("root element of json string must be an object type, got ");
msg.append(json.get_type_c_str());
return {};
}
}
Expand All @@ -216,4 +218,6 @@ ClassName f$JsonEncoder$$from_json_impl(Tag /*tag*/, const string &json_string,
return from_json_impl<typename ClassName::ClassType, Tag>(json, json_path);
}

string f$JsonEncoder$$getLastError() noexcept;
inline string f$JsonEncoder$$getLastError() noexcept {
return StringLibContext::get().last_json_processor_error;
}
Loading
Loading