From 4abaa1b12b81cfe2fb45d5b2fa36a282c15c328d Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Fri, 18 Apr 2014 00:00:15 +0300
Subject: [PATCH] unix modffi: Convert to static module structures.

---
 unix/main.c         |  4 ----
 unix/modffi.c       | 29 ++++++++++++++++++++++-------
 unix/mpconfigport.h |  2 ++
 unix/qstrdefsport.h |  4 ++++
 4 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/unix/main.c b/unix/main.c
index fd3419a1c..940fe48c1 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -357,10 +357,6 @@ int main(int argc, char **argv) {
     mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
 #endif
 
-#if MICROPY_MOD_FFI
-    ffi_init();
-#endif
-
     // Here is some example code to create a class and instance of that class.
     // First is the Python, then the C code.
     //
diff --git a/unix/modffi.c b/unix/modffi.c
index f2bd6fcec..fd48aa17e 100644
--- a/unix/modffi.c
+++ b/unix/modffi.c
@@ -365,11 +365,26 @@ mp_obj_t mod_ffi_as_bytearray(mp_obj_t ptr, mp_obj_t size) {
 }
 MP_DEFINE_CONST_FUN_OBJ_2(mod_ffi_as_bytearray_obj, mod_ffi_as_bytearray);
 
+STATIC const mp_map_elem_t mp_module_ffi_globals_table[] = {
+    { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ffi) },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mod_ffi_open_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&mod_ffi_callback_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_as_bytearray), (mp_obj_t)&mod_ffi_as_bytearray_obj },
+};
 
-void ffi_init() {
-    mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("ffi"));
-    mp_store_attr(m, MP_QSTR_open, (mp_obj_t)&mod_ffi_open_obj);
-    mp_store_attr(m, QSTR_FROM_STR_STATIC("callback"), (mp_obj_t)&mod_ffi_callback_obj);
-    // there would be as_bytes, but bytes currently is value, not reference type!
-    mp_store_attr(m, QSTR_FROM_STR_STATIC("as_bytearray"), (mp_obj_t)&mod_ffi_as_bytearray_obj);
-}
+STATIC const mp_obj_dict_t mp_module_ffi_globals = {
+    .base = {&mp_type_dict},
+    .map = {
+        .all_keys_are_qstrs = 1,
+        .table_is_fixed_array = 1,
+        .used = sizeof(mp_module_ffi_globals_table) / sizeof(mp_map_elem_t),
+        .alloc = sizeof(mp_module_ffi_globals_table) / sizeof(mp_map_elem_t),
+        .table = (mp_map_elem_t*)mp_module_ffi_globals_table,
+    },
+};
+
+const mp_obj_module_t mp_module_ffi = {
+    .base = { &mp_type_module },
+    .name = MP_QSTR_ffi,
+    .globals = (mp_obj_dict_t*)&mp_module_ffi_globals,
+};
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 21d11ebea..5abfe2d9d 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -19,7 +19,9 @@
 
 extern const struct _mp_obj_module_t mp_module_time;
 extern const struct _mp_obj_module_t mp_module_socket;
+extern const struct _mp_obj_module_t mp_module_ffi;
 #define MICROPY_EXTRA_BUILTIN_MODULES \
+    { MP_OBJ_NEW_QSTR(MP_QSTR_ffi), (mp_obj_t)&mp_module_ffi }, \
     { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_time }, \
     { MP_OBJ_NEW_QSTR(MP_QSTR_microsocket), (mp_obj_t)&mp_module_socket }, \
 
diff --git a/unix/qstrdefsport.h b/unix/qstrdefsport.h
index 4de00aa11..2e7a6aa1e 100644
--- a/unix/qstrdefsport.h
+++ b/unix/qstrdefsport.h
@@ -10,10 +10,14 @@ Q(write)
 Q(makefile)
 
 Q(FileIO)
+
+Q(ffi)
 Q(ffimod)
 Q(ffifunc)
 Q(fficallback)
 Q(ffivar)
+Q(as_bytearray)
+Q(callback)
 Q(func)
 Q(var)
 
-- 
GitLab