hpb: Migrate MoveMessage out of internal to interop

PiperOrigin-RevId: 673115936
pull/18210/head
Hong Shin 6 months ago committed by Copybara-Service
parent 4a039d89dd
commit 69dbe6fd6f
  1. 34
      hpb/BUILD
  2. 20
      hpb/backend/upb/BUILD
  3. 14
      hpb/backend/upb/interop.h
  4. 10
      hpb/backend/upb/interop_test.cc
  5. 25
      hpb/internal.h
  6. 3
      hpb_generator/gen_messages.cc
  7. 1
      hpb_generator/protoc-gen-upb-protos.cc

@ -82,53 +82,19 @@ cc_library(
copts = UPB_DEFAULT_CPPOPTS, copts = UPB_DEFAULT_CPPOPTS,
) )
cc_library(
name = "internal",
hdrs = ["internal.h"],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//upb:friends"],
deps = [
":hpb",
"//upb:mem",
"//upb:message",
"//upb:mini_table",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:str_format",
],
)
# Common support code for C++ generated code. # Common support code for C++ generated code.
cc_library( cc_library(
name = "generated_hpb_support", name = "generated_hpb_support",
hdrs = [
"internal.h",
],
copts = UPB_DEFAULT_CPPOPTS, copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//hpb/bazel:__pkg__"], visibility = ["//hpb/bazel:__pkg__"],
deps = [ deps = [
":hpb", ":hpb",
":internal",
":repeated_field", ":repeated_field",
"//upb:mem", "//upb:mem",
"//upb:message", "//upb:message",
], ],
) )
cc_test(
name = "internal_test",
srcs = ["internal_test.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":internal",
"//src/google/protobuf/compiler/hpb/tests:test_model_upb_cc_proto",
"//src/google/protobuf/compiler/hpb/tests:test_model_upb_proto",
"//upb:mem",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
upb_cc_proto_library_copts( upb_cc_proto_library_copts(
name = "upb_cc_proto_library_copts", name = "upb_cc_proto_library_copts",
copts = UPB_DEFAULT_CPPOPTS, copts = UPB_DEFAULT_CPPOPTS,

@ -5,6 +5,11 @@
# license that can be found in the LICENSE file or at # license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd # https://developers.google.com/open-source/licenses/bsd
load(
"//upb/bazel:build_defs.bzl",
"UPB_DEFAULT_CPPOPTS",
)
package(default_applicable_licenses = ["//:license"]) package(default_applicable_licenses = ["//:license"])
cc_library( cc_library(
@ -35,3 +40,18 @@ cc_library(
"//upb:mini_table", "//upb:mini_table",
], ],
) )
cc_test(
name = "interop_test",
srcs = ["interop_test.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":interop",
"//src/google/protobuf/compiler/hpb/tests:test_model_upb_cc_proto",
"//src/google/protobuf/compiler/hpb/tests:test_model_upb_proto",
"//upb:mem",
"//upb:message",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

@ -18,6 +18,20 @@
namespace hpb::interop::upb { namespace hpb::interop::upb {
/**
* Moves ownership of a message created in a source arena.
*
* Utility function to provide a way to move ownership across languages or VMs.
*
* Warning: any minitable skew will incur arbitrary memory access. Ensuring
* minitable compatibility is the responsibility of the caller.
*/
// TODO: b/365824801 - consider rename to OwnMessage
template <typename T>
T MoveMessage(upb_Message* msg, upb_Arena* arena) {
return T(msg, arena);
}
template <typename T> template <typename T>
const upb_MiniTable* GetMiniTable(const T*) { const upb_MiniTable* GetMiniTable(const T*) {
return T::minitable(); return T::minitable();

@ -5,19 +5,19 @@
// license that can be found in the LICENSE file or at // license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
#include "google/protobuf/hpb/internal.h" #include "google/protobuf/hpb/backend/upb/interop.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "google/protobuf/compiler/hpb/tests/test_model.upb.h" #include "google/protobuf/compiler/hpb/tests/test_model.upb.h"
#include "google/protobuf/compiler/hpb/tests/test_model.upb.proto.h" #include "google/protobuf/compiler/hpb/tests/test_model.upb.proto.h"
#include "upb/mem/arena.h" #include "upb/mem/arena.h"
#include "upb/message/message.h"
namespace hpb::testing { namespace hpb::testing {
namespace { namespace {
using ::hpb_unittest::protos::TestModel; using ::hpb_unittest::protos::TestModel;
TEST(CppGeneratedCode, InternalMoveMessage) { TEST(CppGeneratedCode, InteropMoveMessage) {
// Generate message (simulating message created in another VM/language) // Generate message (simulating message created in another VM/language)
upb_Arena* source_arena = upb_Arena_New(); upb_Arena* source_arena = upb_Arena_New();
hpb_unittest_TestModel* message = hpb_unittest_TestModel_new(source_arena); hpb_unittest_TestModel* message = hpb_unittest_TestModel_new(source_arena);
@ -25,8 +25,8 @@ TEST(CppGeneratedCode, InternalMoveMessage) {
hpb_unittest_TestModel_set_int_value_with_default(message, 123); hpb_unittest_TestModel_set_int_value_with_default(message, 123);
// Move ownership. // Move ownership.
TestModel model = hpb::internal::MoveMessage<TestModel>((upb_Message*)message, TestModel model = hpb::interop::upb::MoveMessage<TestModel>(
source_arena); (upb_Message*)message, source_arena);
// Now that we have moved ownership, free original arena. // Now that we have moved ownership, free original arena.
upb_Arena_Free(source_arena); upb_Arena_Free(source_arena);
EXPECT_EQ(model.int_value_with_default(), 123); EXPECT_EQ(model.int_value_with_default(), 123);

@ -1,25 +0,0 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef PROTOBUF_HPB_INTERNAL_H_
#define PROTOBUF_HPB_INTERNAL_H_
#include "upb/mem/arena.h"
#include "upb/message/message.h"
namespace hpb::internal {
// Moves ownership of a message created in a source arena.
//
// Utility function to provide a way to move ownership across languages or VMs.
template <typename T>
T MoveMessage(upb_Message* msg, upb_Arena* arena) {
return T(msg, arena);
}
} // namespace hpb::internal
#endif

@ -252,7 +252,8 @@ void WriteModelPublicDeclaration(
const ::hpb::ExtensionRegistry& extension_registry, int options)); const ::hpb::ExtensionRegistry& extension_registry, int options));
friend upb_Arena* hpb::interop::upb::GetArena<$0>($0* message); friend upb_Arena* hpb::interop::upb::GetArena<$0>($0* message);
friend upb_Arena* hpb::interop::upb::GetArena<$0>(::hpb::Ptr<$0> message); friend upb_Arena* hpb::interop::upb::GetArena<$0>(::hpb::Ptr<$0> message);
friend $0(::hpb::internal::MoveMessage<$0>(upb_Message* msg, upb_Arena* arena)); friend $0(hpb::interop::upb::MoveMessage<$0>(upb_Message* msg,
upb_Arena* arena));
)cc", )cc",
ClassName(descriptor), MessageName(descriptor), ClassName(descriptor), MessageName(descriptor),
QualifiedClassName(descriptor)); QualifiedClassName(descriptor));

@ -134,7 +134,6 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output,
#ifndef $0_HPB_PROTO_H_ #ifndef $0_HPB_PROTO_H_
#define $0_HPB_PROTO_H_ #define $0_HPB_PROTO_H_
#include "google/protobuf/hpb/internal.h"
#include "google/protobuf/hpb/repeated_field.h" #include "google/protobuf/hpb/repeated_field.h"
#include "protos/protos.h" #include "protos/protos.h"

Loading…
Cancel
Save