// Copyright 2023 Google LLC
//
// 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

// This disables inlining and forces all public functions to be exported.
#include <stdint.h>

#define UPB_BUILD_API

#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/accessors.h"
#include "upb/message/accessors_split64.h"  // IWYU pragma: keep
#include "upb/message/array.h"              // IWYU pragma: keep
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_descriptor/decode.h"  // IWYU pragma: keep
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"  // IWYU pragma: keep
#include "upb/wire/decode.h"         // IWYU pragma: keep

// Must be last.
#include "upb/port/def.inc"

UPB_API void upb_Message_SetString_FromDataAndSize(
    upb_Message* msg, const upb_MiniTableField* field, const char* data,
    size_t size, upb_Arena* arena) {
  UPB_ASSERT(!upb_Message_IsFrozen(msg));
  upb_Message_SetString(msg, field, upb_StringView_FromDataAndSize(data, size),
                        arena);
}

/**
 * An OutPointer version of the upb_Array_Get.
 *
 * Wasm only natively supports a limited scope of scalars as function return
 * types, so it cannot return `upb_MessageValue` across the JS/Wasm border.
 *
 * Note that our current Wasm build already does emits the function:
 *  `upb_MessageValue upb_Array_Get(upb_Array*, size_t)
 * as though was typed as:
 *   `void upb_Array_Get(upb_MessageValue* out, upb_Array*, size_t)`
 * which is a signature we could write in our exports without having to create
 * a new util function. But to reduce confusion and risk of future brittleness,
 * we explicitly spell the supported exportable function signature here.
 */
UPB_API void upb_Array_Get_OutPointer(const upb_Array* array, size_t i,
                                      upb_MessageValue* out) {
  *out = upb_Array_Get(array, i);
}

UPB_API void upb_Map_IterInit(size_t* iter) { *iter = kUpb_Map_Begin; }

// This is unused when we compile for WASM, but Builder doesn't like it
// if this file cannot compile as a regular cc_binary().
int main(void) {}
