in the middle of revising the ffi for the world
This commit is contained in:
parent
5c881e6487
commit
a88e4192e3
|
@ -85,39 +85,6 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
var coersePrimitiveToJavaScript = function (v, MACHINE) {
|
|
||||||
return function (succ, fail) {
|
|
||||||
try {
|
|
||||||
succ = succ || function () {};
|
|
||||||
fail = fail || function () {};
|
|
||||||
|
|
||||||
var oldArgcount = MACHINE.a, i;
|
|
||||||
MACHINE.a = arguments.length - 2;
|
|
||||||
for (i = 0; i < arguments.length - 2; i++) {
|
|
||||||
MACHINE.e.push(arguments[arguments.length - 1 - i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(baselib.arity.isArityMatching(v.racketArity, MACHINE.a))) {
|
|
||||||
var msg = baselib.format.format("arity mismatch: ~s expected ~s arguments, but received ~s",
|
|
||||||
[v.displayName, v.racketArity, MACHINE.a]);
|
|
||||||
return fail(new baselib.exceptions.RacketError(
|
|
||||||
msg,
|
|
||||||
baselib.exceptions.makeExnFailContractArity(msg,
|
|
||||||
MACHINE.captureContinuationMarks())));
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = v(MACHINE);
|
|
||||||
MACHINE.a = oldArgcount;
|
|
||||||
for (i = 0; i < arguments.length - 2; i++) {
|
|
||||||
MACHINE.e.pop();
|
|
||||||
}
|
|
||||||
succ(result);
|
|
||||||
} catch (e) {
|
|
||||||
fail(e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
var coerseClosureToJavaScript = function (v, MACHINE) {
|
var coerseClosureToJavaScript = function (v, MACHINE) {
|
||||||
var f = function (succ, fail) {
|
var f = function (succ, fail) {
|
||||||
var args = [];
|
var args = [];
|
||||||
|
|
|
@ -20,52 +20,53 @@
|
||||||
* Creates an event source coupled to a JavaScript function. Calling the function
|
* Creates an event source coupled to a JavaScript function. Calling the function
|
||||||
* should cause the event source to fire.
|
* should cause the event source to fire.
|
||||||
*/
|
*/
|
||||||
var makeJsEventSource = function() {
|
var makeJsEventSource = function(startupProcedure, shutdownProcedure) {
|
||||||
var enabled = false;
|
var enabled = false;
|
||||||
var fireEvent;
|
var fireEvent;
|
||||||
|
|
||||||
var JsEventSource = function() {};
|
|
||||||
JsEventSource.prototype = plt.baselib.heir(EventSource.prototype);
|
|
||||||
JsEventSource.prototype.onStart = function(_fireEvent) {
|
|
||||||
enabled = true;
|
|
||||||
fireEvent = _fireEvent;
|
|
||||||
};
|
|
||||||
JsEventSource.prototype.onStop = function() {
|
|
||||||
enabled = false;
|
|
||||||
fireEvent = void(0);
|
|
||||||
};
|
|
||||||
|
|
||||||
var sender = function(v) {
|
var sender = function(v) {
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
fireEvent(void(0), v);
|
fireEvent(void(0), v);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return { eventSource: new JsEventSource(),
|
|
||||||
sender: sender };
|
var JsEventSource = function() {
|
||||||
|
this.startupData = void(0);
|
||||||
|
};
|
||||||
|
JsEventSource.prototype = plt.baselib.heir(EventSource.prototype);
|
||||||
|
JsEventSource.prototype.onStart = function(_fireEvent) {
|
||||||
|
this.startupData = startupProcedure(sender);
|
||||||
|
enabled = true;
|
||||||
|
fireEvent = _fireEvent;
|
||||||
|
};
|
||||||
|
JsEventSource.prototype.onStop = function() {
|
||||||
|
shutdownProcedure(sender, this.startupData);
|
||||||
|
enabled = false;
|
||||||
|
fireEvent = void(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
return new JsEventSource();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
var makeJsWorldEvent = makeClosure(
|
var makeWorldEventHandler = makeClosure(
|
||||||
'make-js-world-event',
|
'make-world-event-handler',
|
||||||
0,
|
2,
|
||||||
function(M) {
|
function(M) {
|
||||||
var eventSourceRecord = makeJsEventSource();
|
var setupProcedure = checkProcedure(M, 'make-world-event-handler', 0);
|
||||||
eventSourceRecord.eventSource
|
var shutdownProcedure = checkProcedure(M, 'make-world-event-handler', 1);
|
||||||
|
var eventSource = makeJsEventSource(setupProcedure, shutdownProcedure);
|
||||||
var makeHandler = makePrimitiveProcedure(
|
var makeHandler = makePrimitiveProcedure(
|
||||||
'make-js-world-event',
|
'make-js-world-event',
|
||||||
1,
|
1,
|
||||||
function(M) {
|
function(M) {
|
||||||
var onEvent = wrapFunction(checkProcedure(M, 'js-world-event-handler', 0));
|
var onEvent = wrapFunction(checkProcedure(M, 'js-world-event-handler', 0));
|
||||||
return new EventHandler('js-world-event',
|
return new EventHandler('js-world-event', eventSource, onEvent);
|
||||||
eventSourceRecord.eventSource,
|
|
||||||
onEvent);
|
|
||||||
});
|
});
|
||||||
finalizeClosureCall(M,
|
finalizeClosureCall(M, makeHandler);
|
||||||
makeHandler,
|
|
||||||
eventSourceRecord.sender);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
EXPORTS['make-js-world-event'] = makeJsWorldEvent;
|
EXPORTS['make-world-event-handler'] = makeWorldEventHandler;
|
||||||
|
|
||||||
}());
|
}());
|
|
@ -1,3 +1,3 @@
|
||||||
#lang s-exp "../../lang/base.rkt"
|
#lang s-exp "../../lang/base.rkt"
|
||||||
(require "make-js-world-event.rkt")
|
(require "world-event-handler.rkt")
|
||||||
(provide (all-from-out "make-js-world-event.rkt"))
|
(provide (all-from-out "world-event-handler.rkt"))
|
||||||
|
|
|
@ -2,5 +2,5 @@
|
||||||
|
|
||||||
(provide make-js-world-event)
|
(provide make-js-world-event)
|
||||||
|
|
||||||
(define (make-js-world-event)
|
(define (make-world-event-handler setup shutdown)
|
||||||
(error 'make-js-world-event "Must be run under a JavaScript context."))
|
(error 'make-world-event-handler "Must be run under a JavaScript context."))
|
||||||
|
|
|
@ -3,4 +3,4 @@
|
||||||
(declare-implementation
|
(declare-implementation
|
||||||
#:racket "racket-impl.rkt"
|
#:racket "racket-impl.rkt"
|
||||||
#:javascript ("js-impl.js")
|
#:javascript ("js-impl.js")
|
||||||
#:provided-values (make-js-world-event))
|
#:provided-values (make-world-event-handler))
|
17
web-world/geolocation.rkt
Normal file
17
web-world/geolocation.rkt
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#lang s-exp "../lang/base.rkt"
|
||||||
|
(require "../js/world.rkt"
|
||||||
|
"../js.rkt")
|
||||||
|
|
||||||
|
;; Create a new event handler type
|
||||||
|
(define-values (on-geo-change locationCallback)
|
||||||
|
(make-js-event-type))
|
||||||
|
|
||||||
|
(define start-up-geo
|
||||||
|
(js-function->procedure
|
||||||
|
"function(locationCallback) {
|
||||||
|
navigator.geolocation.watchPosition(
|
||||||
|
function(evt) { locationCallback(evt.latitude, evt.longitude); })}"))
|
||||||
|
|
||||||
|
;; TODO: adjust the FFI so we can start-up and shutdown this more easily.
|
||||||
|
(start-up-geo locationCallback)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user