Skip to content

Commit

Permalink
Move serialize & unserialize builtins to runtime-common (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolyakov authored Dec 9, 2024
1 parent 8490a28 commit c21b311
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 51 deletions.
6 changes: 2 additions & 4 deletions builtin-functions/kphp-light/math.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function mt_getrandmax() ::: int;

function mt_srand ($seed ::: int = PHP_INT_MIN) ::: void;

function rand ($l ::: int = TODO_OVERLOAD, $r ::: int = TODO_OVERLOAD) ::: int;

// === UNSUPPORTED ===

/**
Expand Down Expand Up @@ -113,10 +115,6 @@ function levenshtein ($str1 ::: string, $str2 ::: string) ::: int;
/** @kphp-extern-func-info generate-stub */
function uniqid ($prefix ::: string = '', $more_entropy ::: bool = false) ::: string;

/** @kphp-extern-func-info generate-stub */
function rand ($l ::: int = TODO_OVERLOAD, $r ::: int = TODO_OVERLOAD) ::: int;


/**
* @kphp-extern-func-info cpp_template_call cpp_variadic_call
* @kphp-pure-function
Expand Down
9 changes: 4 additions & 5 deletions builtin-functions/kphp-light/serialize.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ class JsonEncoder {
static function from_json_impl(string $encoder_tag, string $json, string $class_name) ::: instance<^3>;
}

// ===== UNSUPPORTED =====
function serialize($v ::: mixed): string;

/** @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;
function unserialize ($v ::: string): mixed;

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

/** @kphp-extern-func-info generate-stub */
function msgpack_serialize($v ::: mixed) ::: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,53 @@
// Copyright (c) 2020 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

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

#include "runtime/context/runtime-context.h"
#include "runtime-common/core/runtime-core.h"

void impl_::PhpSerializer::serialize(bool b) noexcept {
kphp_runtime_context.static_SB.reserve(4);
kphp_runtime_context.static_SB.append_char('b');
kphp_runtime_context.static_SB.append_char(':');
kphp_runtime_context.static_SB.append_char(static_cast<char>(b + '0'));
kphp_runtime_context.static_SB.append_char(';');
auto &static_SB{RuntimeContext::get().static_SB};
static_SB.reserve(4);
static_SB.append_char('b');
static_SB.append_char(':');
static_SB.append_char(static_cast<char>(b + '0'));
static_SB.append_char(';');
}

void impl_::PhpSerializer::serialize(int64_t i) noexcept {
kphp_runtime_context.static_SB.reserve(24);
kphp_runtime_context.static_SB.append_char('i');
kphp_runtime_context.static_SB.append_char(':');
kphp_runtime_context.static_SB << i;
kphp_runtime_context.static_SB.append_char(';');
auto &static_SB{RuntimeContext::get().static_SB};
static_SB.reserve(24);
static_SB.append_char('i');
static_SB.append_char(':');
static_SB << i;
static_SB.append_char(';');
}

void impl_::PhpSerializer::serialize(double f) noexcept {
kphp_runtime_context.static_SB.append("d:", 2);
kphp_runtime_context.static_SB << f << ';';
void impl_::PhpSerializer::serialize(double d) noexcept {
auto &static_SB{RuntimeContext::get().static_SB};
static_SB.append("d:", 2);
static_SB << d << ';';
}

void impl_::PhpSerializer::serialize(const string &s) noexcept {
auto &static_SB{RuntimeContext::get().static_SB};
string::size_type len = s.size();
kphp_runtime_context.static_SB.reserve(25 + len);
kphp_runtime_context.static_SB.append_char('s');
kphp_runtime_context.static_SB.append_char(':');
kphp_runtime_context.static_SB << len;
kphp_runtime_context.static_SB.append_char(':');
kphp_runtime_context.static_SB.append_char('"');
kphp_runtime_context.static_SB.append_unsafe(s.c_str(), len);
kphp_runtime_context.static_SB.append_char('"');
kphp_runtime_context.static_SB.append_char(';');
static_SB.reserve(25 + len);
static_SB.append_char('s');
static_SB.append_char(':');
static_SB << len;
static_SB.append_char(':');
static_SB.append_char('"');
static_SB.append_unsafe(s.c_str(), len);
static_SB.append_char('"');
static_SB.append_char(';');
}

void impl_::PhpSerializer::serialize_null() noexcept {
kphp_runtime_context.static_SB.reserve(2);
kphp_runtime_context.static_SB.append_char('N');
kphp_runtime_context.static_SB.append_char(';');
auto &static_SB{RuntimeContext::get().static_SB};
static_SB.reserve(2);
static_SB.append_char('N');
static_SB.append_char(';');
}

void impl_::PhpSerializer::serialize(const mixed &v) noexcept {
Expand Down Expand Up @@ -159,7 +164,7 @@ int do_unserialize(const char *s, int s_len, mixed &out_var_value) noexcept {
s_len -= j + 4;

array_size size(len, false);
if (s[0] == 'i') {//try to cheat
if (s[0] == 'i') { // try to cheat
size = array_size(len, s[1] == ':' && s[2] == '0' && s[3] == ';');
}
array<mixed> res(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "common/mixin/not_copyable.h"
#include "runtime-common/core/runtime-core.h"
#include "runtime/context/runtime-context.h"

namespace impl_ {

Expand All @@ -30,9 +29,10 @@ class PhpSerializer : vk::not_copyable {

template<class T>
void PhpSerializer::serialize(const array<T> &arr) noexcept {
kphp_runtime_context.static_SB.append("a:", 2);
kphp_runtime_context.static_SB << arr.count();
kphp_runtime_context.static_SB.append(":{", 2);
auto &static_SB{RuntimeContext::get().static_SB};
static_SB.append("a:", 2);
static_SB << arr.count();
static_SB.append(":{", 2);
for (auto p : arr) {
auto key = p.get_key();
if (array<T>::is_int_key(key)) {
Expand All @@ -42,7 +42,7 @@ void PhpSerializer::serialize(const array<T> &arr) noexcept {
}
serialize(p.get_value());
}
kphp_runtime_context.static_SB << '}';
static_SB << '}';
}

template<class T>
Expand All @@ -61,9 +61,10 @@ void PhpSerializer::serialize(const Optional<T> &opt) noexcept {

template<class T>
string f$serialize(const T &v) noexcept {
kphp_runtime_context.static_SB.clean();
auto &static_SB{RuntimeContext::get().static_SB};
static_SB.clean();
impl_::PhpSerializer::serialize(v);
return kphp_runtime_context.static_SB.str();
return static_SB.str();
}

mixed f$unserialize(const string &v) noexcept;
Expand Down
7 changes: 4 additions & 3 deletions runtime-common/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
prepend(STDLIB_ARRAY stdlib/array/ array-functions.cpp)
prepend(STDLIB_SERIALIZATION stdlib/serialization/ json-functions.cpp
json-writer.cpp)
json-writer.cpp serialize-functions.cpp)
prepend(STDLIB_STRING stdlib/string/ mbstring-functions.cpp
string-functions.cpp)
string-functions.cpp)
prepend(STDLIB_SERVER stdlib/server/ url-functions.cpp)

set(STDLIB_SRC ${STDLIB_ARRAY} ${STDLIB_SERIALIZATION} ${STDLIB_STRING} ${STDLIB_SERVER})
set(STDLIB_SRC ${STDLIB_ARRAY} ${STDLIB_SERIALIZATION} ${STDLIB_STRING}
${STDLIB_SERVER})
8 changes: 8 additions & 0 deletions runtime-light/stdlib/math/random-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ inline void f$mt_srand(int64_t seed = std::numeric_limits<int64_t>::min()) noexc
}
RandomInstanceState::get().mt_gen.seed(static_cast<std::mt19937_64::result_type>(seed));
}

inline int64_t f$rand() noexcept {
return f$mt_rand();
}

inline int64_t f$rand(int64_t l, int64_t r) noexcept {
return f$mt_rand(l, r);
}
2 changes: 1 addition & 1 deletion runtime/memcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "runtime-common/core/utils/kphp-assert-core.h"
#include "runtime-common/stdlib/serialization/json-functions.h"
#include "runtime-common/stdlib/serialization/serialize-functions.h"
#include "runtime/array_functions.h"
#include "runtime/net_events.h"
#include "runtime/serialize-functions.h"
#include "runtime/zlib.h"
#include "server/php-queries.h"

Expand Down
1 change: 0 additions & 1 deletion runtime/runtime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ prepend(KPHP_RUNTIME_SOURCES ${BASE_DIR}/runtime/
rpc.cpp
rpc_extra_info.cpp
serialize-context.cpp
serialize-functions.cpp
storage.cpp
streams.cpp
string_functions.cpp
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/444_serialize.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok benchmark k2_skip
@ok benchmark
<?php

$str[0] = 'a:40:{s:2:"id";s:7:"6354412";s:5:"email";s:13:"levlam@levlam";s:10:"first_name";s:7:"Àëåêñåé";s:9:"last_name";s:5:"Ëåâèí";s:3:"sex";s:1:"2";s:5:"photo";s:15:"9xxxbxxxd8x:001";s:10:"university";s:1:"1";s:15:"university_name";s:5:"ÑÏáÃÓ";s:12:"faculty_name";s:23:"Ìàòåìàòèêî-ìåõàíè÷åñêèé";s:10:"graduation";s:4:"2010";s:7:"faculty";s:1:"7";s:6:"domain";s:6:"levlam";s:6:"joined";s:10:"1200875212";s:6:"logged";s:10:"1252407393";s:10:"photo_data";s:0:"";s:6:"server";s:4:"4347";s:7:"server2";s:1:"0";s:11:"uni_country";s:1:"1";s:8:"uni_city";s:1:"2";s:11:"time_offset";s:1:"0";s:14:"services_bits1";s:9:"633211943";s:14:"services_bits2";s:9:"562802944";s:14:"services_bits3";s:4:"1880";s:14:"services_bits4";s:2:"87";s:8:"bday_day";s:1:"9";s:10:"bday_month";s:1:"9";s:9:"bday_year";s:4:"1987";s:15:"bday_visibility";s:1:"1";s:12:"mobile_phone";s:0:"";s:10:"home_phone";s:0:"";s:4:"lang";s:1:"0";s:7:"site_id";s:1:"0";s:9:"reg_phone";s:11:"12345678901";s:16:"reg_phone_status";s:1:"2";s:9:"is_banned";s:1:"0";s:8:"reg_step";s:1:"0";s:12:"profile_type";s:1:"0";s:7:"version";i:6;s:14:"first_name_lat";s:6:"Alexey";s:13:"last_name_lat";s:5:"Levin";}';
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/490_serialize.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok k2_skip
@ok
<?php
$x = null;
$y = null;
Expand Down

0 comments on commit c21b311

Please sign in to comment.