fix: Pointer str & prn (#1060)

* fix: Make `str` work for (Pointer a) types

* test: Make sure that the Pointer.str compiles

* fix: Remove test case, keep function around to make sure it compiles
This commit is contained in:
Erik Svedäng 2020-12-09 06:19:07 +01:00 committed by GitHub
parent dd19c049c6
commit 40d55562df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View File

@ -461,9 +461,13 @@
)
(defmodule Pointer
(register str (Fn [(Ptr a)] String) "Pointer_str")
(register strp (Fn [(Ptr a)] String) "Pointer_strp")
(defn str [a] (Pointer.strp a))
(implements str Pointer.str)
(defn prn [a] (Pointer.str a))
(defn prn [a] (Pointer.strp a))
(implements prn Pointer.prn)
)
(defndynamic build-str* [forms]
@ -474,4 +478,4 @@
(list 'ref (list 'String.append (list 'ref (list 'str (car forms))) (build-str* (cdr forms)))))))
(defmacro str* [:rest forms]
(list 'copy (build-str* forms)))
(list 'copy (build-str* forms)))

View File

@ -362,7 +362,7 @@ int String_index_MINUS_of(const String *s, char c) {
return String_index_MINUS_of_MINUS_from(s, c, -1);
}
String Pointer_str(void *in) {
String Pointer_strp(void *in) {
int size = snprintf(NULL, 0, "%p", in) + 1;
String buffer = CARP_MALLOC(size);
sprintf(buffer, "%p", in);

View File

@ -10,6 +10,12 @@
(defn ref-to-ptr [r]
(the (Ptr a) (Unsafe.coerce (the (Ref a) r))))
;; Just make sure the 'str' for a Pointer compiles
;; (testing out its contents is platform specific)
(defn pointer-to-str []
(let [p (the (Ptr CChar) NULL)]
(str p)))
; these tests are sadly a little unsafe
(deftest test
(assert-equal test
@ -42,5 +48,5 @@
(to-value (ref-to-ptr &123))
123
"Pointer.to-value works as expected"
)
)
)