@react-native-windows/codegen 0.72.0 → 0.72.2
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 +22 -5
- package/lib-commonjs/Cli.js +18 -0
- package/lib-commonjs/Cli.js.map +1 -1
- package/lib-commonjs/generators/AliasGen.d.ts +2 -1
- package/lib-commonjs/generators/AliasGen.js +33 -17
- package/lib-commonjs/generators/AliasGen.js.map +1 -1
- package/lib-commonjs/generators/GenerateNM2.d.ts +5 -2
- package/lib-commonjs/generators/GenerateNM2.js +68 -20
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -1
- package/lib-commonjs/generators/ObjectTypes.d.ts +6 -2
- package/lib-commonjs/generators/ObjectTypes.js +9 -9
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -1
- package/lib-commonjs/generators/ParamTypes.d.ts +3 -2
- package/lib-commonjs/generators/ParamTypes.js +27 -27
- package/lib-commonjs/generators/ParamTypes.js.map +1 -1
- package/lib-commonjs/generators/ReturnTypes.d.ts +3 -2
- package/lib-commonjs/generators/ReturnTypes.js +7 -7
- package/lib-commonjs/generators/ReturnTypes.js.map +1 -1
- package/lib-commonjs/generators/ValidateMethods.d.ts +2 -1
- package/lib-commonjs/generators/ValidateMethods.js +15 -15
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -1
- package/lib-commonjs/index.d.ts +11 -13
- package/lib-commonjs/index.js +8 -4
- package/lib-commonjs/index.js.map +1 -1
- package/package.json +1 -1
- package/src/Cli.ts +24 -2
- package/src/generators/AliasGen.ts +61 -17
- package/src/generators/GenerateNM2.ts +86 -17
- package/src/generators/ObjectTypes.ts +15 -3
- package/src/generators/ParamTypes.ts +43 -9
- package/src/generators/ReturnTypes.ts +18 -3
- package/src/generators/ValidateMethods.ts +32 -4
- package/src/index.ts +23 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
# Change Log - @react-native-windows/codegen
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Mon, 21 Aug 2023 15:13:37 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
-
## 0.72.
|
|
7
|
+
## 0.72.2
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Mon, 21 Aug 2023 15:13:37 GMT
|
|
10
10
|
|
|
11
11
|
### Patches
|
|
12
12
|
|
|
13
|
-
-
|
|
14
|
-
- Bump @react-native-windows/fs to v0.72.0
|
|
13
|
+
- Add `separateDataFiles` to @rnw/cli and @rnw/codegen (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
15
14
|
|
|
15
|
+
## 0.72.1
|
|
16
|
+
|
|
17
|
+
Mon, 24 Jul 2023 15:14:19 GMT
|
|
18
|
+
|
|
19
|
+
### Patches
|
|
20
|
+
|
|
21
|
+
- Port #11833, #11848 and #11891 to 0.72 (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
22
|
+
- Fix RNW not being detected on case-sensitive file systems (4123478+tido64@users.noreply.github.com)
|
|
23
|
+
|
|
24
|
+
## 0.72.0
|
|
25
|
+
|
|
26
|
+
Fri, 23 Jun 2023 22:30:08 GMT
|
|
27
|
+
|
|
28
|
+
### Patches
|
|
29
|
+
|
|
30
|
+
- Promote 0.72 to latest (jthysell@microsoft.com)
|
|
31
|
+
- Bump @react-native-windows/fs to v0.72.0
|
|
32
|
+
|
|
16
33
|
## 0.72.0-preview.2
|
|
17
34
|
|
|
18
35
|
Thu, 04 May 2023 20:58:54 GMT
|
package/lib-commonjs/Cli.js
CHANGED
|
@@ -61,11 +61,29 @@ const argv = yargs_1.default.options({
|
|
|
61
61
|
required: true,
|
|
62
62
|
describe: 'Used for part of the path generated within the codegen dir',
|
|
63
63
|
},
|
|
64
|
+
cppStringType: {
|
|
65
|
+
choices: ['std::string', 'std::wstring'],
|
|
66
|
+
describe: 'C++ string type in generated code, should be "std::string" or "std::wstring"',
|
|
67
|
+
default: 'std::string',
|
|
68
|
+
},
|
|
69
|
+
separateDataTypes: {
|
|
70
|
+
type: 'boolean',
|
|
71
|
+
describe: 'generate data types in a separate file',
|
|
72
|
+
default: false,
|
|
73
|
+
},
|
|
64
74
|
}).argv;
|
|
65
75
|
if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
|
|
66
76
|
console.error('You must specify either --file or --files.');
|
|
67
77
|
process.exit(1);
|
|
68
78
|
}
|
|
79
|
+
if (argv.cppStringType !== 'std::string' &&
|
|
80
|
+
argv.cppStringType !== 'std::wstring') {
|
|
81
|
+
console.error('cppStringType should be "std::string" or "std::wstring".');
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
// type casting is necessary here because
|
|
85
|
+
// cppStringType does not become union of string literals
|
|
86
|
+
// until yargs.options get improved in the future
|
|
69
87
|
const changesNecessary = (0, index_1.runCodeGen)(argv);
|
|
70
88
|
if (argv.test && changesNecessary) {
|
|
71
89
|
console.error('There is a change in the output of codegen. Rerun "react-native codegen-windows" to regenerate.');
|
package/lib-commonjs/Cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;AAEH,kDAA0B;AAC1B,
|
|
1
|
+
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;AAEH,kDAA0B;AAC1B,mCAAmD;AAEnD,MAAM,IAAI,GAAG,eAAK,CAAC,OAAO,CAAC;IACzB,IAAI,EAAE;QACJ,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,0BAA0B;KACrC;IACD,KAAK,EAAE;QACL,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,IAAI;QACX,QAAQ,EAAE,8CAA8C;KACzD;IACD,sBAAsB,EAAE;QACtB,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,s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,aAAa,KAAK,aAAa;IACpC,IAAI,CAAC,aAAa,KAAK,cAAc,EACrC;IACA,OAAO,CAAC,KAAK,CAAC,0DAA0D,CAAC,CAAC;IAC1E,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;CACjB;AAED,yCAAyC;AACzC,yDAAyD;AACzD,iDAAiD;AACjD,MAAM,gBAAgB,GAAG,IAAA,kBAAU,EAAiB,IAAI,CAAC,CAAC;AAE1D,IAAI,IAAI,CAAC,IAAI,IAAI,gBAAgB,EAAE;IACjC,OAAO,CAAC,KAAK,CACX,kGAAkG,CACnG,CAAC;IACF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;CACjB","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport yargs from 'yargs';\nimport {CodeGenOptions, runCodeGen} from './index';\n\nconst argv = yargs.options({\n file: {\n type: 'string',\n describe: 'file which contains spec',\n },\n files: {\n type: 'string',\n array: true,\n describe: 'glob patterns for files which contains specs',\n },\n modulesTypeScriptTypes: {\n type: 'boolean',\n describe: 'generate turbo module definition files in TypeScript',\n default: false,\n },\n modulesCxx: {\n type: 'boolean',\n describe: 'generate C++ JSI turbo module spec files',\n default: false,\n },\n modulesWindows: {\n type: 'boolean',\n describe: 'generate turbo module spec files for REACT_MODULE',\n default: false,\n },\n methodOnly: {\n type: 'boolean',\n describe: 'generate only method metadata in C++ turbo module spec',\n default: false,\n },\n outputDirectory: {\n type: 'string',\n describe: 'output directory',\n default: 'codegen',\n },\n test: {\n type: 'boolean',\n describe: 'Verify that the generated output is unchanged',\n default: false,\n },\n namespace: {\n type: 'string',\n describe: 'C++/C# Namespace to put generated native modules in',\n default: 'MyNamespace',\n },\n libraryName: {\n type: 'string',\n required: true,\n describe: 'Used for part of the path generated within the codegen dir',\n },\n cppStringType: {\n choices: ['std::string', 'std::wstring'],\n describe:\n 'C++ string type in generated code, should be \"std::string\" or \"std::wstring\"',\n default: 'std::string',\n },\n separateDataTypes: {\n type: 'boolean',\n describe: 'generate data types in a separate file',\n default: false,\n },\n}).argv;\n\nif ((argv.file && argv.files) || (!argv.file && !argv.files)) {\n console.error('You must specify either --file or --files.');\n process.exit(1);\n}\n\nif (\n argv.cppStringType !== 'std::string' &&\n argv.cppStringType !== 'std::wstring'\n) {\n console.error('cppStringType should be \"std::string\" or \"std::wstring\".');\n process.exit(1);\n}\n\n// type casting is necessary here because\n// cppStringType does not become union of string literals\n// until yargs.options get improved in the future\nconst changesNecessary = runCodeGen(<CodeGenOptions>argv);\n\nif (argv.test && changesNecessary) {\n console.error(\n 'There is a change in the output of codegen. Rerun \"react-native codegen-windows\" to regenerate.',\n );\n process.exit(2);\n}\n"]}
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { NativeModuleObjectTypeAnnotation } from '@react-native/codegen/lib/CodegenSchema';
|
|
7
7
|
import { AliasMap } from './AliasManaging';
|
|
8
|
+
import { CppCodegenOptions } from './ObjectTypes';
|
|
8
9
|
export declare function createAliasMap(nativeModuleAliases: {
|
|
9
10
|
[name: string]: NativeModuleObjectTypeAnnotation;
|
|
10
11
|
}): AliasMap;
|
|
11
|
-
export declare function generateAliases(aliases: AliasMap): string;
|
|
12
|
+
export declare function generateAliases(aliases: AliasMap, options: CppCodegenOptions): [string, string];
|
|
@@ -8,16 +8,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.generateAliases = exports.createAliasMap = void 0;
|
|
9
9
|
const AliasManaging_1 = require("./AliasManaging");
|
|
10
10
|
const ObjectTypes_1 = require("./ObjectTypes");
|
|
11
|
-
function
|
|
11
|
+
function translateObjectMembersDefinition(type, aliases, baseAliasName, prefix, options) {
|
|
12
12
|
return type.properties
|
|
13
13
|
.map((prop) => {
|
|
14
14
|
let propType = prop.typeAnnotation;
|
|
15
15
|
if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
|
|
16
16
|
propType = { type: 'NullableTypeAnnotation', typeAnnotation: propType };
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
return `${prefix}${(0, ObjectTypes_1.translateField)(propType, aliases, `${baseAliasName}_${prop.name}`, options)} ${prop.name};`;
|
|
19
|
+
})
|
|
20
|
+
.join('\n');
|
|
21
|
+
}
|
|
22
|
+
function translateObjectMembersReflection(type, aliasCppName, prefix) {
|
|
23
|
+
return type.properties
|
|
24
|
+
.map((prop) => {
|
|
25
|
+
return `${prefix}{L"${prop.name}", &${aliasCppName}::${prop.name}},`;
|
|
21
26
|
})
|
|
22
27
|
.join('\n');
|
|
23
28
|
}
|
|
@@ -29,16 +34,25 @@ function createAliasMap(nativeModuleAliases) {
|
|
|
29
34
|
return aliases;
|
|
30
35
|
}
|
|
31
36
|
exports.createAliasMap = createAliasMap;
|
|
32
|
-
function generateSingleAlias(aliases, aliasName, aliasCode) {
|
|
37
|
+
function generateSingleAlias(aliases, aliasName, aliasCode, options) {
|
|
38
|
+
const aliasCppName = (0, AliasManaging_1.getAliasCppName)(aliasName);
|
|
33
39
|
const aliasType = aliases.types[aliasName];
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
${translateObjectBody(aliasType, aliases, aliasName, ' ')}
|
|
40
|
+
const definition = `
|
|
41
|
+
struct ${aliasCppName} {
|
|
42
|
+
${translateObjectMembersDefinition(aliasType, aliases, aliasName, ' ', options)}
|
|
38
43
|
};
|
|
39
44
|
`;
|
|
45
|
+
const reflection = `
|
|
46
|
+
inline winrt::Microsoft::ReactNative::FieldMap GetStructInfo(${aliasCppName}*) noexcept {
|
|
47
|
+
winrt::Microsoft::ReactNative::FieldMap fieldMap {
|
|
48
|
+
${translateObjectMembersReflection(aliasType, aliasCppName, ' ')}
|
|
49
|
+
};
|
|
50
|
+
return fieldMap;
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
aliasCode[aliasName] = { definition, reflection };
|
|
40
54
|
}
|
|
41
|
-
function generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder) {
|
|
55
|
+
function generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options) {
|
|
42
56
|
// retrieve and clean all ungenerated aliases
|
|
43
57
|
const jobs = aliases.jobs;
|
|
44
58
|
aliases.jobs = [];
|
|
@@ -47,26 +61,28 @@ function generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder) {
|
|
|
47
61
|
// generate a new struct and all fields will be examined
|
|
48
62
|
// new anonymous objects could be found
|
|
49
63
|
// they will be stored in aliases.jobs
|
|
50
|
-
generateSingleAlias(aliases, aliasName, aliasCode);
|
|
64
|
+
generateSingleAlias(aliases, aliasName, aliasCode, options);
|
|
51
65
|
// nested C++ structs must be put before the current C++ struct
|
|
52
66
|
// as they will be used in the current C++ struct
|
|
53
67
|
// the order will be perfectly and easily ensured by doing this recursively
|
|
54
|
-
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
|
|
68
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);
|
|
55
69
|
// all referenced C++ structs are generated
|
|
56
70
|
// put the current one following them
|
|
57
71
|
aliasOrder.push(aliasName);
|
|
58
72
|
}
|
|
59
73
|
}
|
|
60
|
-
function generateAliases(aliases) {
|
|
74
|
+
function generateAliases(aliases, options) {
|
|
61
75
|
const aliasCode = {};
|
|
62
76
|
const aliasOrder = [];
|
|
63
|
-
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
|
|
77
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);
|
|
64
78
|
// aliasOrder now has the correct order of C++ struct code
|
|
65
|
-
let
|
|
79
|
+
let customTypes = '';
|
|
80
|
+
let customReflection = '';
|
|
66
81
|
for (const aliasName of aliasOrder) {
|
|
67
|
-
|
|
82
|
+
customTypes = `${customTypes}${aliasCode[aliasName].definition}`;
|
|
83
|
+
customReflection = `${customReflection}${aliasCode[aliasName].reflection}`;
|
|
68
84
|
}
|
|
69
|
-
return
|
|
85
|
+
return [customTypes, customReflection];
|
|
70
86
|
}
|
|
71
87
|
exports.generateAliases = generateAliases;
|
|
72
88
|
//# sourceMappingURL=AliasGen.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AliasGen.js","sourceRoot":"","sources":["../../src/generators/AliasGen.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAQb,mDAA0D;AAC1D,+
|
|
1
|
+
{"version":3,"file":"AliasGen.js","sourceRoot":"","sources":["../../src/generators/AliasGen.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAQb,mDAA0D;AAC1D,+CAAgE;AAEhE,SAAS,gCAAgC,CACvC,IAAsC,EACtC,OAAiB,EACjB,aAAqB,EACrB,MAAc,EACd,OAA0B;IAE1B,OAAO,IAAI,CAAC,UAAU;SACnB,GAAG,CAAC,CAAC,IAA0D,EAAE,EAAE;QAClE,IAAI,QAAQ,GAAG,IAAI,CAAC,cAAc,CAAC;QACnC,IAAI,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,IAAI,KAAK,wBAAwB,EAAE;YAC/D,QAAQ,GAAG,EAAC,IAAI,EAAE,wBAAwB,EAAE,cAAc,EAAE,QAAQ,EAAC,CAAC;SACvE;QACD,OAAO,GAAG,MAAM,GAAG,IAAA,4BAAc,EAC/B,QAAQ,EACR,OAAO,EACP,GAAG,aAAa,IAAI,IAAI,CAAC,IAAI,EAAE,EAC/B,OAAO,CACR,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC;IACpB,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAED,SAAS,gCAAgC,CACvC,IAAsC,EACtC,YAAoB,EACpB,MAAc;IAEd,OAAO,IAAI,CAAC,UAAU;SACnB,GAAG,CAAC,CAAC,IAA0D,EAAE,EAAE;QAClE,OAAO,GAAG,MAAM,MAAM,IAAI,CAAC,I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D;QACxD,uCAAuC;QACvC,sCAAsC;QACtC,mBAAmB,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;QAC5D,+DAA+D;QAC/D,iDAAiD;QACjD,2EAA2E;QAC3E,mCAAmC,CACjC,OAAO,EACP,SAAS,EACT,UAAU,EACV,OAAO,CACR,CAAC;QACF,2CAA2C;QAC3C,qCAAqC;QACrC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;KAC5B;AACH,CAAC;AAED,SAAgB,eAAe,CAC7B,OAAiB,EACjB,OAA0B;IAE1B,MAAM,SAAS,GAAiB,EAAE,CAAC;IACnC,MAAM,UAAU,GAAa,EAAE,CAAC;IAChC,mCAAmC,CAAC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAE7E,0DAA0D;IAC1D,IAAI,WAAW,GAAG,EAAE,CAAC;IACrB,IAAI,gBAAgB,GAAG,EAAE,CAAC;IAC1B,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE;QAClC,WAAW,GAAG,GAAG,WAAW,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,CAAC;QACjE,gBAAgB,GAAG,GAAG,gBAAgB,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,CAAC;KAC5E;IACD,OAAO,CAAC,WAAW,EAAE,gBAAgB,CAAC,CAAC;AACzC,CAAC;AAhBD,0CAgBC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {\n NativeModuleBaseTypeAnnotation,\n NativeModuleObjectTypeAnnotation,\n NamedShape,\n Nullable,\n} from '@react-native/codegen/lib/CodegenSchema';\nimport {AliasMap, getAliasCppName} from './AliasManaging';\nimport {CppCodegenOptions, translateField} from './ObjectTypes';\n\nfunction translateObjectMembersDefinition(\n type: NativeModuleObjectTypeAnnotation,\n aliases: AliasMap,\n baseAliasName: string,\n prefix: string,\n options: CppCodegenOptions,\n) {\n return type.properties\n .map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {\n let propType = prop.typeAnnotation;\n if (prop.optional && propType.type !== 'NullableTypeAnnotation') {\n propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};\n }\n return `${prefix}${translateField(\n propType,\n aliases,\n `${baseAliasName}_${prop.name}`,\n options,\n )} ${prop.name};`;\n })\n .join('\\n');\n}\n\nfunction translateObjectMembersReflection(\n type: NativeModuleObjectTypeAnnotation,\n aliasCppName: string,\n prefix: string,\n) {\n return type.properties\n .map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {\n return `${prefix}{L\"${prop.name}\", &${aliasCppName}::${prop.name}},`;\n })\n .join('\\n');\n}\n\nexport function createAliasMap(nativeModuleAliases: {\n [name: string]: NativeModuleObjectTypeAnnotation;\n}): AliasMap {\n const aliases: AliasMap = {types: {}, jobs: Object.keys(nativeModuleAliases)};\n for (const aliasName of aliases.jobs) {\n aliases.types[aliasName] = nativeModuleAliases[aliasName];\n }\n return aliases;\n}\n\ninterface AliasCode {\n definition: string;\n reflection: string;\n}\n\ninterface AliasCodeMap {\n [name: string]: AliasCode;\n}\n\nfunction generateSingleAlias(\n aliases: AliasMap,\n aliasName: string,\n aliasCode: AliasCodeMap,\n options: CppCodegenOptions,\n): void {\n const aliasCppName = getAliasCppName(aliasName);\n const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];\n const definition = `\nstruct ${aliasCppName} {\n${translateObjectMembersDefinition(\n aliasType,\n aliases,\n aliasName,\n ' ',\n options,\n)}\n};\n`;\n const reflection = `\ninline winrt::Microsoft::ReactNative::FieldMap GetStructInfo(${aliasCppName}*) noexcept {\n winrt::Microsoft::ReactNative::FieldMap fieldMap {\n${translateObjectMembersReflection(aliasType, aliasCppName, ' ')}\n };\n return fieldMap;\n}\n`;\n aliasCode[aliasName] = {definition, reflection};\n}\n\nfunction generateNestedAliasesInCorrectOrder(\n aliases: AliasMap,\n aliasCode: AliasCodeMap,\n aliasOrder: string[],\n options: CppCodegenOptions,\n): void {\n // retrieve and clean all ungenerated aliases\n const jobs = aliases.jobs;\n aliases.jobs = [];\n\n // generate each one in its found order\n for (const aliasName of jobs) {\n // generate a new struct and all fields will be examined\n // new anonymous objects could be found\n // they will be stored in aliases.jobs\n generateSingleAlias(aliases, aliasName, aliasCode, options);\n // nested C++ structs must be put before the current C++ struct\n // as they will be used in the current C++ struct\n // the order will be perfectly and easily ensured by doing this recursively\n generateNestedAliasesInCorrectOrder(\n aliases,\n aliasCode,\n aliasOrder,\n options,\n );\n // all referenced C++ structs are generated\n // put the current one following them\n aliasOrder.push(aliasName);\n }\n}\n\nexport function generateAliases(\n aliases: AliasMap,\n options: CppCodegenOptions,\n): [string, string] {\n const aliasCode: AliasCodeMap = {};\n const aliasOrder: string[] = [];\n generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);\n\n // aliasOrder now has the correct order of C++ struct code\n let customTypes = '';\n let customReflection = '';\n for (const aliasName of aliasOrder) {\n customTypes = `${customTypes}${aliasCode[aliasName].definition}`;\n customReflection = `${customReflection}${aliasCode[aliasName].reflection}`;\n }\n return [customTypes, customReflection];\n}\n"]}
|
|
@@ -4,9 +4,12 @@
|
|
|
4
4
|
* @format
|
|
5
5
|
*/
|
|
6
6
|
import type { SchemaType } from '@react-native/codegen/lib/CodegenSchema';
|
|
7
|
+
import type { CppStringTypes } from './ObjectTypes';
|
|
8
|
+
export type { CppStringTypes } from './ObjectTypes';
|
|
7
9
|
type FilesOutput = Map<string, string>;
|
|
8
|
-
export declare function createNM2Generator({ methodOnly, namespace, }: {
|
|
10
|
+
export declare function createNM2Generator({ methodOnly, namespace, cppStringType, separateDataTypes, }: {
|
|
9
11
|
methodOnly: boolean;
|
|
10
12
|
namespace: string;
|
|
13
|
+
cppStringType: CppStringTypes;
|
|
14
|
+
separateDataTypes: boolean;
|
|
11
15
|
}): (_libraryName: string, schema: SchemaType, _moduleSpecName: string) => FilesOutput;
|
|
12
|
-
export {};
|
|
@@ -10,21 +10,15 @@ const AliasManaging_1 = require("./AliasManaging");
|
|
|
10
10
|
const AliasGen_1 = require("./AliasGen");
|
|
11
11
|
const ValidateConstants_1 = require("./ValidateConstants");
|
|
12
12
|
const ValidateMethods_1 = require("./ValidateMethods");
|
|
13
|
-
const
|
|
14
|
-
/*
|
|
13
|
+
const headerTemplate = `/*
|
|
15
14
|
* This file is auto-generated from a NativeModule spec file in js.
|
|
16
15
|
*
|
|
17
16
|
* This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules
|
|
18
17
|
* in a way that also verifies at compile time that the native module matches the interface required
|
|
19
18
|
* by the TurboModule JS spec.
|
|
20
19
|
*/
|
|
21
|
-
#pragma once
|
|
22
|
-
|
|
23
|
-
#include "NativeModules.h"
|
|
24
|
-
#include <tuple>
|
|
25
|
-
|
|
26
|
-
namespace ::_NAMESPACE_:: {
|
|
27
|
-
::_MODULE_ALIASED_STRUCTS_::
|
|
20
|
+
#pragma once`;
|
|
21
|
+
const specTemplate = `::_MODULE_CUSTPM_TYPES_REFLECTION_::
|
|
28
22
|
struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
29
23
|
::_MODULE_MEMBERS_TUPLES_::
|
|
30
24
|
|
|
@@ -33,12 +27,46 @@ struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
|
33
27
|
::_MODULE_MEMBERS_CHECKS_::
|
|
34
28
|
|
|
35
29
|
::_MODULE_MEMBERS_ERRORS_::
|
|
36
|
-
}
|
|
30
|
+
}`;
|
|
31
|
+
const typeOnlyTemplate = `
|
|
32
|
+
${headerTemplate}
|
|
33
|
+
|
|
34
|
+
#include <string>
|
|
35
|
+
#include <optional>
|
|
36
|
+
#include <functional>
|
|
37
|
+
#include <vector>
|
|
38
|
+
|
|
39
|
+
namespace ::_NAMESPACE_:: {
|
|
40
|
+
::_MODULE_CUSTPM_TYPES_::
|
|
41
|
+
} // namespace ::_NAMESPACE_::
|
|
42
|
+
`;
|
|
43
|
+
const moduleOnlyTemplate = `
|
|
44
|
+
${headerTemplate}
|
|
45
|
+
|
|
46
|
+
::_TYPE_DEFINITION_INCLUDE_::
|
|
47
|
+
#include <NativeModules.h>
|
|
48
|
+
#include <tuple>
|
|
49
|
+
|
|
50
|
+
namespace ::_NAMESPACE_:: {
|
|
51
|
+
${specTemplate}
|
|
37
52
|
};
|
|
38
53
|
|
|
39
54
|
} // namespace ::_NAMESPACE_::
|
|
40
55
|
`;
|
|
41
|
-
|
|
56
|
+
const allInOneTemplate = `
|
|
57
|
+
${headerTemplate}
|
|
58
|
+
|
|
59
|
+
#include <NativeModules.h>
|
|
60
|
+
#include <tuple>
|
|
61
|
+
|
|
62
|
+
namespace ::_NAMESPACE_:: {
|
|
63
|
+
::_MODULE_CUSTPM_TYPES_::
|
|
64
|
+
${specTemplate}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
} // namespace ::_NAMESPACE_::
|
|
68
|
+
`;
|
|
69
|
+
function createNM2Generator({ methodOnly, namespace, cppStringType, separateDataTypes, }) {
|
|
42
70
|
return (_libraryName, schema, _moduleSpecName) => {
|
|
43
71
|
const files = new Map();
|
|
44
72
|
for (const moduleName of Object.keys(schema.modules)) {
|
|
@@ -55,7 +83,9 @@ function createNM2Generator({ methodOnly, namespace, }) {
|
|
|
55
83
|
// copy all explicit to a map
|
|
56
84
|
const aliases = (0, AliasGen_1.createAliasMap)(nativeModule.aliasMap);
|
|
57
85
|
// prepare methods
|
|
58
|
-
const methods = (0, ValidateMethods_1.generateValidateMethods)(nativeModule, aliases
|
|
86
|
+
const methods = (0, ValidateMethods_1.generateValidateMethods)(nativeModule, aliases, {
|
|
87
|
+
cppStringType,
|
|
88
|
+
});
|
|
59
89
|
let tuples = `
|
|
60
90
|
static constexpr auto methods = std::tuple{
|
|
61
91
|
${methods[0]}
|
|
@@ -77,14 +107,32 @@ ${constants[0]}
|
|
|
77
107
|
${errors}`;
|
|
78
108
|
}
|
|
79
109
|
// generate code for structs
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
110
|
+
const [customTypes, customReflection] = (0, AliasGen_1.generateAliases)(aliases, {
|
|
111
|
+
cppStringType,
|
|
112
|
+
});
|
|
113
|
+
const customTypesExist = customTypes !== '';
|
|
114
|
+
const replaceContent = function (template) {
|
|
115
|
+
return template
|
|
116
|
+
.replace(/::_MODULE_CUSTPM_TYPES_::/g, customTypes)
|
|
117
|
+
.replace(/::_MODULE_CUSTPM_TYPES_REFLECTION_::/g, customReflection)
|
|
118
|
+
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))
|
|
119
|
+
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))
|
|
120
|
+
.replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
|
|
121
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
122
|
+
.replace(/::_TYPE_DEFINITION_INCLUDE_::/g, customTypesExist
|
|
123
|
+
? `// #include "Native${preferredModuleName}DataTypes.g.h" before this file to use the generated type definition`
|
|
124
|
+
: '')
|
|
125
|
+
.replace(/::_NAMESPACE_::/g, namespace);
|
|
126
|
+
};
|
|
127
|
+
if (separateDataTypes) {
|
|
128
|
+
if (customTypesExist) {
|
|
129
|
+
files.set(`Native${preferredModuleName}DataTypes.g.h`, replaceContent(typeOnlyTemplate));
|
|
130
|
+
}
|
|
131
|
+
files.set(`Native${preferredModuleName}Spec.g.h`, replaceContent(moduleOnlyTemplate));
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
files.set(`Native${preferredModuleName}Spec.g.h`, replaceContent(allInOneTemplate));
|
|
135
|
+
}
|
|
88
136
|
}
|
|
89
137
|
}
|
|
90
138
|
return files;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GenerateNM2.js","sourceRoot":"","sources":["../../src/generators/GenerateNM2.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAiE;AACjE,yCAA2D;AAC3D,2DAA8D;AAC9D,uDAA0D;
|
|
1
|
+
{"version":3,"file":"GenerateNM2.js","sourceRoot":"","sources":["../../src/generators/GenerateNM2.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAiE;AACjE,yCAA2D;AAC3D,2DAA8D;AAC9D,uDAA0D;AAO1D,MAAM,cAAc,GAAG;;;;;;;aAOV,CAAC;AAEd,MAAM,YAAY,GAAG;;;;;;;;;IASjB,CAAC;AAEL,MAAM,gBAAgB,GAAG;EACvB,cAAc;;;;;;;;;;CAUf,CAAC;AAEF,MAAM,kBAAkB,GAAG;EACzB,cAAc;;;;;;;EAOd,YAAY;;;;CAIb,CAAC;AAEF,MAAM,gBAAgB,GAAG;EACvB,cAAc;;;;;;;EAOd,YAAY;;;;CAIb,CAAC;AAEF,SAAgB,kBAAkB,CAAC,EACjC,UAAU,EACV,SAAS,EACT,aAAa,EACb,iBAAiB,GAMlB;IACC,OAAO,CACL,YAAoB,EACpB,MAAkB,EAClB,eAAuB,EACV,EAAE;QACf,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC;QAExC,KAAK,MAAM,UAAU,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE;YACpD,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;YAChD,4CAA4C;YAC5C,0DAA0D;YAC1D,iCAAiC;YACjC,MAAM,mBAAmB,GAAG,UAAU,CAAC,UAAU,CAAC,QAAQ,CAAC;gBACzD,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;gBACtB,CAAC,CAAC,UAAU,CAAC;YACf,IAAA,sCAAsB,EAAC,mBAAmB,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C,SAAS,CAAC,CAAC,CAAC,CAAC;yBAC5D,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;yBAC5D,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC;yBAC/C,OAAO,CAAC,oBAAoB,EAAE,mBAAmB,CAAC;yBAClD,OAAO,CACN,gCAAgC,EAChC,gBAAgB;wBACd,CAAC,CAAC,sBAAsB,mBAAmB,sEAAsE;wBACjH,CAAC,CAAC,EAAE,CACP;yBACA,OAAO,CAAC,kBAAkB,EAAE,SAAS,CAAC,CAAC;gBAC5C,CAAC,CAAC;gBAEF,IAAI,iBAAiB,EAAE;oBACrB,IAAI,gBAAgB,EAAE;wBACpB,KAAK,CAAC,GAAG,CACP,SAAS,mBAAmB,eAAe,EAC3C,cAAc,CAAC,gBAAgB,CAAC,CACjC,CAAC;qBACH;oBACD,KAAK,CAAC,GAAG,CACP,SAAS,mBAAmB,UAAU,EACtC,cAAc,CAAC,kBAAkB,CAAC,CACnC,CAAC;iBACH;qBAAM;oBACL,KAAK,CAAC,GAAG,CACP,SAAS,mBAAmB,UAAU,EACtC,cAAc,CAAC,gBAAgB,CAAC,CACjC,CAAC;iBACH;aACF;SACF;QAED,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AA1GD,gDA0GC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {SchemaType} from '@react-native/codegen/lib/CodegenSchema';\nimport {AliasMap, setPreferredModuleName} from './AliasManaging';\nimport {createAliasMap, generateAliases} from './AliasGen';\nimport {generateValidateConstants} from './ValidateConstants';\nimport {generateValidateMethods} from './ValidateMethods';\nimport type {CppStringTypes} from './ObjectTypes';\n\nexport type {CppStringTypes} from './ObjectTypes';\n\ntype FilesOutput = Map<string, string>;\n\nconst headerTemplate = `/*\n * This file is auto-generated from a NativeModule spec file in js.\n *\n * This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules\n * in a way that also verifies at compile time that the native module matches the interface required\n * by the TurboModule JS spec.\n */\n#pragma once`;\n\nconst specTemplate = `::_MODULE_CUSTPM_TYPES_REFLECTION_::\nstruct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {\n::_MODULE_MEMBERS_TUPLES_::\n\n template <class TModule>\n static constexpr void ValidateModule() noexcept {\n::_MODULE_MEMBERS_CHECKS_::\n\n::_MODULE_MEMBERS_ERRORS_::\n }`;\n\nconst typeOnlyTemplate = `\n${headerTemplate}\n\n#include <string>\n#include <optional>\n#include <functional>\n#include <vector>\n\nnamespace ::_NAMESPACE_:: {\n::_MODULE_CUSTPM_TYPES_::\n} // namespace ::_NAMESPACE_::\n`;\n\nconst moduleOnlyTemplate = `\n${headerTemplate}\n\n::_TYPE_DEFINITION_INCLUDE_::\n#include <NativeModules.h>\n#include <tuple>\n\nnamespace ::_NAMESPACE_:: {\n${specTemplate}\n};\n\n} // namespace ::_NAMESPACE_::\n`;\n\nconst allInOneTemplate = `\n${headerTemplate}\n\n#include <NativeModules.h>\n#include <tuple>\n\nnamespace ::_NAMESPACE_:: {\n::_MODULE_CUSTPM_TYPES_::\n${specTemplate}\n};\n\n} // namespace ::_NAMESPACE_::\n`;\n\nexport function createNM2Generator({\n methodOnly,\n namespace,\n cppStringType,\n separateDataTypes,\n}: {\n methodOnly: boolean;\n namespace: string;\n cppStringType: CppStringTypes;\n separateDataTypes: boolean;\n}) {\n return (\n _libraryName: string,\n schema: SchemaType,\n _moduleSpecName: string,\n ): FilesOutput => {\n const files = new Map<string, string>();\n\n for (const moduleName of Object.keys(schema.modules)) {\n const nativeModule = schema.modules[moduleName];\n // from 0.65 facebook's react-native-codegen\n // the module name has the Native prefix comparing to 0.63\n // when reading files we provided\n const preferredModuleName = moduleName.startsWith('Native')\n ? moduleName.substr(6)\n : moduleName;\n setPreferredModuleName(preferredModuleName);\n\n if (nativeModule.type === 'NativeModule') {\n console.log(`Generating Native${preferredModuleName}Spec.g.h`);\n\n // copy all explicit to a map\n const aliases: AliasMap = createAliasMap(nativeModule.aliasMap);\n\n // prepare methods\n const methods = generateValidateMethods(nativeModule, aliases, {\n cppStringType,\n });\n let tuples = `\n static constexpr auto methods = std::tuple{\n${methods[0]}\n };`;\n let checks = `\n constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;\n let errors = methods[1];\n\n // prepare constants\n const constants = generateValidateConstants(nativeModule, aliases);\n if (constants !== undefined && !methodOnly) {\n tuples = `\n static constexpr auto constants = std::tuple{\n${constants[0]}\n };${tuples}`;\n checks = `\n constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;\n errors = `${constants[1]}\n\n${errors}`;\n }\n\n // generate code for structs\n const [customTypes, customReflection] = generateAliases(aliases, {\n cppStringType,\n });\n\n const customTypesExist = customTypes !== '';\n\n const replaceContent = function (template: string): string {\n return template\n .replace(/::_MODULE_CUSTPM_TYPES_::/g, customTypes)\n .replace(/::_MODULE_CUSTPM_TYPES_REFLECTION_::/g, customReflection)\n .replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))\n .replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))\n .replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)\n .replace(/::_MODULE_NAME_::/g, preferredModuleName)\n .replace(\n /::_TYPE_DEFINITION_INCLUDE_::/g,\n customTypesExist\n ? `// #include \"Native${preferredModuleName}DataTypes.g.h\" before this file to use the generated type definition`\n : '',\n )\n .replace(/::_NAMESPACE_::/g, namespace);\n };\n\n if (separateDataTypes) {\n if (customTypesExist) {\n files.set(\n `Native${preferredModuleName}DataTypes.g.h`,\n replaceContent(typeOnlyTemplate),\n );\n }\n files.set(\n `Native${preferredModuleName}Spec.g.h`,\n replaceContent(moduleOnlyTemplate),\n );\n } else {\n files.set(\n `Native${preferredModuleName}Spec.g.h`,\n replaceContent(allInOneTemplate),\n );\n }\n }\n }\n\n return files;\n };\n}\n"]}
|
|
@@ -5,5 +5,9 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { NativeModuleBaseTypeAnnotation, Nullable } from '@react-native/codegen/lib/CodegenSchema';
|
|
7
7
|
import { AliasMap } from './AliasManaging';
|
|
8
|
-
export
|
|
9
|
-
export
|
|
8
|
+
export type CppStringTypes = 'std::string' | 'std::wstring';
|
|
9
|
+
export interface CppCodegenOptions {
|
|
10
|
+
cppStringType: CppStringTypes;
|
|
11
|
+
}
|
|
12
|
+
export declare function translateFieldOrReturnType(type: Nullable<NativeModuleBaseTypeAnnotation>, aliases: AliasMap, baseAliasName: string, callerName: 'translateField' | 'translateReturnType', options: CppCodegenOptions): string;
|
|
13
|
+
export declare function translateField(type: Nullable<NativeModuleBaseTypeAnnotation>, aliases: AliasMap, baseAliasName: string, options: CppCodegenOptions): string;
|
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.translateField = exports.translateFieldOrReturnType = void 0;
|
|
9
9
|
const AliasManaging_1 = require("./AliasManaging");
|
|
10
|
-
function translateUnionReturnType(type) {
|
|
10
|
+
function translateUnionReturnType(type, options) {
|
|
11
11
|
const memberType = type.memberType;
|
|
12
12
|
switch (type.memberType) {
|
|
13
13
|
case 'StringTypeAnnotation':
|
|
14
|
-
return
|
|
14
|
+
return options.cppStringType;
|
|
15
15
|
case 'NumberTypeAnnotation':
|
|
16
16
|
return 'double';
|
|
17
17
|
case 'ObjectTypeAnnotation':
|
|
@@ -20,12 +20,12 @@ function translateUnionReturnType(type) {
|
|
|
20
20
|
throw new Error(`Unknown enum/union member type in translateReturnType: ${memberType}`);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
function translateFieldOrReturnType(type, aliases, baseAliasName, callerName) {
|
|
23
|
+
function translateFieldOrReturnType(type, aliases, baseAliasName, callerName, options) {
|
|
24
24
|
// avoid: Property 'type' does not exist on type 'never'
|
|
25
25
|
const returnType = type.type;
|
|
26
26
|
switch (type.type) {
|
|
27
27
|
case 'StringTypeAnnotation':
|
|
28
|
-
return
|
|
28
|
+
return options.cppStringType;
|
|
29
29
|
case 'NumberTypeAnnotation':
|
|
30
30
|
case 'FloatTypeAnnotation':
|
|
31
31
|
case 'DoubleTypeAnnotation':
|
|
@@ -36,7 +36,7 @@ function translateFieldOrReturnType(type, aliases, baseAliasName, callerName) {
|
|
|
36
36
|
return 'bool';
|
|
37
37
|
case 'ArrayTypeAnnotation':
|
|
38
38
|
if (type.elementType) {
|
|
39
|
-
return `std::vector<${translateFieldOrReturnType(type.elementType, aliases, `${baseAliasName}_element`, callerName)}>`;
|
|
39
|
+
return `std::vector<${translateFieldOrReturnType(type.elementType, aliases, `${baseAliasName}_element`, callerName, options)}>`;
|
|
40
40
|
}
|
|
41
41
|
else {
|
|
42
42
|
return `::React::JSValueArray`;
|
|
@@ -57,17 +57,17 @@ function translateFieldOrReturnType(type, aliases, baseAliasName, callerName) {
|
|
|
57
57
|
case 'TypeAliasTypeAnnotation':
|
|
58
58
|
return (0, AliasManaging_1.getAliasCppName)(type.name);
|
|
59
59
|
case 'NullableTypeAnnotation':
|
|
60
|
-
return `std::optional<${translateFieldOrReturnType(type.typeAnnotation, aliases, baseAliasName, callerName)}>`;
|
|
60
|
+
return `std::optional<${translateFieldOrReturnType(type.typeAnnotation, aliases, baseAliasName, callerName, options)}>`;
|
|
61
61
|
case 'EnumDeclaration':
|
|
62
62
|
case 'UnionTypeAnnotation':
|
|
63
|
-
return translateUnionReturnType(type);
|
|
63
|
+
return translateUnionReturnType(type, options);
|
|
64
64
|
default:
|
|
65
65
|
throw new Error(`Unhandled type in ${callerName}: ${returnType}`);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
exports.translateFieldOrReturnType = translateFieldOrReturnType;
|
|
69
|
-
function translateField(type, aliases, baseAliasName) {
|
|
70
|
-
return translateFieldOrReturnType(type, aliases, baseAliasName, 'translateField');
|
|
69
|
+
function translateField(type, aliases, baseAliasName, options) {
|
|
70
|
+
return translateFieldOrReturnType(type, aliases, baseAliasName, 'translateField', options);
|
|
71
71
|
}
|
|
72
72
|
exports.translateField = translateField;
|
|
73
73
|
//# sourceMappingURL=ObjectTypes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ObjectTypes.js","sourceRoot":"","sources":["../../src/generators/ObjectTypes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAQb,mDAIyB;
|
|
1
|
+
{"version":3,"file":"ObjectTypes.js","sourceRoot":"","sources":["../../src/generators/ObjectTypes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAQb,mDAIyB;AAQzB,SAAS,wBAAwB,CAC/B,IAAmE,EACnE,OAA0B;IAE1B,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;IACnC,QAAQ,IAAI,CAAC,UAAU,EAAE;QACvB,KAAK,sBAAsB;YACzB,OAAO,OAAO,CAAC,aAAa,CAAC;QAC/B,KAAK,sBAAsB;YACzB,OAAO,QAAQ,CAAC;QAClB,KAAK,sBAAsB;YACzB,OAAO,kBAAkB,CAAC;QAC5B;YACE,MAAM,IAAI,KAAK,CACb,0DAA0D,UAAU,EAAE,CACvE,CAAC;KACL;AACH,CAAC;AAED,SAAgB,0BAA0B,CACxC,IAA8C,EAC9C,OAAiB,EACjB,aAAqB,EACrB,UAAoD,EACpD,OAA0B;IAE1B,wDAAwD;IACxD,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC;IAC7B,QAAQ,IAAI,CAAC,IAAI,EAAE;QACjB,KAAK,sBAAsB;YACzB,OAAO,OAAO,CAAC,aAAa,CAAC;QAC/B,KAAK,sBAAsB,CAAC;QAC5B,KAAK,qBAAqB,CAAC;QAC3B,KAAK,sBAAsB;YACzB,OAAO,QAAQ,CAAC;QAClB,KAAK,qBAAqB;YACxB,OAAO,KAAK,CA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ACP,aAAa,EACb,gBAAgB,EAChB,OAAO,CACR,CAAC;AACJ,CAAC;AAbD,wCAaC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {\n NativeModuleEnumDeclaration,\n NativeModuleBaseTypeAnnotation,\n NativeModuleUnionTypeAnnotation,\n Nullable,\n} from '@react-native/codegen/lib/CodegenSchema';\nimport {\n AliasMap,\n getAliasCppName,\n getAnonymousAliasCppName,\n} from './AliasManaging';\n\nexport type CppStringTypes = 'std::string' | 'std::wstring';\n\nexport interface CppCodegenOptions {\n cppStringType: CppStringTypes;\n}\n\nfunction translateUnionReturnType(\n type: NativeModuleEnumDeclaration | NativeModuleUnionTypeAnnotation,\n options: CppCodegenOptions,\n): string {\n const memberType = type.memberType;\n switch (type.memberType) {\n case 'StringTypeAnnotation':\n return options.cppStringType;\n case 'NumberTypeAnnotation':\n return 'double';\n case 'ObjectTypeAnnotation':\n return '::React::JSValue';\n default:\n throw new Error(\n `Unknown enum/union member type in translateReturnType: ${memberType}`,\n );\n }\n}\n\nexport function translateFieldOrReturnType(\n type: Nullable<NativeModuleBaseTypeAnnotation>,\n aliases: AliasMap,\n baseAliasName: string,\n callerName: 'translateField' | 'translateReturnType',\n options: CppCodegenOptions,\n): string {\n // avoid: Property 'type' does not exist on type 'never'\n const returnType = type.type;\n switch (type.type) {\n case 'StringTypeAnnotation':\n return options.cppStringType;\n case 'NumberTypeAnnotation':\n case 'FloatTypeAnnotation':\n case 'DoubleTypeAnnotation':\n return 'double';\n case 'Int32TypeAnnotation':\n return 'int';\n case 'BooleanTypeAnnotation':\n return 'bool';\n case 'ArrayTypeAnnotation':\n if (type.elementType) {\n return `std::vector<${translateFieldOrReturnType(\n type.elementType,\n aliases,\n `${baseAliasName}_element`,\n callerName,\n options,\n )}>`;\n } else {\n return `::React::JSValueArray`;\n }\n case 'GenericObjectTypeAnnotation':\n return '::React::JSValue';\n case 'ObjectTypeAnnotation':\n return getAnonymousAliasCppName(aliases, baseAliasName, type);\n case 'ReservedTypeAnnotation': {\n // avoid: Property 'name' does not exist on type 'never'\n const name = type.name;\n // (#6597)\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (name !== 'RootTag')\n throw new Error(`Unknown reserved function: ${name} in ${callerName}`);\n return 'double';\n }\n case 'TypeAliasTypeAnnotation':\n return getAliasCppName(type.name);\n case 'NullableTypeAnnotation':\n return `std::optional<${translateFieldOrReturnType(\n type.typeAnnotation,\n aliases,\n baseAliasName,\n callerName,\n options,\n )}>`;\n case 'EnumDeclaration':\n case 'UnionTypeAnnotation':\n return translateUnionReturnType(type, options);\n default:\n throw new Error(`Unhandled type in ${callerName}: ${returnType}`);\n }\n}\n\nexport function translateField(\n type: Nullable<NativeModuleBaseTypeAnnotation>,\n aliases: AliasMap,\n baseAliasName: string,\n options: CppCodegenOptions,\n): string {\n return translateFieldOrReturnType(\n type,\n aliases,\n baseAliasName,\n 'translateField',\n options,\n );\n}\n"]}
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { NamedShape, NativeModuleParamTypeAnnotation, Nullable } from '@react-native/codegen/lib/CodegenSchema';
|
|
7
7
|
import { AliasMap } from './AliasManaging';
|
|
8
|
+
import type { CppCodegenOptions } from './ObjectTypes';
|
|
8
9
|
type NativeModuleParamShape = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
|
|
9
|
-
export declare function translateSpecArgs(params: ReadonlyArray<NativeModuleParamShape>, aliases: AliasMap, baseAliasName: string): string[];
|
|
10
|
-
export declare function translateArgs(params: ReadonlyArray<NativeModuleParamShape>, aliases: AliasMap, baseAliasName: string): string[];
|
|
10
|
+
export declare function translateSpecArgs(params: ReadonlyArray<NativeModuleParamShape>, aliases: AliasMap, baseAliasName: string, options: CppCodegenOptions): string[];
|
|
11
|
+
export declare function translateArgs(params: ReadonlyArray<NativeModuleParamShape>, aliases: AliasMap, baseAliasName: string, options: CppCodegenOptions): string[];
|
|
11
12
|
export {};
|