1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { isNullPtr, KInt, KNativePointer as KPtr, KNativePointer, nullptr } from "@koalaui/interop" 17import { global } from "../static/global" 18import { allFlags, nodeType, unpackNodeArray, unpackNonNullableNode, unpackString } from "../utilities/private" 19import { throwError } from "../../utils" 20import { Es2pandaModifierFlags } from "../../generated/Es2pandaEnums" 21import { ArktsObject } from "./ArktsObject" 22import { Es2pandaAstNodeType } from "../../Es2pandaEnums" 23 24export abstract class AstNode extends ArktsObject { 25 protected constructor(peer: KNativePointer) { 26 if (isNullPtr(peer)) { 27 throwError(`attempted to create AstNode from nullptr`) 28 } 29 super(peer) 30 this.updateChildren() 31 } 32 33 public get originalPeer(): KNativePointer { 34 const result = global.generatedEs2panda._AstNodeOriginalNodeConst(global.context, this.peer) 35 if (result === nullptr) { 36 this.originalPeer = this.peer 37 return this.peer 38 } 39 return result 40 } 41 42 public set originalPeer(peer: KNativePointer) { 43 global.generatedEs2panda._AstNodeSetOriginalNode(global.context, this.peer, peer) 44 } 45 46 public getChildren(): readonly AstNode[] { 47 return unpackNodeArray(global.es2panda._AstNodeChildren(global.context, this.peer)) 48 } 49 50 public getSubtree(): readonly AstNode[] { 51 return this.getChildren().reduce( 52 (prev: readonly AstNode[], curr) => { 53 return prev.concat(curr.getSubtree()) 54 }, 55 [this] 56 ) 57 } 58 59 public updateChildren(): void { 60 if (this.peer === nullptr) { 61 throwError('updateChildren called on NULLPTR') 62 } 63 global.es2panda._AstNodeUpdateChildren(global.context, this.peer) 64 } 65 66 public updateModifiers(modifierFlags: KInt | undefined): this { 67 global.generatedEs2panda._AstNodeClearModifier(global.context, this.peer, allFlags) 68 global.generatedEs2panda._AstNodeAddModifier(global.context, this.peer, modifierFlags ?? Es2pandaModifierFlags.MODIFIER_FLAGS_NONE) 69 return this 70 } 71 72 public dump(indentation: number = 0): string { 73 const children = this.getChildren() 74 .map((it) => it.dump(indentation + 1)) 75 const msg = 76 `${indentation}_` 77 + ` <mods: ${this.modifiers}>` 78 + this.dumpMessage() 79 return "> " + " ".repeat(4 * indentation) + msg + "\n" + children.join("") 80 } 81 82 protected dumpMessage(): string { 83 return `` 84 } 85 86 public dumpJson(): string { 87 return unpackString(global.generatedEs2panda._AstNodeDumpJSONConst(global.context, this.peer)) 88 } 89 90 public dumpSrc(): string { 91 return unpackString(global.generatedEs2panda._AstNodeDumpEtsSrcConst(global.context, this.peer)) 92 } 93 94 public dumpModifiers(): string { 95 return unpackString(global.es2panda._AstNodeDumpModifiers(global.context, this.peer)) 96 } 97 98 public clone(): this { 99 return unpackNonNullableNode(global.generatedEs2panda._AstNodeClone(global.context, this.peer, this.parent.peer)); 100 } 101 102 public get parent(): AstNode { 103 const parent = global.generatedEs2panda._AstNodeParent(global.context, this.peer) 104 if (parent === nullptr) { 105 throwError(`no parent`) 106 } 107 return unpackNonNullableNode(parent) 108 } 109 110 public set parent(node: AstNode) { 111 global.generatedEs2panda._AstNodeSetParent(global.context, this.peer, node.peer) 112 } 113 114 public get modifiers(): KInt { 115 return global.generatedEs2panda._AstNodeModifiers(global.context, this.peer) 116 } 117 118 public set modifiers(flags: KInt | undefined) { 119 global.generatedEs2panda._AstNodeClearModifier(global.context, this.peer, allFlags) 120 global.generatedEs2panda._AstNodeAddModifier(global.context, this.peer, flags ?? Es2pandaModifierFlags.MODIFIER_FLAGS_NONE) 121 } 122 123 public get isStatic(): boolean { 124 return global.generatedEs2panda._AstNodeIsStaticConst(global.context, this.peer) 125 } 126} 127 128 129export class UnsupportedNode extends AstNode { 130 constructor(peer: KPtr) { 131 super(peer) 132 133 } 134} 135