Skip to content

Commit

Permalink
Merge pull request #2271 from rainke/patch-3
Browse files Browse the repository at this point in the history
fix: keep `this` in parse expression function
  • Loading branch information
1ncounter authored Apr 26, 2024
2 parents a4ecdd0 + fc947d2 commit 5d220ce
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/renderer-core/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,22 +221,19 @@ function parseExpression(a: any, b?: any, c = false) {
thisRequired = c;
}
try {
const contextArr = ['"use strict";', 'var __self = arguments[0];'];
contextArr.push('return ');
let tarStr: string;

tarStr = (str.value || '').trim();

// NOTE: use __self replace 'this' in the original function str
// may be wrong in extreme case which contains '__self' already
tarStr = tarStr.replace(/this(\W|$)/g, (_a: any, b: any) => `__self${b}`);
tarStr = contextArr.join('\n') + tarStr;
let code = `"use strict"; function __wrapper(){ return ${tarStr}} return __wrapper.call(arguments[0])`;

// 默认调用顶层窗口的parseObj, 保障new Function的window对象是顶层的window对象
if (inSameDomain() && (window.parent as any).__newFunc) {
return (window.parent as any).__newFunc(tarStr)(self);
return (window.parent as any).__newFunc(code)(self);
}
if (!thisRequired) {
code = `with($scope){${code}}`;
}
const code = `with(${thisRequired ? '{}' : '$scope || {}'}) { ${tarStr} }`;
return new Function('$scope', code)(self);
} catch (err) {
logger.error(`${logScope || ''} parseExpression.error`, err, str, self?.__self ?? self);
Expand Down

0 comments on commit 5d220ce

Please sign in to comment.