Skip to content

Commit 5a972c2

Browse files
authored
[k2] add msgpack serialization functions (#1473)
1 parent 36f5582 commit 5a972c2

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

builtin-functions/kphp-light/stdlib/serialize-functions.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ function instance_serialize_safe(object $instance) ::: string;
5454
/** @kphp-extern-func-info cpp_template_call can_throw */
5555
function instance_deserialize_safe($serialized ::: string, $to_type ::: string) ::: instance<^2>;
5656

57-
// ===== UNSUPPORTED =====
58-
59-
/** @kphp-extern-func-info can_throw stub generation-required */
57+
/** @kphp-extern-func-info can_throw */
6058
function msgpack_serialize_safe($v ::: mixed) ::: string;
61-
/** @kphp-extern-func-info can_throw stub generation-required */
59+
60+
/** @kphp-extern-func-info can_throw */
6261
function msgpack_deserialize_safe($v ::: string) ::: mixed;

runtime-light/stdlib/serialization/msgpack-functions.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,26 @@ ResultClass f$instance_deserialize_safe(const string& buffer, const string& /*un
7777
}
7878
return res;
7979
}
80+
81+
template<class T>
82+
string f$msgpack_serialize_safe(const T& value) noexcept {
83+
string err_msg;
84+
auto res{f$msgpack_serialize(value, std::addressof(err_msg))};
85+
if (!err_msg.empty()) {
86+
THROW_EXCEPTION(kphp::exception::make_throwable<C$Exception>(std::move(err_msg)));
87+
return {};
88+
}
89+
kphp::log::assertion(res.has_value());
90+
return res.val();
91+
}
92+
93+
template<class ResultType = mixed>
94+
ResultType f$msgpack_deserialize_safe(const string& buffer) noexcept {
95+
string err_msg;
96+
auto res{f$msgpack_deserialize(buffer, std::addressof(err_msg))};
97+
if (!err_msg.empty()) {
98+
THROW_EXCEPTION(kphp::exception::make_throwable<C$Exception>(std::move(err_msg)));
99+
return {};
100+
}
101+
return res;
102+
}

tests/phpt/msgpack_serialize/014_deserialize_safe.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@ok k2_skip
1+
@ok
22
<?php
33

44
require_once 'kphp_tester_include.php';

tests/phpt/msgpack_serialize/024_serialize_vector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@ok k2_skip
1+
@ok
22
<?php
33
require_once 'kphp_tester_include.php';
44

0 commit comments

Comments
 (0)