adding load-script to the FFI; modified examples/raphael-demo.rkt so that it loads the raphael library dynamically

This commit is contained in:
Danny Yoo 2012-02-27 21:38:52 -05:00
parent 7b2618bf14
commit d41aa187e3
6 changed files with 100 additions and 7 deletions

View File

@ -6,14 +6,13 @@
; This is a small demonstration of the Javascript ; This is a small demonstration of the Javascript
; graphics library Raphael from http://raphaeljs.com/ . ; 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. ; The example below the bindings draws a Lissajous curve.
;;; ;;;
;;; Whalesong binding of Raphael ;;; Whalesong binding of Raphael
(load-script "http://yandex.st/raphael/1.5.2/raphael.js")
;;; ;;;
(define paper #f) (define paper #f)

View File

@ -1,4 +0,0 @@
<script type="text/javascript" src="http://yandex.st/raphael/1.5.2/raphael.js">
</script>

View File

@ -5,7 +5,9 @@
"use strict"; "use strict";
var VOID = plt.baselib.constants.VOID_VALUE; var VOID = plt.baselib.constants.VOID_VALUE;
var PAUSE = plt.runtime.PAUSE;
var makePrimitiveProcedure = plt.baselib.functions.makePrimitiveProcedure; var makePrimitiveProcedure = plt.baselib.functions.makePrimitiveProcedure;
var makeClosure = plt.baselib.functions.makeClosure;
var makeCheckArgumentType = plt.baselib.check.makeCheckArgumentType; var makeCheckArgumentType = plt.baselib.check.makeCheckArgumentType;
var checkSymbolOrString = plt.baselib.check.checkSymbolOrString; var checkSymbolOrString = plt.baselib.check.checkSymbolOrString;
var checkString = plt.baselib.check.checkString; var checkString = plt.baselib.check.checkString;
@ -22,6 +24,56 @@
var checkJsNumber = makeCheckArgumentType(isJsNumber, 'JavaScript number'); 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 <dao at design-noir.de>
*
* @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'] = EXPORTS['alert'] =
makePrimitiveProcedure( makePrimitiveProcedure(
'alert', 'alert',
@ -42,6 +94,41 @@
return obj; 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['body'] = $(document.body);
EXPORTS['$'] = EXPORTS['$'] =

View File

@ -29,4 +29,6 @@
js-null js-null
js-eval js-eval
load-script
)) ))

View File

@ -19,6 +19,8 @@
js-null js-null
js-eval js-eval
load-script
) )
(define (alert x) (define (alert x)
@ -90,3 +92,10 @@
(define (viewport-height) (define (viewport-height)
(error 'viewport-width "Not available outside JavaScript context.")) (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."))

View File

@ -7,4 +7,4 @@
(provide version) (provide version)
(: version String) (: version String)
(define version "1.189") (define version "1.191")