From 683c9e2af6b794b6cacfd9604d907fe287895cd6 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 17 Jul 2004 23:33:35 +0000 Subject: [PATCH] catch/throw added to Java Factor --- factor/.FactorInterpreter.java.marks | Bin 45 -> 45 bytes factor/FactorInterpreter.java | 56 ++++------------ library/errors.factor | 39 ++++------- library/inspector.factor | 2 + library/platform/jvm/boot-mini.factor | 10 +-- library/platform/jvm/boot-sumo.factor | 10 +-- library/platform/jvm/boot.factor | 4 ++ library/platform/jvm/debugger.factor | 83 +----------------------- library/platform/jvm/errors.factor | 6 ++ library/platform/jvm/init.factor | 4 +- library/platform/jvm/kernel.factor | 2 + library/platform/jvm/listener.factor | 2 +- library/platform/jvm/prettyprint.factor | 2 +- library/platform/native/boot.factor | 2 + library/platform/native/init.factor | 2 - library/platform/native/kernel.factor | 3 + library/stdio.factor | 7 -- 17 files changed, 53 insertions(+), 181 deletions(-) diff --git a/factor/.FactorInterpreter.java.marks b/factor/.FactorInterpreter.java.marks index 67054ae34bb883861392bdccfeeef9dfb2e70582..58e4bbe58de03ecf4e5454aae2a8173a61115862 100644 GIT binary patch literal 45 kcmY#Pv^F%dFfxOXT#5|Vh9*Yl<`5FXH8!^Za)Bfl0LHBdV*mgE literal 45 lcmY#Pv^F%dv@n8@T#5|Vh9;KA1`ra=H8n6cv^E5hTmZ&%2uA<_ diff --git a/factor/FactorInterpreter.java b/factor/FactorInterpreter.java index 20b360041b..9e334ab986 100644 --- a/factor/FactorInterpreter.java +++ b/factor/FactorInterpreter.java @@ -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; } //}}} } diff --git a/library/errors.factor b/library/errors.factor index 5490e82ae9..708cdcd1b4 100644 --- a/library/errors.factor +++ b/library/errors.factor @@ -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 ; diff --git a/library/inspector.factor b/library/inspector.factor index 220d0affbd..19d642395d 100644 --- a/library/inspector.factor +++ b/library/inspector.factor @@ -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 -- ) [ diff --git a/library/platform/jvm/boot-mini.factor b/library/platform/jvm/boot-mini.factor index 94d998e739..31c0cafe27 100644 --- a/library/platform/jvm/boot-mini.factor +++ b/library/platform/jvm/boot-mini.factor @@ -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 diff --git a/library/platform/jvm/boot-sumo.factor b/library/platform/jvm/boot-sumo.factor index e81f1d0c08..6897381244 100644 --- a/library/platform/jvm/boot-sumo.factor +++ b/library/platform/jvm/boot-sumo.factor @@ -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 diff --git a/library/platform/jvm/boot.factor b/library/platform/jvm/boot.factor index 0b7422ff18..3d82ede77d 100644 --- a/library/platform/jvm/boot.factor +++ b/library/platform/jvm/boot.factor @@ -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 diff --git a/library/platform/jvm/debugger.factor b/library/platform/jvm/debugger.factor index 4030f38b21..ec4f9cfaaa 100644 --- a/library/platform/jvm/debugger.factor +++ b/library/platform/jvm/debugger.factor @@ -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 - [ - "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 ; diff --git a/library/platform/jvm/errors.factor b/library/platform/jvm/errors.factor index fafed7b4ea..afb702cc5c 100644 --- a/library/platform/jvm/errors.factor +++ b/library/platform/jvm/errors.factor @@ -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 ; diff --git a/library/platform/jvm/init.factor b/library/platform/jvm/init.factor index f073c59cef..cc157768b5 100644 --- a/library/platform/jvm/init.factor +++ b/library/platform/jvm/init.factor @@ -79,7 +79,7 @@ USE: strings : init-stdio ( -- ) #! Initialize standard input/output. - stdin stdout set-stdio ; + stdin stdout "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. diff --git a/library/platform/jvm/kernel.factor b/library/platform/jvm/kernel.factor index ac0e60316a..6573e2778f 100644 --- a/library/platform/jvm/kernel.factor +++ b/library/platform/jvm/kernel.factor @@ -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 ; diff --git a/library/platform/jvm/listener.factor b/library/platform/jvm/listener.factor index 236263dd2a..7d58e4d98e 100644 --- a/library/platform/jvm/listener.factor +++ b/library/platform/jvm/listener.factor @@ -155,7 +155,7 @@ USE: unparser #! Called when user opens a new listener in the desktop. [ dup "listener" set - set-stdio + "stdio" set initial-interpreter-loop "listener" get close-listener ] bind ; diff --git a/library/platform/jvm/prettyprint.factor b/library/platform/jvm/prettyprint.factor index 213bbe8d2a..313b8a8ed0 100644 --- a/library/platform/jvm/prettyprint.factor +++ b/library/platform/jvm/prettyprint.factor @@ -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 ; diff --git a/library/platform/native/boot.factor b/library/platform/native/boot.factor index e50aaef56b..038c9b3881 100644 --- a/library/platform/native/boot.factor +++ b/library/platform/native/boot.factor @@ -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" diff --git a/library/platform/native/init.factor b/library/platform/native/init.factor index d5febf2702..6e15553fe4 100644 --- a/library/platform/native/init.factor +++ b/library/platform/native/init.factor @@ -49,8 +49,6 @@ USE: vocabularies USE: words USE: unparser -IN: kernel - : boot ( -- ) init-namespaces diff --git a/library/platform/native/kernel.factor b/library/platform/native/kernel.factor index 39892cf058..259ae12808 100644 --- a/library/platform/native/kernel.factor +++ b/library/platform/native/kernel.factor @@ -88,6 +88,9 @@ USE: words 0 set-datastack 0 set-callstack ; +: java? f ; +: native? t ; + !!! HACK IN: strings diff --git a/library/stdio.factor b/library/stdio.factor index 8cf04e0167..eb099bb69c 100644 --- a/library/stdio.factor +++ b/library/stdio.factor @@ -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 -- ) [ swap "stdio" set call "stdio" get fclose