checkargumentype

This commit is contained in:
Danny Yoo 2011-07-10 15:58:42 -04:00
parent 73cb9776fc
commit 9afc4accdc
4 changed files with 289 additions and 160 deletions

View File

@ -1,3 +1,28 @@
var checkString = plt.runtime.makeCheckArgumentType(
plt.runtime.strings.isString,
'string');
var checkByte = plt.runtime.makeCheckArgumentType(
plt.runtime.numbers.isByte,
'byte');
var checkColor = plt.runtime.makeCheckArgumentType(
isColorOrColorString,
'color');
var checkImage = plt.runtime.makeCheckArgumentType(
isImage,
'image');
var checkReal = plt.runtime.makeCheckArgumentType(
plt.runtime.numbers.isReal,
'real');
//////////////////////////////////////////////////////////////////////
EXPORTS['image-color?'] = EXPORTS['image-color?'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'image-color?', 'image-color?',
@ -9,188 +34,259 @@ EXPORTS['image-color?'] =
EXPORTS['text'] = EXPORTS['text'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'text', 'text',
???, 3,
function(MACHINE) { function(MACHINE) {
... var aString = checkString(MACHINE,'text', 0);
var aSize = checkByte(MACHINE, 'text', 1);
var aColor = checkColor(MACHINE, 'text', 2);
if (colorDb.get(aColor)) {
aColor = colorDb.get(aColor);
}
return makeTextImage(aString.toString(),
jsnums.toFixnum(aSize),
aColor);
}); });
EXPORTS['text/font'] = // FIXME
plt.runtime.makePrimitiveProcedure( // EXPORTS['text/font'] =
'text/font', // plt.runtime.makePrimitiveProcedure(
???, // 'text/font',
function(MACHINE) { // ???,
... // function(MACHINE) {
}); // ...
// });
EXPORTS['image-url'] = // FIXME
plt.runtime.makePrimitiveProcedure( // EXPORTS['image-url'] =
'image-url', // plt.runtime.makePrimitiveProcedure(
???, // 'image-url',
function(MACHINE) { // ???,
... // function(MACHINE) {
}); // ...
// });
EXPORTS['open-image-url'] = // FIXME
plt.runtime.makePrimitiveProcedure( // EXPORTS['open-image-url'] =
'open-image-url', // plt.runtime.makePrimitiveProcedure(
???, // 'open-image-url',
function(MACHINE) { // ???,
... // function(MACHINE) {
}); // ...
// });
EXPORTS['overlay'] = EXPORTS['overlay'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'overlay', 'overlay',
???, plt.baselib.arity.makeArityAtLeast(2),
function(MACHINE) { function(MACHINE) {
...
var img1 = checkImage(MACHINE, "overlay", 0);
var img2 = checkImage(MACHINE, "overlay", 1);
var restImages;
for (var i = 2; i < MACHINE.argcount; i++) {
restImages.push(checkImage(MACHINE, "overlay", i));
}
var img = makeOverlayImage(img1, img2, 0, 0);
for (var i = 0; i < restImages.length; i++) {
img = makeOverlayImage(img, restImages[i], 0, 0);
}
return img;
}); });
EXPORTS['overlay/xy'] = EXPORTS['overlay/xy'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'overlay/xy', 'overlay/xy',
???, 4,
function(MACHINE) { function(MACHINE) {
... var img1 = checkImage("overlay/xy", 0);
var deltaX = checkReal("overlay/xy", 1);
var deltaY = checkReal("overlay/xy", 2);
var img2 = checkImage("overlay/xy", 2);
return makeOverlayImage(img1.updatePinhole(0, 0),
img2.updatePinhole(0, 0),
jsnums.toFixnum(deltaX),
jsnums.toFixnum(deltaY));
}); });
EXPORTS['overlay/align'] = // FIXME
plt.runtime.makePrimitiveProcedure( // EXPORTS['overlay/align'] =
'overlay/align', // plt.runtime.makePrimitiveProcedure(
???, // 'overlay/align',
function(MACHINE) { // ???,
... // function(MACHINE) {
}); // ...
// });
EXPORTS['underlay'] = EXPORTS['underlay'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'underlay', 'underlay',
???, plt.baselib.arity.makeArityAtLeast(2),
function(MACHINE) { function(MACHINE) {
... var img1 = checkImage(MACHINE, "underlay", 0);
var img2 = checkImage(MACHINE, "underlay", 1);
var restImages = [];
for (var i = 2; i < MACHINE.argcount; i++) {
restImages.push(checkImage(MACHINE, "underlay", i));
}
var img = makeOverlayImage(img2, img1, 0, 0);
for (var i = 0; i < restImages.length; i++) {
img = makeOverlayImage(restImages[i], img, 0, 0);
}
return img;
}); });
EXPORTS['underlay/xy'] = EXPORTS['underlay/xy'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'underlay/xy', 'underlay/xy',
???, 4,
function(MACHINE) { function(MACHINE) {
... var img1 = checkImage(MACHINE, "underlay/xy", 0);
var deltaX = checkReal(MACHINE, "underlay/xy", 1);
var deltaY = checkReal(MACHINE, "underlay/xy", 2);
var img2 = checkImage(MACHINE, "underlay/xy", 3);
return makeOverlayImage(img2.updatePinhole(0, 0),
img1.updatePinhole(0, 0),
-(jsnums.toFixnum(deltaX)),
-(jsnums.toFixnum(deltaY)));
}); });
EXPORTS['underlay/align'] = // EXPORTS['underlay/align'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'underlay/align', // 'underlay/align',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['beside'] = // EXPORTS['beside'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'beside', // 'beside',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['beside/align'] = // EXPORTS['beside/align'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'beside/align', // 'beside/align',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['above'] = // EXPORTS['above'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'above', // 'above',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['above/align'] = // EXPORTS['above/align'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'above/align', // 'above/align',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['place-image/align'] = // EXPORTS['place-image/align'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'place-image/align', // 'place-image/align',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['rotate'] = EXPORTS['rotate'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'rotate', 'rotate',
???, 2,
function(MACHINE) { function(MACHINE) {
... var angle = checkReal(MACHINE, "rotate", 0);
var img = checkImage(MACHINE, "rotate", 1);
return makeRotateImage(jsnums.toFixnum(angle), img);
}); });
EXPORTS['scale'] = EXPORTS['scale'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'scale', 'scale',
???, 2,
function(MACHINE) { function(MACHINE) {
... var factor = checkReal(MACHINE, "scale", 0);
var img = checkImage(MACHINE, "scale", 1);
return makeScaleImage(jsnums.toFixnum(factor),
jsnums.toFixnum(factor),
img);
}); });
EXPORTS['scale/xy'] = EXPORTS['scale/xy'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'scale/xy', 'scale/xy',
???, 3,
function(MACHINE) { function(MACHINE) {
... var xFactor = checkReal(MACHINE, "scale/xy", 0);
var yFactor = checkReal(MACHINE, "scale/xy", 1);
var img = checkImage(MACHINE, "scale/xy", 2);
return makeScaleImage(jsnums.toFixnum(xFactor),
jsnums.toFixnum(yFactor),
img);
}); });
EXPORTS['flip-horizontal'] = // EXPORTS['flip-horizontal'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'flip-horizontal', // 'flip-horizontal',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['flip-vertical'] = // EXPORTS['flip-vertical'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'flip-vertical', // 'flip-vertical',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['frame'] = // EXPORTS['frame'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'frame', // 'frame',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['crop'] = // EXPORTS['crop'] =
plt.runtime.makePrimitiveProcedure( // plt.runtime.makePrimitiveProcedure(
'crop', // 'crop',
???, // ???,
function(MACHINE) { // function(MACHINE) {
... // ...
}); // });
EXPORTS['line'] = EXPORTS['line'] =
plt.runtime.makePrimitiveProcedure( plt.runtime.makePrimitiveProcedure(
'line', 'line',
???, 3,
function(MACHINE) { function(MACHINE) {
... var x = checkReal(MACHINE, "line", 0);
var y = checkReal(MACHINE, "line", 1);
var c = checkColor(MACHINE, "line", 2);
if (colorDb.get(c)) {
c = colorDb.get(c);
}
var line = makeLineImage(jsnums.toFixnum(x),
jsnums.toFixnum(y),
c);
return line;
}); });
EXPORTS['add-line'] = EXPORTS['add-line'] =

View File

@ -925,6 +925,44 @@ LineImage.prototype.isEqual = function(other, aUnionFind) {
var makeSceneImage = function(width, height, children, withBorder) {
return new SceneImage(width, height, children, withBorder);
};
var makeCircleImage = function(radius, style, color) {
return new CircleImage(radius, style, color);
};
var makeStarImage = function(points, outer, inner, style, color) {
return new StarImage(points, outer, inner, style, color);
};
var makeRectangleImage = function(width, height, style, color) {
return new RectangleImage(width, height, style, color);
};
var makeTriangleImage = function(side, style, color) {
return new TriangleImage(side, style, color);
};
var makeEllipseImage = function(width, height, style, color) {
return new EllipseImage(width, height, style, color);
};
var makeLineImage = function(x, y, color) {
return new LineImage(x, y, color);
};
var makeOverlayImage = function(img1, img2, shiftX, shiftY) {
return new OverlayImage(img1, img2, shiftX, shiftY);
};
var makeRotateImage = function(angle, img) {
return new RotateImage(angle, img);
};
var makeScaleImage = function(xFactor, yFactor, img) {
return new ScaleImage(xFactor, yFactor, img);
};
var makeTextImage = function(msg, size, color) {
return new TextImage(msg, size, color);
};
var makeFileImage = function(path, rawImage) {
return FileImage.makeInstance(path, rawImage);
};
@ -951,56 +989,26 @@ EXPORTS.ScaleImage = ScaleImage;
EXPORTS.TextImage = TextImage; EXPORTS.TextImage = TextImage;
EXPORTS.FileImage = FileImage; EXPORTS.FileImage = FileImage;
EXPORTS.colorDb = colorDb; EXPORTS.colorDb = colorDb;
EXPORTS.makeSceneImage = makeSceneImage;
EXPORTS.makeCircleImage = makeCircleImage;
EXPORTS.makeStarImage = makeStarImage;
EXPORTS.makeRectangleImage = makeRectangleImage;
EXPORTS.makeTriangleImage = makeTriangleImage;
EXPORTS.makeEllipseImage = makeEllipseImage;
EXPORTS.makeLineImage = makeLineImage;
EXPORTS.makeOverlayImage = makeOverlayImage;
EXPORTS.makeRotateImage = makeRotateImage;
EXPORTS.makeScaleImage = makeScaleImage;
EXPORTS.makeTextImage = makeTextImage;
EXPORTS.makeFileImage = makeFileImage;
EXPORTS.isImage = isImage; EXPORTS.isImage = isImage;
EXPORTS.isScene = isScene; EXPORTS.isScene = isScene;
EXPORTS.isColorOrColorString = isColorOrColorString; EXPORTS.isColorOrColorString = isColorOrColorString;
EXPORTS.makeSceneImage = function(width, height, children, withBorder) {
return new SceneImage(width, height, children, withBorder);
};
EXPORTS.makeCircleImage = function(radius, style, color) {
return new CircleImage(radius, style, color);
};
EXPORTS.makeStarImage = function(points, outer, inner, style, color) {
return new StarImage(points, outer, inner, style, color);
};
EXPORTS.makeRectangleImage = function(width, height, style, color) {
return new RectangleImage(width, height, style, color);
};
EXPORTS.makeTriangleImage = function(side, style, color) {
return new TriangleImage(side, style, color);
};
EXPORTS.makeEllipseImage = function(width, height, style, color) {
return new EllipseImage(width, height, style, color);
};
EXPORTS.makeLineImage = function(x, y, color) {
return new LineImage(x, y, color);
};
EXPORTS.makeOverlayImage = function(img1, img2, shiftX, shiftY) {
return new OverlayImage(img1, img2, shiftX, shiftY);
};
EXPORTS.makeRotateImage = function(angle, img) {
return new RotateImage(angle, img);
};
EXPORTS.makeScaleImage = function(xFactor, yFactor, img) {
return new ScaleImage(xFactor, yFactor, img);
};
EXPORTS.makeTextImage = function(msg, size, color) {
return new TextImage(msg, size, color);
};
EXPORTS.makeFileImage = function(path, rawImage) {
return FileImage.makeInstance(path, rawImage);
};
EXPORTS.isSceneImage = function(x) { return x instanceof SceneImage; }; EXPORTS.isSceneImage = function(x) { return x instanceof SceneImage; };
EXPORTS.isCircleImage = function(x) { return x instanceof CircleImage; }; EXPORTS.isCircleImage = function(x) { return x instanceof CircleImage; };
EXPORTS.isStarImage = function(x) { return x instanceof StarImage; }; EXPORTS.isStarImage = function(x) { return x instanceof StarImage; };

View File

@ -21,6 +21,11 @@
return isReal(x) && jsnums.greaterThanOrEqual(x, 0); return isReal(x) && jsnums.greaterThanOrEqual(x, 0);
}; };
var isByte = function(x) {
return (isNatural(x) &&
jsnums.lessThan(x, 256));
};
@ -42,6 +47,7 @@
exports.isComplex = isComplex; exports.isComplex = isComplex;
exports.isInteger = isInteger; exports.isInteger = isInteger;
exports.isNatural = isNatural; exports.isNatural = isNatural;
exports.isByte = isByte;
exports.isNonNegativeReal = isNonNegativeReal; exports.isNonNegativeReal = isNonNegativeReal;

View File

@ -384,6 +384,24 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
} }
}; };
// Helper function for argument checking.
var makeCheckArgumentType = function(predicate, predicateName) {
return function(MACHINE, callerName, position) {
testArgument(
MACHINE,
predicateName,
predicate,
MACHINE.env[MACHINE.env.length - 1 - position],
position,
callerName);
return MACHINE.env[MACHINE.env.length - 1 - position];
}
};
var testArity = function(callerName, observed, minimum, maximum) { var testArity = function(callerName, observed, minimum, maximum) {
if (observed < minimum || observed > maximum) { if (observed < minimum || observed > maximum) {
raise(MACHINE, new Error(callerName + ": expected at least " + minimum raise(MACHINE, new Error(callerName + ": expected at least " + minimum
@ -2518,6 +2536,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
exports['testArgument'] = testArgument; exports['testArgument'] = testArgument;
exports['testArity'] = testArity; exports['testArity'] = testArity;
exports['makeCheckArgumentType'] = makeCheckArgumentType;
exports['raise'] = raise; exports['raise'] = raise;