@@ -39,13 +39,21 @@ export const setVar = (sentence: ISentence): IPerform => {
39
39
// 将变量替换为变量的值,然后合成表达式字符串
40
40
const valExp2 = valExpArr
41
41
. map ( ( e ) => {
42
- if ( e . match ( / \$ ? [ . a - z A - Z ] / ) ) {
43
- return String ( getValueFromState ( e . trim ( ) ) ) ;
44
- } else return e ;
42
+ if ( ! e . trim ( ) . match ( / ^ [ a - z A - Z _ $ ] [ a - z A - Z 0 - 9 _ . ] * $ / ) ) {
43
+ // 检查是否是变量名,不是就返回本身
44
+ return e ;
45
+ }
46
+ const _r = getValueFromStateElseKey ( e . trim ( ) ) ;
47
+ return typeof _r === 'string' ? `'${ _r } '` : _r ;
45
48
} )
46
49
. reduce ( ( pre , curr ) => pre + curr , '' ) ;
47
- const exp = compile ( valExp2 ) ;
48
- const result = exp ( ) ;
50
+ let result = '' ;
51
+ try {
52
+ const exp = compile ( valExp2 ) ;
53
+ result = exp ( ) ;
54
+ } catch ( e ) {
55
+ logger . error ( 'expression compile error' , e ) ;
56
+ }
49
57
webgalStore . dispatch ( targetReducerFunction ( { key, value : result } ) ) ;
50
58
} else if ( valExp . match ( / t r u e | f a l s e / ) ) {
51
59
if ( valExp . match ( / t r u e / ) ) {
@@ -54,11 +62,13 @@ export const setVar = (sentence: ISentence): IPerform => {
54
62
if ( valExp . match ( / f a l s e / ) ) {
55
63
webgalStore . dispatch ( targetReducerFunction ( { key, value : false } ) ) ;
56
64
}
65
+ } else if ( valExp . length === 0 ) {
66
+ webgalStore . dispatch ( targetReducerFunction ( { key, value : '' } ) ) ;
57
67
} else {
58
68
if ( ! isNaN ( Number ( valExp ) ) ) {
59
69
webgalStore . dispatch ( targetReducerFunction ( { key, value : Number ( valExp ) } ) ) ;
60
70
} else {
61
- webgalStore . dispatch ( targetReducerFunction ( { key, value : valExp } ) ) ;
71
+ webgalStore . dispatch ( targetReducerFunction ( { key, value : getValueFromStateElseKey ( valExp ) } ) ) ;
62
72
}
63
73
}
64
74
if ( setGlobal ) {
@@ -79,10 +89,13 @@ export const setVar = (sentence: ISentence): IPerform => {
79
89
} ;
80
90
} ;
81
91
82
- type BaseVal = string | number | boolean ;
92
+ type BaseVal = string | number | boolean | undefined ;
83
93
94
+ /**
95
+ * 取不到时返回 undefined
96
+ */
84
97
export function getValueFromState ( key : string ) {
85
- let ret : any = 0 ;
98
+ let ret : any ;
86
99
const stage = webgalStore . getState ( ) . stage ;
87
100
const userData = webgalStore . getState ( ) . userData ;
88
101
const _Merge = { stage, userData } ; // 不要直接合并到一起,防止可能的键冲突
@@ -92,7 +105,19 @@ export function getValueFromState(key: string) {
92
105
ret = userData . globalGameVar [ key ] ;
93
106
} else if ( key . startsWith ( '$' ) ) {
94
107
const propertyKey = key . replace ( '$' , '' ) ;
95
- ret = get ( _Merge , propertyKey , 0 ) as BaseVal ;
108
+ ret = get ( _Merge , propertyKey , undefined ) as BaseVal ;
96
109
}
97
110
return ret ;
98
111
}
112
+
113
+ /**
114
+ * 取不到时返回 key
115
+ */
116
+ export function getValueFromStateElseKey ( key : string ) {
117
+ const valueFromState = getValueFromState ( key ) ;
118
+ if ( valueFromState === null || valueFromState === undefined ) {
119
+ logger . warn ( 'valueFromState result null, key = ' + key ) ;
120
+ return key ;
121
+ }
122
+ return valueFromState ;
123
+ }
0 commit comments