Skip to content
Snippets Groups Projects
Select Git revision
  • 8bc3516389cd148ebeaa58ceaf3d3f7fb13440d8
  • wip-bootstrap default
  • dualcore
  • ch3/leds
  • ch3/time
  • master
6 results

ffi.c

Blame
  • objgenerator.c 9.29 KiB
    /*
     * This file is part of the Micro Python project, http://micropython.org/
     *
     * The MIT License (MIT)
     *
     * Copyright (c) 2013, 2014 Damien P. George
     * Copyright (c) 2014 Paul Sokolovsky
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     *
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     */
    
    #include <stdlib.h>
    #include <assert.h>
    
    #include "py/nlr.h"
    #include "py/obj.h"
    #include "py/runtime.h"
    #include "py/bc.h"
    #include "py/objgenerator.h"
    #include "py/objfun.h"
    
    /******************************************************************************/
    /* generator wrapper                                                          */
    
    typedef struct _mp_obj_gen_wrap_t {
        mp_obj_base_t base;
        mp_obj_t *fun;
    } mp_obj_gen_wrap_t;
    
    typedef struct _mp_obj_gen_instance_t {
        mp_obj_base_t base;
        mp_obj_dict_t *globals;
        mp_code_state code_state;
    } mp_obj_gen_instance_t;
    
    STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
        mp_obj_gen_wrap_t *self = self_in;
        mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
        assert(MP_OBJ_IS_TYPE(self_fun, &mp_type_fun_bc));
    
        // skip code-info block
        const byte *code_info = self_fun->bytecode;
        mp_uint_t code_info_size = mp_decode_uint(&code_info);
        const byte *ip = self_fun->bytecode + code_info_size;
    
        // bytecode prelude: skip arg names
        ip += (self_fun->n_pos_args + self_fun->n_kwonly_args) * sizeof(mp_obj_t);
    
        // bytecode prelude: get state size and exception stack size
        mp_uint_t n_state = mp_decode_uint(&ip);
        mp_uint_t n_exc_stack = mp_decode_uint(&ip);
    
        // allocate the generator object, with room for local stack and exception stack
        mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte,