Make PaperScript math operator methods 'hidden' and use aliases to visible methods.

Avoids Path#add() being misinterpreted as an operator, and console.log(path + ' hi'); causing weird issues.
This commit is contained in:
Jürg Lehni 2013-06-28 05:15:49 -07:00
parent 5e7209a7bb
commit 5b574877e9
2 changed files with 21 additions and 8 deletions

View file

@ -19,7 +19,7 @@
"test"
],
"devDependencies": {
"straps": "~1.1.2",
"straps": "~1.1.3",
"acorn": "~0.3.1",
"esprima": "~1.0.3",
"stats.js": "r11"

View file

@ -31,20 +31,34 @@ paper.PaperScope.prototype.PaperScript = new function() {
// Operators to overload
var binaryOperators = {
'+': 'add',
'-': 'subtract',
'*': 'multiply',
'/': 'divide',
'%': 'modulo',
// The hidden math functions are to be injected specifically, see below.
'+': '_add',
'-': '_subtract',
'*': '_multiply',
'/': '_divide',
'%': '_modulo',
// Use the real equals.
'==': 'equals',
'!=': 'equals'
};
var unaryOperators = {
'-': 'negate',
'-': '_negate',
'+': null
};
// Add hidden math functions to Point, Size and Color
var fields = Base.each(
'add,subtract,multiply,divide,modulo,negate'.split(','),
function(name) {
this['_' + name] = '#' + name;
},
{}
);
paper.Point.inject(fields);
paper.Size.inject(fields);
paper.Color.inject(fields);
// Use very short name for the binary operator (_$_) as well as the
// unary operator ($_), as operations will be replaced with then.
// The underscores stands for the values, and the $ for the operators.
@ -80,7 +94,6 @@ paper.PaperScope.prototype.PaperScript = new function() {
// AST Helpers
/**
* Compiles PaperScript code into JavaScript code.
*