From 8ea362155938863c07a920bdf851b5527e556880 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 1 May 2017 22:05:36 -0700 Subject: ffi: simple typedef mechanism. * ffi.c (ffi_typedef_hash): New static variable. (ffi_type_compile): Handle undefined atom case by trying through typedef hash. (ffi_typedef): New function. (ffi_init): gc-protect ffi_type_compile variable and initialize it with a hash table. Register ffi-typedef intrinsic. * ffi.h (ffi_typedef): Declared --- ffi.c | 16 ++++++++++++++++ ffi.h | 1 + 2 files changed, 17 insertions(+) diff --git a/ffi.c b/ffi.c index 9b2c242e..f212a03e 100644 --- a/ffi.c +++ b/ffi.c @@ -50,6 +50,7 @@ #include "arith.h" #include "args.h" #include "utf8.h" +#include "hash.h" #include "ffi.h" val uint8_s, int8_s; @@ -78,6 +79,8 @@ val closure_s; val ffi_type_s, ffi_call_desc_s, ffi_closure_s; +static val ffi_typedef_hash; + struct txr_ffi_type { ffi_type *ft; val lt; @@ -1664,6 +1667,11 @@ val ffi_type_compile(val syntax) return make_ffi_type_builtin(syntax, null_s, 0, &ffi_type_void, ffi_void_put, ffi_void_get); } else { + val sub = gethash(ffi_typedef_hash, syntax); + + if (sub != nil) + return ffi_copy_type(sub); + uw_throwf(error_s, lit("~a: unrecognized type specifier: ~!~s"), self, syntax, nao); } @@ -1915,8 +1923,14 @@ val ffi_copy_type(val type) return type_cp; } +val ffi_typedef(val name, val type) +{ + return sethash(ffi_typedef_hash, name, type); +} + void ffi_init(void) { + prot1(&ffi_typedef_hash); uint8_s = intern(lit("uint8"), user_package); int8_s = intern(lit("int8"), user_package); int8_s = intern(lit("int8"), user_package); @@ -1959,5 +1973,7 @@ void ffi_init(void) reg_fun(intern(lit("ffi-make-closure"), user_package), func_n2(ffi_make_closure)); reg_fun(intern(lit("cptr"), user_package), func_n1o(cptr_make, 0)); reg_fun(intern(lit("ffi-copy-type"), user_package), func_n1(ffi_copy_type)); + reg_fun(intern(lit("ffi-typedef"), user_package), func_n2(ffi_typedef)); reg_varl(intern(lit("cptr-null"), user_package), cptr(0)); + ffi_typedef_hash = make_hash(nil, nil, nil); } diff --git a/ffi.h b/ffi.h index 88da4deb..df9e9628 100644 --- a/ffi.h +++ b/ffi.h @@ -57,4 +57,5 @@ val ffi_make_closure(val fun, val call_desc); mem_t *ffi_closure_get_fptr(val closure); val ffi_call_wrap(val ffi_call_desc, val fptr, val args); val ffi_copy_type(val type); +val ffi_typedef(val name, val type); void ffi_init(void); -- cgit v1.2.3