1 /*
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.
30 #ifndef UPB_BUILD_API
31 #define UPB_BUILD_API
32 #endif
33 
34 #include "upb/collections/array.h"
35 #include "upb/collections/map.h"
36 #include "upb/message/accessors.h"
37 #include "upb/message/message.h"
38 #include "upb/mini_table/decode.h"
39 #include "upbc/upbdev.h"
40 
41 // Must be last.
42 #include "upb/port/def.inc"
43 
44 // JavaScript doesn't directly support 64-bit ints so we must split them.
45 
upb_Array_GetInt64Hi(const upb_Array * array,size_t i)46 UPB_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 
upb_Array_GetInt64Lo(const upb_Array * array,size_t i)50 UPB_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 
upb_Array_SetInt64Split(upb_Array * array,size_t i,uint32_t hi,uint32_t lo)54 UPB_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 
upb_Array_AppendInt64Split(upb_Array * array,uint32_t hi,uint32_t lo,upb_Arena * arena)60 UPB_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 
upb_Array_GetUInt64Hi(const upb_Array * array,size_t i)66 UPB_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 
upb_Array_GetUInt64Lo(const upb_Array * array,size_t i)71 UPB_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 
upb_Array_SetUInt64Split(upb_Array * array,size_t i,uint32_t hi,uint32_t lo)76 UPB_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 
upb_Array_AppendUInt64Split(upb_Array * array,uint32_t hi,uint32_t lo,upb_Arena * arena)82 UPB_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 
upb_Message_GetInt64Hi(const upb_Message * msg,const upb_MiniTableField * field,uint32_t default_value)88 UPB_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 
upb_Message_GetInt64Lo(const upb_Message * msg,const upb_MiniTableField * field,uint32_t default_value)94 UPB_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 
upb_Message_SetInt64Split(upb_Message * msg,const upb_MiniTableField * field,uint32_t hi,uint32_t lo,upb_Arena * arena)100 UPB_API_INLINE bool upb_Message_SetInt64Split(upb_Message* msg,
101                                               const upb_MiniTableField* field,
102                                               uint32_t hi, uint32_t lo,
103                                               upb_Arena* arena) {
104   return upb_Message_SetInt64(msg, field, ((int64_t)hi << 32) | lo, arena);
105 }
106 
upb_Message_GetUInt64Hi(const upb_Message * msg,const upb_MiniTableField * field,uint32_t default_value)107 UPB_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 
upb_Message_GetUInt64Lo(const upb_Message * msg,const upb_MiniTableField * field,uint32_t default_value)113 UPB_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 
upb_Message_SetUInt64Split(upb_Message * msg,const upb_MiniTableField * field,uint32_t hi,uint32_t lo,upb_Arena * arena)119 UPB_API_INLINE bool upb_Message_SetUInt64Split(upb_Message* msg,
120                                                const upb_MiniTableField* field,
121                                                uint32_t hi, uint32_t lo,
122                                                upb_Arena* arena) {
123   return upb_Message_SetUInt64(msg, field, ((uint64_t)hi << 32) | lo, arena);
124 }
125