Skip to content

Commit 63e235c

Browse files
authored
Refactor script reserved name code (playcanvas#2647)
* Refactor script reserved name code. * Fix component * Use Set for reserved names * Fix typo
1 parent e6115bf commit 63e235c

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

src/framework/components/script/component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createScript } from '../../../script/script.js';
1+
import { ScriptAttributes } from '../../../script/script-attributes.js';
22

33
import { Component } from '../component.js';
44
import { Entity } from '../../entity.js';
@@ -970,7 +970,7 @@ Object.defineProperty(ScriptComponent.prototype, 'scripts', {
970970
// attributes
971971
if (typeof value[key].attributes === 'object') {
972972
for (var attr in value[key].attributes) {
973-
if (createScript.reservedAttributes[attr])
973+
if (ScriptAttributes.reservedNames.has(attr))
974974
continue;
975975

976976
if (!script.__attributes.hasOwnProperty(attr)) {

src/script/script-attributes.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import { GraphNode } from '../scene/graph-node.js';
1010

1111
import { Asset } from '../asset/asset.js';
1212

13-
import { createScript } from './script.js';
14-
1513
var components = ['x', 'y', 'z', 'w'];
1614
var vecLookup = [undefined, undefined, Vec2, Vec3, Vec4];
1715

@@ -166,6 +164,12 @@ function ScriptAttributes(scriptType) {
166164
this.index = { };
167165
}
168166

167+
ScriptAttributes.reservedNames = new Set([
168+
'app', 'entity', 'enabled', '_enabled', '_enabledOld', '_destroyed',
169+
'__attributes', '__attributesRaw', '__scriptType', '__executionOrder',
170+
'_callbacks', 'has', 'get', 'on', 'off', 'fire', 'once', 'hasEvent'
171+
]);
172+
169173
/**
170174
* @function
171175
* @name pc.ScriptAttributes#add
@@ -239,7 +243,7 @@ ScriptAttributes.prototype.add = function (name, args) {
239243
console.warn('attribute \'' + name + '\' is already defined for script type \'' + this.scriptType.name + '\'');
240244
// #endif
241245
return;
242-
} else if (createScript.reservedAttributes[name]) {
246+
} else if (ScriptAttributes.reservedNames.has(name)) {
243247
// #ifdef DEBUG
244248
console.warn('attribute \'' + name + '\' is a reserved attribute name');
245249
// #endif

src/script/script.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import { Application } from '../framework/application.js';
66
import { ScriptAttributes } from './script-attributes.js';
77
import { ScriptType } from './script-type.js';
88

9+
const reservedScriptNames = new Set([
10+
'system', 'entity', 'create', 'destroy', 'swap', 'move',
11+
'scripts', '_scripts', '_scriptsIndex', '_scriptsData',
12+
'enabled', '_oldState', 'onEnable', 'onDisable', 'onPostStateChange',
13+
'_onSetEnabled', '_checkState', '_onBeforeRemove',
14+
'_onInitializeAttributes', '_onInitialize', '_onPostInitialize',
15+
'_onUpdate', '_onPostUpdate',
16+
'_callbacks', 'has', 'get', 'on', 'off', 'fire', 'once', 'hasEvent'
17+
]);
18+
919
/* eslint-disable jsdoc/no-undefined-types */
1020
/**
1121
* @static
@@ -47,7 +57,7 @@ function createScript(name, app) {
4757
return null;
4858
}
4959

50-
if (createScript.reservedScripts[name])
60+
if (reservedScriptNames.has(name))
5161
throw new Error('script name: \'' + name + '\' is reserved, please change script name');
5262

5363
var scriptType = function (args) {
@@ -64,6 +74,14 @@ function createScript(name, app) {
6474
return scriptType;
6575
}
6676

77+
// Editor uses this - migrate to ScriptAttributes.reservedNames and delete this
78+
var reservedAttributes = {};
79+
ScriptAttributes.reservedNames.forEach((value, value2, set) => {
80+
reservedAttributes[value] = 1;
81+
});
82+
createScript.reservedAttributes = reservedAttributes;
83+
84+
6785
/* eslint-disable jsdoc/no-undefined-types */
6886
/* eslint-disable jsdoc/check-examples */
6987
/**
@@ -119,7 +137,7 @@ function registerScript(script, name, app) {
119137

120138
name = name || script.__name || ScriptType.__getScriptName(script);
121139

122-
if (createScript.reservedScripts[name])
140+
if (reservedScriptNames.has(name))
123141
throw new Error('script name: \'' + name + '\' is reserved, please change script name');
124142

125143
script.__name = name;
@@ -131,32 +149,4 @@ function registerScript(script, name, app) {
131149
ScriptHandler._push(script);
132150
}
133151

134-
// reserved scripts
135-
createScript.reservedScripts = [
136-
'system', 'entity', 'create', 'destroy', 'swap', 'move',
137-
'scripts', '_scripts', '_scriptsIndex', '_scriptsData',
138-
'enabled', '_oldState', 'onEnable', 'onDisable', 'onPostStateChange',
139-
'_onSetEnabled', '_checkState', '_onBeforeRemove',
140-
'_onInitializeAttributes', '_onInitialize', '_onPostInitialize',
141-
'_onUpdate', '_onPostUpdate',
142-
'_callbacks', 'has', 'get', 'on', 'off', 'fire', 'once', 'hasEvent'
143-
];
144-
var reservedScripts = { };
145-
var i;
146-
for (i = 0; i < createScript.reservedScripts.length; i++)
147-
reservedScripts[createScript.reservedScripts[i]] = 1;
148-
createScript.reservedScripts = reservedScripts;
149-
150-
151-
// reserved script attribute names
152-
createScript.reservedAttributes = [
153-
'app', 'entity', 'enabled', '_enabled', '_enabledOld', '_destroyed',
154-
'__attributes', '__attributesRaw', '__scriptType', '__executionOrder',
155-
'_callbacks', 'has', 'get', 'on', 'off', 'fire', 'once', 'hasEvent'
156-
];
157-
var reservedAttributes = { };
158-
for (i = 0; i < createScript.reservedAttributes.length; i++)
159-
reservedAttributes[createScript.reservedAttributes[i]] = 1;
160-
createScript.reservedAttributes = reservedAttributes;
161-
162152
export { createScript, registerScript };

0 commit comments

Comments
 (0)