#include #include #include #include #include #include #include #include namespace formatjs { namespace jsi = facebook::jsi; namespace { constexpr char wrapperSource[] = #include "durationformat.inc" ; using Handle = std::unique_ptr; using Construction = std::unique_ptr; using Registry = std::unique_ptr; using DurationRecord = hermes_bridge::Record; struct ConstructionRecord final : jsi::HostObject { explicit ConstructionRecord(Construction value) : handle(std::move(value)) {} Construction handle; }; using Bytes = hermes_bridge::Buffer; using hermes_bridge::bytesOf; jsi::Value status(jsi::Runtime &rt, int32_t code) { if (code == 0 || code == 1 || code == 4 || code == 5) return jsi::Value(code); throw jsi::JSError(rt, "Rust DurationFormat bridge failed"); } jsi::Value result(jsi::Runtime &rt, int32_t code, const Bytes &bytes) { if (code != 0) return status(rt, code); return jsi::String::createFromUtf8(rt, bytes.value.data, bytes.value.len); } template std::shared_ptr record(jsi::Runtime &rt, const jsi::Value &value) { if (!value.isObject()) throw jsi::JSError(rt, "Expected DurationFormat record"); auto object = value.asObject(rt); if (!object.isHostObject(rt)) throw jsi::JSError(rt, "Invalid DurationFormat record"); return object.getHostObject(rt); } std::string text(jsi::Runtime &rt, const jsi::Value &value) { if (!value.isString()) throw jsi::JSError(rt, "Expected DurationFormat JSON string"); return value.asString(rt).utf8(rt); } uint32_t index(jsi::Runtime &rt, const jsi::Value &value, uint32_t max) { if (!value.isNumber()) throw jsi::JSError(rt, "Expected DurationFormat index"); auto number = value.asNumber(); if (!std::isfinite(number) || number < 0 || number > max || std::floor(number) != number) throw jsi::JSError(rt, "Invalid DurationFormat index"); return static_cast(number); } } // namespace void installDurationFormat(jsi::Runtime &runtime, jsi::Object &extensions) { FormatjsDurationRegistry *raw = nullptr; if (formatjs_duration_registry_create(&raw) != 0) throw jsi::JSError(runtime, "Cannot create DurationFormat context"); auto registry = std::make_shared(raw, formatjs_duration_registry_destroy); jsi::Object bridge(runtime); hermes_bridge::bind(runtime, bridge, "nativeLoad", "durationLoad", 2, [registry](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 2) throw jsi::JSError(rt, "Expected DurationFormat pack kind and JSON"); auto data = text(rt, args[1]); return status(rt, formatjs_duration_registry_load(registry->get(), index(rt, args[0], 1), bytesOf(data), data.size())); }); hermes_bridge::bind(runtime, bridge, "nativeUnits", "durationUnits", 1, [registry](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 1) throw jsi::JSError(rt, "Expected DurationFormat unit JSON"); auto data = text(rt, args[0]); return status(rt, formatjs_duration_registry_units(registry->get(), bytesOf(data), data.size())); }); hermes_bridge::bind(runtime, bridge, "nativeInfo", "durationInfo", 0, [](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *, size_t) -> jsi::Value { Bytes bytes; return result(rt, formatjs_duration_info(&bytes.value), bytes); }); hermes_bridge::bind(runtime, bridge, "nativeSupported", "durationSupported", 1, [](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 1) throw jsi::JSError(rt, "Expected DurationFormat locale JSON"); auto data = text(rt, args[0]); Bytes bytes; return result(rt, formatjs_duration_supported(bytesOf(data), data.size(), &bytes.value), bytes); }); hermes_bridge::bind(runtime, bridge, "nativeBegin", "durationBegin", 1, [registry](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 1) throw jsi::JSError(rt, "Expected DurationFormat construction JSON"); auto data = text(rt, args[0]); FormatjsDurationConstruction *raw = nullptr; auto code = formatjs_duration_begin(registry->get(), bytesOf(data), data.size(), &raw); Construction handle(raw, formatjs_duration_construction_destroy); if (code != 0) return status(rt, code); return jsi::Object::createFromHostObject(rt, std::make_shared(std::move(handle))); }); hermes_bridge::bind(runtime, bridge, "nativeStyle", "durationStyle", 2, [](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 2) throw jsi::JSError(rt, "Expected DurationFormat style"); auto model = record(rt, args[0]); auto data = text(rt, args[1]); return status(rt, formatjs_duration_style(model->handle.get(), bytesOf(data), data.size())); }); hermes_bridge::bind(runtime, bridge, "nativeUnit", "durationUnit", 4, [](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 4 || !args[2].isBool()) throw jsi::JSError(rt, "Expected DurationFormat unit option"); auto model = record(rt, args[0]); auto unit = index(rt, args[1], 9); auto data = text(rt, args[3]); auto call = args[2].getBool() ? formatjs_duration_unit_display : formatjs_duration_unit_style; return status(rt, call(model->handle.get(), unit, bytesOf(data), data.size())); }); hermes_bridge::bind(runtime, bridge, "nativeFinish", "durationFinish", 2, [](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 2 || (!args[1].isUndefined() && !args[1].isNumber())) throw jsi::JSError(rt, "Expected DurationFormat fractional digits"); auto model = record(rt, args[0]); FormatjsDurationHandle *raw = nullptr; auto code = formatjs_duration_finish(model->handle.get(), args[1].isNumber() ? 1 : 0, args[1].isNumber() ? args[1].asNumber() : 0, &raw); Handle handle(raw, formatjs_duration_destroy); if (code != 0) return status(rt, code); model->handle.reset(); return jsi::Object::createFromHostObject(rt, std::make_shared(std::move(handle))); }); hermes_bridge::bind(runtime, bridge, "nativeResolved", "durationResolved", 1, [](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 1) throw jsi::JSError(rt, "Expected DurationFormat instance"); auto model = record(rt, args[0]); Bytes bytes; return result(rt, formatjs_duration_resolved(model->handle.get(), &bytes.value), bytes); }); hermes_bridge::bind(runtime, bridge, "nativeFormat", "durationFormat", 4, [](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) -> jsi::Value { if (count != 4 || !args[1].isObject() || !args[1].asObject(rt).isArray(rt)) throw jsi::JSError(rt, "Expected DurationFormat values"); auto model = record(rt, args[0]); auto values = args[1].asObject(rt).asArray(rt); if (values.size(rt) != 10) throw jsi::JSError(rt, "Expected ten DurationFormat values"); FormatjsDurationValue value{}; value.defined = static_cast(index(rt, args[2], 1023)); for (size_t i = 0; i < 10; ++i) { auto number = values.getValueAtIndex(rt, i); if (!number.isNumber()) throw jsi::JSError(rt, "Expected numeric duration fields"); value.values[i] = number.asNumber(); } Bytes bytes; return result(rt, formatjs_duration_format(model->handle.get(), &value, index(rt, args[3], 1), &bytes.value), bytes); }); extensions.getPropertyAsFunction(runtime, "IntlDurationFormat").call(runtime, bridge); } void installDurationFormat(jsi::Runtime &runtime) { auto extensions = hermes_bridge::factory(runtime, wrapperSource, "formatjs:DurationFormat", "IntlDurationFormat"); installDurationFormat(runtime, extensions); } } // namespace formatjs