fixing argument passing to the callback

This commit is contained in:
Danny Yoo 2012-03-15 15:07:24 -04:00
parent 45ef3b21f9
commit 441c40d305
4 changed files with 38 additions and 21 deletions

View File

@ -1,6 +1,7 @@
#lang s-exp "../lang/base.rkt" #lang s-exp "../../lang/base.rkt"
(require "../js/world.rkt"
"../js.rkt") (require "../../js.rkt")
(require "../../js/world.rkt")
(provide on-geo) (provide on-geo)
@ -8,8 +9,10 @@
(js-function->procedure (js-function->procedure
"function(locationCallback) { "function(locationCallback) {
return navigator.geolocation.watchPosition( return navigator.geolocation.watchPosition(
function(evt) { locationCallback(plt.runtime.makeFloat(evt.latitude), function(evt) {
plt.runtime.makeFloat(evt.longitude)); })}")) var coords = evt.coords;
locationCallback(plt.runtime.makeFloat(coords.latitude),
plt.runtime.makeFloat(coords.longitude)); })}"))
(define shutdown-geo (define shutdown-geo
(js-function->procedure (js-function->procedure

View File

@ -24,9 +24,12 @@
var enabled = false; var enabled = false;
var fireEvent; var fireEvent;
var sender = function(v) { var sender = function() {
var args;
if (enabled) { if (enabled) {
fireEvent(void(0), v); args = Array.prototype.slice.call(arguments, 0);
args.unshift(void(0));
fireEvent.apply(void(0), args);
} }
}; };
@ -75,11 +78,11 @@
var shutdownProcedure = wrapFunction(checkProcedure(M, 'make-world-event-handler', 1)); var shutdownProcedure = wrapFunction(checkProcedure(M, 'make-world-event-handler', 1));
var eventSource = makeJsEventSource(setupProcedure, shutdownProcedure); var eventSource = makeJsEventSource(setupProcedure, shutdownProcedure);
var makeHandler = makePrimitiveProcedure( var makeHandler = makePrimitiveProcedure(
'make-js-world-event', 'world-event-handler',
1, 1,
function(M) { function(M) {
var onEvent = wrapFunction(checkProcedure(M, 'js-world-event-handler', 0)); var onEvent = wrapFunction(checkProcedure(M, 'world-event-handler', 0));
return new EventHandler('js-world-event', eventSource, onEvent); return new EventHandler('world-event-handler', eventSource, onEvent);
}); });
finalizeClosureCall(M, makeHandler); finalizeClosureCall(M, makeHandler);
}); });

9
js/world/test-geo.rkt Normal file
View File

@ -0,0 +1,9 @@
#lang planet dyoo/whalesong
(require (planet dyoo/whalesong/web-world)
"geo.rkt")
(big-bang (list 'undefined 'undefined)
(on-geo (lambda (w v lat lng)
(list lat lng))))

View File

@ -16,7 +16,7 @@
var makeList = plt.baselib.lists.makeList; var makeList = plt.baselib.lists.makeList;
var makePair = plt.baselib.lists.makePair; var makePair = plt.baselib.lists.makePair;
var makeSymbol = plt.baselib.symbols.makeSymbol; var makeSymbol = plt.baselib.symbols.makeSymbol;
var isArityMatching = plt.baselib.arity.isArityMatching;
// EventHandler and the other classes here will be defined below. // EventHandler and the other classes here will be defined below.
@ -1258,7 +1258,7 @@
// Apply all the events on the queue, call toDraw, and then stop. // Apply all the events on the queue, call toDraw, and then stop.
// If the world ever satisfies stopWhen, stop immediately and quit. // If the world ever satisfies stopWhen, stop immediately and quit.
var nextEvent; var nextEvent;
var data; var args;
var racketWorldCallback; var racketWorldCallback;
var mockView; var mockView;
dispatchingEvents = true; dispatchingEvents = true;
@ -1271,9 +1271,8 @@
mockView = mockView.updateFocus(nextEvent.who.id); mockView = mockView.updateFocus(nextEvent.who.id);
} }
// FIXME: deal with event data here
racketWorldCallback = nextEvent.handler.racketWorldCallback; racketWorldCallback = nextEvent.handler.racketWorldCallback;
data = nextEvent.data[0]; args = nextEvent.data.slice(0);
var onGoodWorldUpdate = var onGoodWorldUpdate =
function(newWorld) { function(newWorld) {
world = newWorld; world = newWorld;
@ -1290,19 +1289,22 @@
}, },
fail); fail);
}; };
if (plt.baselib.arity.isArityMatching(racketWorldCallback.racketArity, 3)) { if (isArityMatching(racketWorldCallback.racketArity, 1)) {
racketWorldCallback(internalCall,
world,
onGoodWorldUpdate,
fail);
} else if (isArityMatching(racketWorldCallback.racketArity, 2)) {
racketWorldCallback(internalCall, racketWorldCallback(internalCall,
world, world,
mockView, mockView,
data,
onGoodWorldUpdate, onGoodWorldUpdate,
fail); fail);
} else { } else {
racketWorldCallback(internalCall, args = ([internalCall, world, mockView]
world, .concat(args)
mockView, .concat([onGoodWorldUpdate, fail]));
onGoodWorldUpdate, racketWorldCallback.apply(null, args);
fail);
} }
} else { } else {
dispatchingEvents = false; dispatchingEvents = false;