PHP 7.4.33
Preview: JsArgumentHelpers.h Size: 4.39 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/react-native/ReactCommon/cxxreact/JsArgumentHelpers.h
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#pragma once

#include <exception>
#include <string>

#include <folly/Conv.h>
#include <folly/dynamic.h>

// When building a cross-platform module for React Native, arguments passed
// from JS are represented as a folly::dynamic.  This class provides helpers to
// extract arguments from the folly::dynamic to concrete types usable by
// cross-platform code, and converting exceptions to a JsArgumentException so
// they can be caught and reported to RN consistently.  The goal is to make the
// jsArgAs... methods at the end simple to use should be most common, but any
// non-detail method can be used when needed.

namespace facebook {
namespace xplat {

class JsArgumentException : public std::logic_error {
 public:
  JsArgumentException(const std::string& msg) : std::logic_error(msg) {}
};

// This extracts a single argument by calling the given method pointer on it.
// If an exception is thrown, the additional arguments are passed to
// folly::to<> to be included in the exception string.  This will be most
// commonly used when extracting values from non-scalar argument.  The second
// overload accepts ref-qualified member functions.

template <typename R, typename... T>
R jsArg(
    const folly::dynamic& arg,
    R (folly::dynamic::*asFoo)() const,
    const T&... desc);
template <typename R, typename... T>
R jsArg(
    const folly::dynamic& arg,
    R (folly::dynamic::*asFoo)() const&,
    const T&... desc);

// This is like jsArg, but a operates on a dynamic representing an array of
// arguments.  The argument n is used both to index the array and build the
// exception message, if any.  It can be used directly, but will more often be
// used by the type-specific methods following.

template <typename R>
R jsArgN(
    const folly::dynamic& args,
    size_t n,
    R (folly::dynamic::*asFoo)() const);
template <typename R>
R jsArgN(
    const folly::dynamic& args,
    size_t n,
    R (folly::dynamic::*asFoo)() const&);

namespace detail {

// This is a type helper to implement functions which should work on both const
// and non-const folly::dynamic arguments, and return a type with the same
// constness.  Basically, it causes the templates which use it to be defined
// only for types compatible with folly::dynamic.
template <typename T>
struct is_dynamic {
  typedef typename std::
      enable_if<std::is_assignable<folly::dynamic, T>::value, T>::type type;
};

} // end namespace detail

// Easy to use conversion helpers are here:

// Extract the n'th arg from the given dynamic, as a dynamic.  Throws a
// JsArgumentException if there is no n'th arg in the input.
template <typename T>
typename detail::is_dynamic<T>::type& jsArgAsDynamic(T&& args, size_t n);

// Extract the n'th arg from the given dynamic, as a dynamic Array.  Throws a
// JsArgumentException if there is no n'th arg in the input, or it is not an
// Array.
template <typename T>
typename detail::is_dynamic<T>::type& jsArgAsArray(T&& args, size_t n);

// Extract the n'th arg from the given dynamic, as a dynamic Object.  Throws a
// JsArgumentException if there is no n'th arg in the input, or it is not an
// Object.
template <typename T>
typename detail::is_dynamic<T>::type& jsArgAsObject(T&& args, size_t n);

// Extract the n'th arg from the given dynamic, as a bool.  Throws a
// JsArgumentException if this fails for some reason.
inline bool jsArgAsBool(const folly::dynamic& args, size_t n) {
  return jsArgN(args, n, &folly::dynamic::asBool);
}

// Extract the n'th arg from the given dynamic, as an integer.  Throws a
// JsArgumentException if this fails for some reason.
inline int64_t jsArgAsInt(const folly::dynamic& args, size_t n) {
  return jsArgN(args, n, &folly::dynamic::asInt);
}

// Extract the n'th arg from the given dynamic, as a double.  Throws a
// JsArgumentException if this fails for some reason.
inline double jsArgAsDouble(const folly::dynamic& args, size_t n) {
  return jsArgN(args, n, &folly::dynamic::asDouble);
}

// Extract the n'th arg from the given dynamic, as a string.  Throws a
// JsArgumentException if this fails for some reason.
inline std::string jsArgAsString(const folly::dynamic& args, size_t n) {
  return jsArgN(args, n, &folly::dynamic::asString);
}

} // namespace xplat
} // namespace facebook

#include <cxxreact/JsArgumentHelpers-inl.h>

Directory Contents

Dirs: 1 × Files: 40
Name Size Perms Modified Actions
tests DIR
- drwxr-xr-x 2025-03-28 11:04:39
Edit Download
801 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
7.28 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
7.83 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.90 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.42 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
12.30 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
6.16 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.98 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.39 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
3.18 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.22 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1018 B lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.75 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1006 B lrw-r--r-- 2025-03-28 11:04:39
Edit Download
4.40 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.01 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.91 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.15 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
760 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.42 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
715 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
7.53 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.70 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.33 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.09 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
11.58 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
4.14 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
552 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
2.37 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.63 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.64 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
3.50 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.14 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
503 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.09 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.67 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.38 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.07 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
789 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
5.11 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).