adding stop-when

This commit is contained in:
Danny Yoo 2011-07-18 14:15:39 -04:00
parent 66327c28ee
commit 6b498d1e7a
5 changed files with 36 additions and 11 deletions

View File

@ -11,7 +11,7 @@ handler
(big-bang 1
(on-tick add1 1)
;;(on-tick (lambda (w) (* w 2)) 1)
;;(stop-when (lambda (w) (> w 10)))
(stop-when (lambda (w) (> w 10)))
)

View File

@ -270,15 +270,17 @@ EXPORTS['image-url'] =
1,
function(MACHINE) {
var url = checkString(MACHINE, 'image-url', 0);
var oldArgcount = MACHINE.argcount;
PAUSE(
function(restart) {
var rawImage = new Image();
rawImage.onload = function() {
restart(function(MACHINE) {
finalizeClosureCall(
MACHINE,
makeFileImage(url.toString(),
rawImage));
MACHINE.argcount = oldArgcount;
finalizeClosureCall(
MACHINE,
makeFileImage(url.toString(),
rawImage));
});
};
rawImage.onerror = function(e) {

View File

@ -456,6 +456,8 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
if(--MACHINE.callsBeforeTrampoline < 0) {
throw arguments.callee;
}
var oldArgcount = MACHINE.argcount;
var elt = MACHINE.env[MACHINE.env.length - 1];
var outputPort =
MACHINE.params.currentOutputPort;
@ -463,6 +465,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
outputPort.writeDomNode(MACHINE, toDomNode(elt, 'print'));
outputPort.writeDomNode(MACHINE, toDomNode("\n", 'display'));
}
MACHINE.argcount = oldArgcount;
return finalizeClosureCall(MACHINE, VOID);
},
1,

View File

@ -1,7 +1,6 @@
var PAUSE = plt.runtime.PAUSE;
var makeClosure = plt.baselib.functions.makeClosure;
var makeRational = plt.baselib.numbers.makeRational;
var finalizeClosureCall = plt.baselib.functions.finalizeClosureCall;
var makePrimitiveProcedure = plt.baselib.functions.makePrimitiveProcedure;
@ -42,7 +41,6 @@ EXPORTS['big-bang'] =
var initialWorldValue = MACHINE.env[MACHINE.env.length - 1];
var handlers = [];
for (var i = 1; i < MACHINE.argcount; i++) {
// FIXME: typecheck for configuration options
handlers.push(checkHandler(MACHINE, 'big-bang', i));
}
bigBang(MACHINE, initialWorldValue, handlers);
@ -74,20 +72,18 @@ EXPORTS['to-draw'] =
function(MACHINE) {
var f = checkProcedure1(MACHINE, "on-tick", 0);
return new OnDraw(f);
// FILL ME IN
});
EXPORTS['stop-when'] =
makePrimitiveProcedure(
'stop-when',
1,
function(MACHINE) {
var f = checkProcedure1(MACHINE, "on-tick", 0);
// FILL ME IN
return new StopWhen(f);
});

View File

@ -21,6 +21,8 @@ var finalizeClosureCall = plt.baselib.functions.finalizeClosureCall;
var bigBang = function(MACHINE, initW, handlers) {
var oldArgcount = MACHINE.argcount;
var toplevelNode = $('<div/>').css('border', '2').appendTo(document.body);
var configs = [];
@ -59,6 +61,7 @@ var bigBang = function(MACHINE, initW, handlers) {
restart(function(MACHINE) {
MACHINE.argcount = oldArgcount;
finalizeClosureCall(
MACHINE,
finalWorldValue);
@ -222,9 +225,30 @@ DefaultOnDraw.prototype.toRawHandler = function(MACHINE) {
var StopWhen = function(handler) {
WorldConfigOption.call(this, 'stop-when');
this.handler = handler;
};
StopWhen.prototype = plt.baselib.heir(WorldConfigOption.prototype);
StopWhen.prototype.toRawHandler = function(MACHINE) {
var that = this;
var worldFunction = function(world, k) {
plt.baselib.functions.internalCallDuringPause(
MACHINE,
that.handler,
function(v) {
k(v);
},
function(err) {
console.log(err);
},
world);
}
return rawJsworld.stop_when(worldFunction);
};