From 00b9d6913fd39266208c7c8b61250967bb0f6636 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 15 Dec 2018 19:34:57 -0400 Subject: Changed the "instance?" macro in "lux/host.jvm.lux" into the "check" macro. --- stdlib/source/lux/host.jvm.lux | 41 ++++++++++++++++++++++++--------------- stdlib/test/test/lux/host.jvm.lux | 8 ++++---- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/stdlib/source/lux/host.jvm.lux b/stdlib/source/lux/host.jvm.lux index 969935dc3..59996d9a2 100644 --- a/stdlib/source/lux/host.jvm.lux +++ b/stdlib/source/lux/host.jvm.lux @@ -1446,24 +1446,33 @@ (with-gensyms [g!_] (wrap (list (`' ("lux try" (.function ((~ g!_) (~ g!_)) (~ expr)))))))) -(syntax: #export (instance? {#let [imports (class-imports *compiler*)]} - {class (generic-type^ imports (list))} - {obj (p.maybe s.any)}) +(syntax: #export (check {#let [imports (class-imports *compiler*)]} + {class (generic-type^ imports (list))} + {unchecked (p.maybe s.any)}) {#.doc (doc "Checks whether an object is an instance of a particular class." "Caveat emptor: Cannot check for polymorphism, so avoid using parameterized classes." - (instance? String "YOLO"))} - (case obj - (#.Some obj) - (wrap (list (` ((~ (code.text (format "jvm instanceof" ":" (simple-class$ (list) class)))) (~ obj))))) - - #.None - (do @ - [g!_ (macro.gensym "_") - g!obj (macro.gensym "obj")] - (wrap (list (` (: (-> (primitive "java.lang.Object") Bit) - (function ((~ g!_) (~ g!obj)) - ((~ (code.text (format "jvm instanceof" ":" (simple-class$ (list) class)))) (~ g!obj)))))))) - )) + (case (check String "YOLO") + (#.Some value-as-string) + #.None))} + (with-gensyms [g!_ g!unchecked] + (let [class-name (simple-class$ (list) class) + class-type (` (.primitive (~ (code.text class-name)))) + check-type (` (.Maybe (~ class-type))) + check-code (` (if ((~ (code.text (format "jvm instanceof" ":" class-name))) (~ g!unchecked)) + (#.Some (.:coerce (~ class-type) + (~ g!unchecked))) + #.None))] + (case unchecked + (#.Some unchecked) + (wrap (list (` (: (~ check-type) + (let [(~ g!unchecked) (~ unchecked)] + (~ check-code)))))) + + #.None + (wrap (list (` (: (-> (primitive "java.lang.Object") (~ check-type)) + (function ((~ g!_) (~ g!unchecked)) + (~ check-code)))))) + )))) (syntax: #export (synchronized lock body) {#.doc (doc "Evaluates body, while holding a lock on a given object." diff --git a/stdlib/test/test/lux/host.jvm.lux b/stdlib/test/test/lux/host.jvm.lux index 2444de871..caa943771 100644 --- a/stdlib/test/test/lux/host.jvm.lux +++ b/stdlib/test/test/lux/host.jvm.lux @@ -83,10 +83,10 @@ (context: "Miscellaneous" ($_ seq (test "Can check if an object is of a certain class." - (and (&.instance? String "") - (not (&.instance? Long "")) - (&.instance? Object "") - (not (&.instance? Object (&.null))))) + (and (case (&.check String "") (#.Some _) true #.None false) + (case (&.check Long "") (#.Some _) false #.None true) + (case (&.check Object "") (#.Some _) true #.None false) + (case (&.check Object (&.null)) (#.Some _) false #.None true))) (test "Can run code in a 'synchronized' block." (&.synchronized "" #1)) -- cgit v1.2.3