@react-native-windows/codegen 0.0.0-canary.7 → 0.0.0-canary.71
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 +620 -11
- package/README.md +1 -1
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +92 -0
- package/lib-commonjs/Cli.js.map +1 -0
- package/lib-commonjs/generators/AliasGen.d.ts +12 -0
- package/lib-commonjs/generators/AliasGen.js +88 -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 +15 -0
- package/lib-commonjs/generators/GenerateNM2.js +142 -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 +75 -0
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.d.ts +12 -0
- package/lib-commonjs/generators/ParamTypes.js +137 -0
- package/lib-commonjs/generators/ParamTypes.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 +9 -0
- package/lib-commonjs/generators/ValidateMethods.js +75 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/lib-commonjs/index.d.ts +37 -0
- package/lib-commonjs/index.js +224 -0
- package/lib-commonjs/index.js.map +1 -0
- package/package.json +37 -20
- package/src/Cli.ts +57 -190
- package/src/generators/AliasGen.ts +149 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +138 -295
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ObjectTypes.ts +130 -0
- package/src/generators/ParamTypes.ts +298 -0
- package/src/generators/ReturnTypes.ts +70 -0
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +177 -0
- package/src/index.ts +393 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -547
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
|
@@ -0,0 +1,149 @@
|
|
|
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 generateSingleAlias(
|
|
73
|
+
aliases: AliasMap,
|
|
74
|
+
aliasName: string,
|
|
75
|
+
aliasCode: AliasCodeMap,
|
|
76
|
+
options: CppCodegenOptions,
|
|
77
|
+
): void {
|
|
78
|
+
const aliasCppName = getAliasCppName(aliasName);
|
|
79
|
+
const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];
|
|
80
|
+
const definition = `
|
|
81
|
+
struct ${aliasCppName} {
|
|
82
|
+
${translateObjectMembersDefinition(
|
|
83
|
+
aliasType,
|
|
84
|
+
aliases,
|
|
85
|
+
aliasName,
|
|
86
|
+
' ',
|
|
87
|
+
options,
|
|
88
|
+
)}
|
|
89
|
+
};
|
|
90
|
+
`;
|
|
91
|
+
const reflection = `
|
|
92
|
+
inline winrt::Microsoft::ReactNative::FieldMap GetStructInfo(${aliasCppName}*) noexcept {
|
|
93
|
+
winrt::Microsoft::ReactNative::FieldMap fieldMap {
|
|
94
|
+
${translateObjectMembersReflection(aliasType, aliasCppName, ' ')}
|
|
95
|
+
};
|
|
96
|
+
return fieldMap;
|
|
97
|
+
}
|
|
98
|
+
`;
|
|
99
|
+
aliasCode[aliasName] = {definition, reflection};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function generateNestedAliasesInCorrectOrder(
|
|
103
|
+
aliases: AliasMap,
|
|
104
|
+
aliasCode: AliasCodeMap,
|
|
105
|
+
aliasOrder: string[],
|
|
106
|
+
options: CppCodegenOptions,
|
|
107
|
+
): void {
|
|
108
|
+
// retrieve and clean all ungenerated aliases
|
|
109
|
+
const jobs = aliases.jobs;
|
|
110
|
+
aliases.jobs = [];
|
|
111
|
+
|
|
112
|
+
// generate each one in its found order
|
|
113
|
+
for (const aliasName of jobs) {
|
|
114
|
+
// generate a new struct and all fields will be examined
|
|
115
|
+
// new anonymous objects could be found
|
|
116
|
+
// they will be stored in aliases.jobs
|
|
117
|
+
generateSingleAlias(aliases, aliasName, aliasCode, options);
|
|
118
|
+
// nested C++ structs must be put before the current C++ struct
|
|
119
|
+
// as they will be used in the current C++ struct
|
|
120
|
+
// the order will be perfectly and easily ensured by doing this recursively
|
|
121
|
+
generateNestedAliasesInCorrectOrder(
|
|
122
|
+
aliases,
|
|
123
|
+
aliasCode,
|
|
124
|
+
aliasOrder,
|
|
125
|
+
options,
|
|
126
|
+
);
|
|
127
|
+
// all referenced C++ structs are generated
|
|
128
|
+
// put the current one following them
|
|
129
|
+
aliasOrder.push(aliasName);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function generateAliases(
|
|
134
|
+
aliases: AliasMap,
|
|
135
|
+
options: CppCodegenOptions,
|
|
136
|
+
): [string, string] {
|
|
137
|
+
const aliasCode: AliasCodeMap = {};
|
|
138
|
+
const aliasOrder: string[] = [];
|
|
139
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);
|
|
140
|
+
|
|
141
|
+
// aliasOrder now has the correct order of C++ struct code
|
|
142
|
+
let customTypes = '';
|
|
143
|
+
let customReflection = '';
|
|
144
|
+
for (const aliasName of aliasOrder) {
|
|
145
|
+
customTypes = `${customTypes}${aliasCode[aliasName].definition}`;
|
|
146
|
+
customReflection = `${customReflection}${aliasCode[aliasName].reflection}`;
|
|
147
|
+
}
|
|
148
|
+
return [customTypes, customReflection];
|
|
149
|
+
}
|
|
@@ -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 {
|
|
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 recordAnonymousAlias(
|
|
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
|
+
recordAnonymousAlias(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
|
+
}
|
|
@@ -6,306 +6,89 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from '
|
|
9
|
+
import type {SchemaType} from '@react-native/codegen/lib/CodegenSchema';
|
|
10
|
+
import {AliasMap, setPreferredModuleName} from './AliasManaging';
|
|
11
|
+
import {createAliasMap, generateAliases} from './AliasGen';
|
|
12
|
+
import {generateValidateConstants} from './ValidateConstants';
|
|
13
|
+
import {generateValidateMethods} from './ValidateMethods';
|
|
14
|
+
import type {CppStringTypes} from './ObjectTypes';
|
|
15
|
+
|
|
16
|
+
export type {CppStringTypes} from './ObjectTypes';
|
|
17
17
|
|
|
18
18
|
type FilesOutput = Map<string, string>;
|
|
19
19
|
|
|
20
|
-
const
|
|
21
|
-
/*
|
|
20
|
+
const headerTemplate = `/*
|
|
22
21
|
* This file is auto-generated from a NativeModule spec file in js.
|
|
23
22
|
*
|
|
24
23
|
* This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules
|
|
25
24
|
* in a way that also verifies at compile time that the native module matches the interface required
|
|
26
25
|
* by the TurboModule JS spec.
|
|
27
26
|
*/
|
|
28
|
-
#pragma once
|
|
29
|
-
|
|
30
|
-
#include "NativeModules.h"
|
|
31
|
-
#include <tuple>
|
|
32
|
-
|
|
33
|
-
namespace ::_NAMESPACE_:: {
|
|
27
|
+
#pragma once`;
|
|
34
28
|
|
|
29
|
+
const specTemplate = `::_MODULE_CUSTPM_TYPES_REFLECTION_::
|
|
35
30
|
struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
36
|
-
|
|
37
|
-
::_MODULE_PROPERTIES_TUPLE_::
|
|
38
|
-
};
|
|
31
|
+
::_MODULE_MEMBERS_TUPLES_::
|
|
39
32
|
|
|
40
33
|
template <class TModule>
|
|
41
34
|
static constexpr void ValidateModule() noexcept {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
::_MODULE_PROPERTIES_SPEC_ERRORS_::
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
} // namespace ::_NAMESPACE_::
|
|
49
|
-
`;
|
|
50
|
-
|
|
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
|
-
}
|
|
35
|
+
::_MODULE_MEMBERS_CHECKS_::
|
|
226
36
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
prop.typeAnnotation.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
|
|
230
|
-
prop.typeAnnotation.returnTypeAnnotation.type !==
|
|
231
|
-
'GenericPromiseTypeAnnotation'
|
|
232
|
-
);
|
|
233
|
-
}
|
|
37
|
+
::_MODULE_MEMBERS_ERRORS_::
|
|
38
|
+
}`;
|
|
234
39
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
prop.typeAnnotation.returnTypeAnnotation.type ===
|
|
238
|
-
'GenericPromiseTypeAnnotation'
|
|
239
|
-
);
|
|
240
|
-
}
|
|
40
|
+
const typeOnlyTemplate = `
|
|
41
|
+
${headerTemplate}
|
|
241
42
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
args.push('React::ReactPromise<React::JSValue> &&result');
|
|
247
|
-
}
|
|
43
|
+
#include <string>
|
|
44
|
+
#include <optional>
|
|
45
|
+
#include <functional>
|
|
46
|
+
#include <vector>
|
|
248
47
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
prop.name
|
|
254
|
-
}(${args.join(', ')}) noexcept { /* implementation */ }}`;
|
|
48
|
+
namespace ::_NAMESPACE_:: {
|
|
49
|
+
::_MODULE_CUSTPM_TYPES_::
|
|
50
|
+
} // namespace ::_NAMESPACE_::
|
|
51
|
+
`;
|
|
255
52
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}) static ${translateImplReturnType(
|
|
259
|
-
prop.typeAnnotation.returnTypeAnnotation,
|
|
260
|
-
)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;
|
|
53
|
+
const moduleOnlyTemplate = `
|
|
54
|
+
${headerTemplate}
|
|
261
55
|
|
|
262
|
-
|
|
263
|
-
|
|
56
|
+
::_TYPE_DEFINITION_INCLUDE_::
|
|
57
|
+
#include <NativeModules.h>
|
|
58
|
+
#include <tuple>
|
|
264
59
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
.join('\n ');
|
|
269
|
-
}
|
|
60
|
+
namespace ::_NAMESPACE_:: {
|
|
61
|
+
${specTemplate}
|
|
62
|
+
};
|
|
270
63
|
|
|
271
|
-
|
|
272
|
-
|
|
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;
|
|
64
|
+
} // namespace ::_NAMESPACE_::
|
|
65
|
+
`;
|
|
280
66
|
|
|
281
|
-
|
|
67
|
+
const allInOneTemplate = `
|
|
68
|
+
${headerTemplate}
|
|
282
69
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
);
|
|
70
|
+
#include <NativeModules.h>
|
|
71
|
+
#include <tuple>
|
|
286
72
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
73
|
+
namespace ::_NAMESPACE_:: {
|
|
74
|
+
::_MODULE_CUSTPM_TYPES_::
|
|
75
|
+
${specTemplate}
|
|
76
|
+
};
|
|
291
77
|
|
|
292
|
-
|
|
293
|
-
|
|
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
|
-
}
|
|
78
|
+
} // namespace ::_NAMESPACE_::
|
|
79
|
+
`;
|
|
307
80
|
|
|
308
|
-
export function createNM2Generator({
|
|
81
|
+
export function createNM2Generator({
|
|
82
|
+
methodOnly,
|
|
83
|
+
namespace,
|
|
84
|
+
cppStringType,
|
|
85
|
+
separateDataTypes,
|
|
86
|
+
}: {
|
|
87
|
+
methodOnly: boolean;
|
|
88
|
+
namespace: string;
|
|
89
|
+
cppStringType: CppStringTypes;
|
|
90
|
+
separateDataTypes: boolean;
|
|
91
|
+
}) {
|
|
309
92
|
return (
|
|
310
93
|
_libraryName: string,
|
|
311
94
|
schema: SchemaType,
|
|
@@ -313,30 +96,90 @@ export function createNM2Generator({namespace}: {namespace: string}) {
|
|
|
313
96
|
): FilesOutput => {
|
|
314
97
|
const files = new Map<string, string>();
|
|
315
98
|
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
.
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
99
|
+
for (const moduleName of Object.keys(schema.modules)) {
|
|
100
|
+
const nativeModule = schema.modules[moduleName];
|
|
101
|
+
// from 0.65 facebook's react-native-codegen
|
|
102
|
+
// the module name has the Native prefix comparing to 0.63
|
|
103
|
+
// when reading files we provided
|
|
104
|
+
const preferredModuleName = moduleName.startsWith('Native')
|
|
105
|
+
? moduleName.substr(6)
|
|
106
|
+
: moduleName;
|
|
107
|
+
setPreferredModuleName(preferredModuleName);
|
|
108
|
+
|
|
109
|
+
if (nativeModule.type === 'NativeModule') {
|
|
110
|
+
console.log(`Generating Native${preferredModuleName}Spec.g.h`);
|
|
111
|
+
|
|
112
|
+
// copy all explicit to a map
|
|
113
|
+
const aliases: AliasMap = createAliasMap(nativeModule.aliasMap);
|
|
114
|
+
|
|
115
|
+
// prepare methods
|
|
116
|
+
const methods = generateValidateMethods(nativeModule, aliases, {
|
|
117
|
+
cppStringType,
|
|
118
|
+
});
|
|
119
|
+
let tuples = `
|
|
120
|
+
static constexpr auto methods = std::tuple{
|
|
121
|
+
${methods[0]}
|
|
122
|
+
};`;
|
|
123
|
+
let checks = `
|
|
124
|
+
constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;
|
|
125
|
+
let errors = methods[1];
|
|
126
|
+
|
|
127
|
+
// prepare constants
|
|
128
|
+
const constants = generateValidateConstants(nativeModule, aliases);
|
|
129
|
+
if (constants !== undefined && !methodOnly) {
|
|
130
|
+
tuples = `
|
|
131
|
+
static constexpr auto constants = std::tuple{
|
|
132
|
+
${constants[0]}
|
|
133
|
+
};${tuples}`;
|
|
134
|
+
checks = `
|
|
135
|
+
constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;
|
|
136
|
+
errors = `${constants[1]}
|
|
137
|
+
|
|
138
|
+
${errors}`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// generate code for structs
|
|
142
|
+
const [customTypes, customReflection] = generateAliases(aliases, {
|
|
143
|
+
cppStringType,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const customTypesExist = customTypes !== '';
|
|
147
|
+
|
|
148
|
+
const replaceContent = function (template: string): string {
|
|
149
|
+
return template
|
|
150
|
+
.replace(/::_MODULE_CUSTPM_TYPES_::/g, customTypes)
|
|
151
|
+
.replace(/::_MODULE_CUSTPM_TYPES_REFLECTION_::/g, customReflection)
|
|
152
|
+
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))
|
|
153
|
+
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))
|
|
154
|
+
.replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
|
|
155
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
332
156
|
.replace(
|
|
333
|
-
/::
|
|
334
|
-
|
|
157
|
+
/::_TYPE_DEFINITION_INCLUDE_::/g,
|
|
158
|
+
customTypesExist
|
|
159
|
+
? `// #include "Native${preferredModuleName}DataTypes.g.h" before this file to use the generated type definition`
|
|
160
|
+
: '',
|
|
335
161
|
)
|
|
336
|
-
.replace(/::
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
162
|
+
.replace(/::_NAMESPACE_::/g, namespace);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
if (separateDataTypes) {
|
|
166
|
+
if (customTypesExist) {
|
|
167
|
+
files.set(
|
|
168
|
+
`Native${preferredModuleName}DataTypes.g.h`,
|
|
169
|
+
replaceContent(typeOnlyTemplate),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
files.set(
|
|
173
|
+
`Native${preferredModuleName}Spec.g.h`,
|
|
174
|
+
replaceContent(moduleOnlyTemplate),
|
|
175
|
+
);
|
|
176
|
+
} else {
|
|
177
|
+
files.set(
|
|
178
|
+
`Native${preferredModuleName}Spec.g.h`,
|
|
179
|
+
replaceContent(allInOneTemplate),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
340
183
|
}
|
|
341
184
|
|
|
342
185
|
return files;
|