diff --git a/ets2panda/bindings/native/CMakeLists.txt b/ets2panda/bindings/native/CMakeLists.txt index b7794eba99e35cfbf120d63bc282fb00821b06b6..00491c922dbdaf7c2f11073c65a50885b9f88264 100644 --- a/ets2panda/bindings/native/CMakeLists.txt +++ b/ets2panda/bindings/native/CMakeLists.txt @@ -72,6 +72,7 @@ set(BINDINGS_NAPI_SRC ./src/callback-resource.cpp ./src/generated/bridges.cpp ./src/lsp.cpp + ./../../declgen_ets2ts/declgenEts2Ts.cpp ) # PUBLIC arkbase es2panda-lib es2panda-public @@ -80,6 +81,8 @@ set(ES2PANDA_LIBS es2panda-lib es2panda-public es2panda_lsp + arkbase + arkbytecodeopt ) set(TS_INTEROP_MODULE diff --git a/ets2panda/bindings/native/src/bridges.cpp b/ets2panda/bindings/native/src/bridges.cpp index f90652abc4d88fe9b61a3ee2197cd2b7b5f66951..33515174ea171228cf3f71dacb90a40380fc2597 100644 --- a/ets2panda/bindings/native/src/bridges.cpp +++ b/ets2panda/bindings/native/src/bridges.cpp @@ -15,6 +15,9 @@ #include "common.h" +#include "public/es2panda_lib.h" +#include "declgen_ets2ts/declgenEts2Ts.h" + KNativePointer impl_ProceedToState(KNativePointer contextPtr, KInt state) { auto context = reinterpret_cast(contextPtr); @@ -39,9 +42,11 @@ TS_INTEROP_3(CreateContextFromString, KNativePointer, KNativePointer, KStringPtr KInt impl_GenerateTsDeclarationsFromContext(KNativePointer contextPtr, KStringPtr &outputDeclEts, KStringPtr &outputEts, KBoolean exportAll) { - auto context = reinterpret_cast(contextPtr); - return static_cast(GetPublicImpl()->GenerateTsDeclarationsFromContext(context, outputDeclEts.data(), - outputEts.data(), exportAll != 0)); + using ark::es2panda::declgen_ets2ts::DeclgenOptions; + using ark::es2panda::declgen_ets2ts::GenerateTsDeclarations; + auto *context = reinterpret_cast(contextPtr); + DeclgenOptions declgenOpts = {exportAll != 0, GetStringCopy(outputDeclEts), GetStringCopy(outputEts)}; + return static_cast(GenerateTsDeclarations(context, declgenOpts)); } TS_INTEROP_4(GenerateTsDeclarationsFromContext, KInt, KNativePointer, KStringPtr, KStringPtr, KBoolean) diff --git a/ets2panda/bindings/src/utils.ts b/ets2panda/bindings/src/utils.ts index b767be2e7a70cb97ccd8c1e0bd7ae0fdb45fe1bf..7cc9a44c4c23f51f0ef50dae981baa1b9d88268f 100644 --- a/ets2panda/bindings/src/utils.ts +++ b/ets2panda/bindings/src/utils.ts @@ -17,6 +17,7 @@ import * as fs from 'fs'; import * as path from 'path'; export function throwError(error: string): never { + console.log("here") throw new Error(error) } diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp index e40c3e5bbf70a56ebdf83fce34bf1fc18d73a3e2..4c75a75caf339c07e6fb71f16c75009b41114467 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp @@ -31,6 +31,7 @@ #include "ir/ts/tsInterfaceBody.h" #include "ir/ts/tsTypeAliasDeclaration.h" #include "ir/ts/tsTypeParameter.h" +#include "public/public.h" #define DEBUG_PRINT 0 @@ -387,9 +388,9 @@ bool TSDeclGen::HandleObjectType(const checker::Type *checkerType) std::string typeStr = checkerType->ToString(); if (typeStr == "Boolean") { OutDts("boolean"); - } else if (stringTypes_.count(typeStr)) { + } else if (stringTypes_.count(typeStr) != 0u) { OutDts("string"); - } else if (numberTypes_.count(typeStr)) { + } else if (numberTypes_.count(typeStr) != 0u) { OutDts("number"); } else if (typeStr == "BigInt") { OutDts("bigint"); @@ -641,26 +642,26 @@ void TSDeclGen::ProcessFunctionReturnTypeRef(const checker::Signature *sig, cons void TSDeclGen::GenUnionType(const checker::ETSUnionType *unionType) { state_.inUnionBodyStack.push(true); - const auto originTypes = unionType->ConstituentTypes(); + const auto &originTypes = unionType->ConstituentTypes(); bool hasNumber = false; bool hasString = false; std::vector filteredTypes; - for (std::size_t i = 0; i < originTypes.size(); ++i) { - std::string typeStr = originTypes[i]->ToString(); - if (stringTypes_.count(typeStr)) { + for (auto originType : originTypes) { + std::string typeStr = originType->ToString(); + if (stringTypes_.count(typeStr) != 0u) { if (hasString) { continue; } - filteredTypes.push_back(originTypes[i]); + filteredTypes.push_back(originType); hasString = true; - } else if (numberTypes_.count(typeStr)) { + } else if (numberTypes_.count(typeStr) != 0u) { if (hasNumber) { continue; } - filteredTypes.push_back(originTypes[i]); + filteredTypes.push_back(originType); hasNumber = true; } else { - filteredTypes.push_back(originTypes[i]); + filteredTypes.push_back(originType); } } GenSeparated( @@ -705,7 +706,7 @@ bool TSDeclGen::HandleSpecificObjectTypes(const checker::ETSObjectType *objectTy void TSDeclGen::HandleTypeArgument(checker::Type *arg, const std::string &typeStr) { - if (typeStr == "Promise" && arg && arg->HasTypeFlag(checker::TypeFlag::ETS_UNDEFINED)) { + if (typeStr == "Promise" && arg != nullptr && arg->HasTypeFlag(checker::TypeFlag::ETS_UNDEFINED)) { OutDts("void"); } else if (arg != nullptr) { if (!state_.currentTypeAliasName.empty() && !arg->HasTypeFlag(checker::TypeFlag::ETS_TYPE_PARAMETER)) { @@ -777,9 +778,9 @@ void TSDeclGen::GenTypeParameters(const ir::TSTypeParameterInstantiation *typePa GenSeparated(typeParams->Params(), [this](ir::TypeNode *param) { if (param->IsETSTypeReference()) { const auto paramName = param->AsETSTypeReference()->Part()->Name()->AsIdentifier()->Name().Mutf8(); - if (stringTypes_.count(paramName)) { + if (stringTypes_.count(paramName) != 0u) { OutDts("string"); - } else if (numberTypes_.count(paramName)) { + } else if (numberTypes_.count(paramName) != 0u) { OutDts("number"); } else if (paramName == "NullPointerError") { OutDts("Error"); @@ -906,7 +907,7 @@ void TSDeclGen::GenAnnotationProperties(const ir::AnnotationUsage *anno) return; } - const auto properties = anno->Properties(); + const auto &properties = anno->Properties(); if (properties.size() == 1 && properties.at(0)->AsClassProperty()->Id()->Name() == compiler::Signatures::ANNOTATION_KEY_VALUE) { OutDts("("); @@ -1264,9 +1265,9 @@ void TSDeclGen::GenPartName(std::string &partName) { if (partName == "Boolean") { partName = "boolean"; - } else if (stringTypes_.count(partName)) { + } else if (stringTypes_.count(partName) != 0u) { partName = "string"; - } else if (numberTypes_.count(partName)) { + } else if (numberTypes_.count(partName) != 0u) { partName = "number"; } else if (partName == "ESObject") { partName = "ESObject"; @@ -1659,6 +1660,13 @@ bool WriteToFile(const std::string &path, const std::string &content, checker::E return true; } +bool GenerateTsDeclarations(es2panda_Context *ctx, const DeclgenOptions &declgenOptions) +{ + auto ctxImpl_ = reinterpret_cast(ctx); + auto *checker = reinterpret_cast(ctxImpl_->checker); + return GenerateTsDeclarations(checker, ctxImpl_->parserProgram, declgenOptions); +} + bool GenerateTsDeclarations(checker::ETSChecker *checker, const ark::es2panda::parser::Program *program, const DeclgenOptions &declgenOptions) { diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.h b/ets2panda/declgen_ets2ts/declgenEts2Ts.h index 0a259f27bfa53825dd13f4d7fd3cef1e7fb8593d..94b6fb91739d5ea730183d07161d8e59edd856a1 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.h +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.h @@ -35,6 +35,8 @@ struct DeclgenOptions { bool GenerateTsDeclarations(checker::ETSChecker *checker, const ark::es2panda::parser::Program *program, const DeclgenOptions &declgenOptions); +bool GenerateTsDeclarations(es2panda_Context *ctx, const DeclgenOptions &declgenOptions); + class TSDeclGen { public: TSDeclGen(checker::ETSChecker *checker, const ark::es2panda::parser::Program *program) diff --git a/ets2panda/driver/build_system/package.json b/ets2panda/driver/build_system/package.json index 597e3abe960a49076d13b6e22bf5cf5462c05391..e4c86cae5d20bb36aa78e756b94e2a18a3c376b7 100644 --- a/ets2panda/driver/build_system/package.json +++ b/ets2panda/driver/build_system/package.json @@ -5,21 +5,23 @@ "private": true, "main": "dist/index.js", "scripts": { - "clean": "rimraf dist", + "clean": "rimraf dist build", "build": "npm run clean && tsc", "build_debug": "npm run clean && tsc --sourceMap", - "mixed_hap:gen_decl": "npm run build && node ./dist/entry.js test/demo_mix_hap/build_config_decl.json", "mixed_hap:gen_abc": "node ./dist/entry.js test/demo_mix_hap/build_config.json", "mixed_hap:run": "npm run mixed_hap:gen_decl && npm run mixed_hap:gen_abc", - "demo_hap:gen_abc": "npm run build && node ./dist/entry.js test/demo_hap/build_config.json" + "demo_hap:gen_abc": "npm run build && node ./dist/entry.js test/demo_hap/build_config.json", + "gen_abc_single": "npm run build && node ./dist/entry.js test/single_file/build_config.json", + "gen_abc_two": "npm run build && node ./dist/entry.js test/two_files/build_config.json" }, "devDependencies": { "typescript": "^5.0.0", "@types/node": "22.10.7", "@tsconfig/recommended": "1.0.8", - "rimraf": "6.0.1" + "rimraf": "6.0.1", + "@es2panda/bindings": "file:../../bindings" }, "dependencies": { diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index 15bf2c109fcc13ae668e980d822125203ac27181..eec5a68780a33eb4d36beef2bc2d4b1463867633 100644 --- a/ets2panda/driver/build_system/src/build/base_mode.ts +++ b/ets2panda/driver/build_system/src/build/base_mode.ts @@ -21,6 +21,9 @@ import cluster, { Cluster, Worker } from 'cluster'; + +import { DriverHelper, Es2pandaContextState } from "@es2panda/bindings" + import { ABC_SUFFIX, ARKTSCONFIG_JSON_FILE, @@ -121,51 +124,24 @@ export abstract class BaseMode { ); ensurePathExists(declEtsOutputPath); ensurePathExists(etsOutputPath); - let arktsGlobal = this.buildConfig.arktsGlobal as any; - let arkts = this.buildConfig.arkts as any; - try { - arktsGlobal.filePath = fileInfo.filePath; - arktsGlobal.config = arkts.Config.create([ - '_', - '--extension', - 'ets', - '--arktsconfig', - fileInfo.arktsConfigFile, - fileInfo.filePath - ]).peer; - arktsGlobal.compilerContext = arkts.Context.createFromString(source); - PluginDriver.getInstance().getPluginContext().setArkTSProgram(arktsGlobal.compilerContext.program); - - arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED, true); - - let ast = arkts.EtsScript.fromContext(); - PluginDriver.getInstance().getPluginContext().setArkTSAst(ast); - PluginDriver.getInstance().runPluginHook(PluginHook.PARSED); - - arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_CHECKED, true); - - ast = arkts.EtsScript.fromContext(); - PluginDriver.getInstance().getPluginContext().setArkTSAst(ast); - PluginDriver.getInstance().runPluginHook(PluginHook.CHECKED); - - arkts.generateTsDeclarationsFromContext( - declEtsOutputPath, - etsOutputPath, - false - ); // Generate 1.0 declaration files & 1.0 glue code - this.logger.printInfo("declaration files generated"); - } catch (error) { - if (error instanceof Error) { - const logData: LogData = LogDataFactory.newInstance( - ErrorCode.BUILDSYSTEM_DECLGEN_FAIL, - 'Generate declaration files failed.', - error.message - ); - this.logger.printError(logData); - } - } finally { - arkts.destroyConfig(arktsGlobal.config); - } + let ets2pandaDeclGenCmd: string[] = [ + '_', + '--extension', + 'ets', + '--arktsconfig', + fileInfo.arktsConfigFile, + fileInfo.filePath + ] + let helper = new DriverHelper(fileInfo.filePath, ets2pandaDeclGenCmd) + helper.createCtx(source) + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_PARSED) + PluginDriver.getInstance().runPluginHook(PluginHook.PARSED); + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_CHECKED) + PluginDriver.getInstance().runPluginHook(PluginHook.CHECKED); + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_CHECKED) + helper.generateTsDecl(declEtsOutputPath, etsOutputPath, false) // Generate 1.0 declaration files & 1.0 glue code + helper.finalize() + this.logger.printInfo("declaration files generated") } public compile(fileInfo: CompileFileInfo): void { @@ -186,31 +162,25 @@ export abstract class BaseMode { } ets2pandaCmd.push(fileInfo.filePath); this.logger.printInfo('ets2pandaCmd: ' + ets2pandaCmd.join(' ')); + let helper = new DriverHelper(fileInfo.filePath, ets2pandaCmd) - let arktsGlobal = this.buildConfig.arktsGlobal as any; - let arkts = this.buildConfig.arkts as any; + const source = fs.readFileSync(fileInfo.filePath).toString(); + let plugDriver: PluginDriver = PluginDriver.getInstance() try { - arktsGlobal.filePath = fileInfo.filePath; - arktsGlobal.config = arkts.Config.create(ets2pandaCmd).peer; - const source = fs.readFileSync(fileInfo.filePath).toString(); - arktsGlobal.compilerContext = arkts.Context.createFromString(source); - PluginDriver.getInstance().getPluginContext().setArkTSProgram(arktsGlobal.compilerContext.program); - - arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED); - this.logger.printInfo('es2panda proceedToState parsed'); - let ast = arkts.EtsScript.fromContext(); - PluginDriver.getInstance().getPluginContext().setArkTSAst(ast); - PluginDriver.getInstance().runPluginHook(PluginHook.PARSED); + let ctx = helper.createCtx(source) + plugDriver.setPluginContext(ctx) + + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_PARSED) + this.logger.printInfo('parsed'); + plugDriver.runPluginHook(PluginHook.PARSED); this.logger.printInfo('plugin parsed finished'); - arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_CHECKED); + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_CHECKED) this.logger.printInfo('es2panda proceedToState checked'); - ast = arkts.EtsScript.fromContext(); - PluginDriver.getInstance().getPluginContext().setArkTSAst(ast); - PluginDriver.getInstance().runPluginHook(PluginHook.CHECKED); + plugDriver.runPluginHook(PluginHook.CHECKED); this.logger.printInfo('plugin checked finished'); - arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED); + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED) this.logger.printInfo('es2panda bin generated'); } catch (error) { if (error instanceof Error) { @@ -222,8 +192,9 @@ export abstract class BaseMode { this.logger.printError(logData); } } finally { - PluginDriver.getInstance().runPluginHook(PluginHook.CLEAN); - arkts.destroyConfig(arktsGlobal.config); + plugDriver.runPluginHook(PluginHook.CLEAN); + // Note(nojpg): we should return err code to not to continue compilation after first fail + helper.finalize() } } @@ -424,7 +395,7 @@ export abstract class BaseMode { await Promise.all(compilePromises); } - public async run(): Promise { + public run(): void { this.generateModuleInfos(); this.generateArkTSConfigForModules(); @@ -435,7 +406,6 @@ export abstract class BaseMode { resolve(); })); }); - await Promise.all(compilePromises); this.mergeAbcFiles(); } @@ -563,4 +533,4 @@ export abstract class BaseMode { execArgv: execArgs, }); } -} \ No newline at end of file +} diff --git a/ets2panda/driver/build_system/src/build/build_mode.ts b/ets2panda/driver/build_system/src/build/build_mode.ts index 4c797a308a7e1f6f67bcee059b1dbfe484d2c7dc..58ef7e6ad4c69e68eff60eff18208e95f713130f 100644 --- a/ets2panda/driver/build_system/src/build/build_mode.ts +++ b/ets2panda/driver/build_system/src/build/build_mode.ts @@ -28,4 +28,4 @@ export class BuildMode extends BaseMode { public async run(): Promise { await super.runParallell(); } -} \ No newline at end of file +} diff --git a/ets2panda/driver/build_system/src/build/compile_worker.ts b/ets2panda/driver/build_system/src/build/compile_worker.ts index b94e6d5b2c0a5df7a2fe71557f806b8c7b428a8c..992ff7f731ea72e6f1eb4776e3d955cbda40d865 100644 --- a/ets2panda/driver/build_system/src/build/compile_worker.ts +++ b/ets2panda/driver/build_system/src/build/compile_worker.ts @@ -17,7 +17,6 @@ import { CompileFileInfo } from '../types'; import * as fs from 'fs'; import * as path from 'path'; import { ensurePathExists } from '../utils'; -import { KOALA_WRAPPER_PATH_FROM_SDK } from '../pre_define'; import { PluginDriver, PluginHook } from '../plugins/plugins_driver'; import { BuildConfig, @@ -31,6 +30,7 @@ import { Logger } from '../logger'; import { ErrorCode } from '../error_code'; +import { DriverHelper, Es2pandaContextState } from '@es2panda/bindings'; process.on('message', (message: { @@ -45,8 +45,6 @@ process.on('message', (message: { Logger.getInstance(buildConfig); PluginDriver.getInstance().initPlugins(buildConfig); - const koalaWrapperPath = path.resolve(buildConfig.buildSdkPath, KOALA_WRAPPER_PATH_FROM_SDK); - let { arkts, arktsGlobal } = require(koalaWrapperPath); for (const fileInfo of taskList) { try { @@ -60,20 +58,15 @@ process.on('message', (message: { ]; if (isDebug) ets2pandaCmd.push('--debug-info'); ets2pandaCmd.push(fileInfo.filePath); - - arktsGlobal.filePath = fileInfo.filePath; - arktsGlobal.config = arkts.Config.create(ets2pandaCmd).peer; - arktsGlobal.compilerContext = arkts.Context.createFromString(source); - - PluginDriver.getInstance().getPluginContext().setArkTSProgram(arktsGlobal.compilerContext.program); - - arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED); + let helper = new DriverHelper(fileInfo.filePath, ets2pandaCmd) + helper.createCtx(source) + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_PARSED) PluginDriver.getInstance().runPluginHook(PluginHook.PARSED); - - arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_CHECKED); + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_CHECKED) PluginDriver.getInstance().runPluginHook(PluginHook.CHECKED); - - arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED); + helper.proceedToState(Es2pandaContextState.ES2PANDA_STATE_CHECKED) + // NOTE(nojpg) move finilize or remove catch with plugindriverhook + helper.finalize() } catch (error) { if (error instanceof Error) { const logData: LogData = LogDataFactory.newInstance( @@ -90,9 +83,8 @@ process.on('message', (message: { }); } finally { PluginDriver.getInstance().runPluginHook(PluginHook.CLEAN); - arkts.destroyConfig(arktsGlobal.config); } } process.exit(0); -}); \ No newline at end of file +}); diff --git a/ets2panda/driver/build_system/src/init/process_build_config.ts b/ets2panda/driver/build_system/src/init/process_build_config.ts index 58900a0dc8fc90b53a1b043cd912767c92d6b4b1..6d92ca33d8b1baa28ce253decf5ab4773f433632 100644 --- a/ets2panda/driver/build_system/src/init/process_build_config.ts +++ b/ets2panda/driver/build_system/src/init/process_build_config.ts @@ -23,7 +23,6 @@ import { } from '../utils'; import { PluginDriver } from '../plugins/plugins_driver'; import { - KOALA_WRAPPER_PATH_FROM_SDK, PANDA_SDK_PATH_FROM_SDK } from '../pre_define'; import { @@ -41,7 +40,6 @@ export function processBuildConfig(projectConfig: BuildConfig): BuildConfig { initPlatformSpecificConfig(buildConfig); initBuildEnv(buildConfig); - initKoalaWrapper(buildConfig); PluginDriver.getInstance().initPlugins(buildConfig); return buildConfig; @@ -55,7 +53,11 @@ function initPlatformSpecificConfig(buildConfig: BuildConfig): void { } if (isMac() || isLinux()) { - buildConfig.abcLinkerPath = path.join(pandaSdkPath, 'bin', 'ark_link'); + if (process.env.BUILD_DIR != undefined) { + buildConfig.abcLinkerPath = path.join(process.env.BUILD_DIR, 'bin', 'ark_link') + } else { + buildConfig.abcLinkerPath = path.join(pandaSdkPath, 'bin', 'ark_link'); + } } if (!fs.existsSync(buildConfig.abcLinkerPath as string)) { @@ -79,10 +81,3 @@ export function initBuildEnv(buildConfig: BuildConfig): void { process.env.PATH = `${currentPath}${path.delimiter}${pandaLibPath}`; logger.printInfo(`Updated PATH: ${process.env.PATH}`); } - -function initKoalaWrapper(buildConfig: BuildConfig): void { - let koalaWrapperPath: string = path.resolve(buildConfig.buildSdkPath as string, KOALA_WRAPPER_PATH_FROM_SDK); - const { arkts, arktsGlobal } = require(koalaWrapperPath); - buildConfig.arkts = arkts; - buildConfig.arktsGlobal = arktsGlobal; -} diff --git a/ets2panda/driver/build_system/src/plugins/plugins_driver.ts b/ets2panda/driver/build_system/src/plugins/plugins_driver.ts index d0905124edc2e171fe6846c78df257a05339c2a8..d78ef47d5279589a06a359460d19f201289769f6 100644 --- a/ets2panda/driver/build_system/src/plugins/plugins_driver.ts +++ b/ets2panda/driver/build_system/src/plugins/plugins_driver.ts @@ -65,12 +65,16 @@ type RawPlugins = { init: PluginInitFunction | undefined }; +type ptr = Uint8Array | number | bigint + class PluginContext { + private readonly ctxPeer: ptr; private ast: object | undefined; private program: object | undefined; private projectConfig: object | undefined; - constructor() { + constructor(ctxPeer: ptr) { + this.ctxPeer = ctxPeer this.ast = undefined; this.program = undefined; this.projectConfig = undefined; @@ -114,13 +118,12 @@ export class PluginDriver { private static instance: PluginDriver | undefined; private sortedPlugins: Map; private allPlugins: Map; - private context: PluginContext; + private context: PluginContext | undefined; private logger: Logger = Logger.getInstance(); constructor() { this.sortedPlugins = new Map(); this.allPlugins = new Map(); - this.context = new PluginContext(); } public static getInstance(): PluginDriver { @@ -134,6 +137,10 @@ export class PluginDriver { PluginDriver.instance = undefined; } + public setPluginContext(ctxPeer: ptr): void { + this.context = new PluginContext(ctxPeer) + } + public initPlugins(projectConfig: BuildConfig): void { if (!projectConfig || !projectConfig.plugins) { return; @@ -144,10 +151,9 @@ export class PluginDriver { let pluginObject = require(value as string); let initFunction = Object.values(pluginObject)[0] as PluginInitFunction; if (typeof initFunction !== 'function') { - throw('Failed to load plugin: plugin in wrong format'); + throw ('Failed to load plugin: plugin in wrong format'); } this.logger.printInfo(`Loaded plugin: ', ${key}, ${pluginObject}`); - return { name: key, init: initFunction @@ -171,11 +177,9 @@ export class PluginDriver { this.allPlugins.set(plugin.name, plugin.init()); } }); - - this.context.setProjectConfig(projectConfig); } - private getPlugins(hook: PluginHook) : PluginExecutor[] | undefined { + private getPlugins(hook: PluginHook): PluginExecutor[] | undefined { if (!this.sortedPlugins.has(hook)) { const sortedPlugins: PluginExecutor[] = this.getSortedPlugins(hook); if (sortedPlugins.length === 0) { @@ -226,11 +230,14 @@ export class PluginDriver { } plugins.forEach((executor: PluginExecutor) => { this.logger.printInfo(`executing plugin: ${executor.name}`); - return (executor.handler as Function).apply(this.context); + return (executor.handler as Function).apply(this.getPluginContext()); }); } public getPluginContext(): PluginContext { + if (this.context == undefined) { + throw new Error("Plugin context not initialized, pls call setPluginContext before") + } return this.context; } } diff --git a/ets2panda/driver/build_system/src/pre_define.ts b/ets2panda/driver/build_system/src/pre_define.ts index 7f1659737db8866f1b4be58f8bdf7f5119303ed8..d5ce95d7f4f3a20a919f858786233ffbd6c9bbe9 100644 --- a/ets2panda/driver/build_system/src/pre_define.ts +++ b/ets2panda/driver/build_system/src/pre_define.ts @@ -36,6 +36,4 @@ export enum LANGUAGE_VERSION { export const PANDA_SDK_PATH_FROM_SDK: string = './build-tools/ets2panda'; export const SYSTEM_SDK_PATH_FROM_SDK: string = './'; -export const KOALA_WRAPPER_PATH_FROM_SDK: string = './build-tools/koala-wrapper/build/lib/es2panda'; - export const DEFAULT_WOKER_NUMS: number = 4; diff --git a/ets2panda/driver/build_system/test/demo_hap/build_config.json b/ets2panda/driver/build_system/test/demo_hap/build_config.json index 59193da7a0c69939c08da835cda3ca67aba92c5f..6a6859db13c902c669db59ee7a428a41ffe2bf0b 100644 --- a/ets2panda/driver/build_system/test/demo_hap/build_config.json +++ b/ets2panda/driver/build_system/test/demo_hap/build_config.json @@ -44,4 +44,4 @@ "language": "1.2" } ] -} \ No newline at end of file +} diff --git a/ets2panda/driver/build_system/test/single_file/build_config.json b/ets2panda/driver/build_system/test/single_file/build_config.json new file mode 100644 index 0000000000000000000000000000000000000000..49462996f5b7565d8d8de398f05a1f67ff429710 --- /dev/null +++ b/ets2panda/driver/build_system/test/single_file/build_config.json @@ -0,0 +1,20 @@ +{ + "buildType": "build", + "buildMode": "Debug", + + "packageName": "entry", + "moduleType": "shared", + "moduleRootPath": "${absolute_path_to_build_system}/test/single_file/", + "sourceRoots": ["src/"], + "compileFiles": [ + "${absolute_path_to_build_system}/test/single_file/src/main.ets" + ], + "loaderOutPath": "./build", + "cachePath": "./build/cache", + "buildSdkPath": ".", + "pandaStdlibPath": "${absolute_path_to_stdlib}", + + "enableDeclgenEts2Ts": false, + + "dependentModuleList": [] +} diff --git a/ets2panda/driver/build_system/test/single_file/src/main.ets b/ets2panda/driver/build_system/test/single_file/src/main.ets new file mode 100644 index 0000000000000000000000000000000000000000..6d41744fc733cd0aeae8c8726bde0bff605d4403 --- /dev/null +++ b/ets2panda/driver/build_system/test/single_file/src/main.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function main() { + let bar = "42" + + console.log(bar) +} diff --git a/ets2panda/driver/build_system/test/two_files/build_config.json b/ets2panda/driver/build_system/test/two_files/build_config.json new file mode 100644 index 0000000000000000000000000000000000000000..14559ac2ba8b5021d5a2cad5adc2a3b76f181c8e --- /dev/null +++ b/ets2panda/driver/build_system/test/two_files/build_config.json @@ -0,0 +1,21 @@ +{ + "buildType": "build", + "buildMode": "Debug", + + "packageName": "entry", + "moduleType": "shared", + "moduleRootPath": "${absolute_path_to_build_system}/test/two_files/", + "sourceRoots": ["src/"], + "compileFiles": [ + "${absolute_path_to_build_system}/test/two_files/src/foo.ets", + "${absolute_path_to_build_system}/test/two_files/src/main.ets" + ], + "loaderOutPath": "./build", + "cachePath": "./build/cache", + "buildSdkPath": ".", + "pandaStdlibPath": "${absolute_path_to_stdlib}", + + "enableDeclgenEts2Ts": false, + + "dependentModuleList": [] +} diff --git a/ets2panda/driver/build_system/test/two_files/src/foo.ets b/ets2panda/driver/build_system/test/two_files/src/foo.ets new file mode 100644 index 0000000000000000000000000000000000000000..9da32a43986f301d133c9587a93062afe8b3ca51 --- /dev/null +++ b/ets2panda/driver/build_system/test/two_files/src/foo.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export function foo(): void { + console.log("foo func from foo") +} diff --git a/ets2panda/driver/build_system/test/two_files/src/main.ets b/ets2panda/driver/build_system/test/two_files/src/main.ets new file mode 100644 index 0000000000000000000000000000000000000000..808a59b2eba68539f009c2a3d22a07333cb84e0c --- /dev/null +++ b/ets2panda/driver/build_system/test/two_files/src/main.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {foo} from "./foo" + +function main() { + let bar = "42" + foo() + + console.log(bar) +} diff --git a/ets2panda/public/CMakeLists.txt b/ets2panda/public/CMakeLists.txt index b887bce27e45c82d4b2ec824a02d6f2795c076ab..527f3c27375222afae473c17f9e2e583dfc222bf 100644 --- a/ets2panda/public/CMakeLists.txt +++ b/ets2panda/public/CMakeLists.txt @@ -616,7 +616,6 @@ add_custom_target(gen_yamls DEPENDS es2panda_options_gen es2panda_keywords ${ES2 set(ES2PANDA_PUBLIC_SOURCES ${LIB_NAME}.cpp - ../declgen_ets2ts/declgenEts2Ts.cpp ../util/generateBin.cpp ../util/options.cpp ../util/plugin.cpp diff --git a/ets2panda/public/es2panda_lib.cpp b/ets2panda/public/es2panda_lib.cpp index 80746cad349a8d432ccebdd513e8e0d31bb67c89..8a1ab7cdf535ad3df6a248c7ea7c7dfc14ad9b84 100644 --- a/ets2panda/public/es2panda_lib.cpp +++ b/ets2panda/public/es2panda_lib.cpp @@ -56,7 +56,6 @@ #include "util/options.h" #include "compiler/lowering/util.h" #include "generated/es2panda_lib/es2panda_lib_include.inc" -#include "declgen_ets2ts/declgenEts2Ts.h" // NOLINTBEGIN @@ -874,22 +873,6 @@ extern "C" es2panda_AstNode **AllDeclarationsByNameFromProgram([[maybe_unused]] return apiRes; } -extern "C" __attribute__((unused)) int GenerateTsDeclarationsFromContext(es2panda_Context *ctx, - const char *outputDeclEts, - const char *outputEts, bool exportAll) -{ - auto *ctxImpl = reinterpret_cast(ctx); - auto *checker = reinterpret_cast(ctxImpl->checker); - - ark::es2panda::declgen_ets2ts::DeclgenOptions declgenOptions; - declgenOptions.exportAll = exportAll; - declgenOptions.outputDeclEts = outputDeclEts ? outputDeclEts : ""; - declgenOptions.outputEts = outputEts ? outputEts : ""; - - return ark::es2panda::declgen_ets2ts::GenerateTsDeclarations(checker, ctxImpl->parserProgram, declgenOptions) ? 0 - : 1; -} - extern "C" void InsertETSImportDeclarationAndParse(es2panda_Context *context, es2panda_Program *program, es2panda_AstNode *importDeclaration) { @@ -957,7 +940,6 @@ es2panda_Impl g_impl = { FirstDeclarationByNameFromProgram, AllDeclarationsByNameFromNode, AllDeclarationsByNameFromProgram, - GenerateTsDeclarationsFromContext, InsertETSImportDeclarationAndParse, #include "generated/es2panda_lib/es2panda_lib_list.inc" diff --git a/ets2panda/public/es2panda_lib.h b/ets2panda/public/es2panda_lib.h index 63b1d4e616513de09f8da67f6b724663d0aae5d0..cae0401bff973dffacad46b526a3f8bcd33e70e6 100644 --- a/ets2panda/public/es2panda_lib.h +++ b/ets2panda/public/es2panda_lib.h @@ -206,9 +206,6 @@ struct CAPI_EXPORT es2panda_Impl { const char *name, size_t *declsLen); es2panda_AstNode **(*AllDeclarationsByNameFromProgram)(es2panda_Context *ctx, const es2panda_Program *program, const char *name, size_t *declsLen); - - int (*GenerateTsDeclarationsFromContext)(es2panda_Context *context, const char *outputDeclEts, - const char *outputEts, bool exportAll); void (*InsertETSImportDeclarationAndParse)(es2panda_Context *context, es2panda_Program *program, es2panda_AstNode *importDeclaration);