diff --git a/upb/message/accessors.h b/upb/message/accessors.h index 6a5f49a90c..697b5ed777 100644 --- a/upb/message/accessors.h +++ b/upb/message/accessors.h @@ -521,6 +521,17 @@ UPB_API_INLINE upb_Array* upb_Message_GetMutableArray( return (upb_Array*)upb_Message_GetArray(msg, field); } +UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray( + upb_Message* msg, const upb_MiniTableField* field, upb_CType ctype, + upb_Arena* arena) { + upb_Array* array = upb_Message_GetMutableArray(msg, field); + if (!array) { + array = upb_Array_New(arena, ctype); + _upb_Message_SetField(msg, field, &array, arena); + } + return array; +} + void* upb_Message_ResizeArray(upb_Message* msg, const upb_MiniTableField* field, size_t len, upb_Arena* arena); diff --git a/upbc/BUILD b/upbc/BUILD index fc06f75f93..c2e7caa5a8 100644 --- a/upbc/BUILD +++ b/upbc/BUILD @@ -75,10 +75,12 @@ cc_binary( linkstatic = 1, visibility = ["//visibility:public"], deps = [ + "//:collections", "//:mem", "//:message", "//:message_accessors", "//:mini_table", + "//:port", "//upbc:upbdev", ], ) diff --git a/upbc/upbc_so.c b/upbc/upbc_so.c index ac109e05c6..16df62e7e1 100644 --- a/upbc/upbc_so.c +++ b/upbc/upbc_so.c @@ -27,10 +27,72 @@ // This disables inlining and forces all public functions to be exported to the // linker. It is used to generate bindings for FFIs from other languages. +#ifndef UPB_BUILD_API #define UPB_BUILD_API +#endif -#include "upb/mem/arena.h" +#include "upb/collections/array.h" #include "upb/message/accessors.h" #include "upb/message/message.h" #include "upb/mini_table/decode.h" #include "upbc/upbdev.h" + +// Must be last. +#include "upb/port/def.inc" + +UPB_API bool upb_Array_AppendBool(upb_Array* array, bool val, + upb_Arena* arena) { + const upb_MessageValue mv = {.bool_val = val}; + return upb_Array_Append(array, mv, arena); +} + +UPB_API bool upb_Array_AppendDouble(upb_Array* array, double val, + upb_Arena* arena) { + const upb_MessageValue mv = {.double_val = val}; + return upb_Array_Append(array, mv, arena); +} + +UPB_API bool upb_Array_AppendFloat(upb_Array* array, float val, + upb_Arena* arena) { + const upb_MessageValue mv = {.float_val = val}; + return upb_Array_Append(array, mv, arena); +} + +UPB_API bool upb_Array_AppendInt32(upb_Array* array, int32_t val, + upb_Arena* arena) { + const upb_MessageValue mv = {.int32_val = val}; + return upb_Array_Append(array, mv, arena); +} + +UPB_API bool upb_Array_AppendUInt32(upb_Array* array, uint32_t val, + upb_Arena* arena) { + const upb_MessageValue mv = {.uint32_val = val}; + return upb_Array_Append(array, mv, arena); +} + +//////////////////////////////////////////////////////////////////////////////// + +UPB_API void upb_Array_SetBool(upb_Array* array, size_t i, bool val) { + const upb_MessageValue mv = {.bool_val = val}; + upb_Array_Set(array, i, mv); +} + +UPB_API void upb_Array_SetDouble(upb_Array* array, size_t i, double val) { + const upb_MessageValue mv = {.double_val = val}; + upb_Array_Set(array, i, mv); +} + +UPB_API void upb_Array_SetFloat(upb_Array* array, size_t i, float val) { + const upb_MessageValue mv = {.float_val = val}; + upb_Array_Set(array, i, mv); +} + +UPB_API void upb_Array_SetInt32(upb_Array* array, size_t i, int32_t val) { + const upb_MessageValue mv = {.int32_val = val}; + upb_Array_Set(array, i, mv); +} + +UPB_API void upb_Array_SetUInt32(upb_Array* array, size_t i, uint32_t val) { + const upb_MessageValue mv = {.uint32_val = val}; + upb_Array_Set(array, i, mv); +}