summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-js/UniFFIScaffolding.cpp
blob: 1fcbea2fa47afb141e8e5cfaa3204f267702574a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -*- 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/Maybe.h"
#include "mozilla/dom/UniFFIScaffolding.h"
#include "mozilla/dom/UniFFICallbacks.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;

namespace mozilla::uniffi {

// Prototypes for the generated functions
Maybe<already_AddRefed<Promise>> UniFFICallAsync(
    const GlobalObject& aGlobal, uint64_t aId,
    const Sequence<UniFFIScaffoldingValue>& aArgs, ErrorResult& aError);
bool UniFFICallSync(const GlobalObject& aGlobal, uint64_t aId,
                    const Sequence<UniFFIScaffoldingValue>& aArgs,
                    RootedDictionary<UniFFIScaffoldingCallResult>& aReturnValue,
                    ErrorResult& aError);
Maybe<already_AddRefed<UniFFIPointer>> UniFFIReadPointer(
    const GlobalObject& aGlobal, uint64_t aId, const ArrayBuffer& aArrayBuff,
    long aPosition, ErrorResult& aError);
bool UniFFIWritePointer(const GlobalObject& aGlobal, uint64_t aId,
                        const UniFFIPointer& aPtr,
                        const ArrayBuffer& aArrayBuff, long aPosition,
                        ErrorResult& aError);

#ifdef MOZ_UNIFFI_FIXTURES
Maybe<already_AddRefed<Promise>> UniFFIFixturesCallAsync(
    const GlobalObject& aGlobal, uint64_t aId,
    const Sequence<UniFFIScaffoldingValue>& aArgs, ErrorResult& aError);
bool UniFFIFixturesCallSync(
    const GlobalObject& aGlobal, uint64_t aId,
    const Sequence<UniFFIScaffoldingValue>& aArgs,
    RootedDictionary<UniFFIScaffoldingCallResult>& aReturnValue,
    ErrorResult& aError);
Maybe<already_AddRefed<UniFFIPointer>> UniFFIFixturesReadPointer(
    const GlobalObject& aGlobal, uint64_t aId, const ArrayBuffer& aArrayBuff,
    long aPosition, ErrorResult& aError);
bool UniFFIFixturesWritePointer(const GlobalObject& aGlobal, uint64_t aId,
                                const UniFFIPointer& aPtr,
                                const ArrayBuffer& aArrayBuff, long aPosition,
                                ErrorResult& aError);
#endif
}  // namespace mozilla::uniffi

namespace mozilla::dom {

// Implement the interface using the generated functions

already_AddRefed<Promise> UniFFIScaffolding::CallAsync(
    const GlobalObject& aGlobal, uint64_t aId,
    const Sequence<UniFFIScaffoldingValue>& aArgs, ErrorResult& aError) {
  Maybe<already_AddRefed<Promise>> firstTry =
      uniffi::UniFFICallAsync(aGlobal, aId, aArgs, aError);
  if (firstTry.isSome()) {
    return firstTry.extract();
  }
#ifdef MOZ_UNIFFI_FIXTURES
  Maybe<already_AddRefed<Promise>> secondTry =
      uniffi::UniFFIFixturesCallAsync(aGlobal, aId, aArgs, aError);
  if (secondTry.isSome()) {
    return secondTry.extract();
  }
#endif

  aError.ThrowUnknownError(
      nsPrintfCString("Unknown function id: %" PRIu64, aId));
  return nullptr;
}

void UniFFIScaffolding::CallSync(
    const GlobalObject& aGlobal, uint64_t aId,
    const Sequence<UniFFIScaffoldingValue>& aArgs,
    RootedDictionary<UniFFIScaffoldingCallResult>& aReturnValue,
    ErrorResult& aError) {
  if (uniffi::UniFFICallSync(aGlobal, aId, aArgs, aReturnValue, aError)) {
    return;
  }
#ifdef MOZ_UNIFFI_FIXTURES
  if (uniffi::UniFFIFixturesCallSync(aGlobal, aId, aArgs, aReturnValue,
                                     aError)) {
    return;
  }
#endif

  aError.ThrowUnknownError(
      nsPrintfCString("Unknown function id: %" PRIu64, aId));
}

already_AddRefed<UniFFIPointer> UniFFIScaffolding::ReadPointer(
    const GlobalObject& aGlobal, uint64_t aId, const ArrayBuffer& aArrayBuff,
    long aPosition, ErrorResult& aError) {
  Maybe<already_AddRefed<UniFFIPointer>> firstTry =
      uniffi::UniFFIReadPointer(aGlobal, aId, aArrayBuff, aPosition, aError);
  if (firstTry.isSome()) {
    return firstTry.extract();
  }
#ifdef MOZ_UNIFFI_FIXTURES
  Maybe<already_AddRefed<UniFFIPointer>> secondTry =
      uniffi::UniFFIFixturesReadPointer(aGlobal, aId, aArrayBuff, aPosition,
                                        aError);
  if (secondTry.isSome()) {
    return secondTry.extract();
  }
#endif

  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::UniFFIWritePointer(aGlobal, aId, aPtr, aArrayBuff, aPosition,
                                 aError)) {
    return;
  }
#ifdef MOZ_UNIFFI_FIXTURES
  if (uniffi::UniFFIFixturesWritePointer(aGlobal, aId, aPtr, aArrayBuff,
                                         aPosition, aError)) {
    return;
  }
#endif

  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