From 6895515bc9dd7db2be082a940684d3f5b95b265e Mon Sep 17 00:00:00 2001 From: Igor Loginov Date: Tue, 17 Jun 2025 20:08:59 +0300 Subject: [PATCH] One more cache test --- ui2abc/libarkts/src/plugin-utils.ts | 16 +- .../arkts-api/cache-chain/arktsconfig.json | 7 + .../arkts-api/cache-chain/files/first.ets | 5 + .../arkts-api/cache-chain/files/second.ets | 5 + .../arkts-api/cache-chain/files/third.ets | 6 + .../test/arkts-api/cache-chain/main.test.ts | 176 ++++++++++++++++++ 6 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 ui2abc/libarkts/test/arkts-api/cache-chain/arktsconfig.json create mode 100644 ui2abc/libarkts/test/arkts-api/cache-chain/files/first.ets create mode 100644 ui2abc/libarkts/test/arkts-api/cache-chain/files/second.ets create mode 100644 ui2abc/libarkts/test/arkts-api/cache-chain/files/third.ets create mode 100644 ui2abc/libarkts/test/arkts-api/cache-chain/main.test.ts diff --git a/ui2abc/libarkts/src/plugin-utils.ts b/ui2abc/libarkts/src/plugin-utils.ts index f491f89697..63e5983c0a 100644 --- a/ui2abc/libarkts/src/plugin-utils.ts +++ b/ui2abc/libarkts/src/plugin-utils.ts @@ -44,7 +44,16 @@ class ASTCache { } } -export function runTransformer(prog: Program, state: Es2pandaContextState, restart: boolean, transform: ProgramTransformer | undefined, pluginContext: PluginContext, hooks: RunTransformerHooks = {}, onlyModifyMain: boolean = false) { +export function runTransformer( + prog: Program, + state: Es2pandaContextState, + restart: boolean, + transform: ProgramTransformer | undefined, + pluginContext: PluginContext, + hooks: RunTransformerHooks = {}, + onlyModifyMain: boolean = false, // TODO: get rid of this param + explicitFileList?: string[] +) { // Program provider used to provide programs to transformer dynamically relative to inserted imports const provider = new ProgramProvider(prog) @@ -53,6 +62,11 @@ export function runTransformer(prog: Program, state: Es2pandaContextState, resta let isMainProgram = true while (currentProgram) { + if (explicitFileList && !explicitFileList.includes(currentProgram.absoluteName)) { + isMainProgram = false + currentProgram = provider.next() + continue + } // Options passed to plugin and hooks const options: CompilationOptions = { isMainProgram, diff --git a/ui2abc/libarkts/test/arkts-api/cache-chain/arktsconfig.json b/ui2abc/libarkts/test/arkts-api/cache-chain/arktsconfig.json new file mode 100644 index 0000000000..c60ee7007f --- /dev/null +++ b/ui2abc/libarkts/test/arkts-api/cache-chain/arktsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "outDir": "./files/build", + "baseUrl": "." + }, + "include": ["./**/*.ts"] +} diff --git a/ui2abc/libarkts/test/arkts-api/cache-chain/files/first.ets b/ui2abc/libarkts/test/arkts-api/cache-chain/files/first.ets new file mode 100644 index 0000000000..0b38eb3ff9 --- /dev/null +++ b/ui2abc/libarkts/test/arkts-api/cache-chain/files/first.ets @@ -0,0 +1,5 @@ +import { Two } from "./second" + +export const One: int = 1 + +class I {} diff --git a/ui2abc/libarkts/test/arkts-api/cache-chain/files/second.ets b/ui2abc/libarkts/test/arkts-api/cache-chain/files/second.ets new file mode 100644 index 0000000000..8e6a2e183b --- /dev/null +++ b/ui2abc/libarkts/test/arkts-api/cache-chain/files/second.ets @@ -0,0 +1,5 @@ +import { Three } from "./third" + +export const Two: int = 2 + +class I {} diff --git a/ui2abc/libarkts/test/arkts-api/cache-chain/files/third.ets b/ui2abc/libarkts/test/arkts-api/cache-chain/files/third.ets new file mode 100644 index 0000000000..c7245e6729 --- /dev/null +++ b/ui2abc/libarkts/test/arkts-api/cache-chain/files/third.ets @@ -0,0 +1,6 @@ +// If this import is uncommented, the circular dependency occurs -> in first.abc there is no second.II (it should be there) +// import { One } from "./first" + +export const Three: int = 3 + +class I {} diff --git a/ui2abc/libarkts/test/arkts-api/cache-chain/main.test.ts b/ui2abc/libarkts/test/arkts-api/cache-chain/main.test.ts new file mode 100644 index 0000000000..e6f9ef5c58 --- /dev/null +++ b/ui2abc/libarkts/test/arkts-api/cache-chain/main.test.ts @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as fs from "node:fs" +import * as util from "../../test-util" +import * as arkts from "../../../src" +import { execSync } from "node:child_process" +import { assert } from "chai" + +class Visitor extends arkts.AbstractVisitor { + visitor(node: arkts.AstNode) { + if (arkts.isIdentifier(node)) { + if (node.name.startsWith("I")) return arkts.factory.createIdentifier(node.name + "I") + } + return this.visitEachChild(node) + } +} + +function visitor(program: arkts.Program) { + arkts.dumpProgramInfo(program) + return new Visitor().visitor(program.ast) +} + +const PANDA_PATH = `${__dirname}/../../../../../incremental/tools/panda/` +const PANDA_SDK_PATH = `${PANDA_PATH}/node_modules/@panda/sdk` + +const FIRST = `${__dirname}/files/first.ets` +const SECOND = `${__dirname}/files/second.ets` +const THIRD = `${__dirname}/files/third.ets` + +const FIRST_ABC = `${__dirname}/files/build/first.abc` +const THIRD_ABC = `${__dirname}/files/build/third.abc` + +function proceedToStateWithLogAndAssert(logText: string, context: arkts.KNativePointer, state: arkts.Es2pandaContextState) { + arkts.global.es2panda._ProceedToState(context, state) + const curState = arkts.global.es2panda._ContextState(context) + console.log(logText, arkts.Es2pandaContextState[curState]) + assert(curState == state) +} + +suite(util.basename(__filename), () => { + test.only('test', () => { + execSync(`rm -rf ${__dirname}/files/build`, { stdio: "inherit" }) + fs.mkdirSync(`${__dirname}/files/build`, { recursive: true }) + + const config = arkts.Config.create( + ['_', '--arktsconfig', `${__dirname}/arktsconfig.json`, '--extension', 'ets', '--stdlib', `${PANDA_SDK_PATH}/ets/stdlib`] + ) + console.log("config", config) + + const configFirst = arkts.Config.create( + ['_', '--arktsconfig', `${__dirname}/arktsconfig.json`, '--extension', 'ets', '--stdlib', `${PANDA_SDK_PATH}/ets/stdlib`, '--output', FIRST_ABC, FIRST] + ) + console.log("configFirst", configFirst) + + const configThird = arkts.Config.create( + ['_', '--arktsconfig', `${__dirname}/arktsconfig.json`, '--extension', 'ets', '--stdlib', `${PANDA_SDK_PATH}/ets/stdlib`, '--output', THIRD_ABC, THIRD] + ) + console.log("configSecond", configThird) + + const globalContext = arkts.global.es2panda._CreateGlobalContext( + config.peer, + arkts.passStringArray([ + FIRST, + SECOND, + THIRD, + ]), + 3, + false + ) + + console.log("globalContext", globalContext) + + const cacheContextFirst = arkts.global.es2panda._CreateCacheContextFromFile( + configFirst.peer, + arkts.passString(FIRST), + globalContext, + false, + ) + + console.log("cacheContextFirst", cacheContextFirst) + + const cacheContextSecond = arkts.global.es2panda._CreateCacheContextFromFile( + config.peer, + arkts.passString(SECOND), + globalContext, + true, + ) + + console.log("cacheContextSecond", cacheContextSecond) + + const cacheContextThird = arkts.global.es2panda._CreateCacheContextFromFile( + configThird.peer, + arkts.passString(THIRD), + globalContext, + false, + ) + + console.log("cacheContextThird", cacheContextThird) + + function actionsForFirst() { + proceedToStateWithLogAndAssert("FIRST TO PARSED", cacheContextFirst, arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED) + + // update first program in its context + arkts.global.compilerContext = new arkts.Context(cacheContextFirst) + arkts.runTransformer( + new arkts.Program(arkts.global.es2panda._ContextProgram(cacheContextFirst)), + arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED, + false, + visitor, + new arkts.PluginContextImpl(), + {}, + false, + [FIRST, THIRD], + ) + + proceedToStateWithLogAndAssert("FIRST TO BIN", cacheContextFirst, arkts.Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED) + execSync(`${PANDA_PATH}/arkts/arkdisasm ${__dirname}/files/build/first.abc`, { stdio: "inherit" }) + } + + function actionsForSecond() { + proceedToStateWithLogAndAssert("SECOND TO PARSED", cacheContextSecond, arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED) + + // update second program in its context + arkts.global.compilerContext = new arkts.Context(cacheContextSecond) + arkts.runTransformer( + new arkts.Program(arkts.global.es2panda._ContextProgram(cacheContextSecond)), + arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED, + false, + visitor, + new arkts.PluginContextImpl(), + {}, + false, + [SECOND], + ) + + proceedToStateWithLogAndAssert("SECOND TO LOWERED", cacheContextSecond, arkts.Es2pandaContextState.ES2PANDA_STATE_LOWERED) + } + + function actionsForThird() { + proceedToStateWithLogAndAssert("THIRD TO PARSED", cacheContextThird, arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED) + + // update third program in its context + arkts.global.compilerContext = new arkts.Context(cacheContextThird) + arkts.runTransformer( + new arkts.Program(arkts.global.es2panda._ContextProgram(cacheContextThird)), + arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED, + false, + visitor, + new arkts.PluginContextImpl(), + {}, + false, + [FIRST, THIRD], + ) + + proceedToStateWithLogAndAssert("THIRD TO BIN", cacheContextThird, arkts.Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED) + execSync(`${PANDA_PATH}/arkts/arkdisasm ${__dirname}/files/build/third.abc`, { stdio: "inherit" }) + } + + actionsForSecond() + actionsForThird() + actionsForFirst() + }) +}) -- Gitee