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

View File

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

View File

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

View File

@ -29,7 +29,6 @@ USE: parser
!!! The standard library. !!! The standard library.
"/library/platform/jvm/kernel.factor" run-resource ! kernel "/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/vectors.factor" run-resource ! vectors
"/library/platform/jvm/stack.factor" run-resource ! stack "/library/platform/jvm/stack.factor" run-resource ! stack
"/library/logic.factor" run-resource ! logic "/library/logic.factor" run-resource ! logic
@ -49,12 +48,14 @@ USE: parser
"/library/platform/jvm/strings.factor" run-resource ! strings "/library/platform/jvm/strings.factor" run-resource ! strings
"/library/platform/jvm/sbuf.factor" run-resource ! strings "/library/platform/jvm/sbuf.factor" run-resource ! strings
"/library/strings.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/platform/jvm/namespaces.factor" run-resource ! namespaces
"/library/namespaces.factor" run-resource ! namespaces "/library/namespaces.factor" run-resource ! namespaces
"/library/sbuf.factor" run-resource ! strings "/library/sbuf.factor" run-resource ! strings
"/library/list-namespaces.factor" run-resource ! namespaces "/library/list-namespaces.factor" run-resource ! namespaces
"/library/math/namespace-math.factor" run-resource ! arithmetic "/library/math/namespace-math.factor" run-resource ! arithmetic
"/library/continuations.factor" run-resource ! continuations "/library/continuations.factor" run-resource ! continuations
"/library/errors.factor" run-resource ! errors
"/library/platform/jvm/vocabularies.factor" run-resource ! vocabularies "/library/platform/jvm/vocabularies.factor" run-resource ! vocabularies
"/library/vocabularies.factor" run-resource ! vocabularies "/library/vocabularies.factor" run-resource ! vocabularies
"/library/platform/jvm/words.factor" run-resource ! words "/library/platform/jvm/words.factor" run-resource ! words
@ -85,14 +86,9 @@ USE: parser
"/library/inspector.factor" run-resource ! inspector "/library/inspector.factor" run-resource ! inspector
"/library/inspect-vocabularies.factor" run-resource ! inspector "/library/inspect-vocabularies.factor" run-resource ! inspector
"/library/platform/jvm/compiler.factor" run-resource ! compiler "/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/debugger.factor" run-resource ! debugger
!!! Final initialization... !!! Final initialization...
! Avoid an error from the parser about boot not being defined
IN: kernel DEFER: boot
"/library/init.factor" run-resource ! init "/library/init.factor" run-resource ! init
"/library/platform/jvm/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. !!! The standard library.
"/library/platform/jvm/kernel.factor" run-resource ! kernel "/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/vectors.factor" run-resource ! vectors
"/library/platform/jvm/stack.factor" run-resource ! stack "/library/platform/jvm/stack.factor" run-resource ! stack
"/library/logic.factor" run-resource ! logic "/library/logic.factor" run-resource ! logic
@ -50,12 +49,14 @@ USE: parser
"/library/platform/jvm/strings.factor" run-resource ! strings "/library/platform/jvm/strings.factor" run-resource ! strings
"/library/platform/jvm/sbuf.factor" run-resource ! strings "/library/platform/jvm/sbuf.factor" run-resource ! strings
"/library/strings.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/platform/jvm/namespaces.factor" run-resource ! namespaces
"/library/namespaces.factor" run-resource ! namespaces "/library/namespaces.factor" run-resource ! namespaces
"/library/sbuf.factor" run-resource ! strings "/library/sbuf.factor" run-resource ! strings
"/library/list-namespaces.factor" run-resource ! namespaces "/library/list-namespaces.factor" run-resource ! namespaces
"/library/math/namespace-math.factor" run-resource ! arithmetic "/library/math/namespace-math.factor" run-resource ! arithmetic
"/library/continuations.factor" run-resource ! continuations "/library/continuations.factor" run-resource ! continuations
"/library/errors.factor" run-resource ! errors
"/library/platform/jvm/vocabularies.factor" run-resource ! vocabularies "/library/platform/jvm/vocabularies.factor" run-resource ! vocabularies
"/library/vocabularies.factor" run-resource ! vocabularies "/library/vocabularies.factor" run-resource ! vocabularies
"/library/platform/jvm/words.factor" run-resource ! words "/library/platform/jvm/words.factor" run-resource ! words
@ -91,6 +92,7 @@ USE: parser
"/library/inspector.factor" run-resource ! inspector "/library/inspector.factor" run-resource ! inspector
"/library/inspect-vocabularies.factor" run-resource ! inspector "/library/inspect-vocabularies.factor" run-resource ! inspector
"/library/platform/jvm/compiler.factor" run-resource ! compiler "/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/debugger.factor" run-resource ! debugger
"/library/platform/jvm/listener.factor" run-resource ! listener "/library/platform/jvm/listener.factor" run-resource ! listener
"/library/test/test.factor" run-resource ! test "/library/test/test.factor" run-resource ! test
@ -115,11 +117,5 @@ USE: parser
"/library/httpd/default-responders.factor" run-resource ! default-responders "/library/httpd/default-responders.factor" run-resource ! default-responders
!!! Final initialization... !!! Final initialization...
! Avoid an error from the parser about boot not being defined
IN: kernel DEFER: boot
"/library/init.factor" run-resource ! init "/library/init.factor" run-resource ! init
"/library/platform/jvm/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 [ interpreter "factor.FactorInterpreter" "mini" jvar-get [
"/library/platform/jvm/boot-mini.factor" run-resource "/library/platform/jvm/boot-mini.factor" run-resource
] [ ] [
"/library/platform/jvm/boot-sumo.factor" run-resource "/library/platform/jvm/boot-sumo.factor" run-resource
] ifte ] ifte
boot

View File

@ -27,35 +27,12 @@
IN: debugger IN: debugger
USE: combinators USE: combinators
USE: continuations
USE: inspector
USE: interpreter
USE: kernel USE: kernel
USE: namespaces USE: namespaces
USE: stack USE: stack
USE: stdio USE: stdio
USE: strings
USE: unparser 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 ) : exception? ( exception -- boolean )
"java.lang.Throwable" is ; "java.lang.Throwable" is ;
@ -63,67 +40,9 @@ USE: unparser
[ ] "java.lang.Throwable" "printStackTrace" jinvoke ; [ ] "java.lang.Throwable" "printStackTrace" jinvoke ;
: :j ( -- ) : :j ( -- )
! Print the stack trace from the exception that caused the #! Print the stack trace from the last exception.
! last break.
"error" get dup exception? [ "error" get dup exception? [
print-stack-trace print-stack-trace
] [ ] [
"Not an exception: " write . "Not an exception: " write .
] ifte ; ] 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. ! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: errors IN: errors
USE: kernel
USE: strings USE: strings
: catchstack* ( -- cs ) : catchstack* ( -- cs )
interpreter
"factor.FactorInterpreter" "catchstack" jvar-get ; "factor.FactorInterpreter" "catchstack" jvar-get ;
: set-catchstack* ( cs -- ) : set-catchstack* ( cs -- )
interpreter
"factor.FactorInterpreter" "catchstack" jvar-set ; "factor.FactorInterpreter" "catchstack" jvar-set ;
: catchstack ( -- cs ) catchstack* clone ;
: set-catchstack ( cs -- ) clone set-catchstack* ;
: error>str ( error -- str ) >str ; : error>str ( error -- str ) >str ;

View File

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

View File

@ -98,4 +98,6 @@ IN: kernel
[ "java.lang.String" ] "java.lang.System" "getProperty" [ "java.lang.String" ] "java.lang.System" "getProperty"
jinvoke-static ; jinvoke-static ;
: java? t ;
: native? f ;
: version "factor.FactorInterpreter" "VERSION" jvar-static-get ; : 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. #! Called when user opens a new listener in the desktop.
<namespace> [ <namespace> [
dup "listener" set dup "listener" set
<listener-stream> set-stdio <listener-stream> "stdio" set
initial-interpreter-loop initial-interpreter-loop
"listener" get close-listener "listener" get close-listener
] bind ; ] bind ;

View File

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

View File

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

View File

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

View File

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

View File

@ -56,13 +56,6 @@ USE: streams
#! Print a newline to standard output. #! Print a newline to standard output.
"\n" write ; "\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 -- ) : with-stream ( stream quot -- )
<namespace> [ <namespace> [
swap "stdio" set call "stdio" get fclose swap "stdio" set call "stdio" get fclose