* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as idl from '@idlizer/core/idl'
import { ArgConvertor, CustomTypeConvertor, isMaterialized,
isImportAttr, IdlNameConvertor, Language, PeerLibrary,
LanguageWriter,
IndentedPrinter,
TSLanguageWriter,
CppConvertor,
CppLanguageWriter,
ETSLanguageWriter,
CJLanguageWriter,
CJIDLTypeToForeignStringConvertor,
TSTypeNameConvertor,
ETSTypeNameConvertor,
maybeRestoreThrows
} from "@idlizer/core";
import { ArkoalaImportTypeConvertor, ArkoalaInterfaceConvertor, ArkoalaMaterializedClassConvertor } from './ArkoalaArgConvertors.js';
import { ArkoalaCJTypeNameConvertor } from './ArkoalaTypeNameConvertors.js';
import { ArkPrimitiveTypesInstance } from './ArkPrimitiveType.js';
import { isNonTrivialModifier, peerGeneratorConfiguration } from '@idlizer/libohos';
export class ArkoalaPeerLibrary extends PeerLibrary {
override createLanguageWriter(language?: Language): LanguageWriter {
language ??= this.language
const printer = new IndentedPrinter()
switch (language) {
case Language.TS: return new TSLanguageWriter(printer, this,
new TSTypeNameConvertor(this))
case Language.ARKTS: return new ETSLanguageWriter(printer, this,
new ETSTypeNameConvertor(this), new CppConvertor(this))
case Language.CPP: return new CppLanguageWriter(printer, this,
new CppConvertor(this), ArkPrimitiveTypesInstance)
case Language.CJ: return new CJLanguageWriter(printer, this,
new ArkoalaCJTypeNameConvertor(this), new CJIDLTypeToForeignStringConvertor(this))
default: return super.createLanguageWriter(language)
}
}
override createTypeNameConvertor(language: Language): IdlNameConvertor {
switch (language) {
case Language.TS: return new TSTypeNameConvertor(this)
case Language.ARKTS: return new ETSTypeNameConvertor(this)
case Language.CJ: return new ArkoalaCJTypeNameConvertor(this)
}
return super.createTypeNameConvertor(language)
}
override typeConvertor(param: string, type: idl.IDLType, isOptionalParam = false): ArgConvertor {
return super.typeConvertor(param, type, isOptionalParam)
}
override declarationConvertor(param: string, type: idl.IDLReferenceType, declaration: idl.IDLEntry | undefined): ArgConvertor {
switch (type.name) {
case `AnimationRange`:
return new CustomTypeConvertor(param, "AnimationRange", false, "AnimationRange<number>")
}
if (declaration) {
if (isImportAttr(declaration))
return new ArkoalaImportTypeConvertor(param, this.createTypeNameConvertor(this.language).convert(type))
if (idl.isInterface(declaration) && !idl.hasExtAttribute(declaration, idl.IDLExtendedAttributes.TransformOnSerialize) && !peerGeneratorConfiguration().forceResource.includes(declaration.name) && !maybeRestoreThrows(declaration, this)) {
if (isMaterialized(declaration, this)) {
return new ArkoalaMaterializedClassConvertor(this, param, declaration)
}
if (declaration.subkind === idl.IDLInterfaceSubkind.Interface)
{
return new ArkoalaInterfaceConvertor(this, (declaration.name!), param, declaration)
}
}
}
return super.declarationConvertor(param, type, declaration)
}
override isHandwritten(node: idl.IDLEntry | idl.IDLReferenceType): boolean {
if (idl.isEntry(node)) {
return super.isHandwritten(node) || peerGeneratorConfiguration().isHandWritten(node.name) ||
isNonTrivialModifier(node, this)
}
const entry = this.resolveTypeReference(node)
if (entry)
return this.isHandwritten(entry)
return false
}
}