|
| 1 | +#include "mpextern.h" |
| 2 | + |
| 3 | +#if MICROPY_MODULE_EXTERN |
| 4 | + |
| 5 | +typedef mp_obj_t (*mp_fun_ext_0_t)(const mp_ext_table_t *et); |
| 6 | +typedef mp_obj_t (*mp_fun_ext_1_t)(const mp_ext_table_t *et, mp_obj_t); |
| 7 | +typedef mp_obj_t (*mp_fun_ext_2_t)(const mp_ext_table_t *et, mp_obj_t, mp_obj_t); |
| 8 | +typedef mp_obj_t (*mp_fun_ext_3_t)(const mp_ext_table_t *et, mp_obj_t, mp_obj_t, mp_obj_t); |
| 9 | +typedef mp_obj_t (*mp_fun_ext_var_t)(const mp_ext_table_t *et, mp_uint_t n, const mp_obj_t *); |
| 10 | +typedef mp_obj_t (*mp_fun_ext_kw_t)(const mp_ext_table_t *et, mp_uint_t n, const mp_obj_t *, mp_map_t *); |
| 11 | + |
| 12 | +STATIC const mp_obj_type_t mp_type_fun_extern; |
| 13 | +STATIC const mp_ext_table_t mp_ext_table; |
| 14 | + |
| 15 | +typedef struct _mp_obj_fun_extern_t { // use this to make const objects that go in ROM |
| 16 | + mp_obj_base_t base; |
| 17 | + bool is_kw : 1; |
| 18 | + mp_uint_t n_args_min : 15; // inclusive |
| 19 | + mp_uint_t n_args_max : 16; // inclusive |
| 20 | + void *fun; // must be a pointer to a callable function in ROM |
| 21 | + mp_obj_dict_t *dict_globals; |
| 22 | +} mp_obj_fun_extern_t; |
| 23 | + |
| 24 | +STATIC mp_obj_t fun_extern_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
| 25 | + assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_extern)); |
| 26 | + mp_obj_fun_extern_t *self = self_in; |
| 27 | + |
| 28 | + // check number of arguments |
| 29 | + mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw); |
| 30 | + |
| 31 | + if (self->is_kw) { |
| 32 | + // function allows keywords |
| 33 | + |
| 34 | + // we create a map directly from the given args array |
| 35 | + mp_map_t kw_args; |
| 36 | + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 37 | + |
| 38 | + return ((mp_fun_ext_kw_t)self->fun)(&mp_ext_table, n_args, args, &kw_args); |
| 39 | + |
| 40 | + } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) { |
| 41 | + // function requires a fixed number of arguments |
| 42 | + |
| 43 | + // dispatch function call |
| 44 | + switch (self->n_args_min) { |
| 45 | + case 0: |
| 46 | + return ((mp_fun_ext_0_t)self->fun)(&mp_ext_table); |
| 47 | + |
| 48 | + case 1: |
| 49 | + return ((mp_fun_ext_1_t)self->fun)(&mp_ext_table, args[0]); |
| 50 | + |
| 51 | + case 2: |
| 52 | + return ((mp_fun_ext_2_t)self->fun)(&mp_ext_table, args[0], args[1]); |
| 53 | + |
| 54 | + case 3: |
| 55 | + default: |
| 56 | + return ((mp_fun_ext_3_t)self->fun)(&mp_ext_table, args[0], args[1], args[2]); |
| 57 | + } |
| 58 | + |
| 59 | + } else { |
| 60 | + // function takes a variable number of arguments, but no keywords |
| 61 | + |
| 62 | + return ((mp_fun_ext_var_t)self->fun)(&mp_ext_table, n_args, args); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +STATIC const mp_obj_type_t mp_type_fun_extern = { |
| 67 | + { &mp_type_type }, |
| 68 | + .name = MP_QSTR_function, |
| 69 | + .call = fun_extern_call, |
| 70 | +}; |
| 71 | + |
| 72 | +STATIC mp_obj_t mp_obj_new_fun_extern(bool is_kw, mp_uint_t n_args_min, mp_uint_t n_args_max, void *f) { |
| 73 | + mp_obj_fun_extern_t *self = m_new_obj(mp_obj_fun_extern_t); |
| 74 | + self->base.type = &mp_type_fun_extern; |
| 75 | + self->is_kw = is_kw; |
| 76 | + self->n_args_min = n_args_min; |
| 77 | + self->n_args_max = n_args_max; |
| 78 | + self->fun = f; |
| 79 | + self->dict_globals = mp_globals_get(); |
| 80 | + return self; |
| 81 | +} |
| 82 | + |
| 83 | +STATIC const mp_ext_table_t mp_ext_table = { |
| 84 | + .mp_const_none_ = mp_const_none, |
| 85 | + .mp_const_false_ = mp_const_false, |
| 86 | + .mp_const_true_ = mp_const_true, |
| 87 | + .mp_obj_new_fun_extern = mp_obj_new_fun_extern, |
| 88 | + .qstr_from_str = qstr_from_str, |
| 89 | + .mp_store_global = mp_store_global, |
| 90 | + .mp_obj_new_list = mp_obj_new_list, |
| 91 | + .mp_binary_op = mp_binary_op, |
| 92 | +}; |
| 93 | + |
| 94 | +void mp_extern_load(const char *ext_name, mp_obj_dict_t *globals) { |
| 95 | + const byte *buf = mp_extern_load_binary(ext_name); |
| 96 | + |
| 97 | + if (buf == NULL) { |
| 98 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "could not load MPY binary")); |
| 99 | + } |
| 100 | + |
| 101 | + if (!(buf[0] == 'M' && buf[1] == 'P' && buf[2] == 'Y')) { |
| 102 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "not a valid MPY binary")); |
| 103 | + } |
| 104 | + |
| 105 | + if (buf[3] != MP_EXT_VERSION_MAJOR || buf[4] > MP_EXT_VERSION_MINOR) { |
| 106 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "MPY binary has wrong version")); |
| 107 | + } |
| 108 | + |
| 109 | + if (buf[6] != MP_EXT_ARCH_CURRENT) { |
| 110 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "MPY binary has wrong arch")); |
| 111 | + } |
| 112 | + |
| 113 | + // push context |
| 114 | + mp_obj_dict_t *old_locals = mp_locals_get(); |
| 115 | + mp_obj_dict_t *old_globals = mp_globals_get(); |
| 116 | + mp_locals_set(globals); |
| 117 | + mp_globals_set(globals); |
| 118 | + |
| 119 | + // call extern init |
| 120 | + mp_obj_t (*f)(const mp_ext_table_t*) = (mp_obj_t(*)(const mp_ext_table_t*))MICROPY_MAKE_POINTER_CALLABLE(buf + 8); |
| 121 | + f(&mp_ext_table); |
| 122 | + |
| 123 | + // pop context |
| 124 | + mp_globals_set(old_globals); |
| 125 | + mp_locals_set(old_locals); |
| 126 | +} |
| 127 | + |
| 128 | +#endif // MICROPY_MODULE_EXTERN |
0 commit comments