-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Expand file tree
/
Copy pathserializeModule.js
More file actions
167 lines (155 loc) · 5.11 KB
/
Copy pathserializeModule.js
File metadata and controls
167 lines (155 loc) · 5.11 KB
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
163
164
165
166
167
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
import type {NativeModuleEventEmitterShape} from '../../../../CodegenSchema';
import type {
MethodSerializationOutput,
StructParameterRecord,
} from '../serializeMethod';
import type {Struct} from '../StructCollector';
const {
EventEmitterImplementationTemplate,
} = require('./../serializeEventEmitter');
const ModuleTemplate = ({
hasteModuleName,
structs,
moduleName,
eventEmitters,
methodSerializationOutputs,
}: Readonly<{
hasteModuleName: string,
structs: ReadonlyArray<Struct>,
moduleName: string,
eventEmitters: ReadonlyArray<NativeModuleEventEmitterShape>,
methodSerializationOutputs: ReadonlyArray<MethodSerializationOutput>,
}>) => `
@implementation ${hasteModuleName}SpecBase
${eventEmitters
.map(eventEmitter => EventEmitterImplementationTemplate(eventEmitter))
.join('\n')}
- (void)setEventEmitterCallback:(EventEmitterCallbackWrapper *)eventEmitterCallbackWrapper
{
_eventEmitterCallback = std::move(eventEmitterCallbackWrapper->_eventEmitterCallback);
}
@end
${structs
.map(struct =>
RCTCxxConvertCategoryTemplate({hasteModuleName, structName: struct.name}),
)
.join('\n')}
namespace facebook::react {
${methodSerializationOutputs
.map(serializedMethodParts =>
InlineHostFunctionTemplate({
hasteModuleName,
methodName: serializedMethodParts.methodName,
returnJSType: serializedMethodParts.returnJSType,
selector: serializedMethodParts.selector,
}),
)
.join('\n')}
${hasteModuleName}SpecJSI::${hasteModuleName}SpecJSI(const ObjCTurboModule::InitParams ¶ms)
: ObjCTurboModule(params) {
${methodSerializationOutputs
.map(({methodName, structParamRecords, argCount}) =>
MethodMapEntryTemplate({
hasteModuleName,
methodName,
structParamRecords,
argCount,
}),
)
.join('\n' + ' '.repeat(8))}${
eventEmitters.length > 0
? eventEmitters
.map(eventEmitter => {
return `
eventEmitterMap_["${eventEmitter.name}"] = std::make_shared<AsyncEventEmitter<id>>();`;
})
.join('')
: ''
}${
// The callback outlives this module, so it captures a copy of the map
// instead of referencing eventEmitterMap_. Every emitter is registered
// directly above, so the copy is complete; an emitter registered after
// construction would not be reachable from the callback.
eventEmitters.length > 0
? `
setEventEmitterCallback([eventEmitterMap = eventEmitterMap_](const std::string &name, id value) {
auto it = eventEmitterMap.find(name);
if (it != eventEmitterMap.end() && it->second) {
static_cast<AsyncEventEmitter<id> &>(*it->second).emit(value);
}
});`
: ''
}
}
} // namespace facebook::react`;
const RCTCxxConvertCategoryTemplate = ({
hasteModuleName,
structName,
}: Readonly<{
hasteModuleName: string,
structName: string,
}>) => `@implementation RCTCxxConvert (${hasteModuleName}_${structName})
+ (RCTManagedPointer *)JS_${hasteModuleName}_${structName}:(id)json
{
return facebook::react::managedPointer<JS::${hasteModuleName}::${structName}>(json);
}
@end`;
const InlineHostFunctionTemplate = ({
hasteModuleName,
methodName,
returnJSType,
selector,
}: Readonly<{
hasteModuleName: string,
methodName: string,
returnJSType: string,
selector: string,
}>) => `
static facebook::jsi::Value __hostFunction_${hasteModuleName}SpecJSI_${methodName}(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ${returnJSType}, "${methodName}", ${selector}, args, count);
}`;
const MethodMapEntryTemplate = ({
hasteModuleName,
methodName,
structParamRecords,
argCount,
}: Readonly<{
hasteModuleName: string,
methodName: string,
structParamRecords: ReadonlyArray<StructParameterRecord>,
argCount: number,
}>) => `
methodMap_["${methodName}"] = MethodMetadata {${argCount}, __hostFunction_${hasteModuleName}SpecJSI_${methodName}};
${structParamRecords
.map(({paramIndex, structName}) => {
return `setMethodArgConversionSelector(@"${methodName}", ${paramIndex}, @"JS_${hasteModuleName}_${structName}:");`;
})
.join('\n' + ' '.repeat(8))}`;
function serializeModuleSource(
hasteModuleName: string,
structs: ReadonlyArray<Struct>,
moduleName: string,
eventEmitters: ReadonlyArray<NativeModuleEventEmitterShape>,
methodSerializationOutputs: ReadonlyArray<MethodSerializationOutput>,
): string {
return ModuleTemplate({
hasteModuleName,
structs: structs.filter(({context}) => context !== 'CONSTANTS'),
moduleName,
eventEmitters,
methodSerializationOutputs,
});
}
module.exports = {
serializeModuleSource,
};