moving javascript-specific functions off to the js module

This commit is contained in:
Danny Yoo 2011-07-14 15:33:04 -04:00
parent 06aaf51cb8
commit 619abb1fca
8 changed files with 86 additions and 69 deletions

View File

@ -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

View File

@ -1,5 +1,7 @@
#lang planet dyoo/whalesong
(require (planet dyoo/whalesong/js))
(when (in-javascript-context?)
(viewport-width))

View File

@ -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;
});
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

View File

@ -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;
});

View File

@ -6,4 +6,9 @@
#:provided-values (alert
body
call-method
$))
$
viewport-width
viewport-height
in-javascript-context?
))

View File

@ -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."))

View File

@ -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!)

View File

@ -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)