Skip to content

fix: expression evaluation#1005

Open
HardyNLee wants to merge 1 commit into
OpenWebGAL:devfrom
HardyNLee:fix/expression-evaluation
Open

fix: expression evaluation#1005
HardyNLee wants to merge 1 commit into
OpenWebGAL:devfrom
HardyNLee:fix/expression-evaluation

Conversation

@HardyNLee

Copy link
Copy Markdown
Contributor

介绍

使用 angular-expression 原生的表达式解析逻辑,同时加个配置选项以便回退到旧版的表达式解析逻辑。

背景

现有的表达式变量提取相当地不可靠,有时出现一些意外的情况
2d5f053ba3ad752ab3bb339602218255

并且有时候我确实需要在 setVar 表达式里判断某个变量是否存在,但是变量有字符串回退的逻辑,这使得变量永远不为空。

更改

移除变量名字符串回退

在表达式中访问不存在的变量,不会回退变量名字符串,而是直接报编译错误,并且不设置游戏变量。

变量名与表达式提取

以 setVar sentenceContent 的第一个等号为分隔,前为变量名,后为表达式。

变量名不作格式要求,甚至可以带空格,用中文,用非标准命名格式,但是取变量需要特殊处理

setVar: 中文变量名 = "测试";
setVar: 等于测试 = this["中文变量名"] == "测试";
:中文变量名: {中文变量名} 等于测试: {等于测试};

setVar: 0foo = "测试";
setVar: 等于测试2 = this["0foo"] == "测试";
:0foo: {0foo} 等于测试2: {等于测试2}

如果变量名是标准命名格式,则可以直接用变量名获取值

setVar:someVar="fd";
jumpLabel:match -when=someVar=="fd";
变量不相等;
jumpLabel:matchEnd;
label:match;
变量相等;
label:matchEnd;

setVar:debugSomeVar=someVar=="fd";
someVar {someVar} debugSomeVar {debugSomeVar};

严格拒绝不支持的返回类型

表达式求值返回类型只允许是 string, number, boolean,其他值均会被拒绝存入变量。

兼容性

配置里新加了一个参数 Legacy_Expression_Parser

Legacy_Expression_Parser:true; 时,就是原来的解析逻辑。不写该配置项,或者该配置项不为 true 时,就是用新的解析逻辑。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new expression evaluation system using angular-expressions as an alternative to the legacy parser, managed by a legacyExpressionParser flag. The new evaluation logic is integrated into script execution and variable assignment. Feedback is provided to optimize the performance of the new evaluateExpression function by caching compiled expressions and using a Proxy to avoid costly object copying during scope resolution.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +46 to +71
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

性能优化建议:缓存编译结果与避免对象拷贝

在当前实现中,每次调用 evaluateExpression 都会执行以下操作:

  1. 调用 expression.compile(expressionString) 重新编译表达式。编译是一个涉及词法分析、语法分析和生成 AST 的 CPU 密集型操作。
  2. 使用 Object.assign 浅拷贝 builtinFunctionsglobalVarstageVar。在大型游戏中,变量(尤其是全局变量和舞台变量)的数量可能非常多,频繁的对象创建和属性拷贝会带来显著的 CPU 开销和垃圾回收(GC)压力。

解决方案:

  1. 引入编译缓存:使用 Map 缓存已编译的表达式函数。由于 angular-expressions 编译出的函数是无状态的,因此可以安全地复用。
  2. 使用 Proxy 代理作用域查找:通过 Proxy 动态拦截属性读取,避免每次求值时都进行对象拷贝,从而实现零拷贝(Zero-copy)查找。

以下是优化后的代码建议:

const expressionCache = new Map<string, (scope: any) => any>();

export function evaluateExpression(expressionString: string): string | number | boolean | undefined {
  try {
    let evaluate = expressionCache.get(expressionString);
    if (!evaluate) {
      evaluate = expression.compile(expressionString);
      expressionCache.set(expressionString, evaluate);
    }

    const stageState = stageStateManager.getCalculationStageState();
    const stageVar = stageState.GameVar;
    const userData = webgalStore.getState().userData;
    const globalVar = userData.globalGameVar;

    const scope = new Proxy({} as any, {
      get(_, prop) {
        if (typeof prop !== 'string') return undefined;
        if (prop === '$stage') return stageState;
        if (prop === '$userData') return userData;
        if (stageVar && Object.prototype.hasOwnProperty.call(stageVar, prop)) {
          return stageVar[prop];
        }
        if (globalVar && Object.prototype.hasOwnProperty.call(globalVar, prop)) {
          return globalVar[prop];
        }
        if (Object.prototype.hasOwnProperty.call(builtinFunctions, prop)) {
          return (builtinFunctions as any)[prop];
        }
        return undefined;
      },
      has(_, prop) {
        if (typeof prop !== 'string') return false;
        return (
          prop === '$stage' ||
          prop === '$userData' ||
          (stageVar && Object.prototype.hasOwnProperty.call(stageVar, prop)) ||
          (globalVar && Object.prototype.hasOwnProperty.call(globalVar, prop)) ||
          Object.prototype.hasOwnProperty.call(builtinFunctions, prop)
        );
      },
    });

    const result = evaluate(scope);
    switch (typeof result) {
      case 'string':
      case 'number':
      case 'boolean':
        return result;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant