From d41aa187e3515a20d2f3922732b7553fc86bfee9 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Mon, 27 Feb 2012 21:38:52 -0500 Subject: [PATCH] adding load-script to the FFI; modified examples/raphael-demo.rkt so that it loads the raphael library dynamically --- examples/raphael-demo.rkt | 3 +- examples/raphael-script.js | 4 -- js/js-impl.js | 87 ++++++++++++++++++++++++++++++++++++++ js/main.rkt | 2 + js/racket-impl.rkt | 9 ++++ version.rkt | 2 +- 6 files changed, 100 insertions(+), 7 deletions(-) delete mode 100644 examples/raphael-script.js diff --git a/examples/raphael-demo.rkt b/examples/raphael-demo.rkt index 8e47dd2..91dd800 100644 --- a/examples/raphael-demo.rkt +++ b/examples/raphael-demo.rkt @@ -6,14 +6,13 @@ ; This is a small demonstration of the Javascript ; graphics library Raphael from http://raphaeljs.com/ . -; Include the small script in raphael-script.js to load Rapahel (old version 1.5.2). -; racket ../../whalesong.rkt build --include-script raphael-script.js raphael-demo.rkt ; The example below the bindings draws a Lissajous curve. ;;; ;;; Whalesong binding of Raphael +(load-script "http://yandex.st/raphael/1.5.2/raphael.js") ;;; (define paper #f) diff --git a/examples/raphael-script.js b/examples/raphael-script.js deleted file mode 100644 index 7ac70fc..0000000 --- a/examples/raphael-script.js +++ /dev/null @@ -1,4 +0,0 @@ - - - diff --git a/js/js-impl.js b/js/js-impl.js index c8397cc..4266c4b 100644 --- a/js/js-impl.js +++ b/js/js-impl.js @@ -5,7 +5,9 @@ "use strict"; var VOID = plt.baselib.constants.VOID_VALUE; + var PAUSE = plt.runtime.PAUSE; var makePrimitiveProcedure = plt.baselib.functions.makePrimitiveProcedure; + var makeClosure = plt.baselib.functions.makeClosure; var makeCheckArgumentType = plt.baselib.check.makeCheckArgumentType; var checkSymbolOrString = plt.baselib.check.checkSymbolOrString; var checkString = plt.baselib.check.checkString; @@ -22,6 +24,56 @@ var checkJsNumber = makeCheckArgumentType(isJsNumber, 'JavaScript number'); + + ////////////////////////////////////////////////////////////////////// + /* Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Contact information: + * Dao Gottwald + * + * @version 1.6 + * @url http://design-noir.de/webdev/JS/loadScript/ + */ + var _loadScriptQueue = {}; + var loadScript = function(url, callback, onError) { + var queue = _loadScriptQueue; + if (url in queue) { // script is already in the document + if (callback) { + if (queue[url]) // still loading + queue[url].push(callback); + else // loaded + callback(); + } + return; + } + queue[url] = callback ? [callback] : []; + var script = document.createElement("script"); + script.type = "text/javascript"; + script.onload = script.onreadystatechange = function() { + if (script.readyState && script.readyState != "loaded" && script.readyState != "complete") + return; + script.onreadystatechange = script.onload = null; + document.getElementsByTagName("head")[0].removeChild(script); + var work = queue[url]; + delete(queue[url]); + while (work.length) + work.shift()(); + }; + script.onerror = function() { + script.onreadystatechange = script.onload = null; + document.getElementsByTagName("head")[0].removeChild(script); + onError(); + }; + script.src = url; + document.getElementsByTagName("head")[0].appendChild(script); + }; + + + EXPORTS['alert'] = makePrimitiveProcedure( 'alert', @@ -42,6 +94,41 @@ return obj; }); + EXPORTS['load-script'] = + makeClosure( + 'load-script', + 1, + function(MACHINE) { + var url = checkString(MACHINE, 'load-string', 0); + PAUSE( + function(restart) { + var onload = function() { + restart(function(MACHINE) { + plt.runtime.finalizeClosureCall( + MACHINE, + VOID); + }); + }; + var onerror = function(e) { + restart(function(MACHINE) { + plt.baselib.exceptions.raiseFailure( + MACHINE, + plt.baselib.format.format( + "unable to load ~a: ~a", + [url, + e.message])); + }); + }; + loadScript(url.toString(), + onload, + onerror); + } + ); + }, + void(0)); + + + EXPORTS['body'] = $(document.body); EXPORTS['$'] = diff --git a/js/main.rkt b/js/main.rkt index e6a3b12..2326d3c 100644 --- a/js/main.rkt +++ b/js/main.rkt @@ -29,4 +29,6 @@ js-null js-eval + + load-script )) \ No newline at end of file diff --git a/js/racket-impl.rkt b/js/racket-impl.rkt index 071fe87..f0021a4 100644 --- a/js/racket-impl.rkt +++ b/js/racket-impl.rkt @@ -19,6 +19,8 @@ js-null js-eval + + load-script ) (define (alert x) @@ -90,3 +92,10 @@ (define (viewport-height) (error 'viewport-width "Not available outside JavaScript context.")) + + +;; load-script: string -> void +;; Load the url as a script. +(define (load-script url) + (error 'load-script "Not available outside JavaScript context.")) + diff --git a/version.rkt b/version.rkt index 5df3903..ae9a8f8 100644 --- a/version.rkt +++ b/version.rkt @@ -7,4 +7,4 @@ (provide version) (: version String) -(define version "1.189") +(define version "1.191")