davide carboni / Mbed 2 deprecated pymite_http_get

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers global.c Source File

global.c

Go to the documentation of this file.
00001 /*
00002 # This file is Copyright 2003, 2006, 2007, 2009 Dean Hall.
00003 #
00004 # This file is part of the PyMite VM.
00005 # The PyMite VM is free software: you can redistribute it and/or modify
00006 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
00007 #
00008 # The PyMite VM is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2
00012 # is seen in the file COPYING in this directory.
00013 */
00014 
00015 
00016 #undef __FILE_ID__
00017 #define __FILE_ID__ 0x05
00018 
00019 
00020 /**
00021  * \file
00022  * \brief VM Globals
00023  *
00024  * VM globals operations.
00025  * PyMite's global struct def and initial values.
00026  */
00027 
00028 
00029 #include "pm.h"
00030 
00031 
00032 extern unsigned char const *stdlib_img;
00033 
00034 static uint8_t const *bistr = (uint8_t const *)"__bi";
00035 
00036 
00037 /** Most PyMite globals all in one convenient place */
00038 volatile PmVmGlobal_t gVmGlobal;
00039 
00040 
00041 PmReturn_t
00042 global_init(void)
00043 {
00044     PmReturn_t retval;
00045     uint8_t *codestr = (uint8_t *)"code";
00046     uint8_t *pchunk;
00047     pPmObj_t pobj;
00048 #ifdef HAVE_CLASSES
00049     uint8_t const *initstr = (uint8_t const *)"__init__"; 
00050 #endif /* HAVE_CLASSES */
00051 #ifdef HAVE_GENERATORS
00052     uint8_t const *genstr = (uint8_t const *)"Generator";
00053     uint8_t const *nextstr = (uint8_t const *)"next";
00054 #endif /* HAVE_GENERATORS */
00055 #ifdef HAVE_ASSERT
00056     uint8_t const *exnstr = (uint8_t const *)"Exception";
00057 #endif /* HAVE_ASSERT */
00058 
00059     /* Clear the global struct */
00060     sli_memset((uint8_t *)&gVmGlobal, '\0', sizeof(PmVmGlobal_t));
00061 
00062     /* Set the PyMite release num (for debug and post mortem) */
00063     gVmGlobal.errVmRelease = PM_RELEASE;
00064 
00065     /* Init zero */
00066     retval = heap_getChunk(sizeof(PmInt_t), &pchunk);
00067     PM_RETURN_IF_ERROR(retval);
00068     pobj = (pPmObj_t)pchunk;
00069     OBJ_SET_TYPE(pobj, OBJ_TYPE_INT);
00070     ((pPmInt_t)pobj)->val = (int32_t)0;
00071     gVmGlobal.pzero = (pPmInt_t)pobj;
00072 
00073     /* Init one */
00074     retval = heap_getChunk(sizeof(PmInt_t), &pchunk);
00075     PM_RETURN_IF_ERROR(retval);
00076     pobj = (pPmObj_t)pchunk;
00077     OBJ_SET_TYPE(pobj, OBJ_TYPE_INT);
00078     ((pPmInt_t)pobj)->val = (int32_t)1;
00079     gVmGlobal.pone = (pPmInt_t)pobj;
00080 
00081     /* Init negone */
00082     retval = heap_getChunk(sizeof(PmInt_t), &pchunk);
00083     PM_RETURN_IF_ERROR(retval);
00084     pobj = (pPmObj_t)pchunk;
00085     OBJ_SET_TYPE(pobj, OBJ_TYPE_INT);
00086     ((pPmInt_t)pobj)->val = (int32_t)-1;
00087     gVmGlobal.pnegone = (pPmInt_t)pobj;
00088 
00089     /* Init False */
00090     retval = heap_getChunk(sizeof(PmBoolean_t), &pchunk);
00091     PM_RETURN_IF_ERROR(retval);
00092     pobj = (pPmObj_t)pchunk;
00093     OBJ_SET_TYPE(pobj, OBJ_TYPE_BOOL);
00094     ((pPmBoolean_t) pobj)->val = (int32_t)C_FALSE;
00095     gVmGlobal.pfalse = (pPmInt_t)pobj;
00096 
00097     /* Init True */
00098     retval = heap_getChunk(sizeof(PmBoolean_t), &pchunk);
00099     PM_RETURN_IF_ERROR(retval);
00100     pobj = (pPmObj_t)pchunk;
00101     OBJ_SET_TYPE(pobj, OBJ_TYPE_BOOL);
00102     ((pPmBoolean_t) pobj)->val = (int32_t)C_TRUE;
00103     gVmGlobal.ptrue = (pPmInt_t)pobj;
00104 
00105     /* Init None */
00106     retval = heap_getChunk(sizeof(PmObj_t), &pchunk);
00107     PM_RETURN_IF_ERROR(retval);
00108     pobj = (pPmObj_t)pchunk;
00109     OBJ_SET_TYPE(pobj, OBJ_TYPE_NON);
00110     gVmGlobal.pnone = pobj;
00111 
00112     /* Init "code" string obj */
00113     retval = string_new((uint8_t const **)&codestr, &pobj);
00114     PM_RETURN_IF_ERROR(retval);
00115     gVmGlobal.pcodeStr = (pPmString_t)pobj;
00116 
00117 #ifdef HAVE_CLASSES
00118     /* Init "__init__" string obj */
00119     retval = string_new((uint8_t const **)&initstr, &pobj);
00120     PM_RETURN_IF_ERROR(retval);
00121     gVmGlobal.pinitStr = (pPmString_t)pobj;
00122 #endif /* HAVE_CLASSES */
00123 
00124 #ifdef HAVE_GENERATORS
00125     /* Init "Generator" string obj */
00126     retval = string_new((uint8_t const **)&genstr, &pobj);
00127     PM_RETURN_IF_ERROR(retval);
00128     gVmGlobal.pgenStr = (pPmString_t)pobj;
00129     
00130     /* Init "next" string obj */
00131     retval = string_new((uint8_t const **)&nextstr, &pobj);
00132     PM_RETURN_IF_ERROR(retval);
00133     gVmGlobal.pnextStr = (pPmString_t)pobj;
00134 #endif /* HAVE_GENERATORS */
00135 
00136 #ifdef HAVE_ASSERT
00137     /* Init "Exception" string obj */
00138     retval = string_new((uint8_t const **)&exnstr, &pobj);
00139     PM_RETURN_IF_ERROR(retval);
00140     gVmGlobal.pexnStr = (pPmString_t)pobj;
00141 #endif /* HAVE_ASSERT */
00142 
00143     /* Init empty builtins */
00144     gVmGlobal.builtins = C_NULL;
00145 
00146     /* Init native frame */
00147     OBJ_SET_SIZE(&gVmGlobal.nativeframe, sizeof(PmNativeFrame_t));
00148     OBJ_SET_TYPE(&gVmGlobal.nativeframe, OBJ_TYPE_NFM);
00149 
00150     /* Create empty threadList */
00151     retval = list_new(&pobj);
00152     gVmGlobal.threadList = (pPmList_t)pobj;
00153 
00154     /* Init the PmImgPaths with std image info */
00155     gVmGlobal.imgPaths.memspace[0] = MEMSPACE_PROG;
00156     gVmGlobal.imgPaths.pimg[0] = (uint8_t *)&stdlib_img;
00157     gVmGlobal.imgPaths.pathcount = 1;
00158 
00159     return retval;
00160 }
00161 
00162 
00163 PmReturn_t
00164 global_setBuiltins(pPmFunc_t pmod)
00165 {
00166     PmReturn_t retval = PM_RET_OK;
00167     pPmObj_t pkey = C_NULL;
00168     uint8_t const *pbistr = bistr;
00169 
00170     if (PM_PBUILTINS == C_NULL)
00171     {
00172         /* Need to load builtins first */
00173         global_loadBuiltins();
00174     }
00175 
00176     /* Put builtins module in the module's attrs dict */
00177     retval = string_new(&pbistr, &pkey);
00178     PM_RETURN_IF_ERROR(retval);
00179 
00180     return dict_setItem((pPmObj_t)pmod->f_attrs, pkey, PM_PBUILTINS);
00181 }
00182 
00183 
00184 PmReturn_t
00185 global_loadBuiltins(void)
00186 {
00187     PmReturn_t retval = PM_RET_OK;
00188     pPmObj_t pkey = C_NULL;
00189     uint8_t const *nonestr = (uint8_t const *)"None";
00190     uint8_t const *falsestr = (uint8_t const *)"False";
00191     uint8_t const *truestr = (uint8_t const *)"True";
00192     pPmObj_t pstr = C_NULL;
00193     pPmObj_t pbimod;
00194     uint8_t const *pbistr = bistr;
00195 
00196     /* Import the builtins */
00197     retval = string_new(&pbistr, &pstr);
00198     PM_RETURN_IF_ERROR(retval);
00199     retval = mod_import(pstr, &pbimod);
00200     PM_RETURN_IF_ERROR(retval);
00201 
00202     /* Must interpret builtins' root code to set the attrs */
00203     C_ASSERT(gVmGlobal.threadList->length == 0);
00204     interp_addThread((pPmFunc_t)pbimod);
00205     retval = interpret(INTERP_RETURN_ON_NO_THREADS);
00206     PM_RETURN_IF_ERROR(retval);
00207 
00208     /* Builtins points to the builtins module's attrs dict */
00209     gVmGlobal.builtins = ((pPmFunc_t)pbimod)->f_attrs;
00210 
00211     /* Set None manually */
00212     retval = string_new(&nonestr, &pkey);
00213     PM_RETURN_IF_ERROR(retval);
00214     retval = dict_setItem(PM_PBUILTINS, pkey, PM_NONE);
00215     PM_RETURN_IF_ERROR(retval);
00216 
00217     /* Set False manually */
00218     retval = string_new(&falsestr, &pkey);
00219     PM_RETURN_IF_ERROR(retval);
00220     retval = dict_setItem(PM_PBUILTINS, pkey, PM_FALSE);
00221     PM_RETURN_IF_ERROR(retval);
00222 
00223     /* Set True manually */
00224     retval = string_new(&truestr, &pkey);
00225     PM_RETURN_IF_ERROR(retval);
00226     retval = dict_setItem(PM_PBUILTINS, pkey, PM_TRUE);
00227     PM_RETURN_IF_ERROR(retval);
00228 
00229     /* Deallocate builtins module */
00230     retval = heap_freeChunk((pPmObj_t)pbimod);
00231 
00232     return retval;
00233 }