From 619abb1fca213730b83292641fb115f7af48bdc6 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Thu, 14 Jul 2011 15:33:04 -0400 Subject: [PATCH] moving javascript-specific functions off to the js module --- Makefile | 3 +- examples/window-size.rkt | 2 ++ js-assembler/runtime-src/runtime.js | 48 +++++++++-------------------- js/js-impl.js | 31 ++++++++++++++++++- js/main.rkt | 7 ++++- js/racket-impl.rkt | 26 +++++++++++++++- lang/kernel.rkt | 31 +------------------ tests/test-browser-evaluate.rkt | 7 ++++- 8 files changed, 86 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index 75779bb..1fca108 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ launcher: raco make -v --disable-inline whalesong.rkt racket make-launcher.rkt -whalesong: +whalesong: raco make -v --disable-inline whalesong.rkt test-all: @@ -39,5 +39,6 @@ test-conform: + doc: scribble ++xref-in setup/xref load-collections-xref --redirect-main http://docs.racket-lang.org/ --dest generated-docs --dest-name index.html scribblings/manual.scrbl \ No newline at end of file diff --git a/examples/window-size.rkt b/examples/window-size.rkt index 8475a84..2ddf635 100644 --- a/examples/window-size.rkt +++ b/examples/window-size.rkt @@ -1,5 +1,7 @@ #lang planet dyoo/whalesong +(require (planet dyoo/whalesong/js)) + (when (in-javascript-context?) (viewport-width)) diff --git a/js-assembler/runtime-src/runtime.js b/js-assembler/runtime-src/runtime.js index 01d34e6..d24476d 100644 --- a/js-assembler/runtime-src/runtime.js +++ b/js-assembler/runtime-src/runtime.js @@ -568,7 +568,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; } plt.baselib.arity.makeArityAtLeast(1), function(MACHINE) { var args = [], i, formatString; - formatString = checkString(MACHINE, 'format', 0); + formatString = checkString(MACHINE, 'format', 0).toString(); for(i = 1; i < MACHINE.argcount; i++) { args.push(MACHINE.env[MACHINE.env.length - 1 - i]); } @@ -581,7 +581,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; } plt.baselib.arity.makeArityAtLeast(1), function(MACHINE) { var args = [], i, formatString, result, outputPort; - formatString = checkString(MACHINE, 'printf', 0); + formatString = checkString(MACHINE, 'printf', 0).toString(); for(i = 1; i < MACHINE.argcount; i++) { args.push(MACHINE.env[MACHINE.env.length - 1 - i]); } @@ -598,7 +598,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; } function(MACHINE) { var args = [], i, formatString, outputPort, result; outputPort = checkOutputPort(MACHINE, 'fprintf', 0); - formatString = checkString(MACHINE, 'fprintf', 1); + formatString = checkString(MACHINE, 'fprintf', 1).toString(); for(i = 2; i < MACHINE.argcount; i++) { args.push(MACHINE.env[MACHINE.env.length - 1 - i]); } @@ -1001,7 +1001,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; } var buffer = []; var i; for (i = 0; i < MACHINE.argcount; i++) { - buffer.push(checkString(MACHINE, 'string-append', i)); + buffer.push(checkString(MACHINE, 'string-append', i).toString()); } return buffer.join(''); }); @@ -1010,7 +1010,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; } 'string-length', 1, function(MACHINE) { - var firstArg = checkString(MACHINE, 'string-length', 0); + var firstArg = checkString(MACHINE, 'string-length', 0).toString(); return firstArg.length; }); @@ -1485,12 +1485,20 @@ if(this['plt'] === undefined) { this['plt'] = {}; } }); + installPrimitiveProcedure( + 'string->symbol', + 1, + function(MACHINE) { + return makeSymbol(checkString(MACHINE, 'string->symbol', 0).toString()); + }); + + installPrimitiveProcedure( 'string->number', 1, function(MACHINE) { return plt.baselib.numbers.fromString( - checkString(MACHINE, 'string->number', 0)); + checkString(MACHINE, 'string->number', 0).toString()); }); @@ -1663,34 +1671,6 @@ if(this['plt'] === undefined) { this['plt'] = {}; } - - - // Javascript-specific extensions. A small experiment. - installPrimitiveProcedure( - 'viewport-width', - 0, - function(MACHINE) { - return $(window).width(); - }); - - installPrimitiveProcedure( - 'viewport-height', - 0, - function(MACHINE) { - return $(window).height(); - }); - - - installPrimitiveProcedure( - 'in-javascript-context?', - 0, - function(MACHINE) { - return true; - }); - - - - ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// diff --git a/js/js-impl.js b/js/js-impl.js index 3e86f0c..7a93cba 100644 --- a/js/js-impl.js +++ b/js/js-impl.js @@ -23,7 +23,7 @@ EXPORTS['$'] = EXPORTS['call-method'] = RUNTIME.makePrimitiveProcedure( 'call-method', - new RUNTIME.ArityAtLeast(2), + plt.baselib.arity.makeArityAtLeast(2), function(MACHINE) { var obj = MACHINE.env[MACHINE.env.length - 1]; var methodName = MACHINE.env[MACHINE.env.length - 2]; @@ -34,3 +34,32 @@ EXPORTS['call-method'] = var result = obj[methodName].apply(obj, args); return result; }); + + + + +// Javascript-specific extensions. A small experiment. +EXPORTS['viewport-width'] = + RUNTIME.makePrimitiveProcedure( + 'viewport-width', + 0, + function(MACHINE) { + return $(window).width(); + }); + +EXPORTS['viewport-height'] = + RUNTIME.makePrimitiveProcedure( + 'viewport-height', + 0, + function(MACHINE) { + return $(window).height(); + }); + + +EXPORTS['in-javascript-context?'] = + RUNTIME.makePrimitiveProcedure( + 'in-javascript-context?', + 0, + function(MACHINE) { + return true; + }); diff --git a/js/main.rkt b/js/main.rkt index 02d1d03..f07bd97 100644 --- a/js/main.rkt +++ b/js/main.rkt @@ -6,4 +6,9 @@ #:provided-values (alert body call-method - $)) \ No newline at end of file + $ + + viewport-width + viewport-height + in-javascript-context? + )) \ No newline at end of file diff --git a/js/racket-impl.rkt b/js/racket-impl.rkt index 910e549..86a26e1 100644 --- a/js/racket-impl.rkt +++ b/js/racket-impl.rkt @@ -1,6 +1,9 @@ #lang s-exp "../lang/base.rkt" -(provide alert body call-method $) +(provide alert body call-method $ + in-javascript-context? + viewport-width + viewport-height) (define (alert x) (display x) @@ -13,3 +16,24 @@ (define ($ name) 'not-done-yet) + + + + +;; in-javascript-context: -> boolean +;; Produces true if we're in a JavaScript context. +(define (in-javascript-context?) + #f) + + +;; viewport-width: -> natural +;; The viewport width in pixels. +(define (viewport-width) + (error 'viewport-width "Not available outside JavaScript context.")) + + +;; viewport-height: -> natural +;; The viewport height in pixels. +(define (viewport-height) + (error 'viewport-width "Not available outside JavaScript context.")) + diff --git a/lang/kernel.rkt b/lang/kernel.rkt index 73a9661..5b13939 100644 --- a/lang/kernel.rkt +++ b/lang/kernel.rkt @@ -342,7 +342,7 @@ memq ;; string->list ;; list->string ;; string-copy -;; string->symbol +string->symbol symbol->string format printf @@ -412,35 +412,6 @@ symbol->string -(provide - ;; FIXME: - ;; Extensions: these may need to be hidden in a JavaScript-implemented module - in-javascript-context? - viewport-width - viewport-height) - - - -;; in-javascript-context: -> boolean -;; Produces true if we're in a JavaScript context. -(define (in-javascript-context?) - #f) - - -;; viewport-width: -> natural -;; The viewport width in pixels. -(define (viewport-width) - (error 'viewport-width "Not available outside JavaScript context.")) - - -;; viewport-height: -> natural -;; The viewport height in pixels. -(define (viewport-height) - (error 'viewport-width "Not available outside JavaScript context.")) - - - - (provide set-car! set-cdr!) diff --git a/tests/test-browser-evaluate.rkt b/tests/test-browser-evaluate.rkt index 8be8563..03273ee 100644 --- a/tests/test-browser-evaluate.rkt +++ b/tests/test-browser-evaluate.rkt @@ -271,7 +271,12 @@ EOF (displayln (fib 5)) (displayln (fib 6))) "2\n3\n5\n8\n") - + + +(test '(displayln (eq? (string->symbol "hello") + 'hello)) + "true\n") + (test '(begin (define (tak x y z) (if (>= y x)