catch/throw added to Java Factor

cvs
Slava Pestov 2004-07-17 23:33:35 +00:00
parent 4f4e27b8c7
commit 683c9e2af6
17 changed files with 53 additions and 181 deletions

Binary file not shown.

View File

@ -49,7 +49,6 @@ public class FactorInterpreter implements FactorObject, Runnable
// boot.factor sets these.
public boolean interactive = true;
public boolean errorFlag = false;
public Throwable error;
public boolean dump = false;
public boolean verboseCompile = false;
@ -353,11 +352,6 @@ public class FactorInterpreter implements FactorObject, Runnable
global.setVariable("interpreter",this);
global.setVariable("error-flag",
new FactorNamespace.VarBinding(
getClass().getField("errorFlag"),
this));
global.setVariable("verbose-compile",
new FactorNamespace.VarBinding(
getClass().getField("verboseCompile"),
@ -445,12 +439,6 @@ public class FactorInterpreter implements FactorObject, Runnable
else
eval(searchVocabulary(INIT_VOCAB,"boot"));
//XXX messy
run();
if(errorFlag)
run();
run();
} //}}}
@ -490,42 +478,22 @@ public class FactorInterpreter implements FactorObject, Runnable
//{{{ handleError() method
private boolean handleError(Throwable e)
{
if(errorFlag)
error = FactorJava.unwrapException(e);
datastack.push(error);
try
{
System.err.println("Exception inside"
+ " error handler:");
eval(searchVocabulary(ERRORS_VOCAB,"throw"));
return false;
}
catch(Throwable e2)
{
System.err.println("Exception when calling throw:");
e.printStackTrace();
System.err.println("Original exception:");
error.printStackTrace();
System.err.println("Factor datastack:");
System.err.println(datastack.toList());
System.err.println("Factor callstack:");
System.err.println(callstack.toList());
topLevel();
return true;
}
else
{
errorFlag = true;
error = FactorJava.unwrapException(e);
datastack.push(error);
try
{
eval(searchVocabulary(ERRORS_VOCAB,"throw"));
return false;
}
catch(Throwable e2)
{
System.err.println("Exception when calling throw:");
e.printStackTrace();
topLevel();
return true;
}
}
} //}}}
//{{{ createCompiledCallframe() method
@ -755,8 +723,10 @@ public class FactorInterpreter implements FactorObject, Runnable
namestack.top = 0;
namestack.push(global);
catchstack.top = 0;
catchstack.push(searchVocabulary(ERRORS_VOCAB,
"default-error-handler"));
// DEFER: the word
define(ERRORS_VOCAB,"default-error-handler");
catchstack.push(new Cons(searchVocabulary(ERRORS_VOCAB,
"default-error-handler"),null));
callframe = null;
} //}}}
}

View File

@ -29,19 +29,13 @@ IN: errors
USE: arithmetic
USE: combinators
USE: continuations
USE: inspector
USE: kernel
USE: lists
USE: namespaces
USE: stack
USE: stdio
USE: strings
USE: unparser
USE: vectors
: catchstack ( -- cs ) catchstack* clone ;
: set-catchstack ( cs -- ) clone set-catchstack* ;
: >c ( catch -- )
#! Push a catch block on the catchstack. Use the catch word
#! instead of invoking this word directly.
@ -52,30 +46,19 @@ USE: vectors
#! instead of invoking this word directly.
catchstack* vector-pop ;
: default-error-handler ( error -- )
#! Print the error and return to the top level.
"Uncaught exception." print
"-------------------" print
terpri
"Datastack:" print
.s
terpri
"Callstack:" print
.r
terpri
"Namestack:" print
.n
terpri
"ERROR: " write error>str print
suspend ;
: >pop> ( stack -- stack )
dup vector-pop drop ;
: save-error ( -- )
: save-error ( error -- )
#! Save the stacks for most-mortem inspection after an
#! error.
datastack "error-datastack" set
callstack dup vector-pop drop "error-callstack" set
namestack "error-namestack" set
catchstack "error-catchstack" set ;
global [
"error" set
datastack >pop> "error-datastack" set
callstack >pop> >pop> "error-callstack" set
namestack "error-namestack" set
catchstack "error-catchstack" set
] bind ;
: catch ( try catch -- )
#! Call the try quotation, restore the datastack to its
@ -92,4 +75,4 @@ USE: vectors
: throw ( error -- )
#! Throw an error. If no catch handlers are installed, the
#! default-error-handler is called.
save-error rethrow ;
dup save-error rethrow ;

View File

@ -27,6 +27,7 @@
IN: inspector
USE: combinators
USE: errors
USE: format
USE: kernel
USE: lists
@ -87,6 +88,7 @@ USE: vocabularies
: .n namestack describe-stack ;
: .s datastack describe-stack ;
: .r callstack describe-stack ;
: .c catchstack describe-stack ;
: describe-object-path ( string -- )
<namespace> [

View File

@ -29,7 +29,6 @@ USE: parser
!!! The standard library.
"/library/platform/jvm/kernel.factor" run-resource ! kernel
"/library/platform/jvm/errors.factor" run-resource ! errors
"/library/platform/jvm/vectors.factor" run-resource ! vectors
"/library/platform/jvm/stack.factor" run-resource ! stack
"/library/logic.factor" run-resource ! logic
@ -49,12 +48,14 @@ USE: parser
"/library/platform/jvm/strings.factor" run-resource ! strings
"/library/platform/jvm/sbuf.factor" run-resource ! strings
"/library/strings.factor" run-resource ! strings
"/library/platform/jvm/errors.factor" run-resource ! errors
"/library/platform/jvm/namespaces.factor" run-resource ! namespaces
"/library/namespaces.factor" run-resource ! namespaces
"/library/sbuf.factor" run-resource ! strings
"/library/list-namespaces.factor" run-resource ! namespaces
"/library/math/namespace-math.factor" run-resource ! arithmetic
"/library/continuations.factor" run-resource ! continuations
"/library/errors.factor" run-resource ! errors
"/library/platform/jvm/vocabularies.factor" run-resource ! vocabularies
"/library/vocabularies.factor" run-resource ! vocabularies
"/library/platform/jvm/words.factor" run-resource ! words
@ -85,14 +86,9 @@ USE: parser
"/library/inspector.factor" run-resource ! inspector
"/library/inspect-vocabularies.factor" run-resource ! inspector
"/library/platform/jvm/compiler.factor" run-resource ! compiler
"/library/debugger.factor" run-resource ! debugger
"/library/platform/jvm/debugger.factor" run-resource ! debugger
!!! Final initialization...
! Avoid an error from the parser about boot not being defined
IN: kernel DEFER: boot
"/library/init.factor" run-resource ! init
"/library/platform/jvm/init.factor" run-resource ! init
boot

View File

@ -29,7 +29,6 @@ USE: parser
!!! The standard library.
"/library/platform/jvm/kernel.factor" run-resource ! kernel
"/library/platform/jvm/errors.factor" run-resource ! errors
"/library/platform/jvm/vectors.factor" run-resource ! vectors
"/library/platform/jvm/stack.factor" run-resource ! stack
"/library/logic.factor" run-resource ! logic
@ -50,12 +49,14 @@ USE: parser
"/library/platform/jvm/strings.factor" run-resource ! strings
"/library/platform/jvm/sbuf.factor" run-resource ! strings
"/library/strings.factor" run-resource ! strings
"/library/platform/jvm/errors.factor" run-resource ! errors
"/library/platform/jvm/namespaces.factor" run-resource ! namespaces
"/library/namespaces.factor" run-resource ! namespaces
"/library/sbuf.factor" run-resource ! strings
"/library/list-namespaces.factor" run-resource ! namespaces
"/library/math/namespace-math.factor" run-resource ! arithmetic
"/library/continuations.factor" run-resource ! continuations
"/library/errors.factor" run-resource ! errors
"/library/platform/jvm/vocabularies.factor" run-resource ! vocabularies
"/library/vocabularies.factor" run-resource ! vocabularies
"/library/platform/jvm/words.factor" run-resource ! words
@ -91,6 +92,7 @@ USE: parser
"/library/inspector.factor" run-resource ! inspector
"/library/inspect-vocabularies.factor" run-resource ! inspector
"/library/platform/jvm/compiler.factor" run-resource ! compiler
"/library/debugger.factor" run-resource ! debugger
"/library/platform/jvm/debugger.factor" run-resource ! debugger
"/library/platform/jvm/listener.factor" run-resource ! listener
"/library/test/test.factor" run-resource ! test
@ -115,11 +117,5 @@ USE: parser
"/library/httpd/default-responders.factor" run-resource ! default-responders
!!! Final initialization...
! Avoid an error from the parser about boot not being defined
IN: kernel DEFER: boot
"/library/init.factor" run-resource ! init
"/library/platform/jvm/init.factor" run-resource ! init
boot

View File

@ -86,8 +86,12 @@ IN: parser
!!!
IN: init DEFER: boot
interpreter "factor.FactorInterpreter" "mini" jvar-get [
"/library/platform/jvm/boot-mini.factor" run-resource
] [
"/library/platform/jvm/boot-sumo.factor" run-resource
] ifte
boot

View File

@ -27,35 +27,12 @@
IN: debugger
USE: combinators
USE: continuations
USE: inspector
USE: interpreter
USE: kernel
USE: namespaces
USE: stack
USE: stdio
USE: strings
USE: unparser
: :g ( -- )
#! Continues execution from the point of the error. Can be
#! dangerous.
"error-continuation" get call ;
: :x ( -- )
#! Returns to the top level.
!XXX
"initial-interpreter-continuation" get dup [
call
] [
suspend
] ifte ;
: :s ( -- )
#! Returns to the top level, retaining the stack.
"initial-interpreter-callstack" get
set-callstack ;
: exception? ( exception -- boolean )
"java.lang.Throwable" is ;
@ -63,67 +40,9 @@ USE: unparser
[ ] "java.lang.Throwable" "printStackTrace" jinvoke ;
: :j ( -- )
! Print the stack trace from the exception that caused the
! last break.
#! Print the stack trace from the last exception.
"error" get dup exception? [
print-stack-trace
] [
"Not an exception: " write .
] ifte ;
: :r ( -- )
#! Print the callstack of the last error.
"error-callstack" get describe-stack ;
: exception. ( exception -- )
#! If this is an Factor exception, just print the message,
#! otherwise print the entire exception as a string.
dup "factor.FactorException" is [
[ ] "java.lang.Throwable" "getMessage" jinvoke
] [
>str
] ifte print ;
: debugger-help ( -- )
"break called." print
"" print
":r prints the callstack." print
":j prints the Java stack." print
":x returns to top level." print
":s returns to top level, retaining the data stack." print
":g continues execution (but expect another error)." print
"" print ;
: clear-error-flag ( -- )
global [ f "error-flag" set ] bind ;
! So that Java code can call it
IN: kernel
: break ( exception -- )
#! Called when the interpreter catches an exception.
callstack
<namespace> [
"error-callstack" set
dup "error" set
"error-stdio" get "stdio" set
debugger-help
"ERROR: " write exception.
! XXX: move this to the game core!
"console" get [
[ t "expanded" set ] bind
] when*
[
"error-continuation" set
clear-error-flag
" DEBUG. " interpreter-loop
! If we end up here, the user just exited the err
! interpreter. If we just call return-from-error
! here, its like :g and this is probably not what
! they wanted. So we :x instead.
:x
] callcc0
] bind ;

View File

@ -26,12 +26,18 @@
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: errors
USE: kernel
USE: strings
: catchstack* ( -- cs )
interpreter
"factor.FactorInterpreter" "catchstack" jvar-get ;
: set-catchstack* ( cs -- )
interpreter
"factor.FactorInterpreter" "catchstack" jvar-set ;
: catchstack ( -- cs ) catchstack* clone ;
: set-catchstack ( cs -- ) clone set-catchstack* ;
: error>str ( error -- str ) >str ;

View File

@ -79,7 +79,7 @@ USE: strings
: init-stdio ( -- )
#! Initialize standard input/output.
stdin stdout <char-stream> set-stdio ;
stdin stdout <char-stream> "stdio" set ;
: init-environment ( -- )
#! Initialize OS-specific constants.
@ -98,8 +98,6 @@ USE: strings
] ifte
] when ;
IN: kernel
: boot ( -- )
#! The boot word is run by the intepreter when starting from
#! an object database.

View File

@ -98,4 +98,6 @@ IN: kernel
[ "java.lang.String" ] "java.lang.System" "getProperty"
jinvoke-static ;
: java? t ;
: native? f ;
: version "factor.FactorInterpreter" "VERSION" jvar-static-get ;

View File

@ -155,7 +155,7 @@ USE: unparser
#! Called when user opens a new listener in the desktop.
<namespace> [
dup "listener" set
<listener-stream> set-stdio
<listener-stream> "stdio" set
initial-interpreter-loop
"listener" get close-listener
] bind ;

View File

@ -56,5 +56,5 @@ USE: words
[ compound-or-compiled? ] [ worddef>list prettyprint-:; ]
[ shuffle? ] [ worddef>list prettyprint-~<<>>~ ]
[ primitive? ] [ "PRIMITIVE: " write unparse write drop ]
[ drop t ] [ 2drop "Not defined" print ]
[ drop t ] [ 2drop "Not defined" write ]
] cond prettyprint-newline ;

View File

@ -29,6 +29,7 @@ USE: arithmetic
USE: combinators
USE: format
USE: inspector
USE: init
USE: kernel
USE: lists
USE: logic
@ -49,6 +50,7 @@ primitives,
"/library/combinators.factor"
"/library/cons.factor"
"/library/continuations.factor"
"/library/debugger.factor"
"/library/errors.factor"
"/library/format.factor"
"/library/hashtables.factor"

View File

@ -49,8 +49,6 @@ USE: vocabularies
USE: words
USE: unparser
IN: kernel
: boot ( -- )
init-namespaces

View File

@ -88,6 +88,9 @@ USE: words
0 <vector> set-datastack
0 <vector> set-callstack ;
: java? f ;
: native? t ;
!!! HACK
IN: strings

View File

@ -56,13 +56,6 @@ USE: streams
#! Print a newline to standard output.
"\n" write ;
: set-stdio ( stdio -- )
#! Redirect standard input/output in the current namespace.
#! This also redirects the debugger standard input/output,
#! which can pose a security risk if stdio is a network
#! socket!
dup "stdio" set "error-stdio" set ;
: with-stream ( stream quot -- )
<namespace> [
swap "stdio" set call "stdio" get fclose