Merge pull request #3027 from alibaba/revert-2271-patch-3

Revert "fix: keep `this` in parse expression function"
This commit is contained in:
唐澜 2024-05-16 15:26:26 +08:00 committed by GitHub
commit 6cd7dfb3d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -221,19 +221,22 @@ function parseExpression(a: any, b?: any, c = false) {
thisRequired = c; thisRequired = c;
} }
try { try {
const contextArr = ['"use strict";', 'var __self = arguments[0];'];
contextArr.push('return ');
let tarStr: string; let tarStr: string;
tarStr = (str.value || '').trim(); tarStr = (str.value || '').trim();
let code = `"use strict"; function __wrapper(){ return ${tarStr}} return __wrapper.call(arguments[0])`; // 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;
// 默认调用顶层窗口的parseObj, 保障new Function的window对象是顶层的window对象 // 默认调用顶层窗口的parseObj, 保障new Function的window对象是顶层的window对象
if (inSameDomain() && (window.parent as any).__newFunc) { if (inSameDomain() && (window.parent as any).__newFunc) {
return (window.parent as any).__newFunc(code)(self); return (window.parent as any).__newFunc(tarStr)(self);
}
if (!thisRequired) {
code = `with($scope){${code}}`;
} }
const code = `with(${thisRequired ? '{}' : '$scope || {}'}) { ${tarStr} }`;
return new Function('$scope', code)(self); return new Function('$scope', code)(self);
} catch (err) { } catch (err) {
logger.error(`${logScope || ''} parseExpression.error`, err, str, self?.__self ?? self); logger.error(`${logScope || ''} parseExpression.error`, err, str, self?.__self ?? self);