Eric Salo | ae24d71 | 2022-12-02 12:14:03 -0800 | [diff] [blame] | 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. |
Eric Salo | abed199 | 2022-12-16 15:38:14 -0800 | [diff] [blame] | 30 | #ifndef UPB_BUILD_API |
Eric Salo | ae24d71 | 2022-12-02 12:14:03 -0800 | [diff] [blame] | 31 | #define UPB_BUILD_API |
Eric Salo | abed199 | 2022-12-16 15:38:14 -0800 | [diff] [blame] | 32 | #endif |
Eric Salo | ae24d71 | 2022-12-02 12:14:03 -0800 | [diff] [blame] | 33 | |
Eric Salo | abed199 | 2022-12-16 15:38:14 -0800 | [diff] [blame] | 34 | #include "upb/collections/array.h" |
Eric Salo | 00623c3 | 2022-12-24 23:35:27 -0800 | [diff] [blame] | 35 | #include "upb/collections/map.h" |
Eric Salo | d9ee7f5 | 2022-12-14 12:22:06 -0800 | [diff] [blame] | 36 | #include "upb/message/accessors.h" |
Eric Salo | ae24d71 | 2022-12-02 12:14:03 -0800 | [diff] [blame] | 37 | #include "upb/message/message.h" |
| 38 | #include "upb/mini_table/decode.h" |
| 39 | #include "upbc/upbdev.h" |
Eric Salo | abed199 | 2022-12-16 15:38:14 -0800 | [diff] [blame] | 40 | |
| 41 | // Must be last. |
| 42 | #include "upb/port/def.inc" |
Eric Salo | 0e286b4 | 2023-01-05 15:00:51 -0800 | [diff] [blame] | 43 | |
| 44 | // JavaScript doesn't directly support 64-bit ints so we must split them. |
| 45 | |
Eric Salo | dfd5f17 | 2023-03-03 09:55:56 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
Eric Salo | a6ce733 | 2023-03-05 22:55:48 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
Eric Salo | 0e286b4 | 2023-01-05 15:00:51 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 100 | UPB_API_INLINE bool upb_Message_SetInt64Split(upb_Message* msg, |
| 101 | const upb_MiniTableField* field, |
| 102 | uint32_t hi, uint32_t lo, |
Eric Salo | dfd5f17 | 2023-03-03 09:55:56 -0800 | [diff] [blame] | 103 | upb_Arena* arena) { |
| 104 | return upb_Message_SetInt64(msg, field, ((int64_t)hi << 32) | lo, arena); |
Eric Salo | 0e286b4 | 2023-01-05 15:00:51 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 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 | |
| 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 | |
| 119 | UPB_API_INLINE bool upb_Message_SetUInt64Split(upb_Message* msg, |
| 120 | const upb_MiniTableField* field, |
| 121 | uint32_t hi, uint32_t lo, |
Eric Salo | dfd5f17 | 2023-03-03 09:55:56 -0800 | [diff] [blame] | 122 | upb_Arena* arena) { |
| 123 | return upb_Message_SetUInt64(msg, field, ((uint64_t)hi << 32) | lo, arena); |
Eric Salo | 0e286b4 | 2023-01-05 15:00:51 -0800 | [diff] [blame] | 124 | } |