nitro-codegen
Version:
The code-generator for react-native-nitro-modules.
75 lines (74 loc) • 2.6 kB
JavaScript
import { NitroConfig } from '../../config/NitroConfig.js';
import { getForwardDeclaration } from '../c++/getForwardDeclaration.js';
import { getHybridObjectName } from '../getHybridObjectName.js';
export class HybridObjectType {
hybridObjectName;
implementationLanguage;
baseTypes;
constructor(...args) {
if (args.length === 1) {
const [spec] = args;
this.hybridObjectName = spec.name;
this.implementationLanguage = spec.language;
this.baseTypes = spec.baseTypes.map((b) => new HybridObjectType(b));
}
else {
const [hybridObjectName, implementationLanguage, baseTypes] = args;
this.hybridObjectName = hybridObjectName;
this.implementationLanguage = implementationLanguage;
this.baseTypes = baseTypes;
}
if (this.hybridObjectName.startsWith('__')) {
throw new Error(`HybridObject name cannot start with two underscores (__)! (In ${this.hybridObjectName})`);
}
}
get canBePassedByReference() {
// It's a shared_ptr<..>, no copy.
return true;
}
get kind() {
return 'hybrid-object';
}
getCode(language, mode = 'strong') {
const name = getHybridObjectName(this.hybridObjectName);
switch (language) {
case 'c++': {
const fullName = NitroConfig.getCxxNamespace('c++', name.HybridTSpec);
if (mode === 'strong') {
return `std::shared_ptr<${fullName}>`;
}
else {
return `std::weak_ptr<${fullName}>`;
}
}
case 'swift': {
return `(any ${name.HybridTSpec})`;
}
case 'kotlin': {
return name.HybridTSpec;
}
default:
throw new Error(`Language ${language} is not yet supported for HybridObjectType!`);
}
}
getExtraFiles() {
return [];
}
getRequiredImports() {
const name = getHybridObjectName(this.hybridObjectName);
const cxxNamespace = NitroConfig.getCxxNamespace('c++');
return [
{
language: 'c++',
name: 'memory',
space: 'system',
},
{
name: `${name.HybridTSpec}.hpp`,
forwardDeclaration: getForwardDeclaration('class', name.HybridTSpec, cxxNamespace),
language: 'c++',
space: 'user',
},
];
}
}