-
-
Notifications
You must be signed in to change notification settings - Fork 353
fix: expression evaluation #1005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HardyNLee
wants to merge
1
commit into
OpenWebGAL:dev
Choose a base branch
from
HardyNLee:fix/expression-evaluation
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
packages/webgal/src/Core/controller/gamePlay/expressionEvaluation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import expression from 'angular-expressions'; | ||
| import { stageStateManager } from '@/Core/Modules/stage/stageStateManager'; | ||
| import { webgalStore } from '@/store/store'; | ||
| import { logger } from '@/Core/util/logger'; | ||
| import random from 'lodash/random'; | ||
|
|
||
| /** | ||
| * 提取变量名和表达式 | ||
| * @param expressionString 表达式字符串,例如 "x = a + 5" | ||
| * @returns 包含变量名和表达式,如果无效则返回 undefined | ||
| */ | ||
| export function extractVariableNameAndExpression( | ||
| expressionString: string, | ||
| ): { variableName: string; expression: string } | undefined { | ||
| const equalIndex = expressionString.indexOf('='); | ||
| if (equalIndex === -1) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const variableName = expressionString.substring(0, equalIndex).trim(); | ||
| if (variableName.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const expression = expressionString.substring(equalIndex + 1).trim(); | ||
| if (expression.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { variableName, expression }; | ||
| } | ||
|
|
||
| // 内置函数 | ||
| const builtinFunctions = { | ||
| random: (...args: any[]) => { | ||
| return args.length ? random(...args) : Math.random(); | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * 评估表达式字符串 | ||
| * @param expressionString 表达式字符串 | ||
| * @returns 评估结果,可能是 string、number、boolean 或 undefined | ||
| * 但也有可能返回其他类型,取决于表达式的内容和上下文 | ||
| */ | ||
| export function evaluateExpression(expressionString: string): string | number | boolean | undefined { | ||
| try { | ||
| const evaluate = expression.compile(expressionString); | ||
|
|
||
| const stageState = stageStateManager.getCalculationStageState(); | ||
| const stageVar = stageState.GameVar; | ||
| const userData = webgalStore.getState().userData; | ||
| const globalVar = userData.globalGameVar; | ||
|
|
||
| const scope: any = {}; | ||
| // 先加入内置函数(最低优先级) | ||
| Object.assign(scope, builtinFunctions); | ||
| // 然后加入全局变量 | ||
| Object.assign(scope, globalVar); | ||
| // 最后加入舞台变量以保证最高优先级 | ||
| Object.assign(scope, stageVar); | ||
| // 支持 $ 前缀的特殊值 | ||
| scope['$stage'] = stageState; | ||
| scope['$userData'] = userData; | ||
|
|
||
| const result = evaluate(scope); | ||
| switch (typeof result) { | ||
| case 'string': | ||
| case 'number': | ||
| case 'boolean': | ||
| return result; | ||
| default: | ||
| logger.warn(`不支持的表达式求值类型: ${typeof result},表达式: ${expressionString}`); | ||
| return undefined; | ||
| } | ||
| } catch (error) { | ||
| logger.warn(`表达式求值失败: ${expressionString}`, error); | ||
| return undefined; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
性能优化建议:缓存编译结果与避免对象拷贝
在当前实现中,每次调用
evaluateExpression都会执行以下操作:expression.compile(expressionString)重新编译表达式。编译是一个涉及词法分析、语法分析和生成 AST 的 CPU 密集型操作。Object.assign浅拷贝builtinFunctions、globalVar和stageVar。在大型游戏中,变量(尤其是全局变量和舞台变量)的数量可能非常多,频繁的对象创建和属性拷贝会带来显著的 CPU 开销和垃圾回收(GC)压力。解决方案:
Map缓存已编译的表达式函数。由于angular-expressions编译出的函数是无状态的,因此可以安全地复用。Proxy代理作用域查找:通过Proxy动态拦截属性读取,避免每次求值时都进行对象拷贝,从而实现零拷贝(Zero-copy)查找。以下是优化后的代码建议: