@cloudpss/expression 0.6.0-alpha.8 → 0.6.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/dist/analyze.d.ts +6 -3
  2. package/dist/analyze.d.ts.map +1 -1
  3. package/dist/analyze.js +13 -33
  4. package/dist/analyze.js.map +1 -1
  5. package/dist/definitions/argument.d.ts +3 -11
  6. package/dist/definitions/argument.d.ts.map +1 -1
  7. package/dist/definitions/constraint.d.ts +2 -2
  8. package/dist/definitions/constraint.d.ts.map +1 -1
  9. package/dist/definitions/constraint.js +1 -1
  10. package/dist/definitions/constraint.js.map +1 -1
  11. package/dist/definitions/parameter-group.js +4 -4
  12. package/dist/definitions/parameter-group.js.map +1 -1
  13. package/dist/definitions/parameter.d.ts +22 -14
  14. package/dist/definitions/parameter.d.ts.map +1 -1
  15. package/dist/definitions/parameter.js +10 -1
  16. package/dist/definitions/parameter.js.map +1 -1
  17. package/dist/definitions/utils.d.ts +28 -0
  18. package/dist/definitions/utils.d.ts.map +1 -0
  19. package/dist/definitions/utils.js +272 -0
  20. package/dist/definitions/utils.js.map +1 -0
  21. package/dist/definitions/variable.js +1 -1
  22. package/dist/definitions/variable.js.map +1 -1
  23. package/dist/definitions.d.ts +1 -2
  24. package/dist/definitions.d.ts.map +1 -1
  25. package/dist/definitions.js +1 -1
  26. package/dist/definitions.js.map +1 -1
  27. package/dist/eval.d.ts +3 -5
  28. package/dist/eval.d.ts.map +1 -1
  29. package/dist/eval.js +12 -20
  30. package/dist/eval.js.map +1 -1
  31. package/dist/expression.d.ts +10 -4
  32. package/dist/expression.d.ts.map +1 -1
  33. package/dist/expression.js +6 -6
  34. package/dist/expression.js.map +1 -1
  35. package/dist/index.d.ts +3 -1
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +1 -0
  38. package/dist/index.js.map +1 -1
  39. package/dist/interface.d.ts +30 -0
  40. package/dist/interface.d.ts.map +1 -0
  41. package/dist/interface.js +6 -0
  42. package/dist/interface.js.map +1 -0
  43. package/dist/main.d.ts +41 -28
  44. package/dist/main.d.ts.map +1 -1
  45. package/dist/main.js +165 -149
  46. package/dist/main.js.map +1 -1
  47. package/dist/migrate.d.ts +3 -2
  48. package/dist/migrate.d.ts.map +1 -1
  49. package/dist/migrate.js +25 -3
  50. package/dist/migrate.js.map +1 -1
  51. package/dist/migrator/access.d.ts.map +1 -1
  52. package/dist/migrator/access.js +65 -30
  53. package/dist/migrator/access.js.map +1 -1
  54. package/dist/migrator/call.d.ts.map +1 -1
  55. package/dist/migrator/call.js +298 -201
  56. package/dist/migrator/call.js.map +1 -1
  57. package/dist/migrator/concat.d.ts.map +1 -1
  58. package/dist/migrator/concat.js +15 -2
  59. package/dist/migrator/concat.js.map +1 -1
  60. package/dist/migrator/function.d.ts +6 -0
  61. package/dist/migrator/function.d.ts.map +1 -0
  62. package/dist/migrator/function.js +25 -0
  63. package/dist/migrator/function.js.map +1 -0
  64. package/dist/migrator/interface.d.ts +3 -1
  65. package/dist/migrator/interface.d.ts.map +1 -1
  66. package/dist/migrator/interface.js.map +1 -1
  67. package/dist/migrator/node.d.ts.map +1 -1
  68. package/dist/migrator/node.js +55 -7
  69. package/dist/migrator/node.js.map +1 -1
  70. package/dist/migrator/operator.d.ts.map +1 -1
  71. package/dist/migrator/operator.js +107 -60
  72. package/dist/migrator/operator.js.map +1 -1
  73. package/dist/migrator/serialize.d.ts +4 -0
  74. package/dist/migrator/serialize.d.ts.map +1 -0
  75. package/dist/migrator/serialize.js +21 -0
  76. package/dist/migrator/serialize.js.map +1 -0
  77. package/dist/migrator/special.d.ts.map +1 -1
  78. package/dist/migrator/special.js +31 -0
  79. package/dist/migrator/special.js.map +1 -1
  80. package/dist/migrator/state.d.ts +2 -2
  81. package/dist/migrator/state.d.ts.map +1 -1
  82. package/dist/migrator/state.js +30 -33
  83. package/dist/migrator/state.js.map +1 -1
  84. package/dist/migrator/symbol.d.ts.map +1 -1
  85. package/dist/migrator/symbol.js +34 -12
  86. package/dist/migrator/symbol.js.map +1 -1
  87. package/dist/migrator/to-type.d.ts.map +1 -1
  88. package/dist/migrator/to-type.js +21 -4
  89. package/dist/migrator/to-type.js.map +1 -1
  90. package/dist/migrator/utils.d.ts +6 -0
  91. package/dist/migrator/utils.d.ts.map +1 -1
  92. package/dist/migrator/utils.js +49 -1
  93. package/dist/migrator/utils.js.map +1 -1
  94. package/dist/parser.d.ts +2 -2
  95. package/dist/parser.d.ts.map +1 -1
  96. package/dist/parser.js +27 -8
  97. package/dist/parser.js.map +1 -1
  98. package/dist/re-exports.d.ts +4 -0
  99. package/dist/re-exports.d.ts.map +1 -0
  100. package/dist/re-exports.js +3 -0
  101. package/dist/re-exports.js.map +1 -0
  102. package/dist/scope.d.ts +17 -18
  103. package/dist/scope.d.ts.map +1 -1
  104. package/dist/scope.js +84 -53
  105. package/dist/scope.js.map +1 -1
  106. package/dist/type.d.ts +22 -10
  107. package/dist/type.d.ts.map +1 -1
  108. package/dist/type.js +21 -24
  109. package/dist/type.js.map +1 -1
  110. package/package.json +8 -5
  111. package/src/analyze.ts +20 -39
  112. package/src/definitions/argument.ts +3 -13
  113. package/src/definitions/constraint.ts +3 -3
  114. package/src/definitions/parameter-group.ts +4 -4
  115. package/src/definitions/parameter.ts +47 -24
  116. package/src/definitions/utils.ts +288 -0
  117. package/src/definitions/variable.ts +1 -1
  118. package/src/definitions.ts +1 -28
  119. package/src/eval.ts +16 -25
  120. package/src/expression.ts +16 -8
  121. package/src/index.ts +3 -1
  122. package/src/interface.ts +35 -0
  123. package/src/main.ts +232 -200
  124. package/src/migrate.ts +30 -6
  125. package/src/migrator/access.ts +68 -38
  126. package/src/migrator/call.ts +287 -190
  127. package/src/migrator/concat.ts +15 -2
  128. package/src/migrator/function.ts +27 -0
  129. package/src/migrator/interface.ts +3 -1
  130. package/src/migrator/node.ts +56 -6
  131. package/src/migrator/operator.ts +110 -64
  132. package/src/migrator/serialize.ts +21 -0
  133. package/src/migrator/special.ts +31 -0
  134. package/src/migrator/state.ts +30 -33
  135. package/src/migrator/symbol.ts +33 -12
  136. package/src/migrator/to-type.ts +23 -4
  137. package/src/migrator/utils.ts +49 -1
  138. package/src/parser.ts +33 -8
  139. package/src/re-exports.ts +47 -0
  140. package/src/scope.ts +101 -65
  141. package/src/type.ts +40 -25
  142. package/tests/analyze.ts +45 -6
  143. package/tests/compile.ts +65 -0
  144. package/tests/condition.ts +33 -17
  145. package/tests/definition.ts +237 -18
  146. package/tests/eval-complex.ts +19 -11
  147. package/tests/eval.ts +59 -12
  148. package/tests/import.ts +21 -7
  149. package/tests/main.ts +58 -0
  150. package/tests/migrate.ts +317 -0
  151. package/tests/scope.ts +3 -3
  152. package/tests/template.ts +36 -0
  153. package/dist/context.d.ts +0 -41
  154. package/dist/context.d.ts.map +0 -1
  155. package/dist/context.js +0 -18
  156. package/dist/context.js.map +0 -1
  157. package/src/context.ts +0 -54
package/src/main.ts CHANGED
@@ -1,211 +1,297 @@
1
- import type { LiteralUnion, SetFieldType } from 'type-fest';
2
- import { isVmArray, wrapToVmValue, type VmValue } from '@mirascript/mirascript';
3
- import { lib } from '@mirascript/mirascript/subtle';
4
- import { klona } from 'klona';
5
- import { Context, type Options } from './context.js';
1
+ import { isVmFunction, wrapToVmValue, type VmValue } from '@mirascript/mirascript';
2
+ import { serialize } from '@mirascript/mirascript/subtle';
3
+ import { defaultOptions, type Logger, type Options } from './interface.js';
6
4
  import { parse } from './parser.js';
7
- import { analyze } from './analyze.js';
5
+ import { analyze, type GlobalReferenceChain } from './analyze.js';
8
6
  import {
9
7
  type CompiledExpression,
10
- Expression,
8
+ type Expression,
9
+ type ExpressionFunction,
11
10
  type ExpressionOrValue,
11
+ type ExpressionSource,
12
12
  ExpressionTag,
13
13
  isExpression,
14
14
  } from './expression.js';
15
15
  import { evaluate } from './eval.js';
16
16
  import { TypeInfo } from './type.js';
17
17
  import { Scope } from './scope.js';
18
- import type {
19
- ArgumentMap,
20
- ArgumentValue,
21
- Choice,
22
- ChoiceArgumentValue,
23
- ConditionExpression,
24
- Parameter,
25
- ParameterFunction,
26
- ParameterGroup,
27
- ParameterType,
18
+ import {
19
+ argumentConverter,
20
+ choiceParameterType,
21
+ fillArgumentMap,
22
+ type ArgumentMap,
23
+ type ArgumentValue,
24
+ type Choice,
25
+ type ChoiceParameterType,
26
+ type ConditionExpression,
27
+ type Parameter,
28
+ type ParameterGroup,
29
+ type ParameterMap,
28
30
  } from './definitions.js';
29
31
  const { isArray } = Array;
30
- const { to_number, to_string } = lib;
32
+ const { defineProperty } = Object;
33
+
34
+ /** 选项类型 */
35
+ export type EvaluatedChoices<T extends ChoiceParameterType> = {
36
+ [K in T]: ParameterMap[K] extends { choices?: infer C } ? (C extends readonly unknown[] ? C : never) : never;
37
+ }[T];
31
38
 
32
39
  /**
33
40
  * 表达式求值
34
41
  */
35
42
  export class Evaluator {
36
- constructor(options?: Partial<Options>) {
37
- this.context = new Context(options);
38
- }
39
- /** 选项 */
40
- get options(): Options {
41
- return this.context.options;
43
+ constructor(options?: Partial<Options> | null) {
44
+ this.options = options ?? defaultOptions;
42
45
  }
43
46
 
44
- /** 上下文 */
45
- private readonly context: Context;
47
+ /** Logger */
48
+ get logger(): Logger {
49
+ return this.options.logger ?? console;
50
+ }
51
+ /** 选项 */
52
+ readonly options: Readonly<Partial<Options>>;
46
53
 
47
54
  /** 默认执行环境 */
48
- private readonly scope = new Scope(undefined, false, '<evaluator>');
55
+ private readonly scope = new Scope(null, true, '<evaluator>');
49
56
 
50
- /** 导入到执行环境 */
57
+ private readonly __imported = new Map<string, VmValue>();
58
+ /** 执行环境 */
59
+ get imported(): ReadonlyMap<string, VmValue> {
60
+ return this.__imported;
61
+ }
62
+ /**
63
+ * 导入到执行环境
64
+ *
65
+ * 使用 `undefined` 删除已导入的变量
66
+ */
51
67
  import(values: Record<string, unknown>): void {
52
68
  for (const [key, value] of Object.entries(values)) {
53
69
  if (value === undefined) {
54
- this.context.imported.delete(key);
70
+ this.__imported.delete(key);
55
71
  } else {
56
- this.context.imported.set(key, wrapToVmValue(value, null));
72
+ this.__imported.set(key, wrapToVmValue(value, null));
57
73
  }
58
74
  }
59
75
  }
60
76
 
61
- /**
62
- * 编译表达式
63
- */
77
+ /** 编译表达式 */
64
78
  compile<T extends VmValue>(expression: Expression<T>, throws = true): CompiledExpression<T> {
65
- const value = parse(this.context, expression.source, throws);
79
+ const value = parse(this, expression.source, throws, false);
66
80
  const tag = expression[ExpressionTag];
67
- const type = tag ? TypeInfo.parse(tag) : null;
68
- const compiled = ((scope, defaults) => {
69
- const ret = evaluate(this.context, value, type, scope, defaults);
70
- return ret;
71
- }) as CompiledExpression<T>;
72
- Object.defineProperty(compiled, ExpressionTag, { value: type, enumerable: true });
73
- Object.defineProperty(compiled, 'source', { value: value.source, enumerable: true });
74
- return compiled;
81
+ const type = tag ? TypeInfo.parse(tag) : '';
82
+ let compiled: ExpressionFunction<T>;
83
+ if (!type) {
84
+ compiled = (scope, evaluator) => evaluate<T>(evaluator, value, scope);
85
+ } else {
86
+ const to = TypeInfo.converter(type);
87
+ compiled = (scope, evaluator) => {
88
+ const ret = evaluate<T>(evaluator, value, scope);
89
+ if (ret == null) return null;
90
+ return to(ret) as T;
91
+ };
92
+ }
93
+ defineProperty(compiled, ExpressionTag, { value: type, enumerable: true });
94
+ defineProperty(compiled, 'source', { value: value.source, enumerable: true });
95
+ return compiled as CompiledExpression<T>;
75
96
  }
76
97
 
77
- /**
78
- * 求值
79
- */
80
- evaluate<T extends VmValue>(expression: ExpressionOrValue<T> | undefined, scope?: Scope): T | undefined;
81
- /**
82
- * 求值
83
- */
84
- evaluate<T extends VmValue, D = T>(
85
- expression: ExpressionOrValue<T> | undefined,
86
- scope: Scope | undefined,
98
+ /** 执行 MiraScript 模板 */
99
+ template(template: string | null | undefined, scope: Scope | undefined): string {
100
+ if (template == null) return '';
101
+ const throws = scope?.throws ?? true;
102
+ const value = parse(this, template, throws, true);
103
+ const result = evaluate<string>(this, value, scope ?? this.scope) ?? template;
104
+ if (typeof result == 'string') return result;
105
+ return TypeInfo.toString(result);
106
+ }
107
+
108
+ /** 求值 */
109
+ evaluate<T extends VmValue>(
110
+ expression: ExpressionOrValue<T> | ExpressionFunction<T> | null | undefined,
111
+ scope?: Scope | null,
112
+ ): T | null;
113
+ /** 求值 */
114
+ evaluate<T extends VmValue, const D = NonNullable<T>>(
115
+ expression: ExpressionOrValue<T> | ExpressionFunction<T> | null | undefined,
116
+ scope: Scope | null | undefined,
87
117
  defaults: D,
88
- ): T | D;
89
- /**
90
- * 求值
91
- */
92
- evaluate<T extends VmValue, D = T>(
93
- expression: ExpressionOrValue<T> | undefined,
94
- scope: Scope | undefined,
95
- defaults?: D,
96
- ): T | D | undefined {
118
+ ): NonNullable<T> | D;
119
+ /** 求值 */
120
+ evaluate<T>(expression: ExpressionFunction<T> | T | null | undefined, scope?: Scope | null): T | null;
121
+ /** 求值 */
122
+ evaluate<T, const D = NonNullable<T>>(
123
+ expression: ExpressionFunction<T> | T | null | undefined,
124
+ scope: Scope | null | undefined,
125
+ defaults: D,
126
+ ): NonNullable<T> | D;
127
+
128
+ /** 求值 */
129
+ evaluate<T extends VmValue, const D = NonNullable<T>>(
130
+ expression: ExpressionOrValue<T> | ExpressionFunction<T> | null | undefined,
131
+ scope: Scope | null | undefined,
132
+ defaults: D | null = null,
133
+ ): T | D | null {
97
134
  if (expression == null) return defaults;
98
- if (!isExpression(expression)) return expression;
99
- const throws = scope?.throws ?? true;
135
+
136
+ if (typeof expression == 'function' && !isVmFunction(expression)) {
137
+ try {
138
+ const ret = expression(scope ?? this.scope, this);
139
+ if (ret == null) return defaults;
140
+ return ret;
141
+ } catch (ex) {
142
+ if (scope?.throws ?? true) {
143
+ throw ex;
144
+ }
145
+ return defaults;
146
+ }
147
+ }
148
+
149
+ if (!isExpression<T>(expression)) {
150
+ return expression;
151
+ }
152
+
153
+ const result = this.evaluateSource(expression.source, scope);
154
+ if (result == null) return defaults;
155
+
100
156
  const tag = expression[ExpressionTag];
101
- const type = tag ? TypeInfo.parse(tag) : null;
102
- const value = parse(this.context, expression.source, throws);
103
- const result = evaluate<T, D | undefined>(this.context, value, type, scope ?? this.scope, defaults);
157
+ if (!tag) return result;
158
+ return TypeInfo.to(result, tag) as T;
159
+ }
160
+
161
+ /** 求值 */
162
+ evaluateSource<T extends VmValue>(
163
+ expression: ExpressionSource<T> | T | null | undefined,
164
+ scope: Scope | null | undefined,
165
+ ): T | null;
166
+ /** 求值 */
167
+ evaluateSource<T extends VmValue, const D = NonNullable<T>>(
168
+ expression: ExpressionSource<T> | T | null | undefined,
169
+ scope: Scope | null | undefined,
170
+ defaults: D,
171
+ ): NonNullable<T> | D;
172
+
173
+ /** 求值 */
174
+ evaluateSource<T extends VmValue, const D = NonNullable<T>>(
175
+ expression: ExpressionSource<T> | T | null | undefined,
176
+ scope: Scope | null | undefined,
177
+ defaults: D | null = null,
178
+ ): NonNullable<T> | D | null {
179
+ if (expression == null) return defaults;
180
+ if (typeof expression != 'string') return expression;
181
+
182
+ scope ??= this.scope;
183
+ const { throws } = scope;
184
+ const value = parse(this, expression, throws, false);
185
+ const result = evaluate<T>(this, value, scope);
186
+ if (result == null) return defaults;
187
+
104
188
  return result;
105
189
  }
106
190
 
107
191
  /** 求条件 */
108
- evaluateCondition(condition: ConditionExpression | undefined, scope: Scope | undefined, defaults = true): boolean {
192
+ evaluateCondition(
193
+ condition: ConditionExpression | null | undefined,
194
+ scope: Scope | null | undefined,
195
+ defaults = true,
196
+ ): boolean {
197
+ if (condition == null || condition === '') return defaults;
109
198
  if (typeof condition == 'boolean') return condition;
110
- if (condition == null) return defaults;
111
- condition = (
112
- typeof condition == 'string' ? condition : lib.to_string(condition)
113
- ).trim() as ConditionExpression & string;
114
- if (condition === '') return defaults;
115
199
  if (condition === 'true') return true;
116
200
  if (condition === 'false') return false;
117
201
  try {
118
- return this.evaluate(Expression(condition, 'boolean'), scope, defaults);
202
+ const result = this.evaluateSource(condition, scope, defaults);
203
+ if (typeof result == 'boolean') return result;
204
+ throw new Error(`Condition evaluates to \`${serialize(result)}\``);
119
205
  } catch (ex) {
120
206
  if (scope?.throws) {
121
207
  throw ex;
122
208
  }
123
- this.options.logger.warn(`Invalid condition`, ex);
209
+ this.logger.warn(`Invalid condition`, ex);
124
210
  return defaults;
125
211
  }
126
212
  }
127
213
 
128
214
  /** 求选项列表 */
129
- evaluateChoices<V extends ChoiceArgumentValue>(
130
- choices: ExpressionOrValue<Array<Choice<V>>> | ParameterFunction<Array<Choice<V>>> | undefined,
131
- scope: Scope | undefined,
132
- ): ReadonlyArray<SetFieldType<Choice<V>, 'key', V>> {
133
- if (choices == null) return [];
134
- const result =
135
- typeof choices == 'function' ? choices(scope ?? this.scope, this) : this.evaluate(choices, scope, []);
136
- if (!isArray(result)) {
137
- if (scope?.throws) {
138
- throw new Error(`Choices is not an array`);
215
+ evaluateChoices<T extends ChoiceParameterType>(
216
+ definition: Pick<ParameterMap[T], 'type' | 'choices' | 'value'>,
217
+ scope: Scope | null | undefined,
218
+ ): EvaluatedChoices<T> {
219
+ const ret: Choice[] = [];
220
+ const { type, choices } = definition;
221
+ const isLogical = type === 'logical';
222
+ const choiceType = choiceParameterType(definition);
223
+ const fallbackKeys = !isLogical
224
+ ? []
225
+ : choiceType === 'boolean'
226
+ ? [false, true]
227
+ : choiceType === 'number'
228
+ ? [0, 1]
229
+ : ['0', '1'];
230
+ if (choices != null) {
231
+ const evaluatedChoices = this.evaluate(choices, scope);
232
+ if (evaluatedChoices == null) {
233
+ // do nothing
234
+ } else if (!isArray(evaluatedChoices as unknown[])) {
235
+ if (scope?.throws ?? true) {
236
+ throw new Error(`Choices is not an array`);
237
+ }
238
+ } else if (evaluatedChoices.length > 0) {
239
+ const to = TypeInfo.converter(choiceType);
240
+ let len = evaluatedChoices.length;
241
+ if (isLogical && len > 2) len = 2;
242
+ for (let i = 0; i < len; i++) {
243
+ try {
244
+ const item = evaluatedChoices[i];
245
+ const key = to(item?.key ?? fallbackKeys[i] ?? i);
246
+ const name = item?.name ?? (isLogical ? '' : String(key));
247
+ const description = item?.description ?? '';
248
+ const condition = item?.condition || undefined;
249
+ ret[i] = { key, name, description, condition };
250
+ } catch (ex) {
251
+ if (scope?.throws ?? true) {
252
+ throw new Error(`Invalid choice value: ${serialize(evaluatedChoices[i])}`, {
253
+ cause: ex,
254
+ });
255
+ }
256
+ ret[i] = { key: to(i, i), name: '', description: '', condition: undefined };
257
+ }
258
+ }
139
259
  }
140
- return [];
141
260
  }
142
- return result.map((item, index): SetFieldType<Choice<V>, 'key', V> => {
143
- if (item == null) {
144
- return { key: index as V, name: String(index), description: '' };
145
- }
146
- if (typeof item == 'string' || typeof item == 'number' || typeof item == 'boolean') {
147
- return { key: item as V, name: String(item), description: '' };
148
- }
149
- return {
150
- key: (item.key ?? index) as V,
151
- name: item.name ?? String(item.key ?? index),
152
- description: item.description ?? '',
153
- condition: item.condition,
154
- };
155
- });
156
- }
157
-
158
- /** 转换参数值的类型 */
159
- toArg<V extends ArgumentValue>(value: VmValue, definition: Parameter): V {
160
- switch (definition.type as LiteralUnion<ParameterType, string>) {
161
- case 'real':
162
- case 'integer':
163
- return to_number(value) as V;
164
- case 'file':
165
- case 'text':
166
- case 'code':
167
- case 'pinLike':
168
- case 'cssColor':
169
- case 'cssBackground':
170
- case 'resourceId':
171
- return to_string(value) as V;
172
-
173
- case 'choice':
174
- case 'logical':
175
- return value as V;
176
-
177
- case 'multiSelect':
178
- case 'table': {
179
- if (isVmArray(value)) {
180
- return value as V;
181
- }
182
- return (value != null ? [value] : []) as ArgumentValue as V;
261
+ if (isLogical) {
262
+ while (ret.length < 2) {
263
+ ret.push({
264
+ key: fallbackKeys[ret.length]!,
265
+ name: '',
266
+ description: '',
267
+ condition: undefined,
268
+ });
183
269
  }
184
- case 'grouped':
185
- case 'record':
186
- default:
187
- return value as V;
188
270
  }
271
+ return ret as EvaluatedChoices<T>;
189
272
  }
190
273
 
191
274
  /** 求参数值 */
192
275
  evaluateArg<V extends ArgumentValue>(
193
- expression: ExpressionOrValue<V> | undefined,
194
- scope: Scope | undefined,
276
+ expression: ExpressionOrValue<V> | null | undefined,
277
+ scope: Scope | null | undefined,
195
278
  definition: Parameter,
196
279
  ): V {
197
- const result =
198
- this.evaluate<V>(expression ?? (definition.value as ExpressionOrValue<V>), scope) ??
199
- (definition.value as V);
200
- return this.toArg<V>(result, definition);
280
+ // 默认值有可能为表达式,因此需要先填充再求值
281
+ expression ??= definition.value as ExpressionOrValue<V>;
282
+ // 求值失败时返回默认值
283
+ const result = this.evaluate<V>(expression, scope, definition.value as V);
284
+ const converter = argumentConverter(definition);
285
+ // 转换失败时返回转换前的值
286
+ return converter(result, result) as V;
201
287
  }
202
288
  /** 求参数表的所有参数值 */
203
289
  evaluateArgs<T extends Record<string, ArgumentValue>>(
204
- args: ArgumentMap<T> | undefined,
205
- scope: Scope | undefined,
290
+ args: ArgumentMap<T> | null | undefined,
291
+ scope: Scope | null | undefined,
206
292
  definition: ReadonlyArray<Pick<ParameterGroup, 'items'>>,
207
293
  ): T {
208
- const a = this.defaultArgs(definition, args);
294
+ const a = fillArgumentMap(definition, args);
209
295
  const ret: Record<string, ArgumentValue> = {};
210
296
  for (const group of definition) {
211
297
  for (const param of group.items) {
@@ -217,63 +303,6 @@ export class Evaluator {
217
303
  }
218
304
  return ret as T;
219
305
  }
220
- /** 获取参数默认值,修复现有的参数,并填充缺失的参数 */
221
- defaultArgs<T extends Record<string, ArgumentValue>>(
222
- definition: ReadonlyArray<Pick<ParameterGroup, 'items'>> | undefined,
223
- args: ArgumentMap<T> = {} as ArgumentMap<T>,
224
- ): ArgumentMap<T> {
225
- if (definition == null || !isArray(definition as unknown[])) {
226
- return args;
227
- }
228
- /** 参数键的类型 */
229
- type K = keyof T & string;
230
- /** 参数值的类型 */
231
- type V = NonNullable<T[K]>;
232
- const keys = new Set(Object.keys(args) as K[]);
233
-
234
- // 根据定义填充或修复参数
235
- for (const group of definition) {
236
- for (const param of group.items) {
237
- const key = param.key as K;
238
- if (!key) continue;
239
- const currentValue = keys.has(key) ? args[key] : undefined;
240
- if (currentValue == null) {
241
- // 使用默认值填充
242
- const defaultValue = param.value;
243
- if (isExpression(defaultValue)) {
244
- args[key] = { [ExpressionTag]: '', source: defaultValue.source } as Expression<V>;
245
- } else {
246
- const value =
247
- defaultValue != null && typeof defaultValue == 'object'
248
- ? klona(defaultValue)
249
- : defaultValue;
250
- args[key] = this.toArg<V>(value, param);
251
- }
252
- } else {
253
- // 已存在的参数值
254
- if (isExpression(currentValue)) {
255
- // 保留表达式
256
- } else {
257
- // 修复现有的参数值
258
- const castedValue = this.toArg<V>(currentValue, param);
259
- if (castedValue != null && castedValue !== currentValue) {
260
- args[key] = castedValue;
261
- }
262
- }
263
- }
264
- keys.delete(key);
265
- }
266
- }
267
-
268
- // 删除多余的参数
269
- for (const key of keys) {
270
- // 保留特殊参数和私有字段
271
- if (key.startsWith('_') || key.startsWith('$') || key.startsWith('@')) continue;
272
- delete args[key];
273
- }
274
- return args;
275
- }
276
-
277
306
  /** 解析表达式 */
278
307
  validate<T extends VmValue>(expression: Expression<T>): void {
279
308
  if (!isExpression(expression) || !expression.source) {
@@ -281,15 +310,18 @@ export class Evaluator {
281
310
  }
282
311
  const tag = expression[ExpressionTag];
283
312
  if (tag) TypeInfo.parse(tag);
284
- parse(this.context, expression.source, true);
313
+ parse(this, expression.source, true, false);
285
314
  }
286
315
 
287
316
  /** 分析表达式 */
288
- analyze<T extends VmValue>(expression: Expression<T>): string[][] {
317
+ analyze<T extends VmValue>(expression: Expression<T>, scope?: Scope | null): readonly GlobalReferenceChain[] {
289
318
  if (!isExpression(expression) || !expression.source) {
290
319
  throw new Error(`${expression.source || String(expression)} is not a valid expression`);
291
320
  }
292
- const exp = parse(this.context, expression.source, true);
293
- return analyze(this.context, exp);
321
+ scope ??= this.scope;
322
+ const exp = parse(this, expression.source, scope.throws, false);
323
+ // 如果 throws=true,则 parse 会抛出错误,此处有 error 一定是 throws=false 的情况
324
+ if (exp.error) return [];
325
+ return analyze(exp, this, scope);
294
326
  }
295
327
  }
package/src/migrate.ts CHANGED
@@ -1,12 +1,16 @@
1
- import type { VmContext, VmValue } from '@mirascript/mirascript';
2
- import { BaseState } from './migrator/state.js';
1
+ import type { VmValue } from '@mirascript/mirascript';
2
+ import type { Scope } from './scope.js';
3
3
  import type { ExpressionSource } from './expression.js';
4
+ import { BaseState } from './migrator/state.js';
5
+ import { Evaluator } from './main.js';
6
+ import { DefaultVmContext } from '@mirascript/mirascript/subtle';
4
7
 
8
+ const evaluator = new Evaluator();
5
9
  /** MathJs 表达式转为 MiraScript 表达式 */
6
10
  export function migrateMathJs<const T extends VmValue>(
7
11
  expr: ExpressionSource<T>,
8
12
  condition: boolean,
9
- context?: VmContext,
13
+ context: Scope | undefined,
10
14
  ): ExpressionSource<T> {
11
15
  // 检查输入
12
16
  if (!expr) return '' as ExpressionSource<T>;
@@ -15,11 +19,31 @@ export function migrateMathJs<const T extends VmValue>(
15
19
  if (expr === 'null') return 'nil' as ExpressionSource<T>;
16
20
  if (expr === 'true') return 'true' as ExpressionSource<T>;
17
21
  if (expr === 'false') return 'false' as ExpressionSource<T>;
22
+ if (condition) {
23
+ // 兼容旧的 evaluateCondition 的宽容处理
24
+ if (expr === 'True' || expr === 'TRUE' || expr === '1' || expr === '"1"' || expr === "'1'")
25
+ return 'true' as ExpressionSource<T>;
26
+ if (
27
+ expr === 'False' ||
28
+ expr === 'FALSE' ||
29
+ expr === '0' ||
30
+ expr === '"0"' ||
31
+ expr === "'0'" ||
32
+ expr === '""' ||
33
+ expr === "''"
34
+ )
35
+ return 'false' as ExpressionSource<T>;
36
+ }
18
37
  if (/^[-+]?(\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/.test(expr)) {
19
38
  return expr;
20
39
  }
21
40
 
22
- const state = new BaseState(expr, condition, context);
23
- state.migrate();
24
- return state.result() as ExpressionSource<T>;
41
+ const dispose = context?.reset(evaluator);
42
+ try {
43
+ const state = new BaseState(expr, condition, context ? context.proxy : DefaultVmContext);
44
+ state.migrate();
45
+ return state.result() as ExpressionSource<T>;
46
+ } finally {
47
+ dispose?.();
48
+ }
25
49
  }