From ebda88d143163929a6148565610947f04bc1807e Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Mon, 16 Dec 2024 16:35:58 +0300 Subject: [PATCH] [k2] support php serialized confdata values (#1190) Also add check to 'from_mixed' that a mixed contains an object --- runtime-common/core/core-types/definition/mixed.inl | 3 ++- runtime-light/stdlib/confdata/confdata-functions.cpp | 3 ++- runtime-light/stdlib/file/file-system-functions.cpp | 6 +++--- runtime-light/stdlib/file/resource.cpp | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/runtime-common/core/core-types/definition/mixed.inl b/runtime-common/core/core-types/definition/mixed.inl index 90c79afa0b..81ad05012c 100644 --- a/runtime-common/core/core-types/definition/mixed.inl +++ b/runtime-common/core/core-types/definition/mixed.inl @@ -296,7 +296,8 @@ ResultClass from_mixed(const mixed &m, const string &) noexcept { php_error("Internal error. Class inside a mixed is not polymorphic"); return {}; } else { - return ResultClass::create_from_base_raw_ptr(dynamic_cast(m.as_object_ptr())); + return m.is_object() ? ResultClass::create_from_base_raw_ptr(dynamic_cast(m.as_object_ptr())) + : ResultClass{}; } } diff --git a/runtime-light/stdlib/confdata/confdata-functions.cpp b/runtime-light/stdlib/confdata/confdata-functions.cpp index ac5e75e872..afc89464ad 100644 --- a/runtime-light/stdlib/confdata/confdata-functions.cpp +++ b/runtime-light/stdlib/confdata/confdata-functions.cpp @@ -12,6 +12,7 @@ #include "runtime-common/core/runtime-core.h" #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-light/coroutine/task.h" #include "runtime-light/k2-platform/k2-api.h" #include "runtime-light/state/instance-state.h" @@ -30,7 +31,7 @@ mixed extract_confdata_value(tl::confdataValue &&confdata_value) noexcept { return {}; } if (confdata_value.is_php_serialized) { - php_critical_error("unimplemented"); // TODO + return f$unserialize(confdata_value.value); } else if (confdata_value.is_json_serialized) { return f$json_decode(confdata_value.value); } else { diff --git a/runtime-light/stdlib/file/file-system-functions.cpp b/runtime-light/stdlib/file/file-system-functions.cpp index 63f9659db6..324290eec3 100644 --- a/runtime-light/stdlib/file/file-system-functions.cpp +++ b/runtime-light/stdlib/file/file-system-functions.cpp @@ -51,7 +51,7 @@ Optional f$file_get_contents(const string &stream) noexcept { task_t> f$fwrite(const resource &stream, const string &text) noexcept { auto rsrc{from_mixed>(stream, {})}; - if (rsrc.is_null()) { + if (rsrc.is_null()) [[unlikely]] { php_warning("wrong resource in fwrite %s", stream.to_string().c_str()); co_return false; } @@ -61,7 +61,7 @@ task_t> f$fwrite(const resource &stream, const string &text) n bool f$fflush(const resource &stream) noexcept { auto rsrc{from_mixed>(stream, {})}; - if (rsrc.is_null()) { + if (rsrc.is_null()) [[unlikely]] { php_warning("wrong resource in fflush %s", stream.to_string().c_str()); return false; } @@ -72,7 +72,7 @@ bool f$fflush(const resource &stream) noexcept { bool f$fclose(const resource &stream) noexcept { auto rsrc{from_mixed>(stream, {})}; - if (rsrc.is_null()) { + if (rsrc.is_null()) [[unlikely]] { php_warning("wrong resource in fclose: %s", stream.to_string().c_str()); return false; } diff --git a/runtime-light/stdlib/file/resource.cpp b/runtime-light/stdlib/file/resource.cpp index 4efecd2fce..ffc9af9057 100644 --- a/runtime-light/stdlib/file/resource.cpp +++ b/runtime-light/stdlib/file/resource.cpp @@ -24,7 +24,7 @@ underlying_resource_t::underlying_resource_t(std::string_view scheme) noexcept break; } case resource_kind::STDOUT: { - if (instance_st.image_kind() == ImageKind::CLI || instance_st.image_kind() == ImageKind::Server) { + if (instance_st.image_kind() == ImageKind::CLI) { stream_d_ = instance_st.standard_stream(); } else { last_errc = k2::errno_einval;