Skip to content

Commit cf75ff1

Browse files
authored
Fix parsing of ES6 scripts (playcanvas#1958)
1 parent 31e6257 commit cf75ff1

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/script/script-type.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Object.assign(pc, function () {
2+
3+
var funcNameRegex = new RegExp('^\\s*function(?:\\s|\\s*\\/\\*.*\\*\\/\\s*)+([^\\(\\s\\/]*)\\s*');
4+
25
/**
36
* @class
47
* @name pc.ScriptType
@@ -76,6 +79,14 @@ Object.assign(pc, function () {
7679
*/
7780
ScriptType.__name = null; // Will be assigned when calling createScript or registerScript.
7881

82+
ScriptType.__getScriptName = function (constructorFn) {
83+
if (typeof constructorFn !== 'function') return undefined;
84+
if ('name' in Function.prototype) return constructorFn.name;
85+
if (constructorFn === Function || constructorFn === Function.prototype.constructor) return 'Function';
86+
var match = ("" + constructorFn).match(funcNameRegex);
87+
return match ? match[1] : undefined;
88+
};
89+
7990
/**
8091
* @field
8192
* @static

src/script/script.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
Object.assign(pc, function () {
2-
var funcNameRegex = new RegExp('^\\s*function(?:\\s|\\s*\\/\\*.*\\*\\/\\s*)+([^\\(\\s\\/]*)\\s*');
3-
4-
var _getFuncName = function (func) {
5-
if (typeof func !== 'function') return undefined;
6-
if ('name' in Function.prototype) return func.name;
7-
if (func === Function || func === Function.prototype.constructor) return 'Function';
8-
var match = ("" + func).match(funcNameRegex);
9-
return match ? match[1] : undefined;
10-
};
11-
122
/* eslint-disable jsdoc/no-undefined-types */
133
/**
144
* @static
@@ -100,6 +90,9 @@ Object.assign(pc, function () {
10090
*
10191
* // register the class as a script
10292
* pc.registerScript(PlayerController);
93+
*
94+
* // declare script attributes (Must be after pc.registerScript())
95+
* PlayerController.attributes.add('attribute1', {type: 'number'});
10396
*/
10497
/* eslint-enable jsdoc/check-examples */
10598
/* eslint-enable jsdoc/no-undefined-types */
@@ -115,9 +108,9 @@ Object.assign(pc, function () {
115108
throw new Error('script class: \'' + script + '\' must be a constructor function (i.e. class).');
116109

117110
if (!(script.prototype instanceof pc.ScriptType))
118-
throw new Error('script class: \'' + _getFuncName(script) + '\' does not extend pc.ScriptType.');
111+
throw new Error('script class: \'' + pc.ScriptType.__getScriptName(script) + '\' does not extend pc.ScriptType.');
119112

120-
name = name || script.__name || _getFuncName(script);
113+
name = name || script.__name || pc.ScriptType.__getScriptName(script);
121114

122115
if (createScript.reservedScripts[name])
123116
throw new Error('script name: \'' + name + '\' is reserved, please change script name');

0 commit comments

Comments
 (0)