Files
tubestation/toolkit/components/uniffi-js/UniFFIScaffolding.cpp
Ben Dean-Kawamura f25b21537e Bug 1918512 - Support for async Rust calls, r=nika,markh
Added the UniffiAsyncCallHandler class.  It follows the same basic
pattern as UniffiSyncCallHandler, but for async calls.
UniffiCallHandlerBase contains shared code between the two.

Replaced `use_async_wrapper` with `call_style`, which returns 1 of three
options rather than a boolean.  This required a slight change to the
async config for constructors.  User now need to specify `Object.new`
instead of `Object`.  One upside is that it also allows configuring
non-primary constructors.

Added a test fixture for this.  I used this fixture as an opportunity to
sketch out how I think this would work with the application-services
code and why this is an improvement over the current system for
auto-generating async wrappers.

Pass the buildconfig ENV when building the uniffi component libraries.
We need this if we want to depend on crates like `moz_task`.

Differential Revision: https://phabricator.services.mozilla.com/D227553
2024-11-25 17:51:01 +00:00

132 lines
5.1 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <inttypes.h>
#include "nsError.h"
#include "nsString.h"
#include "nsPrintfCString.h"
#include "mozilla/Logging.h"
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/UniFFICall.h"
#include "mozilla/dom/UniFFICallbacks.h"
#include "mozilla/dom/UniFFIScaffolding.h"
// This file implements the UniFFI WebIDL interface by leveraging the generate
// code in UniFFIScaffolding.cpp and UniFFIFixtureScaffolding.cpp. It's main
// purpose is to check if MOZ_UNIFFI_FIXTURES is set and only try calling the
// scaffolding code if it is.
using mozilla::dom::ArrayBuffer;
using mozilla::dom::GlobalObject;
using mozilla::dom::Promise;
using mozilla::dom::RootedDictionary;
using mozilla::dom::Sequence;
using mozilla::dom::UniFFICallbackHandler;
using mozilla::dom::UniFFIPointer;
using mozilla::dom::UniFFIScaffoldingCallResult;
using mozilla::dom::UniFFIScaffoldingValue;
using mozilla::uniffi::UniffiAsyncCallHandler;
using mozilla::uniffi::UniffiSyncCallHandler;
namespace mozilla::uniffi {
mozilla::LazyLogModule gUniffiLogger("uniffi");
// Implemented in UniFFIGeneratedScaffolding.cpp
UniquePtr<UniffiSyncCallHandler> GetSyncCallHandler(uint64_t aId);
UniquePtr<UniffiAsyncCallHandler> GetAsyncCallHandler(uint64_t aId);
Maybe<already_AddRefed<UniFFIPointer>> ReadPointer(
const GlobalObject& aGlobal, uint64_t aId, const ArrayBuffer& aArrayBuff,
long aPosition, ErrorResult& aError);
bool WritePointer(const GlobalObject& aGlobal, uint64_t aId,
const UniFFIPointer& aPtr, const ArrayBuffer& aArrayBuff,
long aPosition, ErrorResult& aError);
} // namespace mozilla::uniffi
namespace mozilla::dom {
// Implement the interface using the generated functions
void UniFFIScaffolding::CallSync(
const GlobalObject& aGlobal, uint64_t aId,
const Sequence<UniFFIScaffoldingValue>& aArgs,
RootedDictionary<UniFFIScaffoldingCallResult>& aReturnValue,
ErrorResult& aError) {
if (UniquePtr<UniffiSyncCallHandler> handler =
uniffi::GetSyncCallHandler(aId)) {
return UniffiSyncCallHandler::CallSync(std::move(handler), aGlobal, aArgs,
aReturnValue, aError);
}
aError.ThrowUnknownError(
nsPrintfCString("Unknown function id: %" PRIu64, aId));
}
already_AddRefed<Promise> UniFFIScaffolding::CallAsync(
const GlobalObject& aGlobal, uint64_t aId,
const Sequence<UniFFIScaffoldingValue>& aArgs, ErrorResult& aError) {
if (UniquePtr<UniffiAsyncCallHandler> handler =
uniffi::GetAsyncCallHandler(aId)) {
return UniffiAsyncCallHandler::CallAsync(std::move(handler), aGlobal, aArgs,
aError);
}
aError.ThrowUnknownError(
nsPrintfCString("Unknown function id: %" PRIu64, aId));
return nullptr;
}
already_AddRefed<Promise> UniFFIScaffolding::CallAsyncWrapper(
const GlobalObject& aGlobal, uint64_t aId,
const Sequence<UniFFIScaffoldingValue>& aArgs, ErrorResult& aError) {
if (UniquePtr<UniffiSyncCallHandler> handler =
uniffi::GetSyncCallHandler(aId)) {
return UniffiSyncCallHandler::CallAsyncWrapper(std::move(handler), aGlobal,
aArgs, aError);
}
aError.ThrowUnknownError(
nsPrintfCString("Unknown function id: %" PRIu64, aId));
return nullptr;
}
already_AddRefed<UniFFIPointer> UniFFIScaffolding::ReadPointer(
const GlobalObject& aGlobal, uint64_t aId, const ArrayBuffer& aArrayBuff,
long aPosition, ErrorResult& aError) {
Maybe<already_AddRefed<UniFFIPointer>> firstTry =
uniffi::ReadPointer(aGlobal, aId, aArrayBuff, aPosition, aError);
if (firstTry.isSome()) {
return firstTry.extract();
}
aError.ThrowUnknownError(nsPrintfCString("Unknown object id: %" PRIu64, aId));
return nullptr;
}
void UniFFIScaffolding::WritePointer(const GlobalObject& aGlobal, uint64_t aId,
const UniFFIPointer& aPtr,
const ArrayBuffer& aArrayBuff,
long aPosition, ErrorResult& aError) {
if (uniffi::WritePointer(aGlobal, aId, aPtr, aArrayBuff, aPosition, aError)) {
return;
}
aError.ThrowUnknownError(nsPrintfCString("Unknown object id: %" PRIu64, aId));
}
void UniFFIScaffolding::RegisterCallbackHandler(
GlobalObject& aGlobal, uint64_t aInterfaceId,
UniFFICallbackHandler& aCallbackHandler, ErrorResult& aError) {
uniffi::RegisterCallbackHandler(aInterfaceId, aCallbackHandler, aError);
}
void UniFFIScaffolding::DeregisterCallbackHandler(GlobalObject& aGlobal,
uint64_t aInterfaceId,
ErrorResult& aError) {
uniffi::DeregisterCallbackHandler(aInterfaceId, aError);
}
} // namespace mozilla::dom