Skip to content

Commit 07fc8a9

Browse files
authored
[k2] add fetch_lookup_int, fetch_lookup_data (#1708)
1 parent 3fd73cb commit 07fc8a9

5 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎builtin-functions/kphp-light/stdlib/rpc.txt‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ function rpc_fetch_typed_responses_synchronously(int[] $query_ids) ::: @tl\RpcRe
6969
/** @kphp-extern-func-info can_throw */
7070
function fetch_int () ::: int;
7171

72+
/** @kphp-extern-func-info can_throw */
73+
function fetch_lookup_int () ::: int;
74+
75+
/** @kphp-extern-func-info can_throw */
76+
function fetch_lookup_data ($x4_bytes_length ::: int) ::: string;
77+
7278
/** @kphp-extern-func-info can_throw */
7379
function fetch_byte () ::: int;
7480

@@ -139,9 +145,6 @@ function new_rpc_connection ($str ::: string, $port ::: int, $actor_id ::: mixed
139145
/** @kphp-extern-func-info interruptible stub generation-required */
140146
function store_finish() ::: bool;
141147

142-
/** @kphp-extern-func-info interruptible stub generation-required can_throw */
143-
function fetch_lookup_int () ::: int;
144-
145148
/** @kphp-generate-stub-class */
146149
final class RpcConnection {
147150
/** @kphp-extern-func-info stub generation-required */

‎runtime-light/stdlib/rpc/rpc-api.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ inline int64_t f$fetch_int() noexcept {
143143
return DEFAULT_VALUE;
144144
}
145145

146+
inline int64_t f$fetch_lookup_int() noexcept {
147+
static constexpr int64_t DEFAULT_VALUE{0};
148+
auto tl_fetcher{RpcServerInstanceState::get().tl_fetcher}; // copy
149+
if (tl::i32 val{}; val.fetch(tl_fetcher)) [[likely]] {
150+
return static_cast<int64_t>(val.value);
151+
}
152+
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
153+
return DEFAULT_VALUE;
154+
}
155+
156+
inline string f$fetch_lookup_data(int64_t x4_bytes_length) noexcept {
157+
static constexpr size_t BYTES_PER_WORD{sizeof(int32_t)};
158+
const auto& tl_fetcher{RpcServerInstanceState::get().tl_fetcher};
159+
if (x4_bytes_length < 0 || static_cast<uint64_t>(x4_bytes_length) > (tl_fetcher.remaining() / BYTES_PER_WORD)) [[unlikely]] {
160+
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
161+
return {};
162+
}
163+
164+
const auto bytes_length{static_cast<size_t>(x4_bytes_length) * BYTES_PER_WORD};
165+
const auto bytes{tl_fetcher.view().subspan(tl_fetcher.pos(), bytes_length)};
166+
return string{reinterpret_cast<const char*>(bytes.data()), static_cast<string::size_type>(bytes.size())};
167+
}
168+
146169
inline int64_t f$fetch_long() noexcept {
147170
static constexpr int64_t DEFAULT_VALUE = 0;
148171
if (tl::i64 val{}; val.fetch(RpcServerInstanceState::get().tl_fetcher)) [[likely]] {

‎tests/k2_kphp_tracked_builtins_list.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ extract_kphp_rpc_response_extra_info
176176
fclose
177177
fetch_double
178178
fetch_int
179+
fetch_lookup_int
180+
fetch_lookup_data
179181
fetch_long
180182
fetch_string
181183
fflush
@@ -491,7 +493,6 @@ openssl_x509_verify
491493
preg_last_error
492494
new_rpc_connection
493495
store_finish
494-
fetch_lookup_int
495496
gethostbynamel
496497
localtime
497498
system

‎tests/python/tests/tl_primitives/php/index.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@
2323
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
2424
}
2525
break;
26+
case 'fetch_lookup_int':
27+
$pos_before = fetch_get_pos();
28+
$res = fetch_lookup_int();
29+
$pos_after = fetch_get_pos();
30+
if ($pos_after !== $pos_before) {
31+
critical_error('fetch_lookup_int changed fetch position from ' . $pos_before . ' to ' . $pos_after);
32+
}
33+
if ($res != $op['expected']) {
34+
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
35+
}
36+
break;
37+
case 'fetch_lookup_data':
38+
$pos_before = fetch_get_pos();
39+
$res = fetch_lookup_data($op['x4_bytes_length']);
40+
$pos_after = fetch_get_pos();
41+
if ($pos_after !== $pos_before) {
42+
critical_error('fetch_lookup_data changed fetch position from ' . $pos_before . ' to ' . $pos_after);
43+
}
44+
if ($res != $op['expected']) {
45+
critical_error('expected ' . $op['expected'] . ' but ' . $res . ' found');
46+
}
47+
break;
2648
case 'fetch_long':
2749
$res = fetch_long();
2850
if ($res != $op['expected']) {

‎tests/python/tests/tl_primitives/test_tl_primitives.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ def _assert_fetch_int(expected_result: int, expected_exception: bool = False):
3030
return _operation('fetch_int', dict(), expected_result, expected_exception=expected_exception)
3131

3232

33+
def _assert_fetch_lookup_int(expected_result: int, expected_exception: bool = False):
34+
return _operation('fetch_lookup_int', dict(), expected_result, expected_exception=expected_exception)
35+
36+
37+
def _assert_fetch_lookup_data(x4_bytes_length: int, expected_result: str, expected_exception: bool = False):
38+
return _operation(
39+
'fetch_lookup_data', dict(x4_bytes_length=x4_bytes_length), expected_result,
40+
expected_exception=expected_exception
41+
)
42+
43+
3344
def _assert_fetch_long(expected_result: int, expected_exception: bool = False):
3445
return _operation('fetch_long', dict(), expected_result, expected_exception=expected_exception)
3546

@@ -53,6 +64,8 @@ def test_fetch(self):
5364
_assert_rpc_parse(
5465
'\x12\x34\x56\x78\x42\x00\x00\x00\x00\x00\x00\x00\x68\x56\x5b\x56\x06\x22\x09\x40\x03abc', True
5566
),
67+
_assert_fetch_lookup_int(0x78563412),
68+
_assert_fetch_lookup_data(1, '\x12\x34\x56\x78'),
5669
_assert_fetch_int(0x78563412),
5770
_assert_fetch_long(0x42),
5871
_assert_fetch_double(3.14161365),

0 commit comments

Comments
 (0)