@react-native-windows/codegen 0.0.0-canary.3 → 0.0.0-canary.32
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 +283 -6
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +231 -0
- package/lib-commonjs/Cli.js.map +1 -0
- package/lib-commonjs/generators/AliasGen.d.ts +11 -0
- package/lib-commonjs/generators/AliasGen.js +72 -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/GenerateNM2.d.ts +11 -0
- package/lib-commonjs/generators/GenerateNM2.js +94 -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 +8 -0
- package/lib-commonjs/generators/ObjectTypes.js +53 -0
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
- package/lib-commonjs/generators/ParamTypes.js +114 -0
- package/lib-commonjs/generators/ParamTypes.js.map +1 -0
- package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
- package/lib-commonjs/generators/ReturnTypes.js +63 -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 +8 -0
- package/lib-commonjs/generators/ValidateMethods.js +70 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/package.json +28 -16
- package/src/Cli.ts +172 -35
- package/src/generators/AliasGen.ts +105 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +62 -296
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ObjectTypes.ts +70 -0
- package/src/generators/ParamTypes.ts +220 -0
- package/src/generators/ReturnTypes.ts +92 -0
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +135 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -447
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
NativeModuleBaseTypeAnnotation,
|
|
11
|
+
NativeModuleObjectTypeAnnotation,
|
|
12
|
+
NamedShape,
|
|
13
|
+
Nullable,
|
|
14
|
+
} from 'react-native-tscodegen';
|
|
15
|
+
import {AliasMap, getAliasCppName} from './AliasManaging';
|
|
16
|
+
import {translateField} from './ObjectTypes';
|
|
17
|
+
|
|
18
|
+
function translateObjectBody(
|
|
19
|
+
type: NativeModuleObjectTypeAnnotation,
|
|
20
|
+
aliases: AliasMap,
|
|
21
|
+
baseAliasName: string,
|
|
22
|
+
prefix: string,
|
|
23
|
+
) {
|
|
24
|
+
return type.properties
|
|
25
|
+
.map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
|
|
26
|
+
let propType = prop.typeAnnotation;
|
|
27
|
+
if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
|
|
28
|
+
propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};
|
|
29
|
+
}
|
|
30
|
+
const first = `${prefix}REACT_FIELD(${prop.name})`;
|
|
31
|
+
const second = `${prefix}${translateField(
|
|
32
|
+
propType,
|
|
33
|
+
aliases,
|
|
34
|
+
`${baseAliasName}_${prop.name}`,
|
|
35
|
+
)} ${prop.name};`;
|
|
36
|
+
return `${first}\n${second}`;
|
|
37
|
+
})
|
|
38
|
+
.join('\n');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createAliasMap(nativeModuleAliases: {
|
|
42
|
+
[name: string]: NativeModuleObjectTypeAnnotation;
|
|
43
|
+
}): AliasMap {
|
|
44
|
+
const aliases: AliasMap = {types: {}, jobs: Object.keys(nativeModuleAliases)};
|
|
45
|
+
for (const aliasName of aliases.jobs) {
|
|
46
|
+
aliases.types[aliasName] = nativeModuleAliases[aliasName];
|
|
47
|
+
}
|
|
48
|
+
return aliases;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface AliasCodeMap {
|
|
52
|
+
[name: string]: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function generateSingleAlias(
|
|
56
|
+
aliases: AliasMap,
|
|
57
|
+
aliasName: string,
|
|
58
|
+
aliasCode: AliasCodeMap,
|
|
59
|
+
): void {
|
|
60
|
+
const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];
|
|
61
|
+
aliasCode[aliasName] = `
|
|
62
|
+
REACT_STRUCT(${getAliasCppName(aliasName)})
|
|
63
|
+
struct ${getAliasCppName(aliasName)} {
|
|
64
|
+
${translateObjectBody(aliasType, aliases, aliasName, ' ')}
|
|
65
|
+
};
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function generateNestedAliasesInCorrectOrder(
|
|
70
|
+
aliases: AliasMap,
|
|
71
|
+
aliasCode: AliasCodeMap,
|
|
72
|
+
aliasOrder: string[],
|
|
73
|
+
): void {
|
|
74
|
+
// retrieve and clean all ungenerated aliases
|
|
75
|
+
const jobs = aliases.jobs;
|
|
76
|
+
aliases.jobs = [];
|
|
77
|
+
|
|
78
|
+
// generate each one in its found order
|
|
79
|
+
for (const aliasName of jobs) {
|
|
80
|
+
// generate a new struct and all fields will be examined
|
|
81
|
+
// new anonymous objects could be found
|
|
82
|
+
// they will be stored in aliases.jobs
|
|
83
|
+
generateSingleAlias(aliases, aliasName, aliasCode);
|
|
84
|
+
// nested C++ structs must be put before the current C++ struct
|
|
85
|
+
// as they will be used in the current C++ struct
|
|
86
|
+
// the order will be perfectly and easily ensured by doing this recursively
|
|
87
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
|
|
88
|
+
// all referenced C++ structs are generated
|
|
89
|
+
// put the current one following them
|
|
90
|
+
aliasOrder.push(aliasName);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function generateAliases(aliases: AliasMap): string {
|
|
95
|
+
const aliasCode: AliasCodeMap = {};
|
|
96
|
+
const aliasOrder: string[] = [];
|
|
97
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
|
|
98
|
+
|
|
99
|
+
// aliasOrder now has the correct order of C++ struct code
|
|
100
|
+
let traversedAliasedStructs = '';
|
|
101
|
+
for (const aliasName of aliasOrder) {
|
|
102
|
+
traversedAliasedStructs = `${traversedAliasedStructs}${aliasCode[aliasName]}`;
|
|
103
|
+
}
|
|
104
|
+
return traversedAliasedStructs;
|
|
105
|
+
}
|
|
@@ -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 {NativeModuleObjectTypeAnnotation} from 'react-native-tscodegen';
|
|
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 {
|
|
22
|
+
types: {[name: string]: NativeModuleObjectTypeAnnotation | undefined};
|
|
23
|
+
jobs: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ExtendedObjectKey = '$RNW-TURBOMODULE-ALIAS';
|
|
27
|
+
interface ExtendedObject extends NativeModuleObjectTypeAnnotation {
|
|
28
|
+
'$RNW-TURBOMODULE-ALIAS'?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function recordAnonymouseAlias(
|
|
32
|
+
aliases: AliasMap,
|
|
33
|
+
baseAliasName: string,
|
|
34
|
+
extended: ExtendedObject,
|
|
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(
|
|
43
|
+
aliases: AliasMap,
|
|
44
|
+
baseAliasName: string,
|
|
45
|
+
objectType: NativeModuleObjectTypeAnnotation,
|
|
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>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
|
+
recordAnonymouseAlias(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
|
+
recordAnonymouseAlias(aliases, `${baseAliasName}${index}`, extended),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
@@ -6,14 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
FunctionTypeAnnotationParamTypeAnnotation,
|
|
15
|
-
FunctionTypeAnnotationReturn,
|
|
16
|
-
} from 'react-native-tscodegen';
|
|
9
|
+
import {SchemaType} from 'react-native-tscodegen';
|
|
10
|
+
import {AliasMap, setPreferredModuleName} from './AliasManaging';
|
|
11
|
+
import {createAliasMap, generateAliases} from './AliasGen';
|
|
12
|
+
import {generateValidateConstants} from './ValidateConstants';
|
|
13
|
+
import {generateValidateMethods} from './ValidateMethods';
|
|
17
14
|
|
|
18
15
|
type FilesOutput = Map<string, string>;
|
|
19
16
|
|
|
@@ -31,280 +28,21 @@ const moduleTemplate = `
|
|
|
31
28
|
#include <tuple>
|
|
32
29
|
|
|
33
30
|
namespace ::_NAMESPACE_:: {
|
|
34
|
-
|
|
31
|
+
::_MODULE_ALIASED_STRUCTS_::
|
|
35
32
|
struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
36
|
-
|
|
37
|
-
::_MODULE_PROPERTIES_TUPLE_::
|
|
38
|
-
};
|
|
33
|
+
::_MODULE_MEMBERS_TUPLES_::
|
|
39
34
|
|
|
40
35
|
template <class TModule>
|
|
41
36
|
static constexpr void ValidateModule() noexcept {
|
|
42
|
-
|
|
37
|
+
::_MODULE_MEMBERS_CHECKS_::
|
|
43
38
|
|
|
44
|
-
::
|
|
39
|
+
::_MODULE_MEMBERS_ERRORS_::
|
|
45
40
|
}
|
|
46
41
|
};
|
|
47
42
|
|
|
48
43
|
} // namespace ::_NAMESPACE_::
|
|
49
44
|
`;
|
|
50
45
|
|
|
51
|
-
function translateSpecFunctionParam(
|
|
52
|
-
param: FunctionTypeAnnotationParam,
|
|
53
|
-
): string {
|
|
54
|
-
switch (param.typeAnnotation.type) {
|
|
55
|
-
case 'StringTypeAnnotation':
|
|
56
|
-
return 'std::string';
|
|
57
|
-
case 'NumberTypeAnnotation':
|
|
58
|
-
case 'FloatTypeAnnotation':
|
|
59
|
-
return 'double';
|
|
60
|
-
case 'Int32TypeAnnotation':
|
|
61
|
-
return 'int';
|
|
62
|
-
case 'BooleanTypeAnnotation':
|
|
63
|
-
return 'bool';
|
|
64
|
-
case 'FunctionTypeAnnotation': {
|
|
65
|
-
// Ideally we'd get more information about the expected parameters of the callback
|
|
66
|
-
// But the current schema doesn't seem to provide the necessary information.
|
|
67
|
-
return 'Callback<React::JSValue>';
|
|
68
|
-
}
|
|
69
|
-
case 'ArrayTypeAnnotation':
|
|
70
|
-
// Ideally we'd get more information about the expected type of the array
|
|
71
|
-
// But the current schema doesn't seem to provide the necessary information.
|
|
72
|
-
return 'React::JSValueArray';
|
|
73
|
-
case 'GenericObjectTypeAnnotation':
|
|
74
|
-
return 'React::JSValueObject';
|
|
75
|
-
case 'ObjectTypeAnnotation':
|
|
76
|
-
// TODO we have more information here, and could create a more specific type
|
|
77
|
-
return 'React::JSValueObject';
|
|
78
|
-
case 'ReservedFunctionValueTypeAnnotation':
|
|
79
|
-
// (#6597)
|
|
80
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
81
|
-
if (param.typeAnnotation.name !== 'RootTag')
|
|
82
|
-
throw new Error(
|
|
83
|
-
`Unknown reserved function: ${param.typeAnnotation.name} in translateSpecFunctionParam`,
|
|
84
|
-
);
|
|
85
|
-
return 'double';
|
|
86
|
-
default:
|
|
87
|
-
throw new Error(
|
|
88
|
-
`Unhandled type in translateSpecFunctionParam: ${param.typeAnnotation.type}`,
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function translateFunctionParam(param: FunctionTypeAnnotationParam): string {
|
|
94
|
-
switch (param.typeAnnotation.type) {
|
|
95
|
-
case 'StringTypeAnnotation':
|
|
96
|
-
return 'std::string';
|
|
97
|
-
case 'NumberTypeAnnotation':
|
|
98
|
-
case 'FloatTypeAnnotation':
|
|
99
|
-
return 'double';
|
|
100
|
-
case 'Int32TypeAnnotation':
|
|
101
|
-
return 'int';
|
|
102
|
-
case 'BooleanTypeAnnotation':
|
|
103
|
-
return 'bool';
|
|
104
|
-
case 'FunctionTypeAnnotation': {
|
|
105
|
-
// Ideally we'd get more information about the expected parameters of the callback
|
|
106
|
-
// But the current schema doesn't seem to provide the necessary information.
|
|
107
|
-
return 'std::function<void(React::JSValue const &)> const &';
|
|
108
|
-
}
|
|
109
|
-
case 'ArrayTypeAnnotation':
|
|
110
|
-
// Ideally we'd get more information about the expected type of the array
|
|
111
|
-
// But the current schema doesn't seem to provide the necessary information.
|
|
112
|
-
return 'React::JSValueArray &&';
|
|
113
|
-
case 'GenericObjectTypeAnnotation':
|
|
114
|
-
return 'React::JSValueObject &&';
|
|
115
|
-
case 'ObjectTypeAnnotation':
|
|
116
|
-
// TODO we have more information here, and could create a more specific type
|
|
117
|
-
return 'React::JSValueObject &&';
|
|
118
|
-
case 'ReservedFunctionValueTypeAnnotation':
|
|
119
|
-
// (#6597)
|
|
120
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
121
|
-
if (param.typeAnnotation.name !== 'RootTag')
|
|
122
|
-
throw new Error(
|
|
123
|
-
`Unknown reserved function: ${param.typeAnnotation.name} in translateFunctionParam`,
|
|
124
|
-
);
|
|
125
|
-
return 'double';
|
|
126
|
-
default:
|
|
127
|
-
throw new Error(
|
|
128
|
-
`Unhandled type in translateFunctionParam: ${param.typeAnnotation.type} in translateFunctionParam`,
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function translateSpecReturnType(
|
|
134
|
-
type:
|
|
135
|
-
| FunctionTypeAnnotationParamTypeAnnotation
|
|
136
|
-
| FunctionTypeAnnotationReturn,
|
|
137
|
-
) {
|
|
138
|
-
switch (type.type) {
|
|
139
|
-
case 'VoidTypeAnnotation':
|
|
140
|
-
return 'void';
|
|
141
|
-
case 'StringTypeAnnotation':
|
|
142
|
-
return 'std::string';
|
|
143
|
-
case 'NumberTypeAnnotation':
|
|
144
|
-
case 'FloatTypeAnnotation':
|
|
145
|
-
return 'double';
|
|
146
|
-
case 'Int32TypeAnnotation':
|
|
147
|
-
return 'int';
|
|
148
|
-
case 'BooleanTypeAnnotation':
|
|
149
|
-
return 'bool';
|
|
150
|
-
case 'GenericPromiseTypeAnnotation':
|
|
151
|
-
return 'void';
|
|
152
|
-
case 'ArrayTypeAnnotation':
|
|
153
|
-
// Ideally we'd get more information about the expected type of the array
|
|
154
|
-
// But the current schema doesn't seem to provide the necessary information.
|
|
155
|
-
return 'React::JSValueArray';
|
|
156
|
-
case 'GenericObjectTypeAnnotation':
|
|
157
|
-
return 'React::JSValueObject';
|
|
158
|
-
case 'ReservedFunctionValueTypeAnnotation':
|
|
159
|
-
// (#6597)
|
|
160
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
161
|
-
if (type.name !== 'RootTag')
|
|
162
|
-
throw new Error(
|
|
163
|
-
`Unknown reserved function: ${type.name} in translateSpecReturnType`,
|
|
164
|
-
);
|
|
165
|
-
return 'double';
|
|
166
|
-
default:
|
|
167
|
-
throw new Error(
|
|
168
|
-
`Unhandled type in translateSpecReturnType: ${type.type}`,
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function translateImplReturnType(
|
|
174
|
-
type:
|
|
175
|
-
| FunctionTypeAnnotationParamTypeAnnotation
|
|
176
|
-
| FunctionTypeAnnotationReturn,
|
|
177
|
-
) {
|
|
178
|
-
switch (type.type) {
|
|
179
|
-
case 'VoidTypeAnnotation':
|
|
180
|
-
return 'void';
|
|
181
|
-
case 'StringTypeAnnotation':
|
|
182
|
-
return 'std::string';
|
|
183
|
-
case 'NumberTypeAnnotation':
|
|
184
|
-
case 'FloatTypeAnnotation':
|
|
185
|
-
return 'double';
|
|
186
|
-
case 'Int32TypeAnnotation':
|
|
187
|
-
return 'int';
|
|
188
|
-
case 'BooleanTypeAnnotation':
|
|
189
|
-
return 'bool';
|
|
190
|
-
case 'GenericPromiseTypeAnnotation':
|
|
191
|
-
return 'void';
|
|
192
|
-
case 'ArrayTypeAnnotation':
|
|
193
|
-
// Ideally we'd get more information about the expected type of the array
|
|
194
|
-
// But the current schema doesn't seem to provide the necessary information.
|
|
195
|
-
return 'React::JSValueArray';
|
|
196
|
-
case 'GenericObjectTypeAnnotation':
|
|
197
|
-
return 'React::JSValueObject';
|
|
198
|
-
case 'ReservedFunctionValueTypeAnnotation':
|
|
199
|
-
// (#6597)
|
|
200
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
201
|
-
if (type.name !== 'RootTag')
|
|
202
|
-
throw new Error(
|
|
203
|
-
`Unknown reserved function: ${type.name} in translateSpecReturnType`,
|
|
204
|
-
);
|
|
205
|
-
return 'double';
|
|
206
|
-
default:
|
|
207
|
-
throw new Error(
|
|
208
|
-
`Unhandled type in translateImplReturnType: ${type.type}`,
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function translateSpecArgs(params: ReadonlyArray<FunctionTypeAnnotationParam>) {
|
|
214
|
-
return params.map(param => {
|
|
215
|
-
const translatedParam = translateSpecFunctionParam(param);
|
|
216
|
-
return `${translatedParam}`;
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function translateArgs(params: ReadonlyArray<FunctionTypeAnnotationParam>) {
|
|
221
|
-
return params.map(param => {
|
|
222
|
-
const translatedParam = translateFunctionParam(param);
|
|
223
|
-
return `${translatedParam} ${param.name}`;
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function isMethodSync(prop: MethodTypeShape) {
|
|
228
|
-
return (
|
|
229
|
-
prop.typeAnnotation.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
|
|
230
|
-
prop.typeAnnotation.returnTypeAnnotation.type !==
|
|
231
|
-
'GenericPromiseTypeAnnotation'
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function isPromise(prop: MethodTypeShape) {
|
|
236
|
-
return (
|
|
237
|
-
prop.typeAnnotation.returnTypeAnnotation.type ===
|
|
238
|
-
'GenericPromiseTypeAnnotation'
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function getPossibleMethodSignatures(prop: MethodTypeShape): string[] {
|
|
243
|
-
const args = translateArgs(prop.typeAnnotation.params);
|
|
244
|
-
if (isPromise(prop)) {
|
|
245
|
-
// Sadly, currently, the schema doesn't currently provide us information on the type of the promise.
|
|
246
|
-
args.push('React::ReactPromise<React::JSValue> &&result');
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// TODO be much more exhastive on the possible method signatures that can be used..
|
|
250
|
-
const sig = `REACT_${isMethodSync(prop) ? 'SYNC_' : ''}METHOD(${
|
|
251
|
-
prop.name
|
|
252
|
-
}) ${translateImplReturnType(prop.typeAnnotation.returnTypeAnnotation)} ${
|
|
253
|
-
prop.name
|
|
254
|
-
}(${args.join(', ')}) noexcept { /* implementation */ }}`;
|
|
255
|
-
|
|
256
|
-
const staticsig = `REACT_${isMethodSync(prop) ? 'SYNC_' : ''}METHOD(${
|
|
257
|
-
prop.name
|
|
258
|
-
}) static ${translateImplReturnType(
|
|
259
|
-
prop.typeAnnotation.returnTypeAnnotation,
|
|
260
|
-
)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;
|
|
261
|
-
|
|
262
|
-
return [sig, staticsig];
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function translatePossibleMethodSignatures(prop: MethodTypeShape): string {
|
|
266
|
-
return getPossibleMethodSignatures(prop)
|
|
267
|
-
.map(sig => `" ${sig}\\n"`)
|
|
268
|
-
.join('\n ');
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function renderProperties(
|
|
272
|
-
properties: ReadonlyArray<MethodTypeShape>,
|
|
273
|
-
tuple: boolean,
|
|
274
|
-
): string {
|
|
275
|
-
// We skip the constants for now, since we dont have Spec file validation of them.
|
|
276
|
-
return properties
|
|
277
|
-
.filter(prop => prop.name !== 'getConstants')
|
|
278
|
-
.map((prop, index) => {
|
|
279
|
-
const params = prop.typeAnnotation.params;
|
|
280
|
-
|
|
281
|
-
const traversedArgs = translateSpecArgs(params);
|
|
282
|
-
|
|
283
|
-
const translatedReturnParam = translateSpecReturnType(
|
|
284
|
-
prop.typeAnnotation.returnTypeAnnotation,
|
|
285
|
-
);
|
|
286
|
-
|
|
287
|
-
if (isPromise(prop)) {
|
|
288
|
-
// Sadly, currently, the schema doesn't currently provide us information on the type of the promise.
|
|
289
|
-
traversedArgs.push('Promise<React::JSValue>');
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
if (tuple) {
|
|
293
|
-
return ` ${
|
|
294
|
-
isMethodSync(prop) ? 'Sync' : ''
|
|
295
|
-
}Method<${translatedReturnParam}(${traversedArgs.join(
|
|
296
|
-
', ',
|
|
297
|
-
)}) noexcept>{${index}, L"${prop.name}"},`;
|
|
298
|
-
} else {
|
|
299
|
-
return ` REACT_SHOW_METHOD_SPEC_ERRORS(
|
|
300
|
-
${index},
|
|
301
|
-
"${prop.name}",
|
|
302
|
-
${translatePossibleMethodSignatures(prop)});`;
|
|
303
|
-
}
|
|
304
|
-
})
|
|
305
|
-
.join('\n');
|
|
306
|
-
}
|
|
307
|
-
|
|
308
46
|
export function createNM2Generator({namespace}: {namespace: string}) {
|
|
309
47
|
return (
|
|
310
48
|
_libraryName: string,
|
|
@@ -313,33 +51,61 @@ export function createNM2Generator({namespace}: {namespace: string}) {
|
|
|
313
51
|
): FilesOutput => {
|
|
314
52
|
const files = new Map<string, string>();
|
|
315
53
|
|
|
316
|
-
const
|
|
317
|
-
.
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
54
|
+
for (const moduleName of Object.keys(schema.modules)) {
|
|
55
|
+
const nativeModule = schema.modules[moduleName];
|
|
56
|
+
// from 0.65 facebook's react-native-codegen
|
|
57
|
+
// the module name has the Native prefix comparing to 0.63
|
|
58
|
+
// when reading files we provided
|
|
59
|
+
const preferredModuleName = moduleName.startsWith('Native')
|
|
60
|
+
? moduleName.substr(6)
|
|
61
|
+
: moduleName;
|
|
62
|
+
setPreferredModuleName(preferredModuleName);
|
|
63
|
+
|
|
64
|
+
if (nativeModule.type === 'NativeModule') {
|
|
65
|
+
console.log(`Generating Native${preferredModuleName}Spec.g.h`);
|
|
66
|
+
|
|
67
|
+
// copy all explicit to a map
|
|
68
|
+
const aliases: AliasMap = createAliasMap(nativeModule.aliases);
|
|
69
|
+
|
|
70
|
+
// prepare methods
|
|
71
|
+
const methods = generateValidateMethods(nativeModule, aliases);
|
|
72
|
+
let tuples = `
|
|
73
|
+
static constexpr auto methods = std::tuple{
|
|
74
|
+
${methods[0]}
|
|
75
|
+
};`;
|
|
76
|
+
let checks = `
|
|
77
|
+
constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;
|
|
78
|
+
let errors = methods[1];
|
|
79
|
+
|
|
80
|
+
// prepare constants
|
|
81
|
+
const constants = generateValidateConstants(nativeModule, aliases);
|
|
82
|
+
if (constants !== undefined) {
|
|
83
|
+
tuples = `
|
|
84
|
+
static constexpr auto constants = std::tuple{
|
|
85
|
+
${constants[0]}
|
|
86
|
+
};${tuples}`;
|
|
87
|
+
checks = `
|
|
88
|
+
constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;
|
|
89
|
+
errors = `${constants[1]}
|
|
90
|
+
|
|
91
|
+
${errors}`;
|
|
321
92
|
}
|
|
322
93
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
.replace(/::_MODULE_PROPERTIES_SPEC_ERRORS_::/g, traversedProperties)
|
|
339
|
-
.replace(/::_MODULE_NAME_::/g, name)
|
|
340
|
-
.replace(/::_NAMESPACE_::/g, namespace),
|
|
341
|
-
);
|
|
342
|
-
});
|
|
94
|
+
// generate code for structs
|
|
95
|
+
const traversedAliasedStructs = generateAliases(aliases);
|
|
96
|
+
|
|
97
|
+
files.set(
|
|
98
|
+
`Native${preferredModuleName}Spec.g.h`,
|
|
99
|
+
moduleTemplate
|
|
100
|
+
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, traversedAliasedStructs)
|
|
101
|
+
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substr(1))
|
|
102
|
+
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substr(1))
|
|
103
|
+
.replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
|
|
104
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
105
|
+
.replace(/::_NAMESPACE_::/g, namespace),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
343
109
|
|
|
344
110
|
return files;
|
|
345
111
|
};
|