blob: fb61c01dd1584aadd67e141d3c1ca28740b682cb [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +030027typedef enum {
28 MP_VM_RETURN_NORMAL,
29 MP_VM_RETURN_YIELD,
30 MP_VM_RETURN_EXCEPTION,
31} mp_vm_return_kind_t;
32
Damien Georgea3f94e02014-04-20 00:13:22 +010033typedef enum {
Damien Georgedbc81df2014-04-26 11:19:17 +010034 MP_ARG_BOOL = 0x001,
35 MP_ARG_INT = 0x002,
36 MP_ARG_OBJ = 0x003,
37 MP_ARG_KIND_MASK = 0x0ff,
38 MP_ARG_REQUIRED = 0x100,
39 MP_ARG_KW_ONLY = 0x200,
40} mp_arg_flag_t;
Damien Georgea3f94e02014-04-20 00:13:22 +010041
Damien Georgedbc81df2014-04-26 11:19:17 +010042typedef union _mp_arg_val_t {
Damien Georgea3f94e02014-04-20 00:13:22 +010043 bool u_bool;
44 machine_int_t u_int;
45 mp_obj_t u_obj;
Damien Georgedbc81df2014-04-26 11:19:17 +010046} mp_arg_val_t;
Damien Georgea3f94e02014-04-20 00:13:22 +010047
Damien Georgedbc81df2014-04-26 11:19:17 +010048typedef struct _mp_arg_t {
Damien Georgea3f94e02014-04-20 00:13:22 +010049 qstr qstr;
50 machine_uint_t flags;
Damien Georgedbc81df2014-04-26 11:19:17 +010051 mp_arg_val_t defval;
52} mp_arg_t;
Damien Georgea3f94e02014-04-20 00:13:22 +010053
Damien Georged17926d2014-03-30 13:35:08 +010054void mp_init(void);
55void mp_deinit(void);
Damien George2326d522014-03-27 23:26:35 +000056
Damien Georgea3f94e02014-04-20 00:13:22 +010057void mp_arg_check_num(uint n_args, uint n_kw, uint n_args_min, uint n_args_max, bool takes_kw);
Damien Georgedbc81df2014-04-26 11:19:17 +010058void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
Damien George491cbd62014-05-06 16:38:54 +000059void mp_arg_parse_all_kw_array(uint n_pos, uint n_kw, const mp_obj_t *args, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
Damien Georgec53b4082014-05-06 16:52:35 +000060NORETURN void mp_arg_error_unimpl_kw(void);
Dave Hylands51dabac2014-02-17 17:57:13 -080061
Damien George7efc5b32014-04-05 22:36:42 +010062mp_obj_dict_t *mp_locals_get(void);
63void mp_locals_set(mp_obj_dict_t *d);
64mp_obj_dict_t *mp_globals_get(void);
65void mp_globals_set(mp_obj_dict_t *d);
Damien429d7192013-10-04 19:53:11 +010066
Damien Georged17926d2014-03-30 13:35:08 +010067mp_obj_t mp_load_name(qstr qstr);
68mp_obj_t mp_load_global(qstr qstr);
69mp_obj_t mp_load_build_class(void);
70void mp_store_name(qstr qstr, mp_obj_t obj);
71void mp_store_global(qstr qstr, mp_obj_t obj);
72void mp_delete_name(qstr qstr);
Damien George1d24ea52014-04-08 21:11:49 +010073void mp_delete_global(qstr qstr);
Damien George66028ab2014-01-03 14:03:48 +000074
Damien Georged17926d2014-03-30 13:35:08 +010075mp_obj_t mp_unary_op(int op, mp_obj_t arg);
76mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs);
77
Damien George503d6112014-05-28 14:07:21 +010078mp_obj_t mp_load_const_int(qstr qstr);
Damien Georged17926d2014-03-30 13:35:08 +010079mp_obj_t mp_load_const_dec(qstr qstr);
80mp_obj_t mp_load_const_str(qstr qstr);
81mp_obj_t mp_load_const_bytes(qstr qstr);
82
Damien Georged17926d2014-03-30 13:35:08 +010083mp_obj_t mp_make_function_n(int n_args, void *fun); // fun must have the correct signature for n_args fixed arguments
84mp_obj_t mp_make_function_var(int n_args_min, mp_fun_var_t fun);
85mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun); // min and max are inclusive
Damien Georged17926d2014-03-30 13:35:08 +010086
87mp_obj_t mp_call_function_0(mp_obj_t fun);
88mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg);
89mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
90mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args);
91mp_obj_t mp_call_function_n_kw(mp_obj_t fun, uint n_args, uint n_kw, const mp_obj_t *args);
92mp_obj_t mp_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args);
Damien George523b5752014-03-31 11:59:23 +010093mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_t *args);
Damien Georged17926d2014-03-30 13:35:08 +010094
Damien Georged17926d2014-03-30 13:35:08 +010095void mp_unpack_sequence(mp_obj_t seq, uint num, mp_obj_t *items);
Damien George495d7812014-04-08 17:51:47 +010096void mp_unpack_ex(mp_obj_t seq, uint num, mp_obj_t *items);
Damien Georged17926d2014-03-30 13:35:08 +010097mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value);
98mp_obj_t mp_load_attr(mp_obj_t base, qstr attr);
99void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
Damien Georgee44d26a2014-03-31 22:57:56 +0100100void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
Damien Georged17926d2014-03-30 13:35:08 +0100101void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
Damien Georged17926d2014-03-30 13:35:08 +0100102
103mp_obj_t mp_getiter(mp_obj_t o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100104mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration()
105mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...)
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300106mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val);
Damien Georged17926d2014-03-30 13:35:08 +0100107
108mp_obj_t mp_make_raise_obj(mp_obj_t o);
109
Damien Georgedf6567e2014-03-30 13:54:02 +0100110mp_map_t *mp_loaded_modules_get(void);
Damien Georged17926d2014-03-30 13:35:08 +0100111mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
112mp_obj_t mp_import_from(mp_obj_t module, qstr name);
113void mp_import_all(mp_obj_t module);
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300114
Paul Sokolovsky7e4a2b02014-06-07 23:22:41 +0300115// Raise NotImplementedError with given message
116NORETURN void mp_not_implemented(const char *msg);
117
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300118extern struct _mp_obj_list_t mp_sys_path_obj;
119extern struct _mp_obj_list_t mp_sys_argv_obj;
120#define mp_sys_path ((mp_obj_t)&mp_sys_path_obj)
121#define mp_sys_argv ((mp_obj_t)&mp_sys_argv_obj)