extending the ffi for attribute reading, writing, and raw strings

This commit is contained in:
Danny Yoo 2011-11-30 15:19:12 -05:00
parent 2f76431606
commit 8dd8dafdec
4 changed files with 151 additions and 57 deletions

View File

@ -1,68 +1,135 @@
var VOID = plt.baselib.constants.VOID_VALUE; /*jslint devel: true, browser: false, unparam: true, sub: true, windows: false, vars: true, white: true, maxerr: 50, indent: 4 */
var makePrimitiveProcedure = plt.baselib.functions.makePrimitiveProcedure;
EXPORTS['alert'] = /*global $,plt,EXPORTS,document,window*/
makePrimitiveProcedure( (function() {
'alert', "use strict";
1,
function(MACHINE) { var VOID = plt.baselib.constants.VOID_VALUE;
var elt = MACHINE.e[MACHINE.e.length - 1]; var makePrimitiveProcedure = plt.baselib.functions.makePrimitiveProcedure;
alert(String(elt)); var makeCheckArgumentType = plt.baselib.check.makeCheckArgumentType;
return VOID; var checkSymbolOrString = plt.baselib.check.checkSymbolOrString;
}); var checkAny = makeCheckArgumentType(function(x) { return true; },
"any");
EXPORTS['alert'] =
makePrimitiveProcedure(
'alert',
1,
function(MACHINE) {
var elt = MACHINE.e[MACHINE.e.length - 1];
alert(String(elt));
return VOID;
});
EXPORTS['body'] = $(document.body); EXPORTS['body'] = $(document.body);
EXPORTS['$'] = EXPORTS['$'] =
makePrimitiveProcedure( makePrimitiveProcedure(
'$', '$',
1, 1,
function(MACHINE) { function(MACHINE) {
var obj = MACHINE.e[MACHINE.e.length - 1]; var obj = MACHINE.e[MACHINE.e.length - 1];
return $(obj); return $(obj);
}); });
EXPORTS['call-method'] = EXPORTS['call-method'] =
makePrimitiveProcedure( makePrimitiveProcedure(
'call-method', 'call-method',
plt.baselib.arity.makeArityAtLeast(2), plt.baselib.arity.makeArityAtLeast(2),
function(MACHINE) { function(MACHINE) {
var obj = MACHINE.e[MACHINE.e.length - 1]; var obj = MACHINE.e[MACHINE.e.length - 1];
var methodName = MACHINE.e[MACHINE.e.length - 2]; var methodName = MACHINE.e[MACHINE.e.length - 2];
var args = []; var args = [], i;
for (var i = 0; i < MACHINE.a - 2; i++) { for (i = 0; i < MACHINE.a - 2; i = i+1) {
args.push(MACHINE.e[MACHINE.e.length -1 - 2 - i]); args.push(MACHINE.e[MACHINE.e.length -1 - 2 - i]);
} }
var result = obj[methodName].apply(obj, args); var result = obj[methodName].apply(obj, args);
return result; return result;
}); });
EXPORTS['window'] = window;
EXPORTS['get-attr'] =
makePrimitiveProcedure(
'get-attr',
plt.baselib.arity.makeArityAtLeast(2),
function(MACHINE) {
var obj = checkAny(MACHINE, 'get-attr', 0), attr, i;
for (i = 1; i < MACHINE.a; i = i + 1) {
attr = checkSymbolOrString(MACHINE, 'get-attr', i).toString();
obj = obj[attr];
}
return obj;
});
EXPORTS['set-attr!'] =
makePrimitiveProcedure(
'set-attr!',
3,
function(MACHINE) {
var obj = checkAny(MACHINE, 'set-attr!', 0);
var attr = checkSymbolOrString(MACHINE, 'set-attr!', 1).toString();
var val = checkAny(MACHINE, 'set-attr!', 2);
obj[attr] = val;
return VOID;
});
EXPORTS['js-string?'] =
makePrimitiveProcedure(
'js-string?',
1,
function(MACHINE) {
return typeof(checkAny(MACHINE, 'js-string?', 0)) === 'string';
});
EXPORTS['string->js-string'] =
makePrimitiveProcedure(
'string->js-string',
1,
function(MACHINE) {
return checkString(MACHINE, 'string->js-string', 0).toString();
});
EXPORTS['js-string->string'] =
makePrimitiveProcedure(
'js-string->string',
1,
function(MACHINE) {
return checkJsString(MACHINE, 'string->js-string', 0);
});
// Javascript-specific extensions. A small experiment.
EXPORTS['viewport-width'] =
makePrimitiveProcedure(
'viewport-width',
0,
function(MACHINE) {
return $(window).width();
});
EXPORTS['viewport-height'] =
makePrimitiveProcedure(
'viewport-height',
0,
function(MACHINE) {
return $(window).height();
});
EXPORTS['in-javascript-context?'] = // Javascript-specific extensions. A small experiment.
makePrimitiveProcedure( EXPORTS['viewport-width'] =
'in-javascript-context?', makePrimitiveProcedure(
0, 'viewport-width',
function(MACHINE) { 0,
return true; function(MACHINE) {
}); return $(window).width();
});
EXPORTS['viewport-height'] =
makePrimitiveProcedure(
'viewport-height',
0,
function(MACHINE) {
return $(window).height();
});
EXPORTS['in-javascript-context?'] =
makePrimitiveProcedure(
'in-javascript-context?',
0,
function(MACHINE) {
return true;
});
}());

View File

@ -8,6 +8,13 @@
call-method call-method
$ $
get-attr
set-attr!
js-string?
string->js-string
js-string->string
viewport-width viewport-width
viewport-height viewport-height
in-javascript-context? in-javascript-context?

View File

@ -18,6 +18,23 @@
'not-done-yet) 'not-done-yet)
(define (get-attr object attr . other-attrs)
(error 'get-attr "Not available outside JavaScript context"))
(define (set-attr! obj attr value)
(error 'set-attr! "Not available outside JavaScript context"))
(define (js-string? x)
(error 'js-string? "Not available outside JavaScript context"))
(define (string->js-string x)
(error 'string->js-string "Not available outside JavaScript context"))
(define (js-string->string x)
(error 'js-string->string "Not available outside JavaScript context"))
;; in-javascript-context: -> boolean ;; in-javascript-context: -> boolean

3
storage/storage.rkt Normal file
View File

@ -0,0 +1,3 @@
#lang s-exp "../lang/base.rkt"
(require "../js.rkt")