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?'] =
plt.runtime.makePrimitiveProcedure(
'image-color?',
@ -9,188 +34,259 @@ EXPORTS['image-color?'] =
EXPORTS['text'] =
plt.runtime.makePrimitiveProcedure(
'text',
???,
3,
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'] =
plt.runtime.makePrimitiveProcedure(
'text/font',
???,
function(MACHINE) {
...
});
// FIXME
// EXPORTS['text/font'] =
// plt.runtime.makePrimitiveProcedure(
// 'text/font',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['image-url'] =
plt.runtime.makePrimitiveProcedure(
'image-url',
???,
function(MACHINE) {
...
});
// FIXME
// EXPORTS['image-url'] =
// plt.runtime.makePrimitiveProcedure(
// 'image-url',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['open-image-url'] =
plt.runtime.makePrimitiveProcedure(
'open-image-url',
???,
function(MACHINE) {
...
});
// FIXME
// EXPORTS['open-image-url'] =
// plt.runtime.makePrimitiveProcedure(
// 'open-image-url',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['overlay'] =
plt.runtime.makePrimitiveProcedure(
'overlay',
???,
plt.baselib.arity.makeArityAtLeast(2),
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'] =
plt.runtime.makePrimitiveProcedure(
'overlay/xy',
???,
4,
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'] =
plt.runtime.makePrimitiveProcedure(
'overlay/align',
???,
function(MACHINE) {
...
});
// FIXME
// EXPORTS['overlay/align'] =
// plt.runtime.makePrimitiveProcedure(
// 'overlay/align',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['underlay'] =
plt.runtime.makePrimitiveProcedure(
'underlay',
???,
plt.baselib.arity.makeArityAtLeast(2),
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'] =
plt.runtime.makePrimitiveProcedure(
'underlay/xy',
???,
4,
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'] =
plt.runtime.makePrimitiveProcedure(
'underlay/align',
???,
function(MACHINE) {
...
});
// EXPORTS['underlay/align'] =
// plt.runtime.makePrimitiveProcedure(
// 'underlay/align',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['beside'] =
plt.runtime.makePrimitiveProcedure(
'beside',
???,
function(MACHINE) {
...
});
// EXPORTS['beside'] =
// plt.runtime.makePrimitiveProcedure(
// 'beside',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['beside/align'] =
plt.runtime.makePrimitiveProcedure(
'beside/align',
???,
function(MACHINE) {
...
});
// EXPORTS['beside/align'] =
// plt.runtime.makePrimitiveProcedure(
// 'beside/align',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['above'] =
plt.runtime.makePrimitiveProcedure(
'above',
???,
function(MACHINE) {
...
});
// EXPORTS['above'] =
// plt.runtime.makePrimitiveProcedure(
// 'above',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['above/align'] =
plt.runtime.makePrimitiveProcedure(
'above/align',
???,
function(MACHINE) {
...
});
// EXPORTS['above/align'] =
// plt.runtime.makePrimitiveProcedure(
// 'above/align',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['place-image/align'] =
plt.runtime.makePrimitiveProcedure(
'place-image/align',
???,
function(MACHINE) {
...
});
// EXPORTS['place-image/align'] =
// plt.runtime.makePrimitiveProcedure(
// 'place-image/align',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['rotate'] =
plt.runtime.makePrimitiveProcedure(
'rotate',
???,
2,
function(MACHINE) {
...
var angle = checkReal(MACHINE, "rotate", 0);
var img = checkImage(MACHINE, "rotate", 1);
return makeRotateImage(jsnums.toFixnum(angle), img);
});
EXPORTS['scale'] =
plt.runtime.makePrimitiveProcedure(
'scale',
???,
2,
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'] =
plt.runtime.makePrimitiveProcedure(
'scale/xy',
???,
3,
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'] =
plt.runtime.makePrimitiveProcedure(
'flip-horizontal',
???,
function(MACHINE) {
...
});
// EXPORTS['flip-horizontal'] =
// plt.runtime.makePrimitiveProcedure(
// 'flip-horizontal',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['flip-vertical'] =
plt.runtime.makePrimitiveProcedure(
'flip-vertical',
???,
function(MACHINE) {
...
});
// EXPORTS['flip-vertical'] =
// plt.runtime.makePrimitiveProcedure(
// 'flip-vertical',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['frame'] =
plt.runtime.makePrimitiveProcedure(
'frame',
???,
function(MACHINE) {
...
});
// EXPORTS['frame'] =
// plt.runtime.makePrimitiveProcedure(
// 'frame',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['crop'] =
plt.runtime.makePrimitiveProcedure(
'crop',
???,
function(MACHINE) {
...
});
// EXPORTS['crop'] =
// plt.runtime.makePrimitiveProcedure(
// 'crop',
// ???,
// function(MACHINE) {
// ...
// });
EXPORTS['line'] =
plt.runtime.makePrimitiveProcedure(
'line',
???,
3,
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'] =

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.FileImage = FileImage;
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.isScene = isScene;
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.isCircleImage = function(x) { return x instanceof CircleImage; };
EXPORTS.isStarImage = function(x) { return x instanceof StarImage; };

View File

@ -21,6 +21,11 @@
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.isInteger = isInteger;
exports.isNatural = isNatural;
exports.isByte = isByte;
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) {
if (observed < minimum || observed > maximum) {
raise(MACHINE, new Error(callerName + ": expected at least " + minimum
@ -2518,6 +2536,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
exports['testArgument'] = testArgument;
exports['testArity'] = testArity;
exports['makeCheckArgumentType'] = makeCheckArgumentType;
exports['raise'] = raise;