diff --git a/examples/counting-world-program.rkt b/examples/counting-world-program.rkt index 91cb7aa..91791a4 100644 --- a/examples/counting-world-program.rkt +++ b/examples/counting-world-program.rkt @@ -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))) ) diff --git a/image/private/js-impl.js b/image/private/js-impl.js index 532c286..5c38e53 100644 --- a/image/private/js-impl.js +++ b/image/private/js-impl.js @@ -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) { diff --git a/js-assembler/runtime-src/runtime.js b/js-assembler/runtime-src/runtime.js index 92a1ab0..79229c6 100644 --- a/js-assembler/runtime-src/runtime.js +++ b/js-assembler/runtime-src/runtime.js @@ -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, diff --git a/world/js-impl.js b/world/js-impl.js index f63bd05..82924bf 100644 --- a/world/js-impl.js +++ b/world/js-impl.js @@ -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); }); diff --git a/world/kernel.js b/world/kernel.js index 4937ec3..d3c1819 100644 --- a/world/kernel.js +++ b/world/kernel.js @@ -21,6 +21,8 @@ var finalizeClosureCall = plt.baselib.functions.finalizeClosureCall; var bigBang = function(MACHINE, initW, handlers) { + var oldArgcount = MACHINE.argcount; + var toplevelNode = $('
').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); +};