coersing example is working finally

This commit is contained in:
Danny Yoo 2011-07-11 19:23:01 -04:00
parent b9e7ae57e6
commit c13d181361
5 changed files with 30 additions and 19 deletions

View File

@ -225,7 +225,7 @@ MACHINE.modules[~s] =
(define (on-last-src) (define (on-last-src)
(fprintf op "console.log('module loaded'); plt.runtime.setReadyTrue();") (fprintf op "plt.runtime.setReadyTrue();")
(fprintf op "SUCCESS();")) (fprintf op "SUCCESS();"))
@ -247,7 +247,7 @@ MACHINE.modules[~s] =
(fprintf op "var invoke = (function(MACHINE, SUCCESS, FAIL, PARAMS) {") (fprintf op "var invoke = (function(MACHINE, SUCCESS, FAIL, PARAMS) {")
(fprintf op " plt.runtime.ready(function() {") (fprintf op " plt.runtime.ready(function() {")
(fprintf op "console.log('loading module'); plt.runtime.setReadyFalse();") (fprintf op "plt.runtime.setReadyFalse();")
(make (list (make-MainModuleSource source-code)) (make (list (make-MainModuleSource source-code))
packaging-configuration) packaging-configuration)
(fprintf op " });"); (fprintf op " });");

View File

@ -20,7 +20,7 @@
// The function will run on the provided MACHINE. // The function will run on the provided MACHINE.
// //
// It assumes that it must begin its own trampoline. // It assumes that it must begin its own trampoline.
var coerseToJavaScript = function(v, MACHINE) { var asJavaScriptFunction = function(v, MACHINE) {
MACHINE = MACHINE || plt.runtime.currentMachine; MACHINE = MACHINE || plt.runtime.currentMachine;
if (isPrimitiveProcedure(v)) { if (isPrimitiveProcedure(v)) {
return coersePrimitiveToJavaScript(v, MACHINE); return coersePrimitiveToJavaScript(v, MACHINE);
@ -65,6 +65,7 @@
var oldVal = MACHINE.val; var oldVal = MACHINE.val;
var oldArgcount = MACHINE.argcount; var oldArgcount = MACHINE.argcount;
var oldProc = MACHINE.proc;
var oldErrorHandler = MACHINE.params['currentErrorHandler']; var oldErrorHandler = MACHINE.params['currentErrorHandler'];
var afterGoodInvoke = function(MACHINE) { var afterGoodInvoke = function(MACHINE) {
@ -72,7 +73,8 @@
var returnValue = MACHINE.val; var returnValue = MACHINE.val;
MACHINE.val = oldVal; MACHINE.val = oldVal;
MACHINE.argcount = oldArgcount; MACHINE.argcount = oldArgcount;
succ(MACHINE.val); MACHINE.proc = oldProc;
succ(returnValue);
}; };
afterGoodInvoke.multipleValueReturn = function(MACHINE) { afterGoodInvoke.multipleValueReturn = function(MACHINE) {
MACHINE.params['currentErrorHandler'] = oldErrorHandler; MACHINE.params['currentErrorHandler'] = oldErrorHandler;
@ -82,6 +84,7 @@
} }
MACHINE.val = oldVal; MACHINE.val = oldVal;
MACHINE.argcount = oldArgcount; MACHINE.argcount = oldArgcount;
MACHINE.proc = oldProc;
succ.apply(null, returnValues); succ.apply(null, returnValues);
}; };
@ -93,8 +96,15 @@
for (var i = 0; i < args.length; i++) { for (var i = 0; i < args.length; i++) {
MACHINE.env.push(args[i]); MACHINE.env.push(args[i]);
} }
plt.runtime.trampoline(MACHINE, MACHINE.proc = v;
entryPoint); MACHINE.params['currentErrorHandler'] = function(MACHINE, e) {
MACHINE.params['currentErrorHandler'] = oldErrorHandler;
MACHINE.val = oldVal;
MACHINE.argcount = oldArgcount;
MACHINE.proc = oldProc;
fail(e);
};
plt.runtime.trampoline(MACHINE, v.label);
}, },
0); 0);
}; };
@ -199,6 +209,6 @@
exports.isFunction = isFunction; exports.isFunction = isFunction;
exports.coerseToJavaScript = coerseToJavaScript; exports.asJavaScriptFunction = asJavaScriptFunction;
})(this['plt'].baselib); })(this['plt'].baselib);

View File

@ -1785,7 +1785,6 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
(function(scope) { (function(scope) {
scope.ready = function(f) { scope.ready = function(f) {
console.log('request');
if (runtimeIsReady) { if (runtimeIsReady) {
notifyWaiter(f); notifyWaiter(f);
} else { } else {
@ -1794,7 +1793,6 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
}; };
scope.setReadyTrue = function() { scope.setReadyTrue = function() {
console.log("runtime is ready");
runtimeIsReady = true; runtimeIsReady = true;
while(runtimeIsReady && readyWaiters.length > 0) { while(runtimeIsReady && readyWaiters.length > 0) {
notifyWaiter(readyWaiters.shift()); notifyWaiter(readyWaiters.shift());
@ -1802,7 +1800,6 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
}; };
scope.setReadyFalse = function() { scope.setReadyFalse = function() {
console.log("disabled runtime");
runtimeIsReady = false; runtimeIsReady = false;
}; };
@ -1810,7 +1807,6 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
var runtimeIsReady = false; var runtimeIsReady = false;
var readyWaiters = []; var readyWaiters = [];
var notifyWaiter = function(w) { var notifyWaiter = function(w) {
console.log('notifying waiter');
w(); w();
}; };
})(this); })(this);
@ -1823,6 +1819,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
// Executes all programs that have been labeled as a main module // Executes all programs that have been labeled as a main module
var invokeMains = function(machine, succ, fail) { var invokeMains = function(machine, succ, fail) {
runtime.ready(function() { runtime.ready(function() {
setReadyFalse();
machine = machine || runtime.currentMachine; machine = machine || runtime.currentMachine;
succ = succ || function() {}; succ = succ || function() {};
fail = fail || function() {}; fail = fail || function() {};
@ -1832,6 +1829,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
var nextModule = mainModules.shift(); var nextModule = mainModules.shift();
nextModule.invoke(machine, loop, fail); nextModule.invoke(machine, loop, fail);
} else { } else {
setReadyTrue();
succ(); succ();
} }
}; };

View File

@ -8,4 +8,4 @@
(* x (fact (sub1 x)))])) (* x (fact (sub1 x)))]))
(printf "fact invoked\n") (printf "test: ~s\n" (fact 4))

View File

@ -3,20 +3,23 @@
<script src="runtime.js"></script> <script src="runtime.js"></script>
<script src="fact.js"></script> <script src="fact.js"></script>
<script> <script>
</script>
<script>
plt.runtime.invokeMains(); plt.runtime.invokeMains();
</script>
<script>
plt.runtime.ready(function() { plt.runtime.ready(function() {
var myFact = plt.runtime.lookupInMains('fact'); var myFactClosure = plt.runtime.lookupInMains('fact');
var myFact = plt.baselib.functions.asJavaScriptFunction(
myFactClosure);
$(document.body).append("The factorial of 1000 is: ");
myFact(function(v) { myFact(function(v) {
alert(v.toString()); $(document.body).append(v.toString());
}, },
function(err) { function(err) {
alert(err); $(document.body).append(err);
}, },
1000); 1000);
}); });