trying to implement js-function and js-async-function

This commit is contained in:
Danny Yoo 2012-03-04 18:13:20 -05:00
parent 9b2bc3a3dc
commit 60387d58e5
4 changed files with 71 additions and 2 deletions

View File

@ -365,7 +365,7 @@
if (M.a === 1) {
inputPort = checkInputPort(M, 'read-byte', 0);
}
plt.runtime.PAUSE(function(restart) {
return plt.runtime.PAUSE(function(restart) {
inputPort.callWhenReady(M, function() {
restart(function(MACHINE) {
plt.runtime.finalizeClosureCall(MACHINE,

View File

@ -11,6 +11,8 @@
var makeCheckArgumentType = plt.baselib.check.makeCheckArgumentType;
var checkSymbolOrString = plt.baselib.check.checkSymbolOrString;
var checkString = plt.baselib.check.checkString;
var checkJSFunction = makeCheckArgumentType(function(x) { return typeof(x) === 'function'; },
"js function");
var checkAny = makeCheckArgumentType(function(x) { return true; },
"any");
@ -90,7 +92,7 @@
1,
function(MACHINE) {
var elt = MACHINE.e[MACHINE.e.length - 1];
var obj = eval(String(elt));
var obj = eval('(' + String(elt) + ')');
return obj;
});
@ -156,6 +158,64 @@
});
// Lift JavaScript functions to Whalesong functions.
EXPORTS['js-function'] =
makePrimitiveProcedure(
'js-function',
1,
function(MACHINE) {
var f = checkJSFunction(MACHINE, 'js function', 0);
return makePrimitiveProcedure(
'lifted js function',
plt.baselib.arity.makeArityAtLeast(0),
function(MACHINE) {
var args = [], i;
for (i = 0; i < MACHINE.a ; i = i+1) {
args.push(MACHINE.e[MACHINE.e.length - 1 - i]);
}
return f.call(null, args);
});
});
EXPORTS['js-async-function'] =
makePrimitiveProcedure(
'js-async-function',
1,
function(MACHINE) {
var f = checkJSFunction(MACHINE, 'js function', 0);
return makeClosure(
'lifted asynchronous js function',
plt.baselib.arity.makeArityAtLeast(0),
function(MACHINE) {
var args = [], i;
for (i = 0; i < MACHINE.a ; i = i+1) {
args.push(MACHINE.e[MACHINE.e.length - 1 - i]);
}
return plt.runtime.PAUSE(
function(restart) {
var onFail = function(e) {
restart(function(MACHINE) {
plt.baselib.exceptions.raiseFailure(
MACHINE,
plt.baselib.format.format(
"~a",
[((e && e.message) ? e.message : "unknown error")]));
});
};
var onSuccess = function(v) {
restart(function(MACHINE) {
plt.runtime.finalizeClosureCall(MACHINE, v);
});
}
args.unshift(onFail);
args.unshift(onSuccess);
return f.call(null, args);
});
});
});
EXPORTS['window'] = window;

View File

@ -7,6 +7,9 @@
body
call-method
$
js-function
js-async-function
window

View File

@ -99,3 +99,9 @@
(define (load-script url)
(error 'load-script "Not available outside JavaScript context."))
(define (js-function f)
(error 'js-function "Not available outside JavaScript context."))
(define (js-async-function f)
(error 'js-async-function "Not available outside JavaScript context."))