summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-bindgen-gecko-js/src/templates/UniFFIScaffolding.cpp
blob: fef14592f96860cb67b9dd645260e154d75e61f4 (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
// Generated by uniffi-bindgen-gecko-js.  DO NOT EDIT.

#include "nsString.h"
#include "nsPrintfCString.h"
#include "mozilla/Logging.h"
#include "mozilla/Maybe.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/UniFFICallbacks.h"
#include "mozilla/dom/UniFFIScaffolding.h"
#include "mozilla/dom/ScaffoldingCall.h"

namespace mozilla::uniffi {

using dom::ArrayBuffer;
using dom::AutoEntryScript;
using dom::GlobalObject;
using dom::RootedDictionary;
using dom::Promise;
using dom::UniFFIScaffoldingValue;
using dom::Sequence;
using dom::UniFFICallbackHandler;
using dom::UniFFIPointer;
using dom::UniFFIScaffoldingCallResult;

// Define scaffolding functions from UniFFI
extern "C" {
  {%- for (ci, config) in components %}
  {%- for func in ci.iter_user_ffi_function_definitions() %}
  {{ func.rust_return_type() }} {{ func.rust_name() }}({{ func.rust_arg_list() }});
  {%- endfor %}
  {%- endfor %}
}

// Define pointer types
{%- for (ci, config) in components %}
{%- for object in ci.object_definitions() %}
{%- let pointer_type = ci.pointer_type(object) %}
const static mozilla::uniffi::UniFFIPointerType {{ pointer_type }} {
  "{{ "{}::{}"|format(ci.namespace(), object.name()) }}"_ns,
  {{ object.ffi_object_clone().rust_name() }},
  {{ object.ffi_object_free().rust_name() }},
};
{%- endfor %}
{%- endfor %}

// Define the data we need per-callback interface
{%- for (ci, config) in components %}
{%- for cbi in ci.callback_interface_definitions() %}
MOZ_CAN_RUN_SCRIPT
extern "C" int {{ cbi.c_handler(prefix) }}(uint64_t aHandle, uint32_t aMethod, const uint8_t* aArgsData, int32_t aArgsLen, RustBuffer* aOutBuffer) {
    // Currently, we only support "fire-and-forget" async callbacks.  These are
    // callbacks that run asynchronously without returning anything.  The main
    // use case for callbacks is logging, which fits very well with this model.
    //
    // So, here we simple queue the callback and return immediately.
    mozilla::uniffi::QueueCallback({{ callback_ids.get(ci, cbi) }}, aHandle, aMethod, aArgsData, aArgsLen);
    return CALLBACK_INTERFACE_SUCCESS;
}
static StaticRefPtr<dom::UniFFICallbackHandler> {{ cbi.js_handler() }};
{%- endfor %}
{%- endfor %}

// Define a lookup function for our callback interface info
Maybe<CallbackInterfaceInfo> {{ prefix }}GetCallbackInterfaceInfo(uint64_t aInterfaceId) {
    switch(aInterfaceId) {
        {%- for (ci, config) in components %}
        {%- for cbi in ci.callback_interface_definitions() %}
        case {{ callback_ids.get(ci, cbi) }}: { // {{ callback_ids.name(ci, cbi) }}
            return Some(CallbackInterfaceInfo {
                "{{ cbi.name() }}",
                &{{ cbi.js_handler() }},
                {{ cbi.c_handler(prefix) }},
                {{ cbi.ffi_init_callback().name() }},
            });
        }
        {%- endfor %}
        {%- endfor %}

        default:
            return Nothing();
    }
}

Maybe<already_AddRefed<Promise>> {{ prefix }}CallAsync(const GlobalObject& aGlobal, uint64_t aId, const Sequence<UniFFIScaffoldingValue>& aArgs, ErrorResult& aError) {
  switch (aId) {
    {%- for (ci, config) in components %}
    {%- for func in ci.exposed_functions() %}
    case {{ function_ids.get(ci, func) }}: { // {{ function_ids.name(ci, func) }}
      using CallHandler = {{ ci.scaffolding_call_handler(func) }};
      return Some(CallHandler::CallAsync({{ func.rust_name() }}, aGlobal, aArgs, "{{ func.name() }}: "_ns, aError));
    }
    {%- endfor %}
    {%- endfor %}
  }
  return Nothing();
}

bool {{ prefix }}CallSync(const GlobalObject& aGlobal, uint64_t aId, const Sequence<UniFFIScaffoldingValue>& aArgs, RootedDictionary<UniFFIScaffoldingCallResult>& aReturnValue, ErrorResult& aError) {
  switch (aId) {
    {%- for (ci, config) in components %}
    {%- for func in ci.exposed_functions() %}
    case {{ function_ids.get(ci, func) }}: { // {{ function_ids.name(ci, func) }}
      using CallHandler = {{ ci.scaffolding_call_handler(func) }};
      CallHandler::CallSync({{ func.rust_name() }}, aGlobal, aArgs, aReturnValue, "{{ func.name() }}: "_ns, aError);
      return true;
    }
    {%- endfor %}
    {%- endfor %}
  }
  return false;
}

Maybe<already_AddRefed<UniFFIPointer>> {{ prefix }}ReadPointer(const GlobalObject& aGlobal, uint64_t aId, const ArrayBuffer& aArrayBuff, long aPosition, ErrorResult& aError) {
  {%- if self.has_any_objects() %}
  const UniFFIPointerType* type;
  switch (aId) {
    {%- for (ci, config) in components %}
    {%- for object in ci.object_definitions() %}
    case {{ object_ids.get(ci, object) }}: { // {{ object_ids.name(ci, object) }}
      type = &{{ ci.pointer_type(object) }};
      break;
    }
    {%- endfor %}
    {%- endfor %}
    default:
      return Nothing();
  }
  return Some(UniFFIPointer::Read(aArrayBuff, aPosition, type, aError));
  {%- else %}
  return Nothing();
  {%- endif %}
}

bool {{ prefix }}WritePointer(const GlobalObject& aGlobal, uint64_t aId, const UniFFIPointer& aPtr, const ArrayBuffer& aArrayBuff, long aPosition, ErrorResult& aError) {
  {%- if self.has_any_objects() %}
  const UniFFIPointerType* type;
  switch (aId) {
    {%- for (ci, config) in components %}
    {%- for object in ci.object_definitions() %}
    case {{ object_ids.get(ci, object) }}: { // {{ object_ids.name(ci, object) }}
      type = &{{ ci.pointer_type(object) }};
      break;
    }
    {%- endfor %}
    {%- endfor %}
    default:
      return false;
  }
  aPtr.Write(aArrayBuff, aPosition, type, aError);
  return true;
  {%- else %}
  return false;
  {%- endif %}
}

}  // namespace mozilla::uniffi