@react-native-windows/codegen 0.0.0-canary.13 → 0.0.0-canary.130
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1125 -16
- package/README.md +1 -1
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +103 -0
- package/lib-commonjs/Cli.js.map +1 -0
- package/lib-commonjs/generators/AliasGen.d.ts +12 -0
- package/lib-commonjs/generators/AliasGen.js +115 -0
- package/lib-commonjs/generators/AliasGen.js.map +1 -0
- package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
- package/lib-commonjs/generators/AliasManaging.js +49 -0
- package/lib-commonjs/generators/AliasManaging.js.map +1 -0
- package/lib-commonjs/generators/GenerateComponentWindows.d.ts +13 -0
- package/lib-commonjs/generators/GenerateComponentWindows.js +469 -0
- package/lib-commonjs/generators/GenerateComponentWindows.js.map +1 -0
- package/lib-commonjs/generators/GenerateNM2.d.ts +15 -0
- package/lib-commonjs/generators/GenerateNM2.js +145 -0
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
- package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
- package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
- package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
- package/lib-commonjs/generators/ObjectTypes.d.ts +13 -0
- package/lib-commonjs/generators/ObjectTypes.js +81 -0
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.d.ts +13 -0
- package/lib-commonjs/generators/ParamTypes.js +183 -0
- package/lib-commonjs/generators/ParamTypes.js.map +1 -0
- package/lib-commonjs/generators/PropObjectTypes.d.ts +18 -0
- package/lib-commonjs/generators/PropObjectTypes.js +208 -0
- package/lib-commonjs/generators/PropObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ReturnTypes.d.ts +10 -0
- package/lib-commonjs/generators/ReturnTypes.js +29 -0
- package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
- package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
- package/lib-commonjs/generators/ValidateConstants.js +38 -0
- package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
- package/lib-commonjs/generators/ValidateMethods.d.ts +14 -0
- package/lib-commonjs/generators/ValidateMethods.js +112 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/lib-commonjs/index.d.ts +39 -0
- package/lib-commonjs/index.js +227 -0
- package/lib-commonjs/index.js.map +1 -0
- package/package.json +39 -21
- package/src/Cli.ts +69 -232
- package/src/generators/AliasGen.ts +195 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateComponentWindows.ts +616 -0
- package/src/generators/GenerateNM2.ts +128 -131
- package/src/generators/GenerateTypeScript.ts +250 -0
- package/src/generators/ObjectTypes.ts +95 -37
- package/src/generators/ParamTypes.ts +309 -53
- package/src/generators/PropObjectTypes.ts +223 -0
- package/src/generators/ReturnTypes.ts +38 -40
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +270 -0
- package/src/index.ts +415 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -726
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
package/src/Cli.ts
CHANGED
|
@@ -6,14 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import yargs from 'yargs';
|
|
9
|
-
import
|
|
10
|
-
import fs from 'fs';
|
|
11
|
-
import globby from 'globby';
|
|
12
|
-
import {createNM2Generator} from './generators/GenerateNM2';
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
import {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
import schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';
|
|
9
|
+
import {CodeGenOptions, runCodeGen} from './index';
|
|
17
10
|
|
|
18
11
|
const argv = yargs.options({
|
|
19
12
|
file: {
|
|
@@ -21,12 +14,39 @@ const argv = yargs.options({
|
|
|
21
14
|
describe: 'file which contains spec',
|
|
22
15
|
},
|
|
23
16
|
files: {
|
|
24
|
-
type: '
|
|
17
|
+
type: 'string',
|
|
18
|
+
array: true,
|
|
25
19
|
describe: 'glob patterns for files which contains specs',
|
|
26
20
|
},
|
|
21
|
+
modulesTypeScriptTypes: {
|
|
22
|
+
type: 'boolean',
|
|
23
|
+
describe: 'generate turbo module definition files in TypeScript',
|
|
24
|
+
default: false,
|
|
25
|
+
},
|
|
26
|
+
modulesCxx: {
|
|
27
|
+
type: 'boolean',
|
|
28
|
+
describe: 'generate C++ JSI turbo module spec files',
|
|
29
|
+
default: false,
|
|
30
|
+
},
|
|
31
|
+
modulesWindows: {
|
|
32
|
+
type: 'boolean',
|
|
33
|
+
describe: 'generate turbo module spec files for REACT_MODULE',
|
|
34
|
+
default: false,
|
|
35
|
+
},
|
|
36
|
+
methodOnly: {
|
|
37
|
+
type: 'boolean',
|
|
38
|
+
describe: 'generate only method metadata in C++ turbo module spec',
|
|
39
|
+
default: false,
|
|
40
|
+
},
|
|
41
|
+
outputDirectory: {
|
|
42
|
+
type: 'string',
|
|
43
|
+
describe: 'output directory',
|
|
44
|
+
default: 'codegen',
|
|
45
|
+
},
|
|
27
46
|
test: {
|
|
28
47
|
type: 'boolean',
|
|
29
48
|
describe: 'Verify that the generated output is unchanged',
|
|
49
|
+
default: false,
|
|
30
50
|
},
|
|
31
51
|
namespace: {
|
|
32
52
|
type: 'string',
|
|
@@ -38,235 +58,52 @@ const argv = yargs.options({
|
|
|
38
58
|
required: true,
|
|
39
59
|
describe: 'Used for part of the path generated within the codegen dir',
|
|
40
60
|
},
|
|
61
|
+
cppStringType: {
|
|
62
|
+
choices: ['std::string', 'std::wstring'],
|
|
63
|
+
describe:
|
|
64
|
+
'C++ string type in generated code, should be "std::string" or "std::wstring"',
|
|
65
|
+
default: 'std::string',
|
|
66
|
+
},
|
|
67
|
+
separateDataTypes: {
|
|
68
|
+
type: 'boolean',
|
|
69
|
+
describe: 'generate data types in a separate file',
|
|
70
|
+
default: false,
|
|
71
|
+
},
|
|
72
|
+
componentsWindows: {
|
|
73
|
+
type: 'boolean',
|
|
74
|
+
describe: 'generate component cpp files for custom native components',
|
|
75
|
+
default: false,
|
|
76
|
+
},
|
|
77
|
+
internalComponents: {
|
|
78
|
+
type: 'boolean',
|
|
79
|
+
describe:
|
|
80
|
+
'generate non-ABI cpp/h for internal usage of built in native components [Only used within RNW itself]',
|
|
81
|
+
default: false,
|
|
82
|
+
hidden: true,
|
|
83
|
+
},
|
|
41
84
|
}).argv;
|
|
42
85
|
|
|
43
|
-
import {SchemaType} from 'react-native-tscodegen';
|
|
44
|
-
|
|
45
|
-
interface Options {
|
|
46
|
-
libraryName: string;
|
|
47
|
-
schema: SchemaType;
|
|
48
|
-
outputDirectory: string;
|
|
49
|
-
moduleSpecName: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
interface Config {
|
|
53
|
-
generators: any[] /*Generators[]*/;
|
|
54
|
-
test?: boolean;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/*
|
|
58
|
-
const GENERATORS = {
|
|
59
|
-
descriptors: [generateComponentDescriptorH.generate],
|
|
60
|
-
events: [
|
|
61
|
-
generateEventEmitterCpp.generate,
|
|
62
|
-
generateEventEmitterH.generate,
|
|
63
|
-
generateModuleHObjCpp.generate,
|
|
64
|
-
generateModuleMm.generate,
|
|
65
|
-
],
|
|
66
|
-
props: [
|
|
67
|
-
generateComponentHObjCpp.generate,
|
|
68
|
-
generatePropsCpp.generate,
|
|
69
|
-
generatePropsH.generate,
|
|
70
|
-
generatePropsJavaInterface.generate,
|
|
71
|
-
generatePropsJavaDelegate.generate,
|
|
72
|
-
],
|
|
73
|
-
modules: [generateModuleCpp.generate, generateModuleH.generate],
|
|
74
|
-
tests: [generateTests.generate],
|
|
75
|
-
'shadow-nodes': [
|
|
76
|
-
generateShadowNodeCpp.generate,
|
|
77
|
-
generateShadowNodeH.generate,
|
|
78
|
-
],
|
|
79
|
-
};
|
|
80
|
-
*/
|
|
81
|
-
|
|
82
|
-
function normalizeFileMap(
|
|
83
|
-
map: Map<string, string>,
|
|
84
|
-
outputDir: string,
|
|
85
|
-
outMap: Map<string, string>,
|
|
86
|
-
): void {
|
|
87
|
-
for (const [fileName, contents] of map) {
|
|
88
|
-
const location = path.join(outputDir, fileName);
|
|
89
|
-
outMap.set(path.normalize(location), contents);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function checkFilesForChanges(
|
|
94
|
-
map: Map<string, string>,
|
|
95
|
-
outputDir: string,
|
|
96
|
-
): boolean {
|
|
97
|
-
let hasChanges = false;
|
|
98
|
-
|
|
99
|
-
const allExistingFiles = globby
|
|
100
|
-
.sync(`${outputDir}/**`)
|
|
101
|
-
.map(_ => path.normalize(_))
|
|
102
|
-
.sort();
|
|
103
|
-
const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();
|
|
104
|
-
|
|
105
|
-
if (
|
|
106
|
-
allExistingFiles.length !== allGeneratedFiles.length ||
|
|
107
|
-
!allExistingFiles.every((val, index) => val === allGeneratedFiles[index])
|
|
108
|
-
)
|
|
109
|
-
return true;
|
|
110
|
-
|
|
111
|
-
for (const [fileName, contents] of map) {
|
|
112
|
-
if (!fs.existsSync(fileName)) {
|
|
113
|
-
hasChanges = true;
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const currentContents = fs.readFileSync(fileName, 'utf8');
|
|
118
|
-
if (currentContents !== contents) {
|
|
119
|
-
console.error(`- ${fileName} has changed`);
|
|
120
|
-
hasChanges = true;
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return hasChanges;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
129
|
-
let success = true;
|
|
130
|
-
|
|
131
|
-
// This ensures that we delete any generated files from modules that have been deleted
|
|
132
|
-
const allExistingFiles = globby.sync(`${outputDir}/**`);
|
|
133
|
-
allExistingFiles.forEach(existingFile => {
|
|
134
|
-
if (!map.has(path.normalize(existingFile))) {
|
|
135
|
-
fs.unlinkSync(existingFile);
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
for (const [fileName, contents] of map) {
|
|
140
|
-
try {
|
|
141
|
-
fs.mkdirSync(path.dirname(fileName), {recursive: true});
|
|
142
|
-
|
|
143
|
-
if (fs.existsSync(fileName)) {
|
|
144
|
-
const currentContents = fs.readFileSync(fileName, 'utf8');
|
|
145
|
-
// Don't update the files if there are no changes as this breaks incremental builds
|
|
146
|
-
if (currentContents === contents) {
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
fs.writeFileSync(fileName, contents);
|
|
152
|
-
} catch (error) {
|
|
153
|
-
success = false;
|
|
154
|
-
console.error(`Failed to write ${fileName} to ${fileName}`, error);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return success;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function parseFlowFile(filename: string): SchemaType {
|
|
162
|
-
try {
|
|
163
|
-
return parseFile(filename);
|
|
164
|
-
} catch (e) {
|
|
165
|
-
if (e instanceof Error) {
|
|
166
|
-
e.message = `(${filename}): ${e.message}`;
|
|
167
|
-
}
|
|
168
|
-
throw e;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function combineSchemas(files: string[]): SchemaType {
|
|
173
|
-
return files.reduce(
|
|
174
|
-
(merged, filename) => {
|
|
175
|
-
const contents = fs.readFileSync(filename, 'utf8');
|
|
176
|
-
if (
|
|
177
|
-
contents &&
|
|
178
|
-
(/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
|
|
179
|
-
contents.includes('extends TurboModule'))
|
|
180
|
-
) {
|
|
181
|
-
const schema = parseFlowFile(filename);
|
|
182
|
-
merged.modules = {...merged.modules, ...schema.modules};
|
|
183
|
-
}
|
|
184
|
-
return merged;
|
|
185
|
-
},
|
|
186
|
-
{modules: {}},
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function generate(
|
|
191
|
-
{libraryName, schema, outputDirectory, moduleSpecName}: Options,
|
|
192
|
-
{/*generators,*/ test}: Config,
|
|
193
|
-
): boolean {
|
|
194
|
-
schemaValidator.validate(schema);
|
|
195
|
-
|
|
196
|
-
const componentOutputdir = path.join(
|
|
197
|
-
outputDirectory,
|
|
198
|
-
'react/components',
|
|
199
|
-
libraryName,
|
|
200
|
-
);
|
|
201
|
-
|
|
202
|
-
const generatedFiles = new Map<string, string>();
|
|
203
|
-
|
|
204
|
-
generatedFiles.set(
|
|
205
|
-
path.join(outputDirectory, '.clang-format'),
|
|
206
|
-
'DisableFormat: true\nSortIncludes: false',
|
|
207
|
-
);
|
|
208
|
-
|
|
209
|
-
const generateNM2 = createNM2Generator({namespace: argv.namespace});
|
|
210
|
-
const generatorPropsH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH')
|
|
211
|
-
.generate;
|
|
212
|
-
const generatorPropsCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP')
|
|
213
|
-
.generate;
|
|
214
|
-
const generatorShadowNodeH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH')
|
|
215
|
-
.generate;
|
|
216
|
-
const generatorShadowNodeCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP')
|
|
217
|
-
.generate;
|
|
218
|
-
const generatorComponentDescriptorH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH')
|
|
219
|
-
.generate;
|
|
220
|
-
const generatorEventEmitterH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH')
|
|
221
|
-
.generate;
|
|
222
|
-
|
|
223
|
-
normalizeFileMap(
|
|
224
|
-
generateNM2(libraryName, schema, moduleSpecName),
|
|
225
|
-
outputDirectory,
|
|
226
|
-
generatedFiles,
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
const componentGenerators = [
|
|
230
|
-
generatorPropsH,
|
|
231
|
-
generatorPropsCPP,
|
|
232
|
-
generatorShadowNodeH,
|
|
233
|
-
generatorShadowNodeCPP,
|
|
234
|
-
generatorComponentDescriptorH,
|
|
235
|
-
generatorEventEmitterH,
|
|
236
|
-
];
|
|
237
|
-
|
|
238
|
-
componentGenerators.forEach(generator => {
|
|
239
|
-
const generated: Map<string, string> = generator(
|
|
240
|
-
libraryName,
|
|
241
|
-
schema,
|
|
242
|
-
moduleSpecName,
|
|
243
|
-
);
|
|
244
|
-
normalizeFileMap(generated, componentOutputdir, generatedFiles);
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
if (test === true) {
|
|
248
|
-
return checkFilesForChanges(generatedFiles, outputDirectory);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return writeMapToFiles(generatedFiles, outputDirectory);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
86
|
if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
|
|
255
87
|
console.error('You must specify either --file or --files.');
|
|
256
88
|
process.exit(1);
|
|
257
89
|
}
|
|
258
90
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
91
|
+
if (
|
|
92
|
+
argv.cppStringType !== 'std::string' &&
|
|
93
|
+
argv.cppStringType !== 'std::wstring'
|
|
94
|
+
) {
|
|
95
|
+
console.error('cppStringType should be "std::string" or "std::wstring".');
|
|
96
|
+
process.exit(1);
|
|
264
97
|
}
|
|
265
98
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
99
|
+
// type casting is necessary here because
|
|
100
|
+
// cppStringType does not become union of string literals
|
|
101
|
+
// until yargs.options get improved in the future
|
|
102
|
+
const changesNecessary = runCodeGen(<CodeGenOptions>argv);
|
|
103
|
+
|
|
104
|
+
if (argv.test && changesNecessary) {
|
|
105
|
+
console.error(
|
|
106
|
+
'There is a change in the output of codegen. Rerun "npx @react-native-community/cli codegen-windows" to regenerate.',
|
|
107
|
+
);
|
|
108
|
+
process.exit(2);
|
|
109
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
NativeModuleBaseTypeAnnotation,
|
|
11
|
+
NativeModuleObjectTypeAnnotation,
|
|
12
|
+
NamedShape,
|
|
13
|
+
Nullable,
|
|
14
|
+
} from '@react-native/codegen/lib/CodegenSchema';
|
|
15
|
+
import {AliasMap, getAliasCppName} from './AliasManaging';
|
|
16
|
+
import {CppCodegenOptions, translateField} from './ObjectTypes';
|
|
17
|
+
|
|
18
|
+
function translateObjectMembersDefinition(
|
|
19
|
+
type: NativeModuleObjectTypeAnnotation,
|
|
20
|
+
aliases: AliasMap,
|
|
21
|
+
baseAliasName: string,
|
|
22
|
+
prefix: string,
|
|
23
|
+
options: CppCodegenOptions,
|
|
24
|
+
) {
|
|
25
|
+
return type.properties
|
|
26
|
+
.map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
|
|
27
|
+
let propType = prop.typeAnnotation;
|
|
28
|
+
if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
|
|
29
|
+
propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};
|
|
30
|
+
}
|
|
31
|
+
return `${prefix}${translateField(
|
|
32
|
+
propType,
|
|
33
|
+
aliases,
|
|
34
|
+
`${baseAliasName}_${prop.name}`,
|
|
35
|
+
options,
|
|
36
|
+
)} ${prop.name};`;
|
|
37
|
+
})
|
|
38
|
+
.join('\n');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function translateObjectMembersReflection(
|
|
42
|
+
type: NativeModuleObjectTypeAnnotation,
|
|
43
|
+
aliasCppName: string,
|
|
44
|
+
prefix: string,
|
|
45
|
+
) {
|
|
46
|
+
return type.properties
|
|
47
|
+
.map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
|
|
48
|
+
return `${prefix}{L"${prop.name}", &${aliasCppName}::${prop.name}},`;
|
|
49
|
+
})
|
|
50
|
+
.join('\n');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createAliasMap(nativeModuleAliases: {
|
|
54
|
+
[name: string]: NativeModuleObjectTypeAnnotation;
|
|
55
|
+
}): AliasMap {
|
|
56
|
+
const aliases: AliasMap = {types: {}, jobs: Object.keys(nativeModuleAliases)};
|
|
57
|
+
for (const aliasName of aliases.jobs) {
|
|
58
|
+
aliases.types[aliasName] = nativeModuleAliases[aliasName];
|
|
59
|
+
}
|
|
60
|
+
return aliases;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface AliasCode {
|
|
64
|
+
definition: string;
|
|
65
|
+
reflection: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface AliasCodeMap {
|
|
69
|
+
[name: string]: AliasCode;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function getArrayTypeName(
|
|
73
|
+
type: Nullable<NativeModuleBaseTypeAnnotation>,
|
|
74
|
+
): string {
|
|
75
|
+
if (
|
|
76
|
+
type.type === 'ArrayTypeAnnotation' &&
|
|
77
|
+
type.elementType.type === 'TypeAliasTypeAnnotation'
|
|
78
|
+
) {
|
|
79
|
+
return type.elementType.name;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return '';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function checkTypes(
|
|
86
|
+
aliases: AliasMap,
|
|
87
|
+
type: NativeModuleObjectTypeAnnotation,
|
|
88
|
+
aliasOrder: string[],
|
|
89
|
+
): void {
|
|
90
|
+
for (const prop of type.properties) {
|
|
91
|
+
const propType = prop.typeAnnotation;
|
|
92
|
+
let propName = '';
|
|
93
|
+
|
|
94
|
+
if (propType.type === 'TypeAliasTypeAnnotation') {
|
|
95
|
+
propName = propType.name;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (propType.type === 'ArrayTypeAnnotation') {
|
|
99
|
+
propName = getArrayTypeName(propType);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (
|
|
103
|
+
propName !== '' &&
|
|
104
|
+
!aliasOrder.includes(prop.name) &&
|
|
105
|
+
!aliases.jobs.includes(propName)
|
|
106
|
+
) {
|
|
107
|
+
aliases.jobs.push(propName);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function generateSingleAlias(
|
|
113
|
+
aliases: AliasMap,
|
|
114
|
+
aliasName: string,
|
|
115
|
+
aliasCode: AliasCodeMap,
|
|
116
|
+
options: CppCodegenOptions,
|
|
117
|
+
aliasOrder: string[],
|
|
118
|
+
): void {
|
|
119
|
+
const aliasCppName = getAliasCppName(aliasName);
|
|
120
|
+
const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];
|
|
121
|
+
|
|
122
|
+
checkTypes(aliases, aliasType, aliasOrder);
|
|
123
|
+
|
|
124
|
+
const definition = `
|
|
125
|
+
struct ${aliasCppName} {
|
|
126
|
+
${translateObjectMembersDefinition(
|
|
127
|
+
aliasType,
|
|
128
|
+
aliases,
|
|
129
|
+
aliasName,
|
|
130
|
+
' ',
|
|
131
|
+
options,
|
|
132
|
+
)}
|
|
133
|
+
};
|
|
134
|
+
`;
|
|
135
|
+
const reflection = `
|
|
136
|
+
inline winrt::Microsoft::ReactNative::FieldMap GetStructInfo(${aliasCppName}*) noexcept {
|
|
137
|
+
winrt::Microsoft::ReactNative::FieldMap fieldMap {
|
|
138
|
+
${translateObjectMembersReflection(aliasType, aliasCppName, ' ')}
|
|
139
|
+
};
|
|
140
|
+
return fieldMap;
|
|
141
|
+
}
|
|
142
|
+
`;
|
|
143
|
+
aliasCode[aliasName] = {definition, reflection};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function generateNestedAliasesInCorrectOrder(
|
|
147
|
+
aliases: AliasMap,
|
|
148
|
+
aliasCode: AliasCodeMap,
|
|
149
|
+
aliasOrder: string[],
|
|
150
|
+
options: CppCodegenOptions,
|
|
151
|
+
): void {
|
|
152
|
+
// retrieve and clean all ungenerated aliases
|
|
153
|
+
const jobs = aliases.jobs;
|
|
154
|
+
aliases.jobs = [];
|
|
155
|
+
|
|
156
|
+
// generate each one in its found order
|
|
157
|
+
for (const aliasName of jobs) {
|
|
158
|
+
// generate a new struct and all fields will be examined
|
|
159
|
+
// new anonymous objects could be found
|
|
160
|
+
// they will be stored in aliases.jobs
|
|
161
|
+
generateSingleAlias(aliases, aliasName, aliasCode, options, aliasOrder);
|
|
162
|
+
// nested C++ structs must be put before the current C++ struct
|
|
163
|
+
// as they will be used in the current C++ struct
|
|
164
|
+
// the order will be perfectly and easily ensured by doing this recursively
|
|
165
|
+
generateNestedAliasesInCorrectOrder(
|
|
166
|
+
aliases,
|
|
167
|
+
aliasCode,
|
|
168
|
+
aliasOrder,
|
|
169
|
+
options,
|
|
170
|
+
);
|
|
171
|
+
// all referenced C++ structs are generated
|
|
172
|
+
// put the current one following them
|
|
173
|
+
if (!aliasOrder.includes(aliasName)) {
|
|
174
|
+
aliasOrder.push(aliasName);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function generateAliases(
|
|
180
|
+
aliases: AliasMap,
|
|
181
|
+
options: CppCodegenOptions,
|
|
182
|
+
): [string, string] {
|
|
183
|
+
const aliasCode: AliasCodeMap = {};
|
|
184
|
+
const aliasOrder: string[] = [];
|
|
185
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);
|
|
186
|
+
|
|
187
|
+
// aliasOrder now has the correct order of C++ struct code
|
|
188
|
+
let customTypes = '';
|
|
189
|
+
let customReflection = '';
|
|
190
|
+
for (const aliasName of aliasOrder) {
|
|
191
|
+
customTypes = `${customTypes}${aliasCode[aliasName].definition}`;
|
|
192
|
+
customReflection = `${customReflection}${aliasCode[aliasName].reflection}`;
|
|
193
|
+
}
|
|
194
|
+
return [customTypes, customReflection];
|
|
195
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import type {NativeModuleObjectTypeAnnotation} from '@react-native/codegen/lib/CodegenSchema';
|
|
10
|
+
|
|
11
|
+
let preferredModuleName: string = '';
|
|
12
|
+
|
|
13
|
+
export function setPreferredModuleName(moduleName: string): void {
|
|
14
|
+
preferredModuleName = moduleName;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getAliasCppName(typeName: string): string {
|
|
18
|
+
return `${preferredModuleName}Spec_${typeName}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AliasMap<T = NativeModuleObjectTypeAnnotation> {
|
|
22
|
+
types: {[name: string]: T | undefined};
|
|
23
|
+
jobs: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ExtendedObjectKey = '$RNW-TURBOMODULE-ALIAS';
|
|
27
|
+
type ExtendedObject<T> = {
|
|
28
|
+
'$RNW-TURBOMODULE-ALIAS'?: string;
|
|
29
|
+
} & T;
|
|
30
|
+
|
|
31
|
+
function recordAnonymousAlias<T = NativeModuleObjectTypeAnnotation>(
|
|
32
|
+
aliases: AliasMap<T>,
|
|
33
|
+
baseAliasName: string,
|
|
34
|
+
extended: ExtendedObject<T>,
|
|
35
|
+
): string {
|
|
36
|
+
extended[ExtendedObjectKey] = baseAliasName;
|
|
37
|
+
aliases.types[baseAliasName] = extended;
|
|
38
|
+
aliases.jobs.push(baseAliasName);
|
|
39
|
+
return baseAliasName;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function getAnonymousAliasCppName<T = NativeModuleObjectTypeAnnotation>(
|
|
43
|
+
aliases: AliasMap<T>,
|
|
44
|
+
baseAliasName: string,
|
|
45
|
+
objectType: T,
|
|
46
|
+
): string {
|
|
47
|
+
// someone found an anonymous object literal type
|
|
48
|
+
// if the ExtendedObjectKey flag has been set
|
|
49
|
+
// then it is a known one
|
|
50
|
+
// this happens because method signatures are generate twice in spec and error messages
|
|
51
|
+
const extended = <ExtendedObject<T>>objectType;
|
|
52
|
+
const key = extended[ExtendedObjectKey];
|
|
53
|
+
if (key !== undefined) {
|
|
54
|
+
return getAliasCppName(key);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// if the ExtendedObjectKey flag has not been set
|
|
58
|
+
// it means it is a unknown one
|
|
59
|
+
// associate the name with this object literal type and return
|
|
60
|
+
if (aliases.types[baseAliasName] === undefined) {
|
|
61
|
+
return getAliasCppName(
|
|
62
|
+
recordAnonymousAlias<T>(aliases, baseAliasName, extended),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// sometimes names could be anonymous
|
|
67
|
+
let index = 2;
|
|
68
|
+
while (aliases.types[`${baseAliasName}${index}`] !== undefined) {
|
|
69
|
+
index++;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return getAliasCppName(
|
|
73
|
+
recordAnonymousAlias(aliases, `${baseAliasName}${index}`, extended),
|
|
74
|
+
);
|
|
75
|
+
}
|