From 2ad192aaecf77cbe6d9b1a28034f173c4837392f Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 13 Feb 2025 20:55:30 +0800 Subject: [PATCH 1/4] develop State Signed-off-by: yuyi --- .../libarkts/native/src/generated/bridges.cc | 6 + .../plugins/src/builder-lambda-transformer.ts | 15 +- .../plugins/src/checked-stage-plugin.ts | 9 +- .../plugins/src/component-transformer.ts | 5 +- .../plugins/src/property-translators.ts | 116 ++++ .../plugins/src/struct-transformer.ts | 141 +++++ .../libarkts/plugins/src/transform-utils.ts | 57 ++ arkoala-arkts/libarkts/plugins/src/util.ts | 2 +- arkoala-arkts/libarkts/plugins/tsconfig.json | 3 + .../src/arkts-api/factory/nodeFactory.ts | 9 +- .../src/arkts-api/factory/nodeTests.ts | 11 + arkoala-arkts/libarkts/src/arkts-api/types.ts | 18 +- .../src/arkts-api/utilities/public.ts | 4 +- .../libarkts/src/arkts-api/visitor.ts | 47 ++ .../src/generated/Es2pandaNativeModule.ts | 3 + arkoala-arkts/libarkts/user.txt | 0 arkoala-arkts/trivial/user/src/sts/hello.sts | 1 + arkoala-arkts/trivial/user/user.txt | 544 ++++++++++++++++++ 18 files changed, 976 insertions(+), 15 deletions(-) create mode 100644 arkoala-arkts/libarkts/plugins/src/property-translators.ts create mode 100644 arkoala-arkts/libarkts/plugins/src/struct-transformer.ts create mode 100644 arkoala-arkts/libarkts/plugins/src/transform-utils.ts create mode 100644 arkoala-arkts/libarkts/user.txt create mode 100644 arkoala-arkts/trivial/user/user.txt diff --git a/arkoala-arkts/libarkts/native/src/generated/bridges.cc b/arkoala-arkts/libarkts/native/src/generated/bridges.cc index 2a17ab86d..6ad6ecdf6 100644 --- a/arkoala-arkts/libarkts/native/src/generated/bridges.cc +++ b/arkoala-arkts/libarkts/native/src/generated/bridges.cc @@ -10458,3 +10458,9 @@ KNativePointer impl_CreateFunctionDecl(KNativePointer context, KStringPtr& name, } KOALA_INTEROP_3(CreateFunctionDecl, KNativePointer, KNativePointer, KStringPtr, KNativePointer); +KBoolean impl_IsTSInterfaceDeclaration(KNativePointer ast) { + auto&& _ast_ = reinterpret_cast(ast); + auto&& _result_ = GetImpl()->IsTSInterfaceDeclaration(_ast_); + return _result_; +} +KOALA_INTEROP_1(IsTSInterfaceDeclaration, KBoolean, KNativePointer); diff --git a/arkoala-arkts/libarkts/plugins/src/builder-lambda-transformer.ts b/arkoala-arkts/libarkts/plugins/src/builder-lambda-transformer.ts index 6be93bccd..7464b3d8d 100644 --- a/arkoala-arkts/libarkts/plugins/src/builder-lambda-transformer.ts +++ b/arkoala-arkts/libarkts/plugins/src/builder-lambda-transformer.ts @@ -26,15 +26,18 @@ function getLambdaArg(lambdaBody: arkts.AstNode, typeName: string|undefined): ar ) ] ) + console.log('ssssssssssssssssssssss:', body.dumpSrc()) + console.log('----------------') + const param = arkts.factory.createParameterDeclaration( arkts.factory.createIdentifier( builderLambdaInstanceName, // TODO: it should be the return type of the function annotated with the @BuilderLambda - typeName ? arkts.factory.createTypeReference( - arkts.factory.createIdentifier( - typeName - ) + typeName ? arkts.factory.createTypeReferenceFromId( + arkts.factory.createIdentifier( + typeName + ) ) : undefined, ), undefined @@ -45,7 +48,7 @@ function getLambdaArg(lambdaBody: arkts.AstNode, typeName: string|undefined): ar param ], // TODO: it should be the return type of the function annotated with the @BuilderLambda - typeName ? arkts.factory.createTypeReference( + typeName ? arkts.factory.createTypeReferenceFromId( arkts.factory.createIdentifier( typeName ) @@ -194,6 +197,8 @@ export class BuilderLambdaTransformer extends AbstractVisitor { visitor(beforeChildren: arkts.AstNode): arkts.AstNode { const node = this.visitEachChild(beforeChildren) + console.log('--**************----') + if (!arkts.isCallExpression(node)) { return node } diff --git a/arkoala-arkts/libarkts/plugins/src/checked-stage-plugin.ts b/arkoala-arkts/libarkts/plugins/src/checked-stage-plugin.ts index ec0578ffd..17998499a 100644 --- a/arkoala-arkts/libarkts/plugins/src/checked-stage-plugin.ts +++ b/arkoala-arkts/libarkts/plugins/src/checked-stage-plugin.ts @@ -2,6 +2,7 @@ import * as ts from "@koalaui/libarkts" import { PrintVisitor } from './print-visitor' import { BuilderLambdaTransformer } from './builder-lambda-transformer' import { ComponentTransformer } from './component-transformer' +import { StructTransformer } from './struct-transformer' export interface TransformerOptions { trace?: boolean, @@ -11,6 +12,12 @@ export default function exampleTransformer( userPluginOptions?: TransformerOptions ) { return (node: ts.EtsScript) => { - return new BuilderLambdaTransformer().visitor(node) + const builderLambdaTransformer = new BuilderLambdaTransformer(); + const structTransformer = new StructTransformer(); + + let script: ts.EtsScript = node; + script = builderLambdaTransformer.visitor(script) as ts.EtsScript; + script = structTransformer.visitor(script) as ts.EtsScript; + return script; } } diff --git a/arkoala-arkts/libarkts/plugins/src/component-transformer.ts b/arkoala-arkts/libarkts/plugins/src/component-transformer.ts index 28a48ec8e..298285059 100644 --- a/arkoala-arkts/libarkts/plugins/src/component-transformer.ts +++ b/arkoala-arkts/libarkts/plugins/src/component-transformer.ts @@ -71,6 +71,7 @@ export class ComponentTransformer extends AbstractVisitor { processComponent(node: arkts.ClassDeclaration | arkts.StructDeclaration): arkts.ClassDeclaration { const className = node.definition.name.name + arkts.GlobalInfo.getInfoInstance().add(className); this.context.componentNames.push(className) const newDefinition = arkts.factory.updateClassDefinition( @@ -87,10 +88,10 @@ export class ComponentTransformer extends AbstractVisitor { arkts.factory.createIdentifier('StructBase'), arkts.factory.createTSTypeParameterInstantiation( [ - arkts.factory.createTypeReference( + arkts.factory.createTypeReferenceFromId( arkts.factory.createIdentifier(className) ), - arkts.factory.createTypeReference( + arkts.factory.createTypeReferenceFromId( arkts.factory.createIdentifier(`__Options_${className}`) ), ] diff --git a/arkoala-arkts/libarkts/plugins/src/property-translators.ts b/arkoala-arkts/libarkts/plugins/src/property-translators.ts new file mode 100644 index 000000000..136c7b40d --- /dev/null +++ b/arkoala-arkts/libarkts/plugins/src/property-translators.ts @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022-2024 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 arkts from "@koalaui/libarkts"; +import { + STATE_DECORATOR, + isState, + getNameOrError, + backingField, +} from "./transform-utils"; + +export abstract class PropertyTranslator { + constructor( + protected property: arkts.ClassProperty, + protected structName: string + ) { } + + abstract translateMember(): arkts.AstNode[] + + translateStateWithoutInitializer( + property: arkts.ClassProperty, + decoratorName: string, + structName: string + ): arkts.AstNode[] { + const currentStructInfo: arkts.StructInfo = arkts.GlobalInfo.getInfoInstance().getStructInfo(structName); + const originalName = getNameOrError(property.key); + const newName = backingField(originalName) + const originField = arkts.factory.createClassProperty( + arkts.factory.createIdentifier(originalName).setOptional(true), + undefined, + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + // arkts.factory.createIdentifier("MutableState<" + property.typeAnnotation.dumpSrc() + ">") + arkts.factory.createIdentifier(property.typeAnnotation.dumpSrc()) + ) + ), + property.modifiers, + false + ); + const field = arkts.factory.createClassProperty( + arkts.factory.createIdentifier(newName).setOptional(true), + undefined, + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + // arkts.factory.createIdentifier("MutableState<" + property.typeAnnotation.dumpSrc() + ">") + arkts.factory.createIdentifier(property.typeAnnotation.dumpSrc()) + ) + ), + property.modifiers, + false + ) + currentStructInfo.stateVariables.add({ + originNode: originField, + translatedNode: field + }); + arkts.GlobalInfo.getInfoInstance().setStructInfo(structName, currentStructInfo); + + // const getter = this.createStateGetter(originalName, newName) + // const setter = this.createStateSetter(originalName, newName) + + return [field] + } +} + + +// TODO : move func +// export function createStateOf(importer: Importer, type: ts.TypeNode | undefined, ...initializer: ts.Expression[]): ts.Expression { +// return ts.factory.createCallExpression( +// id(importer.withAdaptorImport("stateOf")), +// type ? [type] : undefined, +// initializer +// ) +// } + +class State extends PropertyTranslator { + translateMember(): arkts.AstNode[] { + return this.translateStateWithoutInitializer(this.property, STATE_DECORATOR, this.structName) + } +} + +class StorageProp extends PropertyTranslator { + translateMember(): arkts.AstNode[] { + return this.translateStateWithoutInitializer(this.property, STATE_DECORATOR, this.structName) + } +} + +export function classifyProperty(member: arkts.AstNode, structName: string): PropertyTranslator | undefined { + + if (!arkts.isClassProperty(member)) return undefined + // if (isStatic(member)) return undefined + if (isState(member)) return new State(member, structName); + // if (isStorageProp(member)) return new StorageProp(member, context) + // if (isStorageLink(member)) return new StorageLink(member, context) + // if (isLocalStorageLink(member)) return new LocalStorageLink(member, context) + // if (isLocalStorageProp(member)) return new LocalStorageProp(member, context) + // if (isLink(member)) return new Link(member, context) + // if (isProp(member)) return new Prop(member, context) + // if (isObjectLink(member)) return new ObjectLink(member, context) + // if (isProvide(member)) return new Provide(member, context) + // if (isConsume(member)) return new Consume(member, context) + // if (isBuilderParam(member)) return new BuilderParam(member, context) + + // return new PlainProperty(member, context) +} diff --git a/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts b/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts new file mode 100644 index 000000000..b40fb4a5f --- /dev/null +++ b/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts @@ -0,0 +1,141 @@ +import * as arkts from "@koalaui/libarkts" +import { AbstractVisitor } from "./AbstractVisitor"; +import { filterDefined, collect, prependMemoComment } from "./transform-utils"; +import { classifyProperty, PropertyTranslator } from "./property-translators"; +import { nullptr } from "@koalaui/interop"; + +function isCustomComponentClass(node: arkts.ClassDeclaration): boolean { + const structCollection: Set = arkts.GlobalInfo.getInfoInstance().getStructCollection(); + if (structCollection.has(node.definition.name.name)) { + return true; + } + return false; +} + +function isCustomComponentInterface(node: arkts.TSInterfaceDeclaration): boolean { + const structCollection: Set = arkts.GlobalInfo.getInfoInstance().getStructCollection(); + if (structCollection.has(node.id.name.substr(10))) { + return true; + } + return false; +} + +// function createBuildProlog( +// node: arkts.StructDeclaration, +// // members?: arkts.AstNode[], +// propertyTranslators?: PropertyTranslator[], +// ): arkts.ClassElement | undefined { + +// const propertyInitializationProcessors = propertyTranslators ? +// filterDefined(propertyTranslators.map(it => it.translateToUpdate())) : +// undefined + +// // const watchHandlers = members ? this.translateWatchDecorators(members) : undefined + +// if (!propertyInitializationProcessors?.length && +// !watchHandlers?.length +// ) return undefined + +// const body = arkts.factory.createBlock( +// collect( +// propertyInitializationProcessors, +// watchHandlers, +// ), +// true +// ) + +// const method = arkts.factory.createMethodDeclaration( +// undefined, +// undefined, +// id(RewriteNames.UpdateStruct), +// undefined, +// undefined, +// [ +// parameter(initializers(), orUndefined(this.structOptions.createTypeReference(node))) +// ], +// Void(), +// body +// ) + +// return prependMemoComment(method) +// } + +function tranformPropertyMembers(classNode: arkts.ClassDeclaration, propertyTranslators: PropertyTranslator[]): arkts.AstNode[] { + const propertyMembers = propertyTranslators.map(translator => + translator.translateMember() + ) + // const updateStruct = createBuildProlog(classNode, propertyTranslators) + + // The rest of the struct members are translated here directly. + // const restMembers = [] + return collect( + ...propertyMembers, + // updateStruct, + // ...restMembers + ) +} + +function addVariableInInterface(interfaceNode: arkts.TSInterfaceDeclaration): arkts.TSInterfaceDeclaration { + const interfaceName = interfaceNode.id.name.substr(10); + const currentStructInfo: arkts.StructInfo = arkts.GlobalInfo.getInfoInstance().getStructInfo(interfaceName); + const paramters: arkts.AstNode[] = []; + currentStructInfo.stateVariables.forEach((propertyItem) => { + console.log("stateVariables1: ", propertyItem.originNode.dumpSrc()) + console.log("stateVariables2: ", propertyItem.translatedNode.dumpSrc()) + paramters.push(propertyItem.originNode) + paramters.push(propertyItem.translatedNode) + }) + console.log("addVariableInInterface: ", paramters) + const body = arkts.factory.createBlock(paramters) + const n = arkts.factory.updateInterfaceDeclaration( + interfaceNode, + [], + interfaceNode.id, + nullptr, + body, + false, + false + ); + console.log("addVariableInInterface---n1: ", body.dumpSrc()) + console.log("addVariableInInterface---n2: ", n.dumpSrc()) + + return n +} + +function tranformClassMembers(node: arkts.ClassDeclaration): arkts.ClassDeclaration { + const definition: arkts.ClassDefinition = node.definition; + const propertyTranslators = filterDefined( + definition.members.map(it => classifyProperty(it, definition.name.name)) + ); + const translatedMembers = tranformPropertyMembers(node, propertyTranslators); + + const updateMembers: arkts.AstNode[] = definition.members.map((member: arkts.AstNode) => { + // TODO: update happens here + return member; + }); + + const updateClassDef: arkts.ClassDefinition = arkts.factory.updateClassDefinition( + definition, + definition.name, + [...translatedMembers, ...updateMembers], + definition.modifiers, + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, + definition.typeParamsDecl, + definition.superClass + ); + + return arkts.factory.updateClassDeclaration(node, updateClassDef); +} + +export class StructTransformer extends AbstractVisitor { + + visitor(beforeChildren: arkts.AstNode): arkts.AstNode { + const node = this.visitEachChild(beforeChildren); + if (arkts.isClassDeclaration(node) && isCustomComponentClass(node)) { + return tranformClassMembers(node); + } else if (arkts.isTSInterfaceDeclaration(node) && isCustomComponentInterface(node)) { + return addVariableInInterface(node); + } + return node; + } +} diff --git a/arkoala-arkts/libarkts/plugins/src/transform-utils.ts b/arkoala-arkts/libarkts/plugins/src/transform-utils.ts new file mode 100644 index 000000000..f54134213 --- /dev/null +++ b/arkoala-arkts/libarkts/plugins/src/transform-utils.ts @@ -0,0 +1,57 @@ +import * as arkts from "@koalaui/libarkts"; +import { functionOverValue } from "../../../../incremental/compat/build/src"; + +export const STATE_DECORATOR = "State"; + +export function filterDefined(value: (T | undefined)[]): T[] { + return value.filter((it: T | undefined): it is T => it != undefined) +} + +export function collect(...value: (ReadonlyArray | T | undefined)[]): T[] { + const empty: (T | undefined)[] = [] + return filterDefined(empty.concat(...value)) +} + +export function prependMemoComment(node: arkts.MethodDefinition): arkts.MethodDefinition { + return node; +} + +export function getNameOrError(node: arkts.AstNode): string { + if (arkts.isIdentifier(node)) return node.name; + throw new Error("Expected an identifier, got: " + node.type.toString()) +} + +export function mangle(value: string): string { + return `__${value}` +} + +export function backingField(originalName: string): string { + return mangle(`backing_${originalName}`) +} + +// export function hasDecorator(node: arkts.AstNode, name: string): boolean { +// return getDecorator(node, name) != undefined; +// } + +// export function getDecorator(node: arkts.AstNode, name: string): arkts.Decorator | undefined { +// return filterDecorators(node)?.find(it => isDecorator(it, name)); +// } + +// export function filterDecorators(node: arkts.AstNode): readonly arkts.Decorator[] | undefined { +// return arkts.getAllDecorators(node); +// } + +// export function isStatic(node: arkts.AstNode): boolean { +// if (!arkts.canHaveModifiers(node)) return false +// const modifierLikes = arkts.Es2pandaModifierFlags +// return !!(modifierLikes?.find(it => +// it.kind == arkts.SyntaxKind.StaticKeyword) +// ) +// } + +export function isState(property: arkts.ClassProperty): boolean { + console.log('origin--: ' , property.dumpJson()) + // const a = property.an + // return hasDecorator(property, StateDecorator) + return true +} \ No newline at end of file diff --git a/arkoala-arkts/libarkts/plugins/src/util.ts b/arkoala-arkts/libarkts/plugins/src/util.ts index 3bbd8f639..3770d64d2 100644 --- a/arkoala-arkts/libarkts/plugins/src/util.ts +++ b/arkoala-arkts/libarkts/plugins/src/util.ts @@ -160,4 +160,4 @@ export function findFunctionDeclaration(node: ts.Node): ts.FunctionDeclaration | node = node.parent } return undefined -} +} \ No newline at end of file diff --git a/arkoala-arkts/libarkts/plugins/tsconfig.json b/arkoala-arkts/libarkts/plugins/tsconfig.json index d0ce6f222..8a4068363 100644 --- a/arkoala-arkts/libarkts/plugins/tsconfig.json +++ b/arkoala-arkts/libarkts/plugins/tsconfig.json @@ -14,5 +14,8 @@ "./src/print-visitor.ts", "./src/builder-lambda-transformer.ts", "./src/component-transformer.ts", + "./src/struct-transformer.ts", + "./src/transform-utils.ts", + "./src/property-translators.ts" ] } diff --git a/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts b/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts index ff36ada6f..d1f6696b1 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts @@ -21,6 +21,7 @@ import { CallExpression, ClassDeclaration, ClassDefinition, + ClassProperty, ETSFunctionType, EtsImportDeclaration, ETSParameterExpression, @@ -47,7 +48,7 @@ import { TSTypeParameterDeclaration, TSTypeParameterInstantiation, VariableDeclaration, - VariableDeclarator + VariableDeclarator, } from "../types" import { MemberExpression } from "../to-be-generated/MemberExpression" import { AstNode } from "../peers/AstNode" @@ -238,6 +239,12 @@ export const factory = { get updateClassDefinition() { return compose(ClassDefinition.create) }, + get createClassProperty() { + return ClassProperty.create + }, + get updateClassProperty() { + return compose(ClassProperty.create) + }, get createFunctionType() { return ETSFunctionType.create }, diff --git a/arkoala-arkts/libarkts/src/arkts-api/factory/nodeTests.ts b/arkoala-arkts/libarkts/src/arkts-api/factory/nodeTests.ts index 07750fe2f..71422a1d2 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/factory/nodeTests.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/factory/nodeTests.ts @@ -1,3 +1,4 @@ +import { global } from "../static/global" import { ArrowFunctionExpression, BlockStatement, @@ -15,6 +16,8 @@ import { StringLiteral, StructDeclaration, VariableDeclaration, + ClassProperty, + TSInterfaceDeclaration, } from "../types" import { MemberExpression } from "../to-be-generated/MemberExpression" import { AstNode } from "../peers/AstNode" @@ -23,6 +26,10 @@ export function isIdentifier(node: AstNode): node is Identifier { return node instanceof Identifier } +export function isTSInterfaceDeclaration(node: AstNode): node is TSInterfaceDeclaration { + return global.generatedEs2panda._IsTSInterfaceDeclaration(node.peer); +} + export function isCallExpression(node: AstNode): node is CallExpression { return node instanceof CallExpression } @@ -86,3 +93,7 @@ export function isStringLiteral(node: AstNode): node is StringLiteral { export function isClassDefinition(node: AstNode): node is ClassDefinition { return node instanceof ClassDefinition } + +export function isClassProperty(node: AstNode): node is ClassProperty { + return node instanceof ClassProperty +} diff --git a/arkoala-arkts/libarkts/src/arkts-api/types.ts b/arkoala-arkts/libarkts/src/arkts-api/types.ts index 7be8fafd1..8c3150a39 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/types.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/types.ts @@ -249,6 +249,7 @@ export class ETSTypeReference extends AstNode { constructor(peer: KPtr) { assertValidPeer(peer, Es2pandaAstNodeType.AST_NODE_TYPE_ETS_TYPE_REFERENCE) super(peer) + // this.typeRefPart = unpackNonNullableNode(global.generatedEs2panda._ETSTypeReferencePart(global.context, this.peer)) } static create( @@ -274,14 +275,14 @@ export class ETSTypeReference extends AstNode { } // TODO: - // readonly typeName: Identifier - // readonly typeRefPart: TypeReferencePart + // readonly typeRefPart: ETSTypeReferencePart } export class ETSTypeReferencePart extends AstNode { constructor(peer: KPtr) { assertValidPeer(peer, Es2pandaAstNodeType.AST_NODE_TYPE_ETS_TYPE_REFERENCE_PART) super(peer) + this.name = unpackNonNullableNode(global.generatedEs2panda._ETSTypeReferencePartName(global.context, this.peer)) } // TODO: support type params and prev @@ -300,6 +301,7 @@ export class ETSTypeReferencePart extends AstNode { ) ) } + readonly name: Identifier // readonly typeName: Identifier } @@ -412,6 +414,11 @@ export class Identifier extends AstNode { get identifierFlags(): Es2pandaIdentifierFlags { return global.es2panda._IdentifierIdentifierFlags(global.context, this.peer) } + + setOptional(optional: boolean) { + global.generatedEs2panda._IdentifierSetOptional(global.context, this.peer, optional) + return this + } } export class StringLiteral extends AstNode { @@ -961,11 +968,12 @@ export class ClassProperty extends ClassElement { constructor(peer: KPtr) { assertValidPeer(peer, Es2pandaAstNodeType.AST_NODE_TYPE_CLASS_PROPERTY) super(peer) + this.typeAnnotation = unpackNonNullableNode(global.generatedEs2panda._ClassPropertyTypeAnnotationConst(global.context, this.peer)) } static create( key: AstNode, - value: AstNode, + value: AstNode | undefined, typeAnnotation: AstNode, modifiers: KInt, isComputed: boolean @@ -981,6 +989,7 @@ export class ClassProperty extends ClassElement { ) ) } + readonly typeAnnotation: ETSTypeReference } export class VariableDeclaration extends AstNode { @@ -1176,6 +1185,7 @@ export class TSInterfaceDeclaration extends AstNode { constructor(peer: KPtr) { assertValidPeer(peer, Es2pandaAstNodeType.AST_NODE_TYPE_TS_INTERFACE_DECLARATION) super(peer) + this.id = unpackNonNullableNode(global.generatedEs2panda._TSInterfaceDeclarationId(global.context, this.peer)) } static create( @@ -1199,6 +1209,8 @@ export class TSInterfaceDeclaration extends AstNode { ) ) } + + readonly id: Identifier; } export class UndefinedLiteral extends AstNode { diff --git a/arkoala-arkts/libarkts/src/arkts-api/utilities/public.ts b/arkoala-arkts/libarkts/src/arkts-api/utilities/public.ts index dbea933f3..f3e51b9f4 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/utilities/public.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/utilities/public.ts @@ -18,7 +18,7 @@ import { throwError } from "../../utils" import { KNativePointer, nullptr, withStringResult } from "@koalaui/interop" import { AnnotationUsageIr, MethodDefinition } from "../types" import { passNode, unpackNodeArray, unpackNonNullableNode } from "./private" -import { isClassDefinition, isFunctionDeclaration, isScriptFunction } from "../factory/nodeTests" +import { isClassDefinition, isFunctionDeclaration, isScriptFunction, isClassProperty } from "../factory/nodeTests" import { Es2pandaContextState } from "../../generated/Es2pandaEnums" import { AstNode } from "../peers/AstNode" import { Identifier } from "../types" @@ -63,7 +63,7 @@ export function getDecl(node: AstNode): AstNode | undefined { } export function getAnnotations(node: AstNode): readonly AnnotationUsageIr[] { - if (!isFunctionDeclaration(node) && !isScriptFunction(node) && !isClassDefinition(node)) { + if (!isFunctionDeclaration(node) && !isScriptFunction(node) && !isClassDefinition(node) && !isClassProperty(node)) { throwError('for now annotations allowed only for: functionDeclaration, scriptFunction, classDefinition') } return unpackNodeArray(global.es2panda._AnnotationAllowedAnnotations(global.context, node.peer, nullptr)) diff --git a/arkoala-arkts/libarkts/src/arkts-api/visitor.ts b/arkoala-arkts/libarkts/src/arkts-api/visitor.ts index 7c32de722..d6d1729be 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/visitor.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/visitor.ts @@ -39,6 +39,53 @@ import { MemberExpression } from "../reexport-for-generated" type Visitor = (node: AstNode) => AstNode +export interface DoubleNode { + originNode: AstNode; + translatedNode: AstNode; +} + +export class StructInfo { + stateVariables: Set = new Set(); +} + +export class GlobalInfo { + private _structCollection: Set; + private static instance: GlobalInfo; + private _structMap: Map; + + private constructor() { + this._structCollection = new Set(); + this._structMap = new Map(); + } + + public static getInfoInstance(): GlobalInfo { + if (!this.instance) { + this.instance = new GlobalInfo(); + } + return this.instance; + } + + public add(str: string): void { + this._structCollection.add(str); + } + + public getStructCollection(): Set { + return this._structCollection; + } + + public getStructInfo(structName: string): StructInfo { + const structInfo = this._structMap.get(structName); + if (!structInfo) { + return new StructInfo(); + } + return structInfo; + } + + public setStructInfo(structName: string, info: StructInfo): void { + this._structMap.set(structName, info); + } +} + // TODO: rethink (remove as) function nodeVisitor(node: T, visitor: Visitor): T { if (node === undefined) { diff --git a/arkoala-arkts/libarkts/src/generated/Es2pandaNativeModule.ts b/arkoala-arkts/libarkts/src/generated/Es2pandaNativeModule.ts index d0bd3c804..d380bee9f 100644 --- a/arkoala-arkts/libarkts/src/generated/Es2pandaNativeModule.ts +++ b/arkoala-arkts/libarkts/src/generated/Es2pandaNativeModule.ts @@ -3283,4 +3283,7 @@ export class Es2pandaNativeModule { _CreateFunctionDecl(context: KNativePointer, name: KStringPtr, node: KNativePointer): KNativePointer { throw new Error("This methods was not overloaded by native module initialization") } + _IsTSInterfaceDeclaration(ast: KNativePointer): KBoolean { + throw new Error("Not implemented") + } } \ No newline at end of file diff --git a/arkoala-arkts/libarkts/user.txt b/arkoala-arkts/libarkts/user.txt new file mode 100644 index 000000000..e69de29bb diff --git a/arkoala-arkts/trivial/user/src/sts/hello.sts b/arkoala-arkts/trivial/user/src/sts/hello.sts index 2c01dc061..c554d6bc9 100644 --- a/arkoala-arkts/trivial/user/src/sts/hello.sts +++ b/arkoala-arkts/trivial/user/src/sts/hello.sts @@ -8,6 +8,7 @@ import { Color } from "@ohos.arkui" @Component struct MyStateSample { @State color: Color = Color.White + @State message: string = 'Hello World' build() { Column({ space: 20 } as ColumnOptions, () => { Text("Hello World!") diff --git a/arkoala-arkts/trivial/user/user.txt b/arkoala-arkts/trivial/user/user.txt new file mode 100644 index 000000000..af0bbb0ad --- /dev/null +++ b/arkoala-arkts/trivial/user/user.txt @@ -0,0 +1,544 @@ + +> @koalaui/arkts-framework@1.4.1 build:user:pure-sts +> npm run build:arkui:pure-sts --prefix ../../arkui && ../../../incremental/tools/panda/arkts/arktsc-capi --file src/sts/hello.sts --arktsconfig arktsconfig-pure-sts.json --output build/sts/abc/hello.abc --dump-plugin-ast + + +> @koalaui/arkts-arkui@1.4.1 build:arkui:pure-sts +> mkdir -p build/sts/abc && ../../incremental/tools/panda/arkts/arktsc --arktsconfig arktsconfig-pure-sts.json + +WRAPPER: { + transform: '@koalaui/libarkts/plugins/parsed-stage-plugin', + stage: 'parsed', + arkui: '../../../../arkui/src/sts' +} +InitModule: es2panda + +PLUGINS: 2 Map(2) { 1 => [Function (anonymous)], 4 => [Function (anonymous)] } +InitModule: es2panda + +BEFORE PARSED: +InitModule: es2panda + + +import { Text as Text } from "@ohos.arkui"; + +import { Column as Column, ColumnOptions as ColumnOptions } from "@ohos.arkui"; + +import { Button as Button } from "@ohos.arkui"; + +import { Component as Component, State as State, Entry as Entry } from "@ohos.arkui"; + +import { Color as Color } from "@ohos.arkui"; + +@Entry()@Component()struct MyStateSample { + @State()public color: Color = Color.White; + + @State()public message: string = "Hello World"; + + public build() { + Column(({ + space: 20, + } as ColumnOptions), (() => { + Text("Hello World!").fontColor((this).color); + Button("change").onClick((() => { + (this).color = Color.Red; + })); + })).width(100).height(300); + } + + public constructor() {} + +} + +export class ArkUIEntry { + public static run() { + console.log("About to invoke the struct"); + MyStateSample.instantiateImpl(undefined, ((): MyStateSample => new MyStateSample()), ({} as __Options_MyStateSample), undefined); + } + + public constructor() {} + +} + + +EtsScript +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION + EtsImportDeclaration + EtsImportDeclaration + EtsImportDeclaration + EtsImportDeclaration + EtsImportDeclaration + StructDeclaration + ClassDefinition + Identifier + ClassProperty + ClassProperty + MethodDefinition + BlockStatement + ExpressionStatement + CallExpression + MemberExpression + NumberLiteral +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION + MethodDefinition + BlockStatement +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION + ClassDeclaration + ClassDefinition + Identifier + MethodDefinition + BlockStatement + ExpressionStatement + CallExpression + MemberExpression + StringLiteral + ExpressionStatement + CallExpression + MemberExpression + UndefinedLiteral + ArrowFunctionExpression + TSAsExpression + UndefinedLiteral +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION + MethodDefinition + BlockStatement +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +AFTER PARSED: + +import { StructBase } from "../../../../arkui/src/sts"; + +import { Text as Text } from "@ohos.arkui"; + +import { Column as Column, ColumnOptions as ColumnOptions } from "@ohos.arkui"; + +import { Button as Button } from "@ohos.arkui"; + +import { Component as Component, State as State, Entry as Entry } from "@ohos.arkui"; + +import { Color as Color } from "@ohos.arkui"; + +class MyStateSample extends StructBase { + @State()public color: Color = Color.White; + + @State()public message: string = "Hello World"; + + public build() { + Column(({ + space: 20, + } as ColumnOptions), (() => { + Text("Hello World!").fontColor((this).color); + Button("change").onClick((() => { + (this).color = Color.Red; + })); + })).width(100).height(300); + } + + public constructor() {} + +} + +export class ArkUIEntry { + public static run() { + console.log("About to invoke the struct"); + MyStateSample.instantiateImpl(undefined, ((): MyStateSample => new MyStateSample()), ({} as __Options_MyStateSample), undefined); + } + + public constructor() {} + +} + +interface __Options_MyStateSample { + +} + + +BEFORE CHECKED: + +import { StructBase as StructBase } from "../../../../arkui/src/sts"; + +import { Text as Text } from "@ohos.arkui"; + +import { Column as Column, ColumnOptions as ColumnOptions } from "@ohos.arkui"; + +import { Button as Button } from "@ohos.arkui"; + +import { Component as Component, State as State, Entry as Entry } from "@ohos.arkui"; + +import { Color as Color } from "@ohos.arkui"; + +abstract class ETSGLOBAL { + public static main() {} + + public static _$init$_() {} + + +} + +class MyStateSample extends StructBase { + @State()public color: Color = Color.White; + + @State()public message: string = "Hello World"; + + public build() { + Column.$_instantiate(((): Column => { + return new Column(); + }), ({ + space: 20, + } as ColumnOptions), ((): void => { + Text.$_instantiate(((): Text => { + return new Text(); + }), "Hello World!").fontColor((this).color); + Button.$_instantiate(((): Button => { + return new Button(); + }), "change").onClick(((): void => { + (this).color = Color.Red; + })); + })).width(100).height(300); + } + + public constructor() {} + +} + +export class ArkUIEntry { + public static run() { + console.log("About to invoke the struct"); + MyStateSample.instantiateImpl(undefined, ((): MyStateSample => { + return new MyStateSample(); + }), ({} as __Options_MyStateSample), undefined); + } + + public constructor() {} + +} + +interface __Options_MyStateSample { + +} + + +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +ssssssssssssssssssssss: return instance.width(100).height(300); +---------------- +--**************---- +--**************---- +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +--**************---- +--**************---- +--**************---- +--**************---- +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +--**************---- +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +--**************---- +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +--**************---- +--**************---- +--**************---- +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +--**************---- +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +--**************---- +--**************---- +--**************---- +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +origin--: { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "color", + "decorators": [] + }, + "value": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "Color", + "decorators": [] + }, + "property": { + "type": "Identifier", + "name": "White", + "decorators": [] + }, + "computed": false, + "optional": false + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Color", + "decorators": [] + } + } + }, + "definite": false, + "decorators": [], + "annotations": [ + { + "expr_": { + "type": "Identifier", + "name": "State", + "decorators": [] + }, + "properties": [] + } + ] +} +origin--: { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "message", + "decorators": [] + }, + "value": { + "type": "StringLiteral", + "value": "Hello World" + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [] + } + } + }, + "definite": false, + "decorators": [], + "annotations": [ + { + "expr_": { + "type": "Identifier", + "name": "State", + "decorators": [] + }, + "properties": [] + } + ] +} +yyyy--stateVariables1: color?: Color; + +yyyy--stateVariables2: __backing_color?: Color; + +yyyy--stateVariables1: color?: Color; + +yyyy--stateVariables2: __backing_color?: Color; + +yyyy--stateVariables1: message?: string; + +yyyy--stateVariables2: __backing_message?: string; + +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION +stateVariables1: color?: Color; + +stateVariables2: public __backing_color?: Color; + +stateVariables1: message?: string; + +stateVariables2: public __backing_message?: string; + +addVariableInInterface: [ + ClassProperty { + peer: 140050685270376n, + type: 17, + key: Identifier { peer: 140050685269632n, type: 36 }, + value: undefined, + typeAnnotation: ETSTypeReference { peer: 140050685270208n, type: 71 } + }, + ClassProperty { + peer: 140050685271336n, + type: 17, + key: Identifier { peer: 140050685270592n, type: 36 }, + value: undefined, + typeAnnotation: ETSTypeReference { peer: 140050685271168n, type: 71 } + }, + ClassProperty { + peer: 140050685272352n, + type: 17, + key: Identifier { peer: 140050685271608n, type: 36 }, + value: undefined, + typeAnnotation: ETSTypeReference { peer: 140050685272184n, type: 71 } + }, + ClassProperty { + peer: 140050685273312n, + type: 17, + key: Identifier { peer: 140050685272568n, type: 36 }, + value: undefined, + typeAnnotation: ETSTypeReference { peer: 140050685273144n, type: 71 } + } +] +addVariableInInterface---n1: color?: Color; + +__backing_color?: Color; + +message?: string; + +__backing_message?: string; + +addVariableInInterface---n2: interface __Options_MyStateSample { + +} + +AFTER CHECKED: + +import { StructBase as StructBase } from "../../../../arkui/src/sts"; + +import { Text as Text } from "@ohos.arkui"; + +import { Column as Column, ColumnOptions as ColumnOptions } from "@ohos.arkui"; + +import { Button as Button } from "@ohos.arkui"; + +import { Component as Component, State as State, Entry as Entry } from "@ohos.arkui"; + +import { Color as Color } from "@ohos.arkui"; + +abstract class ETSGLOBAL { + public static main() {} + + public static _$init$_() {} + + +} + +class MyStateSample extends StructBase { + __backing_color?: Color; + + __backing_message?: string; + + @State()public color: Color = Color.White; + + @State()public message: string = "Hello World"; + + public build() { + Column.instantiateImpl(((instance: Column): Column => { + return instance.width(100).height(300); + }), ((): Column => { + return new Column(); + }), ({ + space: 20, + } as ColumnOptions), ((): void => { + Text.$_instantiate(((): Text => { + return new Text(); + }), "Hello World!").fontColor((this).color); + Button.$_instantiate(((): Button => { + return new Button(); + }), "change").onClick(((): void => { + (this).color = Color.Red; + })); + })); + } + + public constructor() {} + +} + +export class ArkUIEntry { + public static run() { + console.log("About to invoke the struct"); + MyStateSample.instantiateImpl(undefined, ((): MyStateSample => { + return new MyStateSample(); + }), ({} as __Options_MyStateSample), undefined); + } + + public constructor() {} + +} + +interface __Options_MyStateSample { + +} + + -- Gitee From 9a97f98e3a9971cf74ab366d78780ee743213ccd Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 14 Feb 2025 15:09:52 +0800 Subject: [PATCH 2/4] develop state 2 Signed-off-by: yuyi --- .../plugins/src/builder-lambda-transformer.ts | 8 +- .../plugins/src/property-translators.ts | 163 +++++++++++++++++- .../plugins/src/struct-transformer.ts | 77 ++++++++- .../libarkts/plugins/src/transform-utils.ts | 3 + .../src/arkts-api/factory/nodeFactory.ts | 15 ++ arkoala-arkts/libarkts/src/arkts-api/types.ts | 7 + arkoala-arkts/trivial/user/user.txt | 130 +++----------- 7 files changed, 282 insertions(+), 121 deletions(-) diff --git a/arkoala-arkts/libarkts/plugins/src/builder-lambda-transformer.ts b/arkoala-arkts/libarkts/plugins/src/builder-lambda-transformer.ts index 7464b3d8d..e8700f781 100644 --- a/arkoala-arkts/libarkts/plugins/src/builder-lambda-transformer.ts +++ b/arkoala-arkts/libarkts/plugins/src/builder-lambda-transformer.ts @@ -26,9 +26,6 @@ function getLambdaArg(lambdaBody: arkts.AstNode, typeName: string|undefined): ar ) ] ) - console.log('ssssssssssssssssssssss:', body.dumpSrc()) - console.log('----------------') - const param = arkts.factory.createParameterDeclaration( arkts.factory.createIdentifier( @@ -99,7 +96,6 @@ function builderLambdaTypeName(annotation: arkts.AnnotationUsageIr): string | un } */ function findBuilderLambdaAnnotation(node: arkts.CallExpression): arkts.AnnotationUsageIr|undefined { - let decl: arkts.AstNode|undefined = undefined if (arkts.isIdentifier(node.expression)) { decl = arkts.getDecl(node.expression) @@ -119,6 +115,7 @@ function findBuilderLambdaAnnotation(node: arkts.CallExpression): arkts.Annotati } const func = decl.scriptFunction const declAnnotations = arkts.getAnnotations(func) + console.log('declAnnotations: ', declAnnotations.length) if (declAnnotations.length === 0) { return undefined } @@ -197,7 +194,7 @@ export class BuilderLambdaTransformer extends AbstractVisitor { visitor(beforeChildren: arkts.AstNode): arkts.AstNode { const node = this.visitEachChild(beforeChildren) - console.log('--**************----') + if (!arkts.isCallExpression(node)) { return node @@ -253,6 +250,7 @@ export class BuilderLambdaTransformer extends AbstractVisitor { ) }) + const typeName = builderLambdaTypeName(leaf) const lambdaArg = getLambdaArg(lambdaBody, typeName) diff --git a/arkoala-arkts/libarkts/plugins/src/property-translators.ts b/arkoala-arkts/libarkts/plugins/src/property-translators.ts index 136c7b40d..50246685c 100644 --- a/arkoala-arkts/libarkts/plugins/src/property-translators.ts +++ b/arkoala-arkts/libarkts/plugins/src/property-translators.ts @@ -42,8 +42,14 @@ export abstract class PropertyTranslator { undefined, arkts.factory.createTypeReference( arkts.factory.createTypeReferencePart( - // arkts.factory.createIdentifier("MutableState<" + property.typeAnnotation.dumpSrc() + ">") - arkts.factory.createIdentifier(property.typeAnnotation.dumpSrc()) + arkts.factory.createIdentifier('MutableState'), + arkts.factory.createTSTypeParameterInstantiation( + [ + arkts.factory.createTypeReferenceFromId( + arkts.factory.createIdentifier(property.typeAnnotation.dumpSrc()) + ), + ] + ) ) ), property.modifiers, @@ -54,8 +60,15 @@ export abstract class PropertyTranslator { undefined, arkts.factory.createTypeReference( arkts.factory.createTypeReferencePart( - // arkts.factory.createIdentifier("MutableState<" + property.typeAnnotation.dumpSrc() + ">") - arkts.factory.createIdentifier(property.typeAnnotation.dumpSrc()) + arkts.factory.createIdentifier('MutableState'), + // arkts.factory.createIdentifier(property.typeAnnotation.dumpSrc()), + arkts.factory.createTSTypeParameterInstantiation( + [ + arkts.factory.createTypeReferenceFromId( + arkts.factory.createIdentifier(property.typeAnnotation.dumpSrc()) + ), + ] + ) ) ), property.modifiers, @@ -67,10 +80,150 @@ export abstract class PropertyTranslator { }); arkts.GlobalInfo.getInfoInstance().setStructInfo(structName, currentStructInfo); + + const initializeStruct = this.generateInitializeStruct(newName, newName, structName, property.typeAnnotation.dumpSrc()); + const updateItem = this.generateUpdateStruct(newName, newName, structName); // const getter = this.createStateGetter(originalName, newName) // const setter = this.createStateSetter(originalName, newName) - return [field] + return [initializeStruct, updateItem, field] + } + + generateUpdateStruct(newName: string, originalName: string, structName: string): arkts.ScriptFunction { + const call = arkts.factory.createCallExpression( + arkts.factory.createMemberExpression( + arkts.factory.createIdentifier(newName).setOptional(true), + arkts.factory.createIdentifier('update'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + undefined, + [arkts.factory.createMemberExpression( + arkts.factory.createIdentifier('initializer').setOptional(true), + arkts.factory.createIdentifier(originalName), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + )] + ) + const body = arkts.factory.createBlock( + [ + arkts.factory.createMemberExpression( + arkts.factory.createIdentifier('this'), + call, + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + ] + ) + + const param = arkts.factory.createParameterDeclaration( + arkts.factory.createIdentifier( + 'initializers', + arkts.factory.createUnionType([ + arkts.factory.createIdentifier(structName + "Options"), + arkts.factory.createIdentifier('undefined') + ]) + ), + undefined + ) + + const signature = arkts.factory.createFunctionSignature( + undefined, + [ + param + ], + arkts.factory.createIdentifier("void") + ) + + return arkts.factory.createScriptFunction( + body, + signature, + arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_GETTER, + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, + true, + arkts.factory.createIdentifier('__updateStruct') + ) + } + + generateInitializeStruct(newName: string, originalName: string, structName: string, typeAnnotationStr: string): arkts.ScriptFunction { + const call = arkts.factory.createCallExpression( + arkts.factory.createIdentifier('stateOf'), + arkts.factory.createTypeParameterDeclaration([ + arkts.factory.createTSTypeParameterInstantiation( + [ + arkts.factory.createTypeReferenceFromId( + arkts.factory.createIdentifier(typeAnnotationStr) + ) + ] + ) + ]), + [arkts.factory.createMemberExpression( + arkts.factory.createIdentifier('initializers').setOptional(true), + arkts.factory.createIdentifier('count ?? (Color.White), this'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + )] + ) + const body = arkts.factory.createBlock( + [ + arkts.factory.createAssignmentExpression( + arkts.factory.createMemberExpression( + arkts.factory.createIdentifier('this'), + arkts.factory.createIdentifier(newName), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_EQUAL, + call + ) + ] + ) + + const paramInitializers = arkts.factory.createParameterDeclaration( + arkts.factory.createIdentifier( + 'initializers', + arkts.factory.createIdentifier(structName + "Options") + ), + undefined + ) + + const paramContent = arkts.factory.createParameterDeclaration( + arkts.factory.createIdentifier( + 'content', + arkts.factory.createFunctionType( + arkts.FunctionSignature.create( + undefined, + [], + arkts.factory.createPrimitiveType(arkts.Es2pandaPrimitiveType.PRIMITIVE_TYPE_VOID) + ), + arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_ARROW + ) + ), + undefined + ) + + const signature = arkts.factory.createFunctionSignature( + undefined, + [ + paramInitializers, + paramContent + ], + arkts.factory.createIdentifier("void") + ) + + return arkts.factory.createScriptFunction( + body, + signature, + arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_GETTER, + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, + true, + arkts.factory.createIdentifier('__updateStruct') + ) } } diff --git a/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts b/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts index b40fb4a5f..401f34923 100644 --- a/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts +++ b/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts @@ -85,7 +85,6 @@ function addVariableInInterface(interfaceNode: arkts.TSInterfaceDeclaration): ar paramters.push(propertyItem.originNode) paramters.push(propertyItem.translatedNode) }) - console.log("addVariableInInterface: ", paramters) const body = arkts.factory.createBlock(paramters) const n = arkts.factory.updateInterfaceDeclaration( interfaceNode, @@ -108,16 +107,80 @@ function tranformClassMembers(node: arkts.ClassDeclaration): arkts.ClassDeclarat definition.members.map(it => classifyProperty(it, definition.name.name)) ); const translatedMembers = tranformPropertyMembers(node, propertyTranslators); - - const updateMembers: arkts.AstNode[] = definition.members.map((member: arkts.AstNode) => { - // TODO: update happens here - return member; - }); + // translatedMembers.forEach((member: arkts.AstNode) => { + // // const memberName = getNameOrError(member.key) + // console.log('translatedMember: ', member.dumpSrc()) + // console.log('translatedMember: ', member.dumpJson()) + // const call = arkts.factory.createCallExpression( + // arkts.factory.createMemberExpression( + // arkts.factory.createIdentifier(member.key).setOptional(true), + // arkts.factory.createIdentifier('update'), + // arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + // false, + // false + // ), + // undefined, + // [arkts.factory.createIdentifier('initializer?.count')] + // ) + // const body = arkts.factory.createBlock( + // [ + // arkts.factory.createMemberExpression( + // arkts.factory.createIdentifier('this'), + // call, + // arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + // false, + // false + // ), + // ] + // ) + + // const param = arkts.factory.createParameterDeclaration( + // arkts.factory.createIdentifier( + // 'initializers', + // arkts.factory.createUnionType([ + // arkts.factory.createIdentifier("EntryComponentOptions"), + // arkts.factory.createIdentifier("undefined") + // ]) + // ), + // undefined + // ) + + // const signature = arkts.factory.createFunctionSignature( + // undefined, + // [ + // param + // ], + // arkts.factory.createIdentifier("void") + // ) + + // const updateItem = arkts.factory.createScriptFunction( + // body, + // signature, + // arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_GETTER, + // arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, + // true, + // arkts.factory.createIdentifier('__updateStruct') + // ) + + // // const updateItem2 = arkts.factory.createMethodDefinition( + // // arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_GET, + // // arkts.factory.createIdentifier('__updateStruct'), + // // body, + // // arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_GETTER, + // // false + // // ) + + // console.log('qqqqqqqqqqqqqqqqqqqqq:', updateItem.dumpSrc()) + // // console.log('qqqqqqqqqqqqqqqqqqqqq:', updateItem2.dumpSrc()) + + // updateMembers.push(member); + // }); + const restMembers = definition.members.filter((member: arkts.AstNode) => arkts.isMethodDefinition(member)) const updateClassDef: arkts.ClassDefinition = arkts.factory.updateClassDefinition( definition, definition.name, - [...translatedMembers, ...updateMembers], + [...translatedMembers, ...restMembers], definition.modifiers, arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, definition.typeParamsDecl, diff --git a/arkoala-arkts/libarkts/plugins/src/transform-utils.ts b/arkoala-arkts/libarkts/plugins/src/transform-utils.ts index f54134213..17252992e 100644 --- a/arkoala-arkts/libarkts/plugins/src/transform-utils.ts +++ b/arkoala-arkts/libarkts/plugins/src/transform-utils.ts @@ -51,6 +51,9 @@ export function backingField(originalName: string): string { export function isState(property: arkts.ClassProperty): boolean { console.log('origin--: ' , property.dumpJson()) + // console.log('~~~~~~~~~~~~~~~~') + // const propertyAnnotations = arkts.getAnnotations(property) + // console.log('origin--: ' , propertyAnnotations) // const a = property.an // return hasDecorator(property, StateDecorator) return true diff --git a/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts b/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts index d1f6696b1..3702c7b1d 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts @@ -49,6 +49,9 @@ import { TSTypeParameterInstantiation, VariableDeclaration, VariableDeclarator, + FunctionSignature, + ETSUndefinedType, + AssignmentExpression } from "../types" import { MemberExpression } from "../to-be-generated/MemberExpression" import { AstNode } from "../peers/AstNode" @@ -281,4 +284,16 @@ export const factory = { get updateInterfaceDeclaration() { return compose(TSInterfaceDeclaration.create) }, + get createFunctionSignature() { + return FunctionSignature.create + }, + get createUndefinedType() { + return ETSUndefinedType.create + }, + get createAssignmentExpression() { + return AssignmentExpression.create + }, + get updateAssignmentExpression() { + return compose(AssignmentExpression.create) + } } diff --git a/arkoala-arkts/libarkts/src/arkts-api/types.ts b/arkoala-arkts/libarkts/src/arkts-api/types.ts index 8c3150a39..1024ca806 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/types.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/types.ts @@ -1051,6 +1051,13 @@ export class ETSUndefinedType extends AstNode { assertValidPeer(peer, Es2pandaAstNodeType.AST_NODE_TYPE_ETS_UNDEFINED_TYPE) super(peer) } + + static create( + ): ETSUndefinedType { + return new ETSUndefinedType( + global.generatedEs2panda._CreateUndefinedLiteral(global.context) + ) + } } export class SuperExpression extends AstNode { diff --git a/arkoala-arkts/trivial/user/user.txt b/arkoala-arkts/trivial/user/user.txt index af0bbb0ad..3da1e030f 100644 --- a/arkoala-arkts/trivial/user/user.txt +++ b/arkoala-arkts/trivial/user/user.txt @@ -242,66 +242,16 @@ interface __Options_MyStateSample { WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- -ssssssssssssssssssssss: return instance.width(100).height(300); ----------------- ---**************---- ---**************---- WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION ---**************---- ---**************---- ---**************---- ---**************---- WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION ---**************---- WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- ---**************---- WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION ---**************---- ---**************---- ---**************---- WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION ---**************---- WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION ---**************---- ---**************---- ---**************---- WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION @@ -402,18 +352,6 @@ origin--: { } ] } -yyyy--stateVariables1: color?: Color; - -yyyy--stateVariables2: __backing_color?: Color; - -yyyy--stateVariables1: color?: Color; - -yyyy--stateVariables2: __backing_color?: Color; - -yyyy--stateVariables1: message?: string; - -yyyy--stateVariables2: __backing_message?: string; - WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION @@ -421,51 +359,21 @@ WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -stateVariables1: color?: Color; +stateVariables1: color?: MutableState; -stateVariables2: public __backing_color?: Color; +stateVariables2: public __backing_color?: MutableState; -stateVariables1: message?: string; +stateVariables1: message?: MutableState; -stateVariables2: public __backing_message?: string; +stateVariables2: public __backing_message?: MutableState; -addVariableInInterface: [ - ClassProperty { - peer: 140050685270376n, - type: 17, - key: Identifier { peer: 140050685269632n, type: 36 }, - value: undefined, - typeAnnotation: ETSTypeReference { peer: 140050685270208n, type: 71 } - }, - ClassProperty { - peer: 140050685271336n, - type: 17, - key: Identifier { peer: 140050685270592n, type: 36 }, - value: undefined, - typeAnnotation: ETSTypeReference { peer: 140050685271168n, type: 71 } - }, - ClassProperty { - peer: 140050685272352n, - type: 17, - key: Identifier { peer: 140050685271608n, type: 36 }, - value: undefined, - typeAnnotation: ETSTypeReference { peer: 140050685272184n, type: 71 } - }, - ClassProperty { - peer: 140050685273312n, - type: 17, - key: Identifier { peer: 140050685272568n, type: 36 }, - value: undefined, - typeAnnotation: ETSTypeReference { peer: 140050685273144n, type: 71 } - } -] -addVariableInInterface---n1: color?: Color; +addVariableInInterface---n1: color?: MutableState; -__backing_color?: Color; +__backing_color?: MutableState; -message?: string; +message?: MutableState; -__backing_message?: string; +__backing_message?: MutableState; addVariableInInterface---n2: interface __Options_MyStateSample { @@ -494,13 +402,27 @@ abstract class ETSGLOBAL { } class MyStateSample extends StructBase { - __backing_color?: Color; + (initializers: MyStateSampleOptions, content: (()=> void)): void { + this.__backing_color == stateOf(initializers?..count ?? (Color.White), this) + } - __backing_message?: string; + (initializers: MyStateSampleOptions | undefined): void { + this.__backing_color?.update(initializer?.__backing_color); + + } - @State()public color: Color = Color.White; + __backing_color?: MutableState; - @State()public message: string = "Hello World"; + (initializers: MyStateSampleOptions, content: (()=> void)): void { + this.__backing_message == stateOf(initializers?..count ?? (Color.White), this) + } + + (initializers: MyStateSampleOptions | undefined): void { + this.__backing_message?.update(initializer?.__backing_message); + + } + + __backing_message?: MutableState; public build() { Column.instantiateImpl(((instance: Column): Column => { -- Gitee From 63fbedca056bd7e77c2018b70e7f809f8d26092a Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 15 Feb 2025 15:09:38 +0800 Subject: [PATCH 3/4] use binaryExpression to create astNode in class Signed-off-by: yuyi --- .../plugins/src/property-translators.ts | 30 ++++++++++++------- .../src/arkts-api/factory/nodeFactory.ts | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/arkoala-arkts/libarkts/plugins/src/property-translators.ts b/arkoala-arkts/libarkts/plugins/src/property-translators.ts index 50246685c..0ea82a1e7 100644 --- a/arkoala-arkts/libarkts/plugins/src/property-translators.ts +++ b/arkoala-arkts/libarkts/plugins/src/property-translators.ts @@ -81,8 +81,8 @@ export abstract class PropertyTranslator { arkts.GlobalInfo.getInfoInstance().setStructInfo(structName, currentStructInfo); - const initializeStruct = this.generateInitializeStruct(newName, newName, structName, property.typeAnnotation.dumpSrc()); - const updateItem = this.generateUpdateStruct(newName, newName, structName); + const initializeStruct = this.generateInitializeStruct(newName, originalName, structName, property.typeAnnotation.dumpSrc(), property); + const updateItem = this.generateUpdateStruct(newName, originalName, structName); // const getter = this.createStateGetter(originalName, newName) // const setter = this.createStateSetter(originalName, newName) @@ -148,7 +148,18 @@ export abstract class PropertyTranslator { ) } - generateInitializeStruct(newName: string, originalName: string, structName: string, typeAnnotationStr: string): arkts.ScriptFunction { + generateInitializeStruct(newName: string, originalName: string, structName: string, typeAnnotationStr: string, property: arkts.ClassProperty): arkts.ScriptFunction { + const binaryItem = arkts.factory.createBinaryExpression( + arkts.factory.createMemberExpression( + arkts.factory.createIdentifier('initializers').setOptional(true), + arkts.factory.createIdentifier(originalName), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NULLISH_COALESCING, + property.value ? property.value : arkts.factory.createIdentifier('undefined') + ) const call = arkts.factory.createCallExpression( arkts.factory.createIdentifier('stateOf'), arkts.factory.createTypeParameterDeclaration([ @@ -160,13 +171,10 @@ export abstract class PropertyTranslator { ] ) ]), - [arkts.factory.createMemberExpression( - arkts.factory.createIdentifier('initializers').setOptional(true), - arkts.factory.createIdentifier('count ?? (Color.White), this'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - )] + [ + binaryItem, + arkts.factory.createIdentifier('this') + ] ) const body = arkts.factory.createBlock( [ @@ -178,7 +186,7 @@ export abstract class PropertyTranslator { false, false ), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_EQUAL, + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, call ) ] diff --git a/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts b/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts index 3702c7b1d..fb86296f5 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/factory/nodeFactory.ts @@ -295,5 +295,5 @@ export const factory = { }, get updateAssignmentExpression() { return compose(AssignmentExpression.create) - } + }, } -- Gitee From 042e041f4ced296e635abaf2d7638b72d1b8d443 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 15 Feb 2025 20:33:52 +0800 Subject: [PATCH 4/4] modify getter and setter Signed-off-by: yuyi --- .../plugins/src/property-translators.ts | 146 +++++- .../plugins/src/struct-transformer.ts | 117 +---- .../libarkts/plugins/src/transform-utils.ts | 6 +- arkoala-arkts/libarkts/src/arkts-api/types.ts | 29 +- arkoala-arkts/trivial/user/user.txt | 466 ------------------ 5 files changed, 156 insertions(+), 608 deletions(-) delete mode 100644 arkoala-arkts/trivial/user/user.txt diff --git a/arkoala-arkts/libarkts/plugins/src/property-translators.ts b/arkoala-arkts/libarkts/plugins/src/property-translators.ts index 0ea82a1e7..7ff871d62 100644 --- a/arkoala-arkts/libarkts/plugins/src/property-translators.ts +++ b/arkoala-arkts/libarkts/plugins/src/property-translators.ts @@ -61,7 +61,6 @@ export abstract class PropertyTranslator { arkts.factory.createTypeReference( arkts.factory.createTypeReferencePart( arkts.factory.createIdentifier('MutableState'), - // arkts.factory.createIdentifier(property.typeAnnotation.dumpSrc()), arkts.factory.createTSTypeParameterInstantiation( [ arkts.factory.createTypeReferenceFromId( @@ -83,13 +82,29 @@ export abstract class PropertyTranslator { const initializeStruct = this.generateInitializeStruct(newName, originalName, structName, property.typeAnnotation.dumpSrc(), property); const updateItem = this.generateUpdateStruct(newName, originalName, structName); - // const getter = this.createStateGetter(originalName, newName) - // const setter = this.createStateSetter(originalName, newName) - return [initializeStruct, updateItem, field] + const member = arkts.factory.createMemberExpression( + arkts.factory.createIdentifier(newName).setOptional(true), + arkts.factory.createIdentifier('value'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ) + const thisValue = arkts.factory.createMemberExpression( + arkts.factory.createIdentifier('this'), + member, + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ) + + const getter = this.createStateGetter(originalName, property.typeAnnotation, thisValue); + const setter = this.createStateSetter(originalName, property.typeAnnotation, thisValue); + + return [initializeStruct, updateItem, field, getter, setter] } - generateUpdateStruct(newName: string, originalName: string, structName: string): arkts.ScriptFunction { + generateUpdateStruct(newName: string, originalName: string, structName: string): arkts.MethodDefinition { const call = arkts.factory.createCallExpression( arkts.factory.createMemberExpression( arkts.factory.createIdentifier(newName).setOptional(true), @@ -138,17 +153,25 @@ export abstract class PropertyTranslator { arkts.factory.createIdentifier("void") ) - return arkts.factory.createScriptFunction( + const scriptFunction = arkts.factory.createScriptFunction( body, signature, arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_GETTER, arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, true, - arkts.factory.createIdentifier('__updateStruct') + undefined ) + + return arkts.factory.createMethodDefinition( + arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_CONSTRUCTOR, + arkts.factory.createIdentifier('__updateStruct'), + arkts.factory.createFunctionExpression(scriptFunction), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC, + false + ); } - generateInitializeStruct(newName: string, originalName: string, structName: string, typeAnnotationStr: string, property: arkts.ClassProperty): arkts.ScriptFunction { + generateInitializeStruct(newName: string, originalName: string, structName: string, typeAnnotationStr: string, property: arkts.ClassProperty): arkts.MethodDefinition { const binaryItem = arkts.factory.createBinaryExpression( arkts.factory.createMemberExpression( arkts.factory.createIdentifier('initializers').setOptional(true), @@ -158,7 +181,7 @@ export abstract class PropertyTranslator { false ), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NULLISH_COALESCING, - property.value ? property.value : arkts.factory.createIdentifier('undefined') + property.value ?? arkts.factory.createIdentifier('undefined') ) const call = arkts.factory.createCallExpression( arkts.factory.createIdentifier('stateOf'), @@ -224,26 +247,109 @@ export abstract class PropertyTranslator { arkts.factory.createIdentifier("void") ) - return arkts.factory.createScriptFunction( + const scriptFunction = arkts.factory.createScriptFunction( body, signature, arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_GETTER, arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, true, - arkts.factory.createIdentifier('__updateStruct') + undefined ) + + return arkts.factory.createMethodDefinition( + arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_CONSTRUCTOR, + arkts.factory.createIdentifier('__initializeStruct'), + arkts.factory.createFunctionExpression(scriptFunction), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC, + false + ); } -} + createStateGetter(originalName: string, typeAnnotation: arkts.AstNode, returnValue: arkts.MemberExpression) { + const body = arkts.factory.createBlock( + [ + arkts.factory.createReturnStatement( + returnValue + ), + ] + ) + + const signature = arkts.factory.createFunctionSignature( + undefined, + [], + typeAnnotation + ) + + const scriptFunction = arkts.factory.createScriptFunction( + body, + signature, + arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_GETTER, + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, + true, + undefined + ) -// TODO : move func -// export function createStateOf(importer: Importer, type: ts.TypeNode | undefined, ...initializer: ts.Expression[]): ts.Expression { -// return ts.factory.createCallExpression( -// id(importer.withAdaptorImport("stateOf")), -// type ? [type] : undefined, -// initializer -// ) -// } + return arkts.factory.createMethodDefinition( + arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_GET, + arkts.factory.createIdentifier(originalName), + arkts.factory.createFunctionExpression(scriptFunction), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_GETTER, + false + ); + } + + createStateSetter(originalName: string, typeAnnotation: arkts.AstNode, left: arkts.MemberExpression) { + + const body = arkts.factory.createBlock( + [ + arkts.factory.createAssignmentExpression( + left, + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, + arkts.factory.createCallExpression( + arkts.factory.createIdentifier('observedProxy'), + undefined, + [arkts.factory.createIdentifier('value')] + ) + ) + ] + ) + + const signature = arkts.factory.createFunctionSignature( + undefined, + [ + arkts.factory.createParameterDeclaration( + arkts.factory.createIdentifier( + 'value', + typeAnnotation + ), + undefined + ) + ], + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + arkts.factory.createIdentifier('void') + ) + ) + ) + + const scriptFunction = arkts.factory.createScriptFunction( + body, + signature, + arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_GETTER, + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, + true, + undefined + ) + + return arkts.factory.createMethodDefinition( + arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_SET, + arkts.factory.createIdentifier(originalName), + arkts.factory.createFunctionExpression(scriptFunction), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_SETTER, + false + ); + } +} class State extends PropertyTranslator { translateMember(): arkts.AstNode[] { diff --git a/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts b/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts index 401f34923..0a0f14322 100644 --- a/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts +++ b/arkoala-arkts/libarkts/plugins/src/struct-transformer.ts @@ -20,51 +20,10 @@ function isCustomComponentInterface(node: arkts.TSInterfaceDeclaration): boolean return false; } -// function createBuildProlog( -// node: arkts.StructDeclaration, -// // members?: arkts.AstNode[], -// propertyTranslators?: PropertyTranslator[], -// ): arkts.ClassElement | undefined { - -// const propertyInitializationProcessors = propertyTranslators ? -// filterDefined(propertyTranslators.map(it => it.translateToUpdate())) : -// undefined - -// // const watchHandlers = members ? this.translateWatchDecorators(members) : undefined - -// if (!propertyInitializationProcessors?.length && -// !watchHandlers?.length -// ) return undefined - -// const body = arkts.factory.createBlock( -// collect( -// propertyInitializationProcessors, -// watchHandlers, -// ), -// true -// ) - -// const method = arkts.factory.createMethodDeclaration( -// undefined, -// undefined, -// id(RewriteNames.UpdateStruct), -// undefined, -// undefined, -// [ -// parameter(initializers(), orUndefined(this.structOptions.createTypeReference(node))) -// ], -// Void(), -// body -// ) - -// return prependMemoComment(method) -// } - function tranformPropertyMembers(classNode: arkts.ClassDeclaration, propertyTranslators: PropertyTranslator[]): arkts.AstNode[] { const propertyMembers = propertyTranslators.map(translator => translator.translateMember() ) - // const updateStruct = createBuildProlog(classNode, propertyTranslators) // The rest of the struct members are translated here directly. // const restMembers = [] @@ -80,13 +39,11 @@ function addVariableInInterface(interfaceNode: arkts.TSInterfaceDeclaration): ar const currentStructInfo: arkts.StructInfo = arkts.GlobalInfo.getInfoInstance().getStructInfo(interfaceName); const paramters: arkts.AstNode[] = []; currentStructInfo.stateVariables.forEach((propertyItem) => { - console.log("stateVariables1: ", propertyItem.originNode.dumpSrc()) - console.log("stateVariables2: ", propertyItem.translatedNode.dumpSrc()) paramters.push(propertyItem.originNode) paramters.push(propertyItem.translatedNode) }) const body = arkts.factory.createBlock(paramters) - const n = arkts.factory.updateInterfaceDeclaration( + const newInterface = arkts.factory.updateInterfaceDeclaration( interfaceNode, [], interfaceNode.id, @@ -95,10 +52,8 @@ function addVariableInInterface(interfaceNode: arkts.TSInterfaceDeclaration): ar false, false ); - console.log("addVariableInInterface---n1: ", body.dumpSrc()) - console.log("addVariableInInterface---n2: ", n.dumpSrc()) - return n + return newInterface } function tranformClassMembers(node: arkts.ClassDeclaration): arkts.ClassDeclaration { @@ -107,74 +62,6 @@ function tranformClassMembers(node: arkts.ClassDeclaration): arkts.ClassDeclarat definition.members.map(it => classifyProperty(it, definition.name.name)) ); const translatedMembers = tranformPropertyMembers(node, propertyTranslators); - // translatedMembers.forEach((member: arkts.AstNode) => { - // // const memberName = getNameOrError(member.key) - // console.log('translatedMember: ', member.dumpSrc()) - // console.log('translatedMember: ', member.dumpJson()) - // const call = arkts.factory.createCallExpression( - // arkts.factory.createMemberExpression( - // arkts.factory.createIdentifier(member.key).setOptional(true), - // arkts.factory.createIdentifier('update'), - // arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - // false, - // false - // ), - // undefined, - // [arkts.factory.createIdentifier('initializer?.count')] - // ) - // const body = arkts.factory.createBlock( - // [ - // arkts.factory.createMemberExpression( - // arkts.factory.createIdentifier('this'), - // call, - // arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - // false, - // false - // ), - // ] - // ) - - // const param = arkts.factory.createParameterDeclaration( - // arkts.factory.createIdentifier( - // 'initializers', - // arkts.factory.createUnionType([ - // arkts.factory.createIdentifier("EntryComponentOptions"), - // arkts.factory.createIdentifier("undefined") - // ]) - // ), - // undefined - // ) - - // const signature = arkts.factory.createFunctionSignature( - // undefined, - // [ - // param - // ], - // arkts.factory.createIdentifier("void") - // ) - - // const updateItem = arkts.factory.createScriptFunction( - // body, - // signature, - // arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_GETTER, - // arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, - // true, - // arkts.factory.createIdentifier('__updateStruct') - // ) - - // // const updateItem2 = arkts.factory.createMethodDefinition( - // // arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_GET, - // // arkts.factory.createIdentifier('__updateStruct'), - // // body, - // // arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_GETTER, - // // false - // // ) - - // console.log('qqqqqqqqqqqqqqqqqqqqq:', updateItem.dumpSrc()) - // // console.log('qqqqqqqqqqqqqqqqqqqqq:', updateItem2.dumpSrc()) - - // updateMembers.push(member); - // }); const restMembers = definition.members.filter((member: arkts.AstNode) => arkts.isMethodDefinition(member)) const updateClassDef: arkts.ClassDefinition = arkts.factory.updateClassDefinition( diff --git a/arkoala-arkts/libarkts/plugins/src/transform-utils.ts b/arkoala-arkts/libarkts/plugins/src/transform-utils.ts index 17252992e..6d4a7f899 100644 --- a/arkoala-arkts/libarkts/plugins/src/transform-utils.ts +++ b/arkoala-arkts/libarkts/plugins/src/transform-utils.ts @@ -50,11 +50,7 @@ export function backingField(originalName: string): string { // } export function isState(property: arkts.ClassProperty): boolean { - console.log('origin--: ' , property.dumpJson()) - // console.log('~~~~~~~~~~~~~~~~') - // const propertyAnnotations = arkts.getAnnotations(property) - // console.log('origin--: ' , propertyAnnotations) - // const a = property.an + // console.log('aaaaaaaaaaaa: ', arkts.getAnnotations(property)) // return hasDecorator(property, StateDecorator) return true } \ No newline at end of file diff --git a/arkoala-arkts/libarkts/src/arkts-api/types.ts b/arkoala-arkts/libarkts/src/arkts-api/types.ts index 1024ca806..f3978b0c9 100644 --- a/arkoala-arkts/libarkts/src/arkts-api/types.ts +++ b/arkoala-arkts/libarkts/src/arkts-api/types.ts @@ -545,6 +545,12 @@ export class ScriptFunction extends AstNode { return new ScriptFunction(peer) } + setIdent(id: KPtr): ScriptFunction { + assertValidPeer(id, Es2pandaAstNodeType.AST_NODE_TYPE_IDENTIFIER); + global.generatedEs2panda._ScriptFunctionSetIdent(global.context, this.peer, id); + return this; + } + protected override dumpMessage(): string { const scriptFunctionFlags = global.generatedEs2panda._ScriptFunctionFlagsConst(global.context, this.peer) return ` ` @@ -923,10 +929,19 @@ export class ClassStaticBlock extends AstNode { } export class MethodDefinition extends AstNode { - constructor(peer: KPtr) { + constructor(peer: KPtr, key?: KPtr) { assertValidPeer(peer, Es2pandaAstNodeType.AST_NODE_TYPE_METHOD_DEFINITION) super(peer) this.scriptFunction = unpackNonNullableNode(global.generatedEs2panda._MethodDefinitionFunction(global.context, this.peer)) + assertValidPeer(this.scriptFunction.peer, Es2pandaAstNodeType.AST_NODE_TYPE_SCRIPT_FUNCTION); + + // Somehow the scriptFunction cannot attach method's key to its ident after checker + if (key) { + assertValidPeer(key, Es2pandaAstNodeType.AST_NODE_TYPE_IDENTIFIER); + const _name = unpackNonNullableNode(key); + this.scriptFunction = this.scriptFunction.setIdent(_name.peer); + } + this.name = unpackNonNullableNode(global.generatedEs2panda._ScriptFunctionId(global.context, this.scriptFunction.peer)) } @@ -945,10 +960,20 @@ export class MethodDefinition extends AstNode { passNode(value), modifiers, isComputed - ) + ), + key.peer ) } + kind(): Es2pandaMethodDefinitionKind { + return global.generatedEs2panda._MethodDefinitionKindConst(global.context, this.peer) + } + + // TODO: does not work + isConstructor(): boolean { + return global.generatedEs2panda._MethodDefinitionIsConstructorConst(global.context, this.peer); + } + readonly scriptFunction: ScriptFunction readonly name: Identifier } diff --git a/arkoala-arkts/trivial/user/user.txt b/arkoala-arkts/trivial/user/user.txt deleted file mode 100644 index 3da1e030f..000000000 --- a/arkoala-arkts/trivial/user/user.txt +++ /dev/null @@ -1,466 +0,0 @@ - -> @koalaui/arkts-framework@1.4.1 build:user:pure-sts -> npm run build:arkui:pure-sts --prefix ../../arkui && ../../../incremental/tools/panda/arkts/arktsc-capi --file src/sts/hello.sts --arktsconfig arktsconfig-pure-sts.json --output build/sts/abc/hello.abc --dump-plugin-ast - - -> @koalaui/arkts-arkui@1.4.1 build:arkui:pure-sts -> mkdir -p build/sts/abc && ../../incremental/tools/panda/arkts/arktsc --arktsconfig arktsconfig-pure-sts.json - -WRAPPER: { - transform: '@koalaui/libarkts/plugins/parsed-stage-plugin', - stage: 'parsed', - arkui: '../../../../arkui/src/sts' -} -InitModule: es2panda - -PLUGINS: 2 Map(2) { 1 => [Function (anonymous)], 4 => [Function (anonymous)] } -InitModule: es2panda - -BEFORE PARSED: -InitModule: es2panda - - -import { Text as Text } from "@ohos.arkui"; - -import { Column as Column, ColumnOptions as ColumnOptions } from "@ohos.arkui"; - -import { Button as Button } from "@ohos.arkui"; - -import { Component as Component, State as State, Entry as Entry } from "@ohos.arkui"; - -import { Color as Color } from "@ohos.arkui"; - -@Entry()@Component()struct MyStateSample { - @State()public color: Color = Color.White; - - @State()public message: string = "Hello World"; - - public build() { - Column(({ - space: 20, - } as ColumnOptions), (() => { - Text("Hello World!").fontColor((this).color); - Button("change").onClick((() => { - (this).color = Color.Red; - })); - })).width(100).height(300); - } - - public constructor() {} - -} - -export class ArkUIEntry { - public static run() { - console.log("About to invoke the struct"); - MyStateSample.instantiateImpl(undefined, ((): MyStateSample => new MyStateSample()), ({} as __Options_MyStateSample), undefined); - } - - public constructor() {} - -} - - -EtsScript -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION - EtsImportDeclaration - EtsImportDeclaration - EtsImportDeclaration - EtsImportDeclaration - EtsImportDeclaration - StructDeclaration - ClassDefinition - Identifier - ClassProperty - ClassProperty - MethodDefinition - BlockStatement - ExpressionStatement - CallExpression - MemberExpression - NumberLiteral -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION - MethodDefinition - BlockStatement -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION - ClassDeclaration - ClassDefinition - Identifier - MethodDefinition - BlockStatement - ExpressionStatement - CallExpression - MemberExpression - StringLiteral - ExpressionStatement - CallExpression - MemberExpression - UndefinedLiteral - ArrowFunctionExpression - TSAsExpression - UndefinedLiteral -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION - MethodDefinition - BlockStatement -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -AFTER PARSED: - -import { StructBase } from "../../../../arkui/src/sts"; - -import { Text as Text } from "@ohos.arkui"; - -import { Column as Column, ColumnOptions as ColumnOptions } from "@ohos.arkui"; - -import { Button as Button } from "@ohos.arkui"; - -import { Component as Component, State as State, Entry as Entry } from "@ohos.arkui"; - -import { Color as Color } from "@ohos.arkui"; - -class MyStateSample extends StructBase { - @State()public color: Color = Color.White; - - @State()public message: string = "Hello World"; - - public build() { - Column(({ - space: 20, - } as ColumnOptions), (() => { - Text("Hello World!").fontColor((this).color); - Button("change").onClick((() => { - (this).color = Color.Red; - })); - })).width(100).height(300); - } - - public constructor() {} - -} - -export class ArkUIEntry { - public static run() { - console.log("About to invoke the struct"); - MyStateSample.instantiateImpl(undefined, ((): MyStateSample => new MyStateSample()), ({} as __Options_MyStateSample), undefined); - } - - public constructor() {} - -} - -interface __Options_MyStateSample { - -} - - -BEFORE CHECKED: - -import { StructBase as StructBase } from "../../../../arkui/src/sts"; - -import { Text as Text } from "@ohos.arkui"; - -import { Column as Column, ColumnOptions as ColumnOptions } from "@ohos.arkui"; - -import { Button as Button } from "@ohos.arkui"; - -import { Component as Component, State as State, Entry as Entry } from "@ohos.arkui"; - -import { Color as Color } from "@ohos.arkui"; - -abstract class ETSGLOBAL { - public static main() {} - - public static _$init$_() {} - - -} - -class MyStateSample extends StructBase { - @State()public color: Color = Color.White; - - @State()public message: string = "Hello World"; - - public build() { - Column.$_instantiate(((): Column => { - return new Column(); - }), ({ - space: 20, - } as ColumnOptions), ((): void => { - Text.$_instantiate(((): Text => { - return new Text(); - }), "Hello World!").fontColor((this).color); - Button.$_instantiate(((): Button => { - return new Button(); - }), "change").onClick(((): void => { - (this).color = Color.Red; - })); - })).width(100).height(300); - } - - public constructor() {} - -} - -export class ArkUIEntry { - public static run() { - console.log("About to invoke the struct"); - MyStateSample.instantiateImpl(undefined, ((): MyStateSample => { - return new MyStateSample(); - }), ({} as __Options_MyStateSample), undefined); - } - - public constructor() {} - -} - -interface __Options_MyStateSample { - -} - - -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -origin--: { - "type": "ClassProperty", - "key": { - "type": "Identifier", - "name": "color", - "decorators": [] - }, - "value": { - "type": "MemberExpression", - "object": { - "type": "Identifier", - "name": "Color", - "decorators": [] - }, - "property": { - "type": "Identifier", - "name": "White", - "decorators": [] - }, - "computed": false, - "optional": false - }, - "accessibility": "public", - "static": false, - "readonly": false, - "declare": false, - "optional": false, - "computed": false, - "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Color", - "decorators": [] - } - } - }, - "definite": false, - "decorators": [], - "annotations": [ - { - "expr_": { - "type": "Identifier", - "name": "State", - "decorators": [] - }, - "properties": [] - } - ] -} -origin--: { - "type": "ClassProperty", - "key": { - "type": "Identifier", - "name": "message", - "decorators": [] - }, - "value": { - "type": "StringLiteral", - "value": "Hello World" - }, - "accessibility": "public", - "static": false, - "readonly": false, - "declare": false, - "optional": false, - "computed": false, - "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "string", - "decorators": [] - } - } - }, - "definite": false, - "decorators": [], - "annotations": [ - { - "expr_": { - "type": "Identifier", - "name": "State", - "decorators": [] - }, - "properties": [] - } - ] -} -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -WARNING: UNSUPPORTED NODE (arkts): AST_NODE_TYPE_OBJECT_EXPRESSION -stateVariables1: color?: MutableState; - -stateVariables2: public __backing_color?: MutableState; - -stateVariables1: message?: MutableState; - -stateVariables2: public __backing_message?: MutableState; - -addVariableInInterface---n1: color?: MutableState; - -__backing_color?: MutableState; - -message?: MutableState; - -__backing_message?: MutableState; - -addVariableInInterface---n2: interface __Options_MyStateSample { - -} - -AFTER CHECKED: - -import { StructBase as StructBase } from "../../../../arkui/src/sts"; - -import { Text as Text } from "@ohos.arkui"; - -import { Column as Column, ColumnOptions as ColumnOptions } from "@ohos.arkui"; - -import { Button as Button } from "@ohos.arkui"; - -import { Component as Component, State as State, Entry as Entry } from "@ohos.arkui"; - -import { Color as Color } from "@ohos.arkui"; - -abstract class ETSGLOBAL { - public static main() {} - - public static _$init$_() {} - - -} - -class MyStateSample extends StructBase { - (initializers: MyStateSampleOptions, content: (()=> void)): void { - this.__backing_color == stateOf(initializers?..count ?? (Color.White), this) - } - - (initializers: MyStateSampleOptions | undefined): void { - this.__backing_color?.update(initializer?.__backing_color); - - } - - __backing_color?: MutableState; - - (initializers: MyStateSampleOptions, content: (()=> void)): void { - this.__backing_message == stateOf(initializers?..count ?? (Color.White), this) - } - - (initializers: MyStateSampleOptions | undefined): void { - this.__backing_message?.update(initializer?.__backing_message); - - } - - __backing_message?: MutableState; - - public build() { - Column.instantiateImpl(((instance: Column): Column => { - return instance.width(100).height(300); - }), ((): Column => { - return new Column(); - }), ({ - space: 20, - } as ColumnOptions), ((): void => { - Text.$_instantiate(((): Text => { - return new Text(); - }), "Hello World!").fontColor((this).color); - Button.$_instantiate(((): Button => { - return new Button(); - }), "change").onClick(((): void => { - (this).color = Color.Red; - })); - })); - } - - public constructor() {} - -} - -export class ArkUIEntry { - public static run() { - console.log("About to invoke the struct"); - MyStateSample.instantiateImpl(undefined, ((): MyStateSample => { - return new MyStateSample(); - }), ({} as __Options_MyStateSample), undefined); - } - - public constructor() {} - -} - -interface __Options_MyStateSample { - -} - - -- Gitee