Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.8.6] Mark some properties as @dontmangle. #18160

Open
wants to merge 12 commits into
base: v3.8.6
Choose a base branch
from
3 changes: 2 additions & 1 deletion cocos/core/curves/keyframe-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
if (values !== undefined) {
assertIsTrue(Array.isArray(times));
this.setKeyframes(
times.slice(),

Check failure on line 225 in cocos/core/curves/keyframe-curve.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any[]` assigned to a parameter of type `number[]`
values.slice(),
);
} else {
Expand Down Expand Up @@ -281,8 +281,9 @@
}

// Times are always sorted and 1-1 correspond to values.
/** @dontmangle */
protected _times: number[] = [];

/** @dontmangle */
protected _values: TKeyframeValue[] = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

protected member variable will be mangled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The file engine-mangle-config.json supports to control whether to mangle protected properties globally, its default value is false which means protected properties will not be mangled. But if developers set it to true, then all protected properties will be mangled too. The file format is :

{
	"common": {
	    "mangleProtected": false, // <------- here
	    "mangleList": [
	    	"MyClass2",
	    	"MyClass.foo",
	    	"MyClass.getBar"
	    ],
	    "dontMangleList": [
	        "BaseFactory",
	        "Slot",
	        "MyClass.hello"
	    ]
	},
	"web-mobile": {
		"extends": "common",
		"mangleList": [
			"MyClass3",
			"MyClass4.haha",
			"MyClass4.w111uwu"
		],
		"dontMangleList": [
			"MyClass5.skldjfl"
		]
	}
}

So if developer set mangleProtected to true, some properties are set by cc.fastDefine to mark them as serializable, so they should not be mangled. In this case, we should make sure some properties in engine should not be mangled by jsdoc tag @dontmangle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example:

CCClass.fastDefine('cc.AnimationCurve', AnimationCurve, {
    _curve: null, // <---- the protected property _curve should not be mangled.
});

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. If so, then how to determine if a protected variable can be mangled or not? What i mean is how to know if need to add @@dontmangle for protected variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need to review the code to decide. So the mangleProtected is disabled (false) by default.

Copy link
Contributor

Choose a reason for hiding this comment

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

Need to review the code to decide.

Is there a simple rule to determine it? If not, then it will be hard to maintain the codes. And how to make sure it work or not, is there a CI to check it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, we haven't enable CI to run the feature of mangleProperties. I will discuss with QA team after editor is updated.

}

Expand Down
2 changes: 2 additions & 0 deletions cocos/core/data/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@
* @internal
*/
public _objFlags: number = 0;

/** @dontmangle */
protected declare _name: string;

constructor (name = '') {
Expand Down Expand Up @@ -384,7 +386,7 @@

const prototype = CCObject.prototype;
if (EDITOR || TEST) {
js.get(prototype, 'isRealValid', function (this: CCObject) {

Check warning on line 389 in cocos/core/data/object.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
return !(this._objFlags & RealDestroyed);
});

Expand All @@ -396,10 +398,10 @@
js.getset(
prototype,
'objFlags',
function (this: CCObject) {

Check warning on line 401 in cocos/core/data/object.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
return this._objFlags;
},
function (this: CCObject, objFlags: CCObject.Flags) {

Check warning on line 404 in cocos/core/data/object.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
this._objFlags = objFlags;
},
);
Expand All @@ -416,7 +418,7 @@
* TODO: this is a dynamic inject method, should be define in class
* issue: https://github.com/cocos/cocos-engine/issues/14643
*/
(prototype as any).realDestroyInEditor = function (): void {

Check warning on line 421 in cocos/core/data/object.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
if (!(this._objFlags & Destroyed)) {
warnID(5001);
return;
Expand Down
1 change: 1 addition & 0 deletions cocos/core/event/eventify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export interface IEventified {
*/
export function Eventify<TBase> (base: Constructor<TBase>): Constructor<TBase & IEventified> {
class Eventified extends (base as unknown as any) {
/** @dontmangle */
protected _callbackTable = createMap(true);

public once<Callback extends (...any) => void> (type: EventType, callback: Callback, target?: any): Callback {
Expand Down
1 change: 1 addition & 0 deletions cocos/core/geometry/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
* 描述一条曲线,其中每个相邻关键帧采用三次hermite插值计算。
*/
export class AnimationCurve {
/** @dontmangle */
private _curve!: RealCurve;

private static defaultKF: Keyframe[] = [{
Expand Down Expand Up @@ -336,7 +337,7 @@
let mid;
while (right - left > 1) {
mid = Math.floor((left + right) / 2);
if (curve.getKeyframeTime(mid) >= t) {

Check failure on line 340 in cocos/core/geometry/curve.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `number`
right = mid;
} else {
left = mid;
Expand Down
78 changes: 44 additions & 34 deletions cocos/scene-graph/component-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@
}

// remove disabled and not invoked component from array
function stableRemoveInactive (iterator, flagToClear): void {
function stableRemoveInactive (iterator: js.array.MutableForwardIterator<Component>, flagToClear: number): void {
const array = iterator.array;
let next = iterator.i + 1;
while (next < array.length) {
const comp = array[next];
if (comp.node._activeInHierarchy) {
if (comp.node.activeInHierarchy) {
++next;
} else {
iterator.removeAt(next);
Expand All @@ -85,7 +85,7 @@
}
}

export type InvokeFunc = (...args: unknown[]) => void;
export type InvokeFunc = (iterator: js.array.MutableForwardIterator<Component>, dt?: number) => void;

// This class contains some queues used to invoke life-cycle methods by script execution order
export class LifeCycleInvoker {
Expand All @@ -94,27 +94,27 @@
/**
* @engineInternal `_zero` is a protected property, we provide this public property for engine internal usage.
*/
public get zero (): js.array.MutableForwardIterator<any> {
public get zero (): js.array.MutableForwardIterator<Component> {
return this._zero;
}
/**
* @engineInternal `_neg` is a protected property, we provide this public property for engine internal usage.
*/
public get neg (): js.array.MutableForwardIterator<any> {
public get neg (): js.array.MutableForwardIterator<Component> {
return this._neg;
}
/**
* @engineInternal `_pos` is a protected property, we provide this public property for engine internal usage.
*/
public get pos (): js.array.MutableForwardIterator<any> {
public get pos (): js.array.MutableForwardIterator<Component> {
return this._pos;
}
// components which priority === 0 (default)
protected declare _zero: js.array.MutableForwardIterator<any>;
protected declare _zero: js.array.MutableForwardIterator<Component>;
// components which priority < 0
protected declare _neg: js.array.MutableForwardIterator<any>;
protected declare _neg: js.array.MutableForwardIterator<Component>;
// components which priority > 0
protected declare _pos: js.array.MutableForwardIterator<any>;
protected declare _pos: js.array.MutableForwardIterator<Component>;
protected declare _invoke: InvokeFunc;
constructor (invokeFunc: InvokeFunc) {
const Iterator = js.array.MutableForwardIterator;
Expand All @@ -129,8 +129,8 @@
}
}

function compareOrder (a, b): number {
return a.constructor._executionOrder - b.constructor._executionOrder;
function compareOrder (a: Component, b: Component): number {
return (a.constructor as typeof Component)._executionOrder - (b.constructor as typeof Component)._executionOrder;
}

// for onLoad: sort once all components registered, invoke once
Expand Down Expand Up @@ -228,7 +228,11 @@
}

// return function to simply call each component with try catch protection
export function createInvokeImplJit (code: string, useDt?, ensureFlag?): (iterator: any, dt: any) => void {
export function createInvokeImplJit (
code: string,
useDt?: boolean,
ensureFlag?: number,
): (iterator: js.array.MutableForwardIterator<Component>, dt?: number) => void {
// function (it) {
// let a = it.array;
// for (it.i = 0; it.i < a.length; ++it.i) {
Expand All @@ -241,12 +245,18 @@
+ 'var c=a[it.i];'}${
code
}}`;
const fastPath = useDt ? Function('it', 'dt', body) : Function('it', body);
const singleInvoke = Function('c', 'dt', code);
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
const fastPath = (useDt ? Function('it', 'dt', body) : Function('it', body)) as (iterator: js.array.MutableForwardIterator<Component>, dt?: number) => void;

Check warning on line 249 in cocos/scene-graph/component-scheduler.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 160. Maximum allowed is 150
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
const singleInvoke = Function('c', 'dt', code) as (c: Component, dt?: number) => void;
return createInvokeImpl(singleInvoke, fastPath, ensureFlag);
}
export function createInvokeImpl (singleInvoke, fastPath, ensureFlag?): (iterator: any, dt: any) => void {
return (iterator, dt: number): void => {
export function createInvokeImpl (
singleInvoke: (c: Component, dt?: number) => void,
fastPath: (iterator: js.array.MutableForwardIterator<Component>, dt?: number) => void,
ensureFlag?: number,
): (iterator: js.array.MutableForwardIterator<Component>, dt?: number) => void {
return (iterator: js.array.MutableForwardIterator<Component>, dt?: number): void => {
try {
fastPath(iterator, dt);
} catch (e) {
Expand All @@ -273,15 +283,15 @@

const invokeStart = SUPPORT_JIT ? createInvokeImplJit(`c.start();c._objFlags|=${IsStartCalled}`, false, IsStartCalled)
: createInvokeImpl(
(c): void => {
c.start();
(c: Component): void => {
(c as any).start();
c._objFlags |= IsStartCalled;
},
(iterator): void => {
(iterator: js.array.MutableForwardIterator<Component>): void => {
const array = iterator.array;
for (iterator.i = 0; iterator.i < array.length; ++iterator.i) {
const comp = array[iterator.i];
comp.start();
(comp as any).start();
comp._objFlags |= IsStartCalled;
}
},
Expand All @@ -290,51 +300,51 @@

const invokeUpdate = SUPPORT_JIT ? createInvokeImplJit('c.update(dt)', true)
: createInvokeImpl(
(c, dt: number): void => {
c.update(dt);
(c: Component, dt?: number): void => {
(c as any).update(dt);
},
(iterator, dt: number): void => {
(iterator: js.array.MutableForwardIterator<Component>, dt?: number): void => {
const array = iterator.array;
for (iterator.i = 0; iterator.i < array.length; ++iterator.i) {
array[iterator.i].update(dt);
(array[iterator.i] as any).update(dt);
}
},
);

const invokeLateUpdate = SUPPORT_JIT ? createInvokeImplJit('c.lateUpdate(dt)', true)
: createInvokeImpl(
(c, dt: number): void => {
c.lateUpdate(dt);
(c: Component, dt?: number): void => {
(c as any).lateUpdate(dt);
},
(iterator, dt: number): void => {
(iterator: js.array.MutableForwardIterator<Component>, dt?: number): void => {
const array = iterator.array;
for (iterator.i = 0; iterator.i < array.length; ++iterator.i) {
array[iterator.i].lateUpdate(dt);
(array[iterator.i] as any).lateUpdate(dt);
}
},
);

export const invokeOnEnable = EDITOR ? (iterator): void => {
export const invokeOnEnable = EDITOR ? (iterator: js.array.MutableForwardIterator<Component>): void => {
const compScheduler = legacyCC.director._compScheduler;
const array = iterator.array;
for (iterator.i = 0; iterator.i < array.length; ++iterator.i) {
const comp = array[iterator.i];
if (comp._enabled) {
callOnEnableInTryCatch(comp);
const deactivatedDuringOnEnable = !comp.node._activeInHierarchy;
const deactivatedDuringOnEnable = !comp.node.activeInHierarchy;
if (!deactivatedDuringOnEnable) {
compScheduler._onEnabled(comp);
}
}
}
} : (iterator): void => {
} : (iterator: js.array.MutableForwardIterator<Component>): void => {
const compScheduler = legacyCC.director._compScheduler;
const array = iterator.array;
for (iterator.i = 0; iterator.i < array.length; ++iterator.i) {
const comp = array[iterator.i];
if (comp._enabled) {
comp.onEnable();
const deactivatedDuringOnEnable = !comp.node._activeInHierarchy;
(comp as any).onEnable();
const deactivatedDuringOnEnable = !comp.node.activeInHierarchy;
if (!deactivatedDuringOnEnable) {
compScheduler._onEnabled(comp);
}
Expand Down Expand Up @@ -365,7 +375,7 @@
*/
public declare lateUpdateInvoker: ReusableInvoker;
// components deferred to schedule
private _deferredComps: any[] = [];
private _deferredComps: Component[] = [];
private declare _updating: boolean;

constructor () {
Expand Down Expand Up @@ -551,7 +561,7 @@
}

if (EDITOR) {
ComponentScheduler.prototype.enableComp = function (comp, invoker): void {

Check warning on line 564 in cocos/scene-graph/component-scheduler.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
// NOTE: _executeInEditMode is dynamically injected on Editor environment
if (legacyCC.GAME_VIEW || (comp.constructor as any)._executeInEditMode) {
if (!(comp._objFlags & IsOnEnableCalled)) {
Expand All @@ -575,7 +585,7 @@
enableInEditor(comp);
};

ComponentScheduler.prototype.disableComp = function (comp): void {

Check warning on line 588 in cocos/scene-graph/component-scheduler.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
// NOTE: _executeInEditMode is dynamically injected on Editor environment
if (legacyCC.GAME_VIEW || (comp.constructor as any)._executeInEditMode) {
if (comp._objFlags & IsOnEnableCalled) {
Expand Down
10 changes: 10 additions & 0 deletions cocos/scene-graph/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@
* 如果回调任务已调度,那么将不会重复调度它,只会更新时间间隔参数。
* @param callback The callback function of the task
* @param interval The time interval between each invocation
* @param repeat The repeat count of this task, the task will be invoked (repeat + 1) times, use [[macro.REPEAT_FOREVER]] to repeat a task forever

Check warning on line 442 in cocos/scene-graph/component.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 153. Maximum allowed is 150
* @param delay The delay time for the first invocation, Unit: s
* @example
* ```ts
Expand Down Expand Up @@ -525,6 +525,7 @@
* @zh 如果该组件启用,则每帧调用 update。<br/>
* 该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
* @param dt - the delta time in seconds it took to complete the last frame
* @dontmangle
*/
protected update? (dt: number): void;

Expand All @@ -542,6 +543,7 @@
* @zh 如果该组件启用,则每帧调用 LateUpdate。<br/>
* 该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
* @param dt - the delta time in seconds it took to complete the last frame
* @dontmangle
*/
protected lateUpdate? (dt: number): void;

Expand All @@ -562,6 +564,7 @@
* 以避免在每次公有方法调用之前检查是否调用了onLoad。<br/>
* 如果支持脚本优先级,则应删除此方法。
* @private
* @dontmangle
*/
protected __preload? (): void;

Expand All @@ -581,6 +584,7 @@
* @zh
* 当附加到一个激活的节点上或者其节点第一次激活时候调用。onLoad 总是会在任何 start 方法调用前执行,这能用于安排脚本的初始化顺序。<br/>
* 该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
* @dontmangle
*/
protected onLoad? (): void;

Expand All @@ -600,6 +604,7 @@
* @zh
* 如果该组件第一次启用,则在所有组件的 update 之前调用。通常用于需要在所有组件的 onLoad 初始化完毕后执行的逻辑。<br/>
* 该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
* @dontmangle
*/
protected start? (): void;

Expand All @@ -616,6 +621,7 @@
* You can only call its super class method inside it. It should not be called manually elsewhere.
* @zh 当该组件被启用,并且它的节点也激活时。<br/>
* 该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
* @dontmangle
*/
protected onEnable? (): void;

Expand All @@ -632,6 +638,7 @@
* You can only call its super class method inside it. It should not be called manually elsewhere.
* @zh 当该组件被禁用或节点变为无效时调用。<br/>
* 该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
* @dontmangle
*/
protected onDisable? (): void;

Expand All @@ -648,6 +655,7 @@
* You can only call its super class method inside it. It should not be called manually elsewhere.
* @zh 当该组件被销毁时调用<br/>
* 该方法为生命周期方法,父类未必会有实现。并且你只能在该方法内部调用父类的实现,不可在其它地方直接调用该方法。
* @dontmangle
*/
protected onDestroy? (): void;

Expand Down Expand Up @@ -680,6 +688,7 @@
* 以便编辑器的场景视图可以正确地执行点选测试。
* @param out_rect - The rect to store the result bounding rect
* @private
* @dontmangle
*/
protected _getLocalBounds? (out_rect: Rect): void;

Expand Down Expand Up @@ -722,6 +731,7 @@
* 这意味着仅仅指定了默认值的属性将被编辑器重置。
* <br/>
* 此方法仅在编辑器下会被调用。
* @dontmangle
*/
protected onRestore? (): void;
}
Expand Down
Loading
Loading