unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Sean Lynch <sean@seanplynch.com>
To: guile-devel@gnu.org
Cc: Sean Lynch <sean@seanplynch.com>
Subject: [PATCH 2/4] Implement ecmascript try/catch.
Date: Wed, 28 Oct 2020 00:03:31 -0400	[thread overview]
Message-ID: <20201028040330.22051-3-sean@seanplynch.com> (raw)
In-Reply-To: <20201028040330.22051-1-sean@seanplynch.com>

Implement ecmascript try/catch using
with-exception-handler/rais-exception respectively so that it is
straight-forward to interact with ecmascript exceptions from scheme.

* module/language/ecmascript/compile-tree-il.scm: Compile try/catch
* module/language/ecmascript/impl.scm: Provide js-try and js-catch
  procedures
---
 .../language/ecmascript/compile-tree-il.scm   | 25 +++++++++++++++++++
 module/language/ecmascript/impl.scm           | 18 ++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/module/language/ecmascript/compile-tree-il.scm b/module/language/ecmascript/compile-tree-il.scm
index 074674142..6f6af5210 100644
--- a/module/language/ecmascript/compile-tree-il.scm
+++ b/module/language/ecmascript/compile-tree-il.scm
@@ -355,6 +355,31 @@
               ,(with-return-prompt
                 (lambda ()
                   (comp-body e body formals syms))))))))
+      ((try (block . ,body)
+            (catch ,err (block . ,catch))
+            ,finally)
+       (let ((err-sym (gensym (symbol->string err))))
+        `(call ,(@implv js-try)
+               (lambda ()
+                 (lambda-case
+                   ((() #f #f #f () ())
+                    (begin ,@(map (lambda (x) (comp x e)) body)))))
+               (lambda ((,err . ,err-sym))
+                 (lambda-case
+                   (((,err) #f #f #f () (,err-sym))
+                    ,(let ((e (econs err err-sym e)))
+                       `(begin ,@(map (lambda (x) (comp x e)) catch))))))
+               (lambda ()
+                 (lambda-case
+                   ((() #f #f #f () ())
+                    ,(pmatch/source finally
+                       ((finally (block . ,finally))
+                        `(begin ,@(map (lambda (x) (comp x e)) finally)))
+                       (#f '(void))
+                       (else (error "syntax error: invalid try statement: " x)))))))))
+      ((throw ,expr)
+       `(call ,(@implv js-throw)
+              ,(comp expr e)))
       ((call/this ,obj ,prop . ,args)
        (@impl call/this*
               obj
diff --git a/module/language/ecmascript/impl.scm b/module/language/ecmascript/impl.scm
index 27c077aed..b1be63403 100644
--- a/module/language/ecmascript/impl.scm
+++ b/module/language/ecmascript/impl.scm
@@ -33,7 +33,9 @@
             shift
             mod
             band bxor bior
-            make-enumerator))
+            make-enumerator
+            js-throw
+            js-try))
 
 
 (define-class <js-module-object> (<js-object>)
@@ -87,6 +89,20 @@
          (fluid-set! *this* (make <js-global-object>))
          (init-js-bindings! (current-module)))))
 
+(define (js-throw obj)
+  (raise-exception obj #:continuable? #t))
+
+(define (js-try body-thunk catch-handler finally-thunk)
+  (with-exception-handler (lambda (e) 
+                            (catch-handler e)
+                            (finally-thunk)
+                            *undefined*)
+                          (lambda ()
+                            (body-thunk)
+                            (finally-thunk)
+                            *undefined*)
+                          #:unwind? #t))
+
 (define (get-this)
   (fluid-ref *this*))
 
-- 
2.29.1




  parent reply	other threads:[~2020-10-28  4:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-28  4:03 [PATCH 0/4] Support ECMAScript Test262 Assertion Harness Sean Lynch
2020-10-28  4:03 ` [PATCH 1/4] Support ecmascript return operator with no operand Sean Lynch
2020-10-28  4:03 ` Sean Lynch [this message]
2020-10-28  4:03 ` [PATCH 3/4] Implement ecmascript function prototype Sean Lynch
2020-10-28  4:03 ` [PATCH 4/4] Fix some reserved word usages in ecmascript implementation Sean Lynch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201028040330.22051-3-sean@seanplynch.com \
    --to=sean@seanplynch.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).