Merge pull request #15 from vishesh/master

Implement last-picture in world
This commit is contained in:
Jens Axel Søgaard 2015-05-05 23:00:14 +02:00
commit 154662078a
4 changed files with 35 additions and 14 deletions

View File

@ -87,10 +87,16 @@ EXPORTS['to-draw'] =
EXPORTS['stop-when'] = EXPORTS['stop-when'] =
makePrimitiveProcedure( makePrimitiveProcedure(
'stop-when', 'stop-when',
1, plt.baselib.lists.makeList(1, 2),
function(MACHINE) { function(MACHINE) {
var f = checkProcedure1(MACHINE, "on-tick", 0); var f = checkProcedure1(MACHINE, "on-tick", 0);
return new StopWhen(f); if (MACHINE.a === 2) {
var lp = checkProcedure1(MACHINE, "to-draw", 1);
} else {
var lp = null;
}
return new StopWhen(f, lp);
}); });

View File

@ -282,8 +282,6 @@ ToDraw.prototype = plt.baselib.heir(OutputConfig.prototype);
ToDraw.prototype.toRawHandler = function(MACHINE, toplevelNode) { ToDraw.prototype.toRawHandler = function(MACHINE, toplevelNode) {
var that = this; var that = this;
var reusableCanvas;
var reusableCanvasNode;
var adaptedWorldFunction = adaptWorldFunction(this.handler); var adaptedWorldFunction = adaptWorldFunction(this.handler);
var worldFunction = function(world, success) { var worldFunction = function(world, success) {
@ -291,6 +289,12 @@ ToDraw.prototype.toRawHandler = function(MACHINE, toplevelNode) {
adaptedWorldFunction( adaptedWorldFunction(
world, world,
function(v) { function(v) {
var reusableCanvas = toplevelNode.getElementsByTagName('canvas')[0];
var reusableCanvasNode;
if (reusableCanvas) {
reusableCanvasNode = rawJsworld.node_to_tree(reusableCanvas);
}
// fixme: once jsworld supports fail continuations, use them // fixme: once jsworld supports fail continuations, use them
// to check the status of the scene object and make sure it's an // to check the status of the scene object and make sure it's an
// image. // image.
@ -325,6 +329,7 @@ ToDraw.prototype.toRawHandler = function(MACHINE, toplevelNode) {
}; };
var cssFunction = function(w, k) { var cssFunction = function(w, k) {
var reusableCanvas = toplevelNode.getElementsByTagName('canvas')[0];
if (reusableCanvas) { if (reusableCanvas) {
k([[reusableCanvas, k([[reusableCanvas,
["padding", "0px"], ["padding", "0px"],
@ -369,9 +374,10 @@ DefaultDrawingOutput.prototype.toRawHandler = function(MACHINE, toplevelNode) {
var StopWhen = function(handler) { var StopWhen = function(handler, last_picture) {
WorldConfigOption.call(this, 'stop-when'); WorldConfigOption.call(this, 'stop-when');
this.handler = handler; this.handler = handler;
this.last_picture = new ToDraw(last_picture);
}; };
StopWhen.prototype = plt.baselib.heir(WorldConfigOption.prototype); StopWhen.prototype = plt.baselib.heir(WorldConfigOption.prototype);
@ -379,5 +385,6 @@ StopWhen.prototype = plt.baselib.heir(WorldConfigOption.prototype);
StopWhen.prototype.toRawHandler = function(MACHINE, toplevelNode) { StopWhen.prototype.toRawHandler = function(MACHINE, toplevelNode) {
var that = this; var that = this;
var worldFunction = adaptWorldFunction(that.handler); var worldFunction = adaptWorldFunction(that.handler);
return rawJsworld.stop_when(worldFunction); var lastPictureHandler = that.last_picture.toRawHandler(MACHINE, toplevelNode);
return rawJsworld.stop_when(worldFunction, undefined, lastPictureHandler);
}; };

View File

@ -40,5 +40,8 @@
(define (key=? key-1 key-2) (define (key=? key-1 key-2)
(error 'key=? "must be run in JavaScript context")) (error 'key=? "must be run in JavaScript context"))
(define (stop-when handler) (define stop-when
(error 'stop-when "must be run in JavaScript context")) (case-lambda [(handler)
(error 'stop-when "must be run in JavaScript context")]
[(handler last-picture)
(error 'stop-when "must be run in JavaScript context")]))

View File

@ -663,9 +663,13 @@ var rawJsworld = {};
stopWhen.test(w, stopWhen.test(w,
function(stop) { function(stop) {
if (stop) { if (stop) {
var handler = stopWhen.last_picture_handler();
handler.onRegister(top);
handler._listener(w, oldW, function(v) {
Jsworld.shutdown({cleanShutdown: true}); Jsworld.shutdown({cleanShutdown: true});
} k2();
else { k2(); } })
} else { k2(); }
}); });
}; };
add_world_listener(watchForTermination); add_world_listener(watchForTermination);
@ -863,17 +867,18 @@ var rawJsworld = {};
StopWhenHandler = function(test, receiver) { StopWhenHandler = function(test, receiver, last_picture_handler) {
this.test = test; this.test = test;
this.receiver = receiver; this.receiver = receiver;
this.last_picture_handler = last_picture_handler;
}; };
// stop_when: CPS(world -> boolean) CPS(world -> boolean) -> handler // stop_when: CPS(world -> boolean) CPS(world -> boolean) -> handler
function stop_when(test, receiver) { function stop_when(test, receiver, last_picture_handler) {
return function() { return function() {
if (receiver === undefined) { if (receiver === undefined) {
receiver = function(w, k) { k(w); }; receiver = function(w, k) { k(w); };
} }
return new StopWhenHandler(test, receiver); return new StopWhenHandler(test, receiver, last_picture_handler);
}; };
} }
Jsworld.stop_when = stop_when; Jsworld.stop_when = stop_when;