[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 38/99: Explicitly test for undefined arguments to handle
From: |
Christopher Allan Webber |
Subject: |
[Guile-commits] 38/99: Explicitly test for undefined arguments to handle false values like 0 |
Date: |
Sun, 10 Oct 2021 21:50:52 -0400 (EDT) |
cwebber pushed a commit to branch compile-to-js-merge
in repository guile.
commit 89029a54f43e7698e7ddb5f72ada4c44ec5a1c68
Author: Ian Price <ianprice90@googlemail.com>
AuthorDate: Wed Jun 24 19:57:28 2015 +0100
Explicitly test for undefined arguments to handle false values like 0
---
module/language/javascript.scm | 39 +++++++++++++++++++++++++---
module/language/javascript/simplify.scm | 10 ++++++-
module/language/js-il/compile-javascript.scm | 11 +++++---
3 files changed, 52 insertions(+), 8 deletions(-)
diff --git a/module/language/javascript.scm b/module/language/javascript.scm
index 741282a..8829b3b 100644
--- a/module/language/javascript.scm
+++ b/module/language/javascript.scm
@@ -15,6 +15,8 @@
make-branch branch
make-var var
make-binop binop
+ make-ternary ternary
+ make-prefix prefix
print-statement))
@@ -59,6 +61,8 @@
(define-js-type branch test then else)
(define-js-type var id exp)
(define-js-type binop op arg1 arg2)
+(define-js-type ternary test then else)
+(define-js-type prefix op expr)
(define (unparse-js exp)
(match exp
@@ -85,7 +89,12 @@
(($ var id exp)
`(var ,id ,(unparse-js exp)))
(($ binop op arg1 arg2)
- `(binop ,op ,(unparse-js arg1) ,(unparse-js arg2)))))
+ `(binop ,op ,(unparse-js arg1) ,(unparse-js arg2)))
+ (($ ternary test then else)
+ `(ternary ,(unparse-js test) ,(unparse-js then) ,(unparse-js else)))
+ (($ prefix op expr)
+ `(prefix ,op ,(unparse-js expr)))
+ ))
(define (print-exp exp port)
(match exp
@@ -136,17 +145,41 @@
(print-binop op port)
(display "(" port)
(print-exp arg2 port)
- (display ")" port))))
+ (display ")" port))
+
+ (($ ternary test then else)
+ (display "(" port)
+ (print-exp test port)
+ (display ") ? (" port)
+ (print-exp then port)
+ (display ") : (" port)
+ (print-exp else port)
+ (display ")" port))
+
+ (($ prefix op exp)
+ (print-prefix op port)
+ (display "(" port)
+ (print-exp exp port)
+ (display ")" port))
+ ))
(define (print-binop op port)
(case op
((or) (display "||" port))
((and) (display "&&" port))
((=) (display "==" port))
- ((+ - < <= > >=) (format port "~a" op))
+ ((+ - < <= > >= ===) (format port "~a" op))
(else
(throw 'unprintable-binop op))))
+(define (print-prefix op port)
+ (case op
+ ((not) (display "!" port))
+ ((typeof + -)
+ (format port "~a" op))
+ (else
+ (throw 'unprintable-prefix op))))
+
(define (print-statement stmt port)
(match stmt
(($ var id exp)
diff --git a/module/language/javascript/simplify.scm
b/module/language/javascript/simplify.scm
index b3360aa..2e3bde5 100644
--- a/module/language/javascript/simplify.scm
+++ b/module/language/javascript/simplify.scm
@@ -40,7 +40,15 @@
(flatten-block else)))
(($ call function args)
(make-call (flatten-exp function)
- (map flatten-exp args)))))
+ (map flatten-exp args)))
+
+ (($ ternary test then else)
+ (make-ternary (flatten-exp test)
+ (flatten-exp then)
+ (flatten-exp else)))
+ (($ prefix op exp)
+ (make-prefix op (flatten-exp exp)))
+ ))
(define (maybe-make-block exp)
(match exp
((exp) exp)
diff --git a/module/language/js-il/compile-javascript.scm
b/module/language/js-il/compile-javascript.scm
index 2645b4c..67a3492 100644
--- a/module/language/js-il/compile-javascript.scm
+++ b/module/language/js-il/compile-javascript.scm
@@ -104,10 +104,13 @@
(define (bind-opt-args opts num-drop)
(map (lambda (opt idx)
(make-var (rename-id opt)
- (make-binop 'or
- (make-refine (make-id "arguments")
- (make-const (+ num-drop idx)))
- (make-refine *scheme* (make-const
"UNDEFINED")))))
+ (let ((arg (make-refine (make-id "arguments")
+ (make-const (+ num-drop idx)))))
+ (make-ternary (make-binop '===
+ (make-prefix 'typeof arg)
+ (make-id "undefined"))
+ (make-refine *scheme* (make-const
"UNDEFINED"))
+ arg))))
opts
(iota (length opts))))
- [Guile-commits] 45/99: Add #:js-inline? and #:js-flatten? debugging options, (continued)
- [Guile-commits] 45/99: Add #:js-inline? and #:js-flatten? debugging options, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 54/99: Implement structs in runtime.js, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 25/99: Implement call-with-values, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 52/99: Add macro type in runtime.js, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 51/99: Implement cached-module-box, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 48/99: Rebuild nested scopes for js continuations, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 18/99: Add more types of constants, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 17/99: Implement Optional arguments, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 27/99: values takes multiple arguments, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 28/99: Implement apply correctly, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 38/99: Explicitly test for undefined arguments to handle false values like 0,
Christopher Allan Webber <=
- [Guile-commits] 44/99: Update primitives in no-values-primitives, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 35/99: Change local type representation and remove var type, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 42/99: compile-js uses the new cps representation, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 43/99: Handle multiple conts in a function body, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 46/99: JS-IL inliner has different count-calls for different clauses, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 40/99: Merge branch 'stable-2.2' into compile-to-js-2017, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 49/99: Compile Syntax Objects to Javascript, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 39/99: Add missing simplify.scm to Makefile, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 37/99: Fixup binop unparsing, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 57/99: Implement built-in syntax procedures., Christopher Allan Webber, 2021/10/10