diff --git a/js-assembler/runtime-src/baselib-functions.js b/js-assembler/runtime-src/baselib-functions.js index 4f4fce0..86b6b3e 100644 --- a/js-assembler/runtime-src/baselib-functions.js +++ b/js-assembler/runtime-src/baselib-functions.js @@ -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 f = function (succ, fail) { var args = []; diff --git a/js/world/js-impl.js b/js/world/js-impl.js index 243e911..1bc2f9a 100644 --- a/js/world/js-impl.js +++ b/js/world/js-impl.js @@ -20,52 +20,53 @@ * Creates an event source coupled to a JavaScript function. Calling the function * should cause the event source to fire. */ - var makeJsEventSource = function() { + var makeJsEventSource = function(startupProcedure, shutdownProcedure) { var enabled = false; 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) { if (enabled) { 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( - 'make-js-world-event', - 0, + var makeWorldEventHandler = makeClosure( + 'make-world-event-handler', + 2, function(M) { - var eventSourceRecord = makeJsEventSource(); - eventSourceRecord.eventSource + var setupProcedure = checkProcedure(M, 'make-world-event-handler', 0); + var shutdownProcedure = checkProcedure(M, 'make-world-event-handler', 1); + var eventSource = makeJsEventSource(setupProcedure, shutdownProcedure); var makeHandler = makePrimitiveProcedure( 'make-js-world-event', 1, function(M) { var onEvent = wrapFunction(checkProcedure(M, 'js-world-event-handler', 0)); - return new EventHandler('js-world-event', - eventSourceRecord.eventSource, - onEvent); + return new EventHandler('js-world-event', eventSource, onEvent); }); - finalizeClosureCall(M, - makeHandler, - eventSourceRecord.sender); + finalizeClosureCall(M, makeHandler); }); - EXPORTS['make-js-world-event'] = makeJsWorldEvent; + EXPORTS['make-world-event-handler'] = makeWorldEventHandler; }()); \ No newline at end of file diff --git a/js/world/main.rkt b/js/world/main.rkt index 723aa39..7be3db5 100644 --- a/js/world/main.rkt +++ b/js/world/main.rkt @@ -1,3 +1,3 @@ #lang s-exp "../../lang/base.rkt" -(require "make-js-world-event.rkt") -(provide (all-from-out "make-js-world-event.rkt")) +(require "world-event-handler.rkt") +(provide (all-from-out "world-event-handler.rkt")) diff --git a/js/world/racket-impl.rkt b/js/world/racket-impl.rkt index 253e4ed..3262150 100644 --- a/js/world/racket-impl.rkt +++ b/js/world/racket-impl.rkt @@ -2,5 +2,5 @@ (provide make-js-world-event) -(define (make-js-world-event) - (error 'make-js-world-event "Must be run under a JavaScript context.")) +(define (make-world-event-handler setup shutdown) + (error 'make-world-event-handler "Must be run under a JavaScript context.")) diff --git a/js/world/make-js-world-event.rkt b/js/world/world-event-handler.rkt similarity index 76% rename from js/world/make-js-world-event.rkt rename to js/world/world-event-handler.rkt index b2d0733..aeef438 100644 --- a/js/world/make-js-world-event.rkt +++ b/js/world/world-event-handler.rkt @@ -3,4 +3,4 @@ (declare-implementation #:racket "racket-impl.rkt" #:javascript ("js-impl.js") - #:provided-values (make-js-world-event)) \ No newline at end of file + #:provided-values (make-world-event-handler)) \ No newline at end of file diff --git a/web-world/geolocation.rkt b/web-world/geolocation.rkt new file mode 100644 index 0000000..4b6c94d --- /dev/null +++ b/web-world/geolocation.rkt @@ -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) +