Skip to content

Commit a4535ed

Browse files
committed
Bring back _class names, since code minification breaks reliance on constructor.name.
Closes paperjs#248.
1 parent dff39df commit a4535ed

39 files changed

Lines changed: 65 additions & 28 deletions

build/minify.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@
1212

1313
# We need to keep dead_code around for now, since the very odd JavaScriptCore
1414
# scope bug fix (nop().nop()) requires it.
15-
# TODO: uglifyjs gets confused about Base and Color constructor naming, so we
16-
# have to tell it to not rename these. It's also not renaming all the local
17-
# references to classes which could yield a lot of size improvements.
18-
uglifyjs ../dist/paper.js -o ../dist/paper-min.js -c unsafe=true,unused=false,dead_code=false,hoist_funs=false -m -r "Base,Color,_$_,$_" -b ascii_only=true,beautify=false --comments /^!/
15+
uglifyjs ../dist/paper.js -o ../dist/paper-min.js -c unsafe=true,unused=false,dead_code=false,hoist_funs=false -m -r "_$_,$_" -b ascii_only=true,beautify=false --comments /^!/

lib/straps.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,6 @@ var Base = new function() {
8383
return _define(obj, name, desc);
8484
};
8585

86-
// Fix Function#name on browsers that do not support it (IE):
87-
if (!(function f() {}).name) {
88-
define(Function.prototype, 'name', {
89-
get: function() {
90-
var name = this.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
91-
// For better performance only parse once, and then cache the
92-
// result through a new accessor for repeated access.
93-
define(this, 'name', { value: name });
94-
return name;
95-
}
96-
});
97-
}
98-
9986
/**
10087
* Private function that injects functions from src into dest, overriding
10188
* (and inherinting from) base.

src/basic/Line.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* @class The Line object represents..
1717
*/
1818
var Line = Base.extend(/** @lends Line# */{
19+
_class: 'Line',
20+
1921
// DOCS: document Line class and constructor
2022
/**
2123
* Creates a Line object.

src/basic/Matrix.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
* matrix multiplication).
3939
*/
4040
var Matrix = Base.extend(/** @lends Matrix# */{
41+
_class: 'Matrix',
42+
4143
/**
4244
* Creates a 2D affine transform.
4345
*

src/basic/Point.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* console.log(point.y); // 5
2525
*/
2626
var Point = Base.extend(/** @lends Point# */{
27+
_class: 'Point',
2728
// Tell Base.read that the Point constructor supports reading with index
2829
_readIndex: true,
2930

src/basic/Rectangle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* rectangular path, it is not an item.
1919
*/
2020
var Rectangle = Base.extend(/** @lends Rectangle# */{
21+
_class: 'Rectangle',
2122
// Tell Base.read that the Rectangle constructor supports reading with index
2223
_readIndex: true,
2324

src/basic/Size.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* console.log(size.height); // 5
2424
*/
2525
var Size = Base.extend(/** @lends Size# */{
26+
_class: 'Size',
2627
// Tell Base.read that the Point constructor supports reading with index
2728
_readIndex: true,
2829

src/core/Base.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Base.inject(/** @lends Base# */{
3636
*/
3737
toString: function() {
3838
return this._id != null
39-
? (this.constructor.name || 'Object') + (this._name
39+
? (this._class || 'Object') + (this._name
4040
? " '" + this._name + "'"
4141
: ' @' + this._id)
4242
: '{ ' + Base.each(this, function(value, key) {
@@ -87,7 +87,7 @@ Base.inject(/** @lends Base# */{
8787
// Override Base.extend() to register named classes in Base.exports,
8888
// for deserialization and injection into PaperScope.
8989
var res = extend.base.apply(this, arguments),
90-
name = res.name;
90+
name = res.prototype._class;
9191
if (name)
9292
Base.exports[name] = res;
9393
return res;
@@ -300,7 +300,7 @@ Base.inject(/** @lends Base# */{
300300
if (!ref) {
301301
this.length++;
302302
var res = create.call(item),
303-
name = item.constructor.name;
303+
name = item._class;
304304
// Also automatically insert class for dictionary
305305
// entries.
306306
if (name && res[0] !== name)
@@ -317,7 +317,7 @@ Base.inject(/** @lends Base# */{
317317
// If we don't serialize to compact form (meaning no type
318318
// identifier), see if _serialize didn't already add the class,
319319
// e.g. for classes that do not support compact form.
320-
var name = obj.constructor.name;
320+
var name = obj._class;
321321
if (name && !compact && !res._compact && res[0] !== name)
322322
res.unshift(name);
323323
} else if (Array.isArray(obj)) {
@@ -326,7 +326,7 @@ Base.inject(/** @lends Base# */{
326326
res[i] = Base.serialize(obj[i], options, compact,
327327
dictionary);
328328
// Mark array as compact, so obj._serialize handling above
329-
// doesn't add the constructor name again.
329+
// doesn't add the class name again.
330330
if (compact)
331331
res._compact = true;
332332
} else if (Base.isPlainObject(obj)) {

src/core/PaperScope.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* {@code PaperScope}.
3434
*/
3535
var PaperScope = Base.extend(/** @lends PaperScope# */{
36+
_class: 'PaperScope',
3637

3738
/**
3839
* Creates a PaperScope object.

src/item/Group.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @extends Item
2121
*/
2222
var Group = Item.extend(/** @lends Group# */{
23+
_class: 'Group',
2324
_serializeFields: {
2425
children: []
2526
},

0 commit comments

Comments
 (0)