mirror of
https://github.com/zoriya/nitro.git
synced 2026-10-13 00:05:23 +00:00
* feat: Create `ObjectUtils` for `Object.freeze`, `Object.defineProperty`, ... * Use Object.create * Fix missing obj * fix: Better error * Cache * fix: Actually cache by preventing copy * Object.create is part of RN 78 * Use `defineProperty(...)` for `Dispatcher` too * Optimize Dispatcher install * Create `getGlobalFunction` * feat: Use `defineGlobal(...)` to define our global props * fix: Move the globals * fix: Don't protect/freeze the globals in JS we already do it in native.
95 lines
3.1 KiB
C++
95 lines
3.1 KiB
C++
//
|
|
// ObjectUtils.hpp
|
|
// react-native-nitro
|
|
//
|
|
// Created by Marc Rousavy on 17.10.25.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include "BorrowingReference.hpp"
|
|
#include <jsi/jsi.h>
|
|
#include <unordered_map>
|
|
|
|
namespace margelo::nitro {
|
|
|
|
using namespace facebook;
|
|
|
|
struct PlainPropertyDescriptor {
|
|
bool configurable;
|
|
bool enumerable;
|
|
jsi::Value value;
|
|
bool writable;
|
|
};
|
|
struct ComputedReadonlyPropertyDescriptor {
|
|
bool configurable;
|
|
bool enumerable;
|
|
jsi::Function get;
|
|
};
|
|
struct ComputedPropertyDescriptor {
|
|
bool configurable;
|
|
bool enumerable;
|
|
jsi::Function get;
|
|
jsi::Function set;
|
|
};
|
|
|
|
enum class KnownGlobalPropertyName { DISPATCHER, JSI_CACHE, NITRO_MODULES_PROXY };
|
|
|
|
class ObjectUtils {
|
|
public:
|
|
ObjectUtils() = delete;
|
|
~ObjectUtils() = delete;
|
|
|
|
public:
|
|
/**
|
|
* Create a new `jsi::Object` with the given `prototype`.
|
|
* Uses a native implementation if possible.
|
|
*/
|
|
static jsi::Object create(jsi::Runtime& runtime, const jsi::Value& prototype, bool allowCache = true);
|
|
|
|
/**
|
|
* Define a global value for the given Runtime.
|
|
* In debug, this performs additional safety checks such as freezing the property.
|
|
*/
|
|
static void defineGlobal(jsi::Runtime& runtime, KnownGlobalPropertyName name, jsi::Value&& value, bool allowCache = true);
|
|
|
|
/**
|
|
* Define a plain property on the given `object` with the given `propertyName`.
|
|
* The `descriptor` defines the attributes of this property.
|
|
*/
|
|
static void defineProperty(jsi::Runtime& runtime, const jsi::Object& object, const char* propertyName,
|
|
PlainPropertyDescriptor&& descriptor, bool allowCache = true);
|
|
/**
|
|
* Define a plain property on the given `object` with the given `propertyName`.
|
|
* The `descriptor` defines the attributes of this property.
|
|
*/
|
|
static void defineProperty(jsi::Runtime& runtime, const jsi::Object& object, const char* propertyName,
|
|
ComputedReadonlyPropertyDescriptor&& descriptor, bool allowCache = true);
|
|
/**
|
|
* Define a plain property on the given `object` with the given `propertyName`.
|
|
* The `descriptor` defines the attributes of this property.
|
|
*/
|
|
static void defineProperty(jsi::Runtime& runtime, const jsi::Object& object, const char* propertyName,
|
|
ComputedPropertyDescriptor&& descriptor, bool allowCache = true);
|
|
|
|
/**
|
|
* Freezes all values of the given `object`.
|
|
*/
|
|
static void freeze(jsi::Runtime& runtime, const jsi::Object& object, bool allowCache = true);
|
|
|
|
/**
|
|
* Get a string name for a known global property name.
|
|
*/
|
|
static const char* getKnownGlobalPropertyNameString(KnownGlobalPropertyName name);
|
|
|
|
private:
|
|
using FunctionCache = std::unordered_map<std::string, BorrowingReference<jsi::Function>>;
|
|
static std::unordered_map<jsi::Runtime*, FunctionCache> _cache;
|
|
|
|
static BorrowingReference<jsi::Function> getGlobalFunction(jsi::Runtime& runtime, const char* key,
|
|
std::function<jsi::Function(jsi::Runtime&)> getFunction,
|
|
bool allowCache = true);
|
|
};
|
|
|
|
} // namespace margelo::nitro
|