blob: 9854babb2a230c0aeab59bbad54d05edfbe665c7 [file] [log] [blame]
Eric Saloae24d712022-12-02 12:14:03 -08001/*
2 * Copyright (c) 2009-2022, Google LLC
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Google LLC nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// This disables inlining and forces all public functions to be exported to the
29// linker. It is used to generate bindings for FFIs from other languages.
Eric Saloabed1992022-12-16 15:38:14 -080030#ifndef UPB_BUILD_API
Eric Saloae24d712022-12-02 12:14:03 -080031#define UPB_BUILD_API
Eric Saloabed1992022-12-16 15:38:14 -080032#endif
Eric Saloae24d712022-12-02 12:14:03 -080033
Eric Saloabed1992022-12-16 15:38:14 -080034#include "upb/collections/array.h"
Eric Salo00623c32022-12-24 23:35:27 -080035#include "upb/collections/map.h"
Eric Salod9ee7f52022-12-14 12:22:06 -080036#include "upb/message/accessors.h"
Eric Saloae24d712022-12-02 12:14:03 -080037#include "upb/message/message.h"
38#include "upb/mini_table/decode.h"
39#include "upbc/upbdev.h"
Eric Saloabed1992022-12-16 15:38:14 -080040
41// Must be last.
42#include "upb/port/def.inc"
Eric Salo0e286b42023-01-05 15:00:51 -080043
44// JavaScript doesn't directly support 64-bit ints so we must split them.
45
Eric Salodfd5f172023-03-03 09:55:56 -080046UPB_API_INLINE uint32_t upb_Array_GetInt64Hi(const upb_Array* array, size_t i) {
47 return (uint32_t)(upb_Array_Get(array, i).int64_val >> 32);
48}
49
50UPB_API_INLINE uint32_t upb_Array_GetInt64Lo(const upb_Array* array, size_t i) {
51 return (uint32_t)upb_Array_Get(array, i).int64_val;
52}
53
54UPB_API_INLINE void upb_Array_SetInt64Split(upb_Array* array, size_t i,
55 uint32_t hi, uint32_t lo) {
56 const upb_MessageValue val = {.int64_val = ((uint64_t)hi) << 32 | lo};
57 upb_Array_Set(array, i, val);
58}
59
60UPB_API_INLINE bool upb_Array_AppendInt64Split(upb_Array* array, uint32_t hi,
61 uint32_t lo, upb_Arena* arena) {
62 const upb_MessageValue val = {.int64_val = ((uint64_t)hi) << 32 | lo};
63 return upb_Array_Append(array, val, arena);
64}
65
Eric Saloa6ce7332023-03-05 22:55:48 -080066UPB_API_INLINE uint32_t upb_Array_GetUInt64Hi(const upb_Array* array,
67 size_t i) {
68 return (uint32_t)(upb_Array_Get(array, i).uint64_val >> 32);
69}
70
71UPB_API_INLINE uint32_t upb_Array_GetUInt64Lo(const upb_Array* array,
72 size_t i) {
73 return (uint32_t)upb_Array_Get(array, i).uint64_val;
74}
75
76UPB_API_INLINE void upb_Array_SetUInt64Split(upb_Array* array, size_t i,
77 uint32_t hi, uint32_t lo) {
78 const upb_MessageValue val = {.uint64_val = ((uint64_t)hi) << 32 | lo};
79 upb_Array_Set(array, i, val);
80}
81
82UPB_API_INLINE bool upb_Array_AppendUInt64Split(upb_Array* array, uint32_t hi,
83 uint32_t lo, upb_Arena* arena) {
84 const upb_MessageValue val = {.uint64_val = ((uint64_t)hi) << 32 | lo};
85 return upb_Array_Append(array, val, arena);
86}
87
Eric Salo0e286b42023-01-05 15:00:51 -080088UPB_API_INLINE uint32_t upb_Message_GetInt64Hi(const upb_Message* msg,
89 const upb_MiniTableField* field,
90 uint32_t default_value) {
91 return (uint32_t)(upb_Message_GetInt64(msg, field, default_value) >> 32);
92}
93
94UPB_API_INLINE uint32_t upb_Message_GetInt64Lo(const upb_Message* msg,
95 const upb_MiniTableField* field,
96 uint32_t default_value) {
97 return (uint32_t)upb_Message_GetInt64(msg, field, default_value);
98}
99
100UPB_API_INLINE bool upb_Message_SetInt64Split(upb_Message* msg,
101 const upb_MiniTableField* field,
102 uint32_t hi, uint32_t lo,
Eric Salodfd5f172023-03-03 09:55:56 -0800103 upb_Arena* arena) {
104 return upb_Message_SetInt64(msg, field, ((int64_t)hi << 32) | lo, arena);
Eric Salo0e286b42023-01-05 15:00:51 -0800105}
106
107UPB_API_INLINE uint32_t upb_Message_GetUInt64Hi(const upb_Message* msg,
108 const upb_MiniTableField* field,
109 uint32_t default_value) {
110 return (uint32_t)(upb_Message_GetUInt64(msg, field, default_value) >> 32);
111}
112
113UPB_API_INLINE uint32_t upb_Message_GetUInt64Lo(const upb_Message* msg,
114 const upb_MiniTableField* field,
115 uint32_t default_value) {
116 return (uint32_t)upb_Message_GetUInt64(msg, field, default_value);
117}
118
119UPB_API_INLINE bool upb_Message_SetUInt64Split(upb_Message* msg,
120 const upb_MiniTableField* field,
121 uint32_t hi, uint32_t lo,
Eric Salodfd5f172023-03-03 09:55:56 -0800122 upb_Arena* arena) {
123 return upb_Message_SetUInt64(msg, field, ((uint64_t)hi << 32) | lo, arena);
Eric Salo0e286b42023-01-05 15:00:51 -0800124}