From ef50677275dc9e7949f19bdd65f162855f9f8c3d Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 26 Sep 2023 21:16:22 +0200 Subject: [PATCH] Fix #1513: interop on Thread/sleep with non-long (#1628) --- CHANGELOG.md | 1 + src/babashka/impl/classes.clj | 14 +++++++++++++- test/babashka/interop_test.clj | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d1d2122..0f0ac3d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ A preview of the next release can be installed from - Bump rewrite-clj to v0.1.1.47 - [#1619](https://github.com/babashka/babashka/issues/1619): Fix reflection issue with `Thread/sleep` in `core.async/timeout` - Support interop on `java.util.stream.IntStream` +- [#1513](https://github.com/babashka/babashka/issues/1513): Fix interop on `Thread/sleep` with numbers that aren't already longs ## 1.3.184 (2023-08-22) diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index 070fde57..f818bcd2 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -644,7 +644,19 @@ ([_# ^String class-name#] (Class/forName class-name#)) ([_# ^String class-name# initialize# ^java.lang.ClassLoader clazz-loader#] - (Class/forName class-name#)))}))]] + (Class/forName class-name#)))}) + (= 'java.lang.Thread c) + (assoc :static-methods + {(list 'quote 'sleep) + `(fn + ([_# x#] + (if (instance? Number x#) + (let [x# (long x#)] + (Thread/sleep x#)) + (let [^java.time.Duration x# x#] + (Thread/sleep x#)))) + ([_# ^java.lang.Long millis# ^java.lang.Long nanos#] + (Thread/sleep millis# nanos#)))}))]] c)) m (assoc m :public-class (fn [v] diff --git a/test/babashka/interop_test.clj b/test/babashka/interop_test.clj index 8ad19e11..98610ffb 100644 --- a/test/babashka/interop_test.clj +++ b/test/babashka/interop_test.clj @@ -44,3 +44,9 @@ (deftest IntStream-test (is (pos? (bb nil "(.count (.codePoints \"woof🐕\"))")))) + +(deftest Thread-sleep-test + (is (bb nil "(Thread/sleep (/ 1 200)) + (Thread/sleep (/ 1 200) (/ 1 200)) + (Thread/sleep (java.time.Duration/ofMillis 1)) + true")))