added exception catching
This commit is contained in:
parent
5d015c7db2
commit
95a3168659
|
@ -17,7 +17,7 @@
|
||||||
(newline op))
|
(newline op))
|
||||||
basic-blocks)
|
basic-blocks)
|
||||||
(fprintf op "MACHINE.cont = k;\n")
|
(fprintf op "MACHINE.cont = k;\n")
|
||||||
(fprintf op "trampoline(~a, function() {}); }"
|
(fprintf op "trampoline(~a, function() {}, function(e) { MACHINE.params.currentErrorHandler(e)}); }"
|
||||||
(BasicBlock-name (first basic-blocks)))))
|
(BasicBlock-name (first basic-blocks)))))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -83,22 +83,28 @@ function createXMLHTTPObject() {
|
||||||
|
|
||||||
|
|
||||||
var whenLoaded = function() {
|
var whenLoaded = function() {
|
||||||
var output = [];
|
var output = [], startTime, endTime;
|
||||||
MACHINE.params.currentDisplayer = function(v) {
|
MACHINE.params.currentDisplayer = function(v) {
|
||||||
output.push(String(v));
|
output.push(String(v));
|
||||||
};
|
};
|
||||||
setTimeout(
|
MACHINE.params.currentErrorHandler = function(e) {
|
||||||
|
endTime = new Date();
|
||||||
|
document.body.appendChild(document.createTextNode(
|
||||||
|
"Program evaluated; sending back to DrRacket."));
|
||||||
|
sendRequest("/eval", function(req) {},
|
||||||
|
"e=" + encodeURIComponent(String(e)) +
|
||||||
|
"&t=" + encodeURIComponent(String(endTime - startTime)));
|
||||||
|
};
|
||||||
|
startTime = new Date();
|
||||||
|
invoke(
|
||||||
function() {
|
function() {
|
||||||
var startTime = new Date();
|
endTime = new Date();
|
||||||
invoke(function() {
|
document.body.appendChild(document.createTextNode(
|
||||||
var endTime = new Date();
|
"Program evaluated; sending back to DrRacket."));
|
||||||
document.body.appendChild(document.createTextNode(
|
sendRequest("/eval", function(req) {},
|
||||||
"Program evaluated; sending back to DrRacket."));
|
"r=" + encodeURIComponent(output.join('')) +
|
||||||
sendRequest("/eval", function(req) {},
|
"&t=" + encodeURIComponent(String(endTime - startTime)));
|
||||||
"r=" + encodeURIComponent(output.join('')) +
|
});
|
||||||
"&t=" + encodeURIComponent(String(endTime - startTime)));
|
|
||||||
});
|
|
||||||
}, 0);
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
@ -114,12 +120,21 @@ EOF
|
||||||
empty
|
empty
|
||||||
(list #"" (get-output-bytes op))))]
|
(list #"" (get-output-bytes op))))]
|
||||||
|
|
||||||
|
;; Normal result came back
|
||||||
|
|
||||||
[(exists-binding? 'r (request-bindings req))
|
[(exists-binding? 'r (request-bindings req))
|
||||||
(channel-put ch (list (extract-binding/single 'r (request-bindings req))
|
(channel-put ch (list (extract-binding/single 'r (request-bindings req))
|
||||||
(extract-binding/single 't (request-bindings req))))
|
(string->number
|
||||||
|
(extract-binding/single 't (request-bindings req)))))
|
||||||
`(html (body (p "ok")))]
|
`(html (body (p "ok")))]
|
||||||
|
|
||||||
|
;; Error occurred
|
||||||
|
[(exists-binding? 'e (request-bindings req))
|
||||||
|
(channel-put ch (make-error-happened
|
||||||
|
(extract-binding/single 'e (request-bindings req))
|
||||||
|
(string->number
|
||||||
|
(extract-binding/single 't (request-bindings req)))))
|
||||||
|
`(html (body (p "ok")))]
|
||||||
|
|
||||||
[else
|
[else
|
||||||
`(html (body (p "Loaded")))]))
|
`(html (body (p "Loaded")))]))
|
||||||
|
|
||||||
|
@ -131,6 +146,8 @@ EOF
|
||||||
#:servlet-path "/eval"))))
|
#:servlet-path "/eval"))))
|
||||||
|
|
||||||
|
|
||||||
|
(define-struct error-happened (str t) #:transparent)
|
||||||
|
|
||||||
|
|
||||||
;; evaluate: sexp -> (values string number)
|
;; evaluate: sexp -> (values string number)
|
||||||
;; A little driver to test the evalution of expressions, using a browser to help.
|
;; A little driver to test the evalution of expressions, using a browser to help.
|
||||||
|
@ -140,5 +157,8 @@ EOF
|
||||||
(send-url (format "http://localhost:~a/eval?p=t" port) #f)
|
(send-url (format "http://localhost:~a/eval?p=t" port) #f)
|
||||||
(channel-put ch e)
|
(channel-put ch e)
|
||||||
(let ([output+time (channel-get ch)])
|
(let ([output+time (channel-get ch)])
|
||||||
(values (first output+time)
|
(cond [(error-happened? output+time)
|
||||||
(string->number (second output+time)))))
|
(raise output+time)]
|
||||||
|
[else
|
||||||
|
(values (first output+time)
|
||||||
|
(second output+time))])))
|
19
runtime.js
19
runtime.js
|
@ -92,7 +92,7 @@ var Closure = function(env, label) {
|
||||||
// JavaScript toplevel.
|
// JavaScript toplevel.
|
||||||
Closure.prototype.adaptToJs = function() {
|
Closure.prototype.adaptToJs = function() {
|
||||||
var that = this;
|
var that = this;
|
||||||
return function(args, k) {
|
return function(args, success, fail) {
|
||||||
var oldEnv = MACHINE.env;
|
var oldEnv = MACHINE.env;
|
||||||
var oldCont = MACHINE.cont;
|
var oldCont = MACHINE.cont;
|
||||||
var oldProc = MACHINE.proc;
|
var oldProc = MACHINE.proc;
|
||||||
|
@ -114,13 +114,16 @@ Closure.prototype.adaptToJs = function() {
|
||||||
MACHINE.proc = oldProc;
|
MACHINE.proc = oldProc;
|
||||||
MACHINE.argl = oldArgl;
|
MACHINE.argl = oldArgl;
|
||||||
MACHINE.val = oldVal;
|
MACHINE.val = oldVal;
|
||||||
k(result);
|
success(result);
|
||||||
};
|
};
|
||||||
|
|
||||||
proc.label();
|
proc.label();
|
||||||
},
|
},
|
||||||
function() {
|
function() {
|
||||||
});
|
},
|
||||||
|
function(e) {
|
||||||
|
return fail(e);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -133,11 +136,11 @@ var MACHINE={callsBeforeTrampoline: 100,
|
||||||
val:undefined,
|
val:undefined,
|
||||||
cont:undefined,
|
cont:undefined,
|
||||||
stack: [],
|
stack: [],
|
||||||
params: {currentDisplayer: function(v) {}}};
|
params: {currentDisplayer: function(v) {},
|
||||||
|
currentErrorHandler: function(e) {}}};
|
||||||
|
|
||||||
|
|
||||||
// harness: (->) (->) -> void
|
var trampoline = function(initialJump, success, fail) {
|
||||||
var trampoline = function(initialJump, k) {
|
|
||||||
var thunk = initialJump;
|
var thunk = initialJump;
|
||||||
MACHINE.callsBeforeTrampoline = 100;
|
MACHINE.callsBeforeTrampoline = 100;
|
||||||
while(thunk) {
|
while(thunk) {
|
||||||
|
@ -149,9 +152,9 @@ var trampoline = function(initialJump, k) {
|
||||||
thunk = e;
|
thunk = e;
|
||||||
MACHINE.callsBeforeTrampoline = 100;
|
MACHINE.callsBeforeTrampoline = 100;
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
return fail(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
k();
|
return success();
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,24 +1,29 @@
|
||||||
#lang racket/base
|
#lang racket
|
||||||
(require "browser-evaluate.rkt")
|
(require "browser-evaluate.rkt")
|
||||||
|
|
||||||
(evaluate '(begin (define (f x)
|
;; test-find-toplevel-variables
|
||||||
(if (= x 0)
|
(define-syntax (test stx)
|
||||||
1
|
(syntax-case stx ()
|
||||||
(+ x (f (- x 1)))))
|
[(_ s exp)
|
||||||
(display (f 3))
|
(with-syntax ([stx stx])
|
||||||
(display "\n")
|
(syntax/loc #'stx
|
||||||
(display (f 4))
|
(let-values ([(output time) (evaluate s)])
|
||||||
(display "\n")
|
(unless (string=? output exp)
|
||||||
(display (f 10000))))
|
(raise-syntax-error #f (format "Expected ~s, got ~s" exp output)
|
||||||
|
#'stx)))))]))
|
||||||
|
|
||||||
|
|
||||||
|
(test '(begin (define (f x)
|
||||||
|
(if (= x 0)
|
||||||
|
0
|
||||||
|
(+ x (f (- x 1)))))
|
||||||
|
(display (f 3))
|
||||||
|
(display "\n")
|
||||||
|
(display (f 4))
|
||||||
|
(display "\n")
|
||||||
|
(display (f 10000)))
|
||||||
|
"6\n10\n50005000")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(evaluate '(begin (define (f x)
|
"ok"
|
||||||
(if (= x 0)
|
|
||||||
1
|
|
||||||
(+ x (f (- x 1)))))
|
|
||||||
(display (f 3))
|
|
||||||
(display "\n")
|
|
||||||
(display (f 4))
|
|
||||||
(display "\n")
|
|
||||||
(display (f 100000))))
|
|
Loading…
Reference in New Issue
Block a user