davide carboni / Mbed 2 deprecated pymite_http_get

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pmstdlib_nat.c Source File

pmstdlib_nat.c

Go to the documentation of this file.
00001 #undef __FILE_ID__
00002 #define __FILE_ID__ 0x0A
00003 /**
00004  * PyMite std native function file
00005  *
00006  * automatically created by pmImgCreator.py
00007  * on Wed Jul 21 11:21:27 2010
00008  *
00009  * DO NOT EDIT THIS FILE.
00010  * ANY CHANGES WILL BE LOST.
00011  *
00012  * @file    pmstdlib_nat.c
00013  */
00014 
00015 #define __IN_LIBNATIVE_C__
00016 #include "pm.h"
00017 
00018 /* From: ../lib/string.py */
00019 #include <stdlib.h>
00020 #include <string.h>
00021 
00022 PmReturn_t
00023 nat_00_list_append(pPmFrame_t *ppframe)
00024 {
00025 
00026     pPmObj_t pl;
00027     PmReturn_t retval = PM_RET_OK;
00028 
00029     /* Raise TypeError if it's not a list or wrong number of args, */
00030     pl = NATIVE_GET_LOCAL(0);
00031     if ((OBJ_GET_TYPE(pl) != OBJ_TYPE_LST) || (NATIVE_GET_NUM_ARGS() != 2))
00032     {
00033         PM_RAISE(retval, PM_RET_EX_TYPE);
00034         return retval;
00035     }
00036 
00037     /* Append the object to the list */
00038     retval = list_append(pl, NATIVE_GET_LOCAL(1));
00039 
00040     NATIVE_SET_TOS(PM_NONE);
00041 
00042     return retval;
00043     
00044 }
00045 
00046 PmReturn_t
00047 nat_01_list_index(pPmFrame_t *ppframe)
00048 {
00049 
00050     pPmObj_t pl;
00051     pPmObj_t po;
00052     pPmObj_t pi;
00053     PmReturn_t retval = PM_RET_OK;
00054     uint16_t i;
00055 
00056     /* Raise TypeError if it's not a list or wrong number of args, */
00057     pl = NATIVE_GET_LOCAL(0);
00058     if ((OBJ_GET_TYPE(pl) != OBJ_TYPE_LST) || (NATIVE_GET_NUM_ARGS() != 2))
00059     {
00060         PM_RAISE(retval, PM_RET_EX_TYPE);
00061         return retval;
00062     }
00063 
00064     /* Get the index of the object in the list */
00065     po = NATIVE_GET_LOCAL(1);
00066     retval = list_index(pl, po, &i);
00067 
00068     if (retval == PM_RET_EX_VAL)
00069     {
00070         PM_RAISE(retval, PM_RET_EX_VAL);
00071         return retval;
00072     }
00073 
00074     int_new((int32_t)i, &pi);
00075     NATIVE_SET_TOS(pi);
00076 
00077     return retval;
00078     
00079 }
00080 
00081 PmReturn_t
00082 nat_02_list_insert(pPmFrame_t *ppframe)
00083 {
00084 
00085     pPmObj_t pl;
00086     pPmObj_t pi;
00087     pPmObj_t po;
00088     PmReturn_t retval = PM_RET_OK;
00089     uint16_t i;
00090 
00091     /*
00092      * Raise TypeError if wrong number of args, first arg is not a list, or
00093      * second arg is not an int
00094      */
00095     pl = NATIVE_GET_LOCAL(0);
00096     pi = NATIVE_GET_LOCAL(1);
00097     po = NATIVE_GET_LOCAL(2);
00098     if ((NATIVE_GET_NUM_ARGS() != 3)
00099         || (OBJ_GET_TYPE(pl) != OBJ_TYPE_LST)
00100         || (OBJ_GET_TYPE(pi) != OBJ_TYPE_INT) )
00101     {
00102         PM_RAISE(retval, PM_RET_EX_TYPE);
00103         return retval;
00104     }
00105 
00106     /* Insert the object before the given index */
00107     i = (uint16_t)((pPmInt_t)pi)->val;
00108     retval = list_insert(pl, i, po);
00109 
00110     if (retval != PM_RET_OK)
00111     {
00112         PM_RAISE(retval, PM_RET_EX_SYS);
00113     }
00114 
00115     NATIVE_SET_TOS(PM_NONE);
00116 
00117     return retval;
00118     
00119 }
00120 
00121 PmReturn_t
00122 nat_03_list_pop(pPmFrame_t *ppframe)
00123 {
00124 
00125     pPmObj_t pl;
00126     pPmObj_t pi;
00127     pPmObj_t po;
00128     PmReturn_t retval = PM_RET_OK;
00129     int16_t i;
00130 
00131     /*
00132      * Raise TypeError if first arg is not a list o second arg is not an int
00133      * or there are the wrong number of arguments
00134      */
00135     pl = NATIVE_GET_LOCAL(0);
00136     if (OBJ_GET_TYPE(pl) != OBJ_TYPE_LST)
00137     {
00138         PM_RAISE(retval, PM_RET_EX_TYPE);
00139         return retval;
00140     }
00141 
00142     pi = NATIVE_GET_LOCAL(1);
00143     if (NATIVE_GET_NUM_ARGS() == 2)
00144     {
00145         if (OBJ_GET_TYPE(pi) != OBJ_TYPE_INT)
00146         {
00147             PM_RAISE(retval, PM_RET_EX_TYPE);
00148             return retval;
00149         }
00150         i = (uint16_t)((pPmInt_t)pi)->val;
00151     }
00152     else
00153     {
00154         i = -1;
00155     }
00156     if ((NATIVE_GET_NUM_ARGS() < 1) || (NATIVE_GET_NUM_ARGS() > 2))
00157     {
00158         PM_RAISE(retval, PM_RET_EX_TYPE);
00159         return retval;
00160     }
00161 
00162     /* Get the object at the given index */
00163     retval = list_getItem(pl, i, &po);
00164     PM_RETURN_IF_ERROR(retval);
00165 
00166     /* Return the object to the caller */
00167     NATIVE_SET_TOS(po);
00168 
00169     /* Remove the object from the given index */
00170     retval = list_delItem(pl, i);
00171     PM_RETURN_IF_ERROR(retval);
00172 
00173     return retval;
00174     
00175 }
00176 
00177 PmReturn_t
00178 nat_04_list_remove(pPmFrame_t *ppframe)
00179 {
00180 
00181     pPmObj_t pl;
00182     pPmObj_t pv;
00183     PmReturn_t retval = PM_RET_OK;
00184 
00185     /* Raise TypeError if it's not a list or wrong number of args, */
00186     pl = NATIVE_GET_LOCAL(0);
00187     if ((OBJ_GET_TYPE(pl) != OBJ_TYPE_LST) || (NATIVE_GET_NUM_ARGS() != 2))
00188     {
00189         PM_RAISE(retval, PM_RET_EX_TYPE);
00190         return retval;
00191     }
00192 
00193     /* Remove the value from the list */
00194     pv = NATIVE_GET_LOCAL(1);
00195     retval = list_remove(pl, pv);
00196     if (retval != PM_RET_OK)
00197     {
00198         PM_RAISE(retval, retval);
00199     }
00200 
00201     NATIVE_SET_TOS(PM_NONE);
00202 
00203     return retval;
00204     
00205 }
00206 
00207 PmReturn_t
00208 nat_05_dict_clear(pPmFrame_t *ppframe)
00209 {
00210 
00211     pPmObj_t pd;
00212     PmReturn_t retval = PM_RET_OK;
00213 
00214     /* Raise TypeError if it's not a dict or wrong number of args, */
00215     pd = NATIVE_GET_LOCAL(0);
00216     if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
00217     {
00218         PM_RAISE(retval, PM_RET_EX_TYPE);
00219         return retval;
00220     }
00221 
00222     /* Clear the contents of the dict */
00223     retval = dict_clear(pd);
00224     PM_RETURN_IF_ERROR(retval);
00225 
00226     NATIVE_SET_TOS(PM_NONE);
00227 
00228     return retval;
00229     
00230 }
00231 
00232 PmReturn_t
00233 nat_06_dict_keys(pPmFrame_t *ppframe)
00234 {
00235 
00236     pPmObj_t pd;
00237     pPmObj_t pl;
00238     pPmObj_t pk;
00239     pSeglist_t psl;
00240     uint16_t i;
00241     PmReturn_t retval = PM_RET_OK;
00242 
00243     /* Raise TypeError if it's not a dict or wrong number of args, */
00244     pd = NATIVE_GET_LOCAL(0);
00245     if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
00246     {
00247         PM_RAISE(retval, PM_RET_EX_TYPE);
00248         return retval;
00249     }
00250 
00251     /* Create empty list */
00252     retval = list_new(&pl);
00253     PM_RETURN_IF_ERROR(retval);
00254 
00255     /* Iterate through the keys seglist */
00256     psl = ((pPmDict_t)pd)->d_keys;
00257     for (i = 0; i < ((pPmDict_t)pd)->length; i++)
00258     {
00259         /* Get the key and append it to the list */
00260         retval = seglist_getItem(psl, i, &pk);
00261         PM_RETURN_IF_ERROR(retval);
00262         retval = list_append(pl, pk);
00263         PM_RETURN_IF_ERROR(retval);
00264     }
00265 
00266     /* Return the list of keys to the caller */
00267     NATIVE_SET_TOS(pl);
00268 
00269     return retval;
00270     
00271 }
00272 
00273 PmReturn_t
00274 nat_07_dict_values(pPmFrame_t *ppframe)
00275 {
00276 
00277     pPmObj_t pd;
00278     pPmObj_t pl;
00279     pPmObj_t pv;
00280     pSeglist_t psl;
00281     uint16_t i;
00282     PmReturn_t retval = PM_RET_OK;
00283 
00284     /* Raise TypeError if it's not a dict or wrong number of args, */
00285     pd = NATIVE_GET_LOCAL(0);
00286     if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
00287     {
00288         PM_RAISE(retval, PM_RET_EX_TYPE);
00289         return retval;
00290     }
00291 
00292     /* Create empty list */
00293     retval = list_new(&pl);
00294     PM_RETURN_IF_ERROR(retval);
00295 
00296     /* Iterate through the values seglist */
00297     psl = ((pPmDict_t)pd)->d_vals;
00298     for (i = 0; i < ((pPmDict_t)pd)->length; i++)
00299     {
00300         /* Get the value and append it to the list */
00301         retval = seglist_getItem(psl, i, &pv);
00302         PM_RETURN_IF_ERROR(retval);
00303         retval = list_append(pl, pv);
00304         PM_RETURN_IF_ERROR(retval);
00305     }
00306 
00307     /* Return the list of values to the caller */
00308     NATIVE_SET_TOS(pl);
00309 
00310     return retval;
00311     
00312 }
00313 
00314 PmReturn_t
00315 nat_08___bi_chr(pPmFrame_t *ppframe)
00316 {
00317 
00318     pPmObj_t ps;
00319     pPmObj_t pn;
00320     int32_t n;
00321     PmReturn_t retval;
00322 
00323     /* If wrong number of args, raise TypeError */
00324     if (NATIVE_GET_NUM_ARGS() != 1)
00325     {
00326         PM_RAISE(retval, PM_RET_EX_TYPE);
00327         return retval;
00328     }
00329 
00330     /* Raise TypeError if arg is not an int */
00331     pn = NATIVE_GET_LOCAL(0);
00332     if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
00333     {
00334         PM_RAISE(retval, PM_RET_EX_TYPE);
00335         return retval;
00336     }
00337 
00338     /* Raise ValueError if arg is not int within range(256) */
00339     n = ((pPmInt_t)pn)->val;
00340     if ((n < 0) || (n > 255))
00341     {
00342         PM_RAISE(retval, PM_RET_EX_VAL);
00343         return retval;
00344     }
00345 
00346     /* Create char string from  integer value */
00347     retval = string_newFromChar((uint8_t)n, &ps);
00348     NATIVE_SET_TOS(ps);
00349     return retval;
00350     
00351 }
00352 
00353 PmReturn_t
00354 nat_09___bi_dir(pPmFrame_t *ppframe)
00355 {
00356 
00357     PmReturn_t retval = PM_RET_OK;
00358     pPmObj_t po;
00359     pPmObj_t pk;
00360     pPmObj_t pl;
00361     pSeglist_t psl;
00362     int16_t i;
00363 
00364     /* Use globals if no arg given */
00365     if (NATIVE_GET_NUM_ARGS() == 0)
00366     {
00367         /* Get the globals dict */
00368         po = (pPmObj_t)NATIVE_GET_PFRAME()->fo_globals;
00369     }
00370 
00371     /* Otherwise use the given arg */
00372     else if (NATIVE_GET_NUM_ARGS() == 1)
00373     {
00374         po = NATIVE_GET_LOCAL(0);
00375 
00376         /* If object is a function or module, use its attrs dict */
00377         if ((OBJ_GET_TYPE(po) == OBJ_TYPE_FXN)
00378             || (OBJ_GET_TYPE(po) == OBJ_TYPE_MOD))
00379         {
00380             po = (pPmObj_t)((pPmFunc_t)po)->f_attrs;
00381         }
00382 
00383 #ifdef HAVE_CLASSES
00384         else if (OBJ_GET_TYPE(po) == OBJ_TYPE_CLO)
00385         {
00386             po = (pPmObj_t)((pPmClass_t)po)->cl_attrs;
00387         }
00388         else if (OBJ_GET_TYPE(po) == OBJ_TYPE_CLI)
00389         {
00390             po = (pPmObj_t)((pPmInstance_t)po)->cli_attrs;
00391         }
00392         else if (OBJ_GET_TYPE(po) == OBJ_TYPE_MTH)
00393         {
00394             po = (pPmObj_t)((pPmMethod_t)po)->m_attrs;
00395         }
00396 #endif /* HAVE_CLASSES */
00397 
00398         else
00399         {
00400             po = C_NULL;
00401         }
00402     }
00403 
00404     /* Raise TypeError if wrong number of args */
00405     else
00406     {
00407         PM_RAISE(retval, PM_RET_EX_TYPE);
00408         return retval;
00409     }
00410 
00411     if (po == C_NULL)
00412     {
00413         pl = PM_NONE;
00414     }
00415     else
00416     {
00417         /* Create new list */
00418         retval = list_new(&pl);
00419         PM_RETURN_IF_ERROR(retval);
00420 
00421         /* Copy dict's keys to the list */
00422         psl = ((pPmDict_t)po)->d_keys;
00423         for (i = 0; i < ((pPmDict_t)po)->length; i++)
00424         {
00425             retval = seglist_getItem(psl, i, &pk);
00426             PM_RETURN_IF_ERROR(retval);
00427             retval = list_append(pl, pk);
00428             PM_RETURN_IF_ERROR(retval);
00429         }
00430     }
00431 
00432     NATIVE_SET_TOS(pl);
00433     return retval;
00434     
00435 }
00436 
00437 PmReturn_t
00438 nat_10___bi_eval(pPmFrame_t *ppframe)
00439 {
00440 
00441     PmReturn_t retval;
00442     pPmObj_t pco;
00443     pPmObj_t pfunc;
00444     pPmObj_t pnewframe;
00445     pPmObj_t pg = C_NULL;
00446     pPmObj_t pl = C_NULL;
00447 
00448     /* If wrong number of args, raise TypeError */
00449     if ((NATIVE_GET_NUM_ARGS() == 0) || (NATIVE_GET_NUM_ARGS() > 3))
00450     {
00451         PM_RAISE(retval, PM_RET_EX_TYPE);
00452         return retval;
00453     }
00454 
00455     /* Raise ValueError if first arg is not a Code Object */
00456     pco = NATIVE_GET_LOCAL(0);
00457     if (OBJ_GET_TYPE(pco) != OBJ_TYPE_COB)
00458     {
00459         PM_RAISE(retval, PM_RET_EX_VAL);
00460         return retval;
00461     }
00462 
00463     /* If 2nd arg exists, raise ValueError if it is not a Dict */
00464     if (NATIVE_GET_NUM_ARGS() >= 2)
00465     {
00466         pg = NATIVE_GET_LOCAL(1);
00467         if (OBJ_GET_TYPE(pg) != OBJ_TYPE_DIC)
00468         {
00469             PM_RAISE(retval, PM_RET_EX_VAL);
00470             return retval;
00471         }
00472     }
00473 
00474     /* If no args are given, use the caller's globals for the function's */
00475     else
00476     {
00477         pg = (pPmObj_t)NATIVE_GET_PFRAME()->fo_globals;
00478     }
00479 
00480     /* If 3rd arg exists, raise ValueError if it is not a Dict */
00481     if (NATIVE_GET_NUM_ARGS() >= 3)
00482     {
00483         pl = NATIVE_GET_LOCAL(2);
00484         if (OBJ_GET_TYPE(pl) != OBJ_TYPE_DIC)
00485         {
00486             PM_RAISE(retval, PM_RET_EX_VAL);
00487             return retval;
00488         }
00489     }
00490 
00491     /* Create func from code object */
00492     retval = func_new(pco, pg, &pfunc);
00493     PM_RETURN_IF_ERROR(retval);
00494 
00495     /* Create frame from module object; globals is set to null */
00496     retval = frame_new(pfunc, &pnewframe);
00497     PM_RETURN_IF_ERROR(retval);
00498 
00499     /* TODO: Reclaim pnewframe's attrs dict created in frame_new */
00500     /*
00501      * By default use calling frame's attrs as local namespace.
00502      * This works for ipm because the interactive mode
00503      * needs a locals namespace that persists across calls to eval()
00504      */
00505     ((pPmFrame_t)pnewframe)->fo_attrs = NATIVE_GET_PFRAME()->fo_attrs;
00506 
00507     /* If 2nd arg exists, use it as the global namespace for the new func */
00508     if (NATIVE_GET_NUM_ARGS() >= 2)
00509     {
00510         ((pPmFrame_t)pnewframe)->fo_globals = (pPmDict_t)pg;
00511 
00512         /* If only globals is given, locals defaults to it */
00513         ((pPmFrame_t)pnewframe)->fo_attrs = (pPmDict_t)pg;
00514     }
00515 
00516     /* If 3rd arg exists, use it as the local namespace for the new func */
00517     if (NATIVE_GET_NUM_ARGS() >= 3)
00518     {
00519         ((pPmFrame_t)pnewframe)->fo_attrs = (pPmDict_t)pl;
00520     }
00521 
00522     /*
00523      * Set the fo_back frame so flow returns to eval()'s caller when completed.
00524      * Set the frame pointer so the new frame is interpreted immediately
00525      * after this function returns.
00526      */
00527     ((pPmFrame_t)pnewframe)->fo_back = NATIVE_GET_PFRAME();
00528     NATIVE_GET_PFRAME() = (pPmFrame_t)pnewframe;
00529     retval = PM_RET_FRAME_SWITCH;
00530 
00531     return retval;
00532     
00533 }
00534 
00535 PmReturn_t
00536 nat_11___bi_globals(pPmFrame_t *ppframe)
00537 {
00538 
00539     pPmObj_t pr = C_NULL;
00540     PmReturn_t retval;
00541 
00542     /* If wrong number of args, raise TypeError */
00543     if (NATIVE_GET_NUM_ARGS() != 0)
00544     {
00545         PM_RAISE(retval, PM_RET_EX_TYPE);
00546         return retval;
00547     }
00548 
00549     /* Return calling frame's globals dict  on stack*/
00550     pr = (pPmObj_t)NATIVE_GET_PFRAME()->fo_globals;
00551     NATIVE_SET_TOS(pr);
00552 
00553     return PM_RET_OK;
00554     
00555 }
00556 
00557 PmReturn_t
00558 nat_12___bi_id(pPmFrame_t *ppframe)
00559 {
00560 
00561     PmReturn_t retval;
00562     pPmObj_t pr = C_NULL;
00563 
00564     /* If wrong number of args, raise TypeError */
00565     if (NATIVE_GET_NUM_ARGS() != 1)
00566     {
00567         PM_RAISE(retval, PM_RET_EX_TYPE);
00568         return retval;
00569     }
00570 
00571     /* Return object's address as an int on the stack */
00572     retval = int_new((intptr_t)NATIVE_GET_LOCAL(0), &pr);
00573     NATIVE_SET_TOS(pr);
00574 
00575     return retval;
00576     
00577 }
00578 
00579 PmReturn_t
00580 nat_13___bi_len(pPmFrame_t *ppframe)
00581 {
00582 
00583     PmReturn_t retval;
00584     pPmObj_t ps = C_NULL;
00585     pPmObj_t pr = C_NULL;
00586 
00587     /* If wrong number of args, raise TypeError */
00588     if (NATIVE_GET_NUM_ARGS() != 1)
00589     {
00590         PM_RAISE(retval, PM_RET_EX_TYPE);
00591         return retval;
00592     }
00593 
00594     /* Get first arg */
00595     ps = NATIVE_GET_LOCAL(0);
00596 
00597 #ifdef HAVE_BYTEARRAY
00598     /* If object is an instance, get the thing it contains */
00599     if (OBJ_GET_TYPE(ps) == OBJ_TYPE_CLI)
00600     {
00601         retval = dict_getItem((pPmObj_t)((pPmInstance_t)ps)->cli_attrs,
00602                               PM_NONE,
00603                               &pr);
00604 
00605         /* If None wasn't in attributes, obj is wrong type for len() */
00606         if (retval == PM_RET_EX_KEY) retval = PM_RET_EX_TYPE;
00607         PM_RETURN_IF_ERROR(retval);
00608         ps = pr;
00609     }
00610 #endif /* HAVE_BYTEARRAY */
00611 
00612     /* Get the length of the arg based on its type */
00613     switch (OBJ_GET_TYPE(ps))
00614     {
00615         case OBJ_TYPE_STR:
00616             retval = int_new(((pPmString_t)ps)->length, &pr);
00617             break;
00618 
00619         case OBJ_TYPE_TUP:
00620             retval = int_new(((pPmTuple_t)ps)->length, &pr);
00621             break;
00622 
00623         case OBJ_TYPE_LST:
00624             retval = int_new(((pPmList_t)ps)->length, &pr);
00625             break;
00626 
00627         case OBJ_TYPE_DIC:
00628             retval = int_new(((pPmDict_t)ps)->length, &pr);
00629             break;
00630 
00631 #ifdef HAVE_BYTEARRAY
00632         case OBJ_TYPE_BYA:
00633             retval = int_new(((pPmBytearray_t)ps)->length, &pr);
00634             break;
00635 #endif /* HAVE_BYTEARRAY */
00636 
00637         default:
00638             /* If not a string or sequence type, raise TypeError */
00639             PM_RAISE(retval, PM_RET_EX_TYPE);
00640     }
00641 
00642     NATIVE_SET_TOS(pr);
00643     return retval;
00644     
00645 }
00646 
00647 PmReturn_t
00648 nat_14___bi_locals(pPmFrame_t *ppframe)
00649 {
00650 
00651     pPmObj_t pr = C_NULL;
00652     PmReturn_t retval;
00653 
00654     /* If wrong number of args, raise TypeError */
00655     if (NATIVE_GET_NUM_ARGS() != 0)
00656     {
00657         PM_RAISE(retval, PM_RET_EX_TYPE);
00658         return retval;
00659     }
00660 
00661     /* Return calling frame's local attrs dict on the stack */
00662     pr = (pPmObj_t)NATIVE_GET_PFRAME()->fo_attrs;
00663     NATIVE_SET_TOS(pr);
00664 
00665     return PM_RET_OK;
00666     
00667 }
00668 
00669 PmReturn_t
00670 nat_15___bi_ord(pPmFrame_t *ppframe)
00671 {
00672 
00673     pPmObj_t ps;
00674     pPmObj_t pn;
00675     int32_t n;
00676     PmReturn_t retval;
00677 
00678     /* If wrong number of args, raise TypeError */
00679     if (NATIVE_GET_NUM_ARGS() != 1)
00680     {
00681         PM_RAISE(retval, PM_RET_EX_TYPE);
00682         return retval;
00683     }
00684 
00685     ps = NATIVE_GET_LOCAL(0);
00686 
00687     /* Raise TypeError if arg is not string of length 1 */
00688     if ((OBJ_GET_TYPE(ps) != OBJ_TYPE_STR)
00689         || (((pPmString_t)ps)->length != 1))
00690 
00691     {
00692         PM_RAISE(retval, PM_RET_EX_TYPE);
00693         return retval;
00694     }
00695 
00696     /* Get integer value of character */
00697     n = ((pPmString_t)ps)->val[0];
00698     retval = int_new(n, &pn);
00699     NATIVE_SET_TOS(pn);
00700     return retval;
00701     
00702 }
00703 
00704 PmReturn_t
00705 nat_16___bi_range(pPmFrame_t *ppframe)
00706 {
00707 
00708     PmReturn_t retval;
00709     pPmObj_t pa = C_NULL;
00710     pPmObj_t pb = C_NULL;
00711     pPmObj_t pc = C_NULL;
00712     pPmObj_t pi = C_NULL;
00713     pPmObj_t pr = C_NULL;
00714     int16_t i = 0;
00715 
00716     switch (NATIVE_GET_NUM_ARGS())
00717     {
00718         case 1:
00719             pa = PM_ZERO;
00720             pb = NATIVE_GET_LOCAL(0);
00721             pc = PM_ONE;
00722             break;
00723 
00724         case 2:
00725             pa = NATIVE_GET_LOCAL(0);
00726             pb = NATIVE_GET_LOCAL(1);
00727             pc = PM_ONE;
00728             break;
00729 
00730         case 3:
00731             pa = NATIVE_GET_LOCAL(0);
00732             pb = NATIVE_GET_LOCAL(1);
00733             pc = NATIVE_GET_LOCAL(2);
00734 
00735             /* If 3rd arg is 0, ValueError */
00736             if (((pPmInt_t)pc)->val == 0)
00737             {
00738                 PM_RAISE(retval, PM_RET_EX_VAL);
00739                 return retval;
00740             }
00741             break;
00742 
00743         default:
00744             /* If wrong number of args, raise TypeError */
00745             PM_RAISE(retval, PM_RET_EX_TYPE);
00746             return retval;
00747     }
00748 
00749     /* Allocate list */
00750     retval = list_new(&pr);
00751     PM_RETURN_IF_ERROR(retval);
00752 
00753     /* Iterate depending on counting direction */
00754     if (((pPmInt_t)pc)->val > 0)
00755     {
00756         for (i = ((pPmInt_t)pa)->val;
00757              i < ((pPmInt_t)pb)->val;
00758              i += ((pPmInt_t)pc)->val)
00759         {
00760             retval = int_new(i, &pi);
00761             PM_RETURN_IF_ERROR(retval);
00762 
00763             retval = list_append(pr, pi);
00764             PM_RETURN_IF_ERROR(retval);
00765         }
00766     }
00767     else
00768     {
00769         for (i = ((pPmInt_t)pa)->val;
00770              i > ((pPmInt_t)pb)->val;
00771              i += ((pPmInt_t)pc)->val)
00772         {
00773             retval = int_new(i, &pi);
00774             PM_RETURN_IF_ERROR(retval);
00775 
00776             retval = list_append(pr, pi);
00777             PM_RETURN_IF_ERROR(retval);
00778         }
00779     }
00780 
00781     /* Return list */
00782     NATIVE_SET_TOS(pr);
00783     return retval;
00784     
00785 }
00786 
00787 PmReturn_t
00788 nat_17___bi_sum(pPmFrame_t *ppframe)
00789 {
00790 
00791     pPmObj_t ps;
00792     pPmObj_t pn;
00793     pPmObj_t po;
00794     int32_t n;
00795     uint16_t len;
00796     uint16_t i;
00797     PmReturn_t retval;
00798 #ifdef HAVE_FLOAT
00799     float f;
00800     uint8_t usefloat = C_FALSE;
00801 #endif /* HAVE_FLOAT */
00802 
00803     /* If wrong number of args, raise TypeError */
00804     if (NATIVE_GET_NUM_ARGS() != 1)
00805     {
00806         PM_RAISE(retval, PM_RET_EX_TYPE);
00807         return retval;
00808     }
00809 
00810     /* Get the length of the sequence */
00811     ps = NATIVE_GET_LOCAL(0);
00812     if (OBJ_GET_TYPE(ps) == OBJ_TYPE_TUP)
00813     {
00814         len = ((pPmTuple_t)ps)->length;
00815     }
00816     else if (OBJ_GET_TYPE(ps) == OBJ_TYPE_LST)
00817     {
00818         len = ((pPmTuple_t)ps)->length;
00819     }
00820 
00821     /* Raise TypeError if arg is not a sequence */
00822     else
00823     {
00824         PM_RAISE(retval, PM_RET_EX_TYPE);
00825         return retval;
00826     }
00827 
00828     /* Calculate the sum of the sequence */
00829     n = 0;
00830 #ifdef HAVE_FLOAT
00831     f = 0.0;
00832 #endif
00833     for (i = 0; i < len; i++)
00834     {
00835         retval = seq_getSubscript(ps, i, &po);
00836 
00837         if (OBJ_GET_TYPE(po) == OBJ_TYPE_INT)
00838         {
00839             /* Add value to sum */
00840             n += ((pPmInt_t)po)->val;
00841 #ifdef HAVE_FLOAT
00842             f += (float)((pPmInt_t)po)->val;
00843 #endif /* HAVE_FLOAT */
00844         }
00845 
00846 #ifdef HAVE_FLOAT
00847         else if (OBJ_GET_TYPE(po) == OBJ_TYPE_FLT)
00848         {
00849             /* Add value to sum */
00850             f += ((pPmFloat_t)po)->val;
00851             usefloat = C_TRUE;
00852         }
00853 #endif /* HAVE_FLOAT */
00854 
00855         /* Raise TypeError if item is not an integer */
00856         else
00857         {
00858             PM_RAISE(retval, PM_RET_EX_TYPE);
00859             return retval;
00860         }
00861     }
00862 
00863 #ifdef HAVE_FLOAT
00864     if (usefloat)
00865     {
00866         retval = float_new(f, &pn);
00867     }
00868     else
00869 #endif /* HAVE_FLOAT */
00870     {
00871         retval = int_new(n, &pn);
00872     }
00873     NATIVE_SET_TOS(pn);
00874     return retval;
00875     
00876 }
00877 
00878 PmReturn_t
00879 nat_18___bi_type(pPmFrame_t *ppframe)
00880 {
00881 
00882     PmReturn_t retval;
00883     pPmObj_t po = C_NULL;
00884     pPmObj_t pr = C_NULL;
00885 
00886     /* If wrong number of args, raise TypeError */
00887     if (NATIVE_GET_NUM_ARGS() != 1)
00888     {
00889         PM_RAISE(retval, PM_RET_EX_TYPE);
00890         return retval;
00891     }
00892 
00893     /* Get arg */
00894     po = NATIVE_GET_LOCAL(0);
00895 
00896     /* Create int from type enum */
00897     retval = int_new(OBJ_GET_TYPE(po), &pr);
00898     NATIVE_SET_TOS(pr);
00899     return retval;
00900     
00901 }
00902 
00903 PmReturn_t
00904 nat_19___bi_Co(pPmFrame_t *ppframe)
00905 {
00906 
00907     PmReturn_t retval;
00908     pPmObj_t pimg;
00909     pPmObj_t pco;
00910 
00911     /* If wrong number of args, raise TypeError */
00912     if (NATIVE_GET_NUM_ARGS() != 1)
00913     {
00914         PM_RAISE(retval, PM_RET_EX_TYPE);
00915         return retval;
00916     }
00917 
00918     /* Raise ValueError if arg is not a string */
00919     pimg = NATIVE_GET_LOCAL(0);
00920     if (OBJ_GET_TYPE(pimg) != OBJ_TYPE_CIO)
00921     {
00922         PM_RAISE(retval, PM_RET_EX_VAL);
00923         return retval;
00924     }
00925 
00926     /* Create a code object from the image */
00927     retval = obj_loadFromImgObj(pimg, &pco);
00928     PM_RETURN_IF_ERROR(retval);
00929 
00930     /* Return the code object */
00931     NATIVE_SET_TOS(pco);
00932     return retval;
00933     
00934 }
00935 
00936 PmReturn_t
00937 nat_20___bi___init__(pPmFrame_t *ppframe)
00938 {
00939 
00940         PmReturn_t retval;
00941         pPmObj_t pself;
00942         pPmObj_t pfa;
00943         pPmObj_t pfunc;
00944         pPmObj_t pframe;
00945         uint8_t i;
00946 
00947         /* Raise TypeError if wrong number of args */
00948         if (NATIVE_GET_NUM_ARGS() != 2)
00949         {
00950             PM_RAISE(retval, PM_RET_EX_TYPE);
00951             return retval;
00952         }
00953 
00954         /* Raise ValueError if first args are not: instance, tuple */
00955         pself = NATIVE_GET_LOCAL(0);
00956         pfa = NATIVE_GET_LOCAL(1);
00957         if ((OBJ_GET_TYPE(pself) != OBJ_TYPE_CLI)
00958             || (OBJ_GET_TYPE(pfa) != OBJ_TYPE_TUP))
00959         {
00960             PM_RAISE(retval, PM_RET_EX_VAL);
00961             return retval;
00962         }
00963 
00964         /* Create a new frame for the function */
00965         pfunc = ((pPmTuple_t)pfa)->val[0];
00966         retval = frame_new(pfunc, &pframe);
00967         PM_RETURN_IF_ERROR(retval);
00968 
00969         /* Copy args into frame's locals */
00970         for (i = 0; i < ((pPmTuple_t)pfa)->length - 1; i++)
00971         {
00972             /* The pfa tuple is (func, [arg0, ... argN]) */
00973             ((pPmFrame_t)pframe)->fo_locals[i] = ((pPmTuple_t)pfa)->val[i + 1];
00974         }
00975 
00976         /* Store frame in None attr of instance */
00977         retval = dict_setItem((pPmObj_t)((pPmInstance_t)pself)->cli_attrs,
00978                               PM_NONE, pframe);
00979 
00980         NATIVE_SET_TOS(PM_NONE);
00981         return retval;
00982         
00983 }
00984 
00985 PmReturn_t
00986 nat_21___bi_send(pPmFrame_t *ppframe)
00987 {
00988 
00989         PmReturn_t retval;
00990         pPmObj_t pself;
00991         pPmObj_t parg;
00992         pPmObj_t pgenframe;
00993 
00994         /* Raise TypeError if wrong number of args */
00995         if (NATIVE_GET_NUM_ARGS() != 2)
00996         {
00997             PM_RAISE(retval, PM_RET_EX_TYPE);
00998             return retval;
00999         }
01000 
01001         /* Raise ValueError if first arg is not an instance */
01002         pself = NATIVE_GET_LOCAL(0);
01003         parg = NATIVE_GET_LOCAL(1);
01004         if (OBJ_GET_TYPE(pself) != OBJ_TYPE_CLI)
01005         {
01006             PM_RAISE(retval, PM_RET_EX_VAL);
01007             return retval;
01008         }
01009 
01010         /* Get the generator's frame */
01011         retval = dict_getItem((pPmObj_t)((pPmInstance_t)pself)->cli_attrs,
01012                               PM_NONE, &pgenframe);
01013         PM_RETURN_IF_ERROR(retval);
01014 
01015         /* Push argument onto generator's frame's stack */
01016         *(((pPmFrame_t)pgenframe)->fo_sp) = parg;
01017         ((pPmFrame_t)pgenframe)->fo_sp++;
01018 
01019         /* Set generator's frame's fo_back so yielded value goes to caller */
01020         ((pPmFrame_t)pgenframe)->fo_back = NATIVE_GET_PFRAME();
01021 
01022         /* Set active frame to run generator */
01023         NATIVE_GET_PFRAME() = (pPmFrame_t)pgenframe;
01024 
01025         return PM_RET_FRAME_SWITCH;
01026         
01027 }
01028 
01029 PmReturn_t
01030 nat_22___bi_ismain(pPmFrame_t *ppframe)
01031 {
01032 
01033 
01034     NATIVE_SET_TOS((NATIVE_GET_PFRAME()->fo_isImport) ? PM_FALSE : PM_TRUE);
01035 
01036     return PM_RET_OK;
01037     
01038 }
01039 
01040 PmReturn_t
01041 nat_23___bi___init__(pPmFrame_t *ppframe)
01042 {
01043 
01044         PmReturn_t retval;
01045         pPmObj_t pself;
01046         pPmObj_t po;
01047         pPmObj_t pba;
01048 
01049         /* If only the self arg, create zero-length bytearray */
01050         if (NATIVE_GET_NUM_ARGS() == 1)
01051         {
01052             po = PM_ZERO;
01053         }
01054 
01055         /* If two args, get the second arg */
01056         else if (NATIVE_GET_NUM_ARGS() == 2)
01057         {
01058             po = NATIVE_GET_LOCAL(1);
01059         }
01060 
01061         /* Raise TypeError if wrong number of args */
01062         else
01063         {
01064             PM_RAISE(retval, PM_RET_EX_TYPE);
01065             return retval;
01066         }
01067         pself = NATIVE_GET_LOCAL(0);
01068 
01069         /* Create new bytearray object */
01070         retval = bytearray_new(po, &pba);
01071         PM_RETURN_IF_ERROR(retval);
01072 
01073         /* Store bytearray in None attr of instance */
01074         retval = dict_setItem((pPmObj_t)((pPmInstance_t)pself)->cli_attrs,
01075                               PM_NONE, pba);
01076 
01077         NATIVE_SET_TOS(PM_NONE);
01078         return retval;
01079         
01080 }
01081 
01082 PmReturn_t
01083 nat_24_sys_exit(pPmFrame_t *ppframe)
01084 {
01085 
01086     pPmObj_t pval = C_NULL;
01087     PmReturn_t retval;
01088 
01089     /* If no arg given, assume return 0 */
01090     if (NATIVE_GET_NUM_ARGS() == 0)
01091     {
01092         NATIVE_SET_TOS(PM_ZERO);
01093     }
01094 
01095     /* If 1 arg given, put it on stack */
01096     else if (NATIVE_GET_NUM_ARGS() == 1)
01097     {
01098         pval = NATIVE_GET_LOCAL(0);
01099         NATIVE_SET_TOS(pval);
01100     }
01101 
01102     /* If wrong number of args, raise TypeError */
01103     else
01104     {
01105         PM_RAISE(retval, PM_RET_EX_TYPE);
01106         return retval;
01107     }
01108 
01109     /* Raise the SystemExit exception */
01110     PM_RAISE(retval, PM_RET_EX_EXIT);
01111     return retval;
01112     
01113 }
01114 
01115 PmReturn_t
01116 nat_25_sys_getb(pPmFrame_t *ppframe)
01117 {
01118 
01119     uint8_t b;
01120     pPmObj_t pb;
01121     PmReturn_t retval;
01122 
01123     /* If wrong number of args, raise TypeError */
01124     if (NATIVE_GET_NUM_ARGS() != 0)
01125     {
01126         PM_RAISE(retval, PM_RET_EX_TYPE);
01127         return retval;
01128     }
01129 
01130     retval = plat_getByte(&b);
01131     PM_RETURN_IF_ERROR(retval);
01132 
01133     retval = int_new((int32_t)b, &pb);
01134     NATIVE_SET_TOS(pb);
01135     return retval;
01136     
01137 }
01138 
01139 PmReturn_t
01140 nat_26_sys_heap(pPmFrame_t *ppframe)
01141 {
01142 
01143     PmReturn_t retval;
01144     pPmObj_t pavail;
01145     pPmObj_t pmax;
01146     pPmObj_t ptup;
01147 
01148     /* If wrong number of args, raise TypeError */
01149     if (NATIVE_GET_NUM_ARGS() != 0)
01150     {
01151         PM_RAISE(retval, PM_RET_EX_TYPE);
01152         return retval;
01153     }
01154 
01155     /* Allocate a tuple to store the return values */
01156     retval = tuple_new(2, &ptup);
01157     PM_RETURN_IF_ERROR(retval);
01158 
01159     /* Get the maximum heap size */
01160     retval = int_new(PM_HEAP_SIZE, &pmax);
01161     PM_RETURN_IF_ERROR(retval);
01162 
01163     /* Allocate an int to hold the amount of heap available */
01164     retval = int_new(heap_getAvail () - sizeof(PmInt_t), &pavail);
01165     PM_RETURN_IF_ERROR(retval);
01166 
01167     /* Put the two heap values in the tuple */
01168     ((pPmTuple_t)ptup)->val[0] = pavail;
01169     ((pPmTuple_t)ptup)->val[1] = pmax;
01170 
01171     /* Return the tuple on the stack */
01172     NATIVE_SET_TOS(ptup);
01173 
01174     return retval;
01175     
01176 }
01177 
01178 PmReturn_t
01179 nat_27_sys_putb(pPmFrame_t *ppframe)
01180 {
01181 
01182     uint8_t b;
01183     pPmObj_t pb;
01184     PmReturn_t retval;
01185 
01186     pb = NATIVE_GET_LOCAL(0);
01187 
01188     /* If wrong number of args, raise TypeError */
01189     if (NATIVE_GET_NUM_ARGS() != 1)
01190     {
01191         PM_RAISE(retval, PM_RET_EX_TYPE);
01192         return retval;
01193     }
01194 
01195     /* If arg is not an int, raise TypeError */
01196     if (OBJ_GET_TYPE(pb) != OBJ_TYPE_INT)
01197     {
01198         PM_RAISE(retval, PM_RET_EX_TYPE);
01199         return retval;
01200     }
01201 
01202     b = ((pPmInt_t)pb)->val & 0xFF;
01203     retval = plat_putByte(b);
01204     NATIVE_SET_TOS(PM_NONE);
01205     return retval;
01206     
01207 }
01208 
01209 PmReturn_t
01210 nat_28_sys_runInThread(pPmFrame_t *ppframe)
01211 {
01212 
01213     PmReturn_t retval;
01214     pPmObj_t pf;
01215 
01216     /* If wrong number of args, raise TypeError */
01217     if (NATIVE_GET_NUM_ARGS() != 1)
01218     {
01219         PM_RAISE(retval, PM_RET_EX_TYPE);
01220         return retval;
01221     }
01222 
01223     /* If arg is not a function, raise TypeError */
01224     pf = NATIVE_GET_LOCAL(0);
01225     if (OBJ_GET_TYPE(pf) != OBJ_TYPE_FXN)
01226     {
01227         PM_RAISE(retval, PM_RET_EX_TYPE);
01228         return retval;
01229     }
01230 
01231     retval = interp_addThread((pPmFunc_t)pf);
01232     NATIVE_SET_TOS(PM_NONE);
01233     return retval;
01234     
01235 }
01236 
01237 PmReturn_t
01238 nat_29_sys_time(pPmFrame_t *ppframe)
01239 {
01240 
01241     uint32_t t;
01242     pPmObj_t pt;
01243     PmReturn_t retval;
01244 
01245     /* If wrong number of args, raise TypeError */
01246     if (NATIVE_GET_NUM_ARGS() != 0)
01247     {
01248         PM_RAISE(retval, PM_RET_EX_TYPE);
01249         return retval;
01250     }
01251 
01252     /* Get the system time (milliseconds since init) */
01253     retval = plat_getMsTicks(&t);
01254     PM_RETURN_IF_ERROR(retval);
01255 
01256     /*
01257      * Raise ValueError if there is an overflow
01258      * (plat_getMsTicks is unsigned; int is signed)
01259      */
01260     if ((int32_t)t < 0)
01261     {
01262         PM_RAISE(retval, PM_RET_EX_VAL);
01263         return retval;
01264     }
01265 
01266     /* Return an int object with the time value */
01267     retval = int_new((int32_t)t, &pt);
01268     NATIVE_SET_TOS(pt);
01269     return retval;
01270     
01271 }
01272 
01273 PmReturn_t
01274 nat_30_string_atoi(pPmFrame_t *ppframe)
01275 {
01276 
01277     pPmObj_t pa;
01278     pPmObj_t pb;
01279     char const *pc;
01280     char *pend;
01281     long i;
01282     int8_t base;
01283     pPmObj_t pi;
01284     PmReturn_t retval = PM_RET_OK;
01285 
01286     /* Raise TypeError if it's not a string or wrong number of args, */
01287     pa = NATIVE_GET_LOCAL(0);
01288     if ((OBJ_GET_TYPE(pa) != OBJ_TYPE_STR) || (NATIVE_GET_NUM_ARGS() < 1)
01289         || (NATIVE_GET_NUM_ARGS() > 2))
01290     {
01291         PM_RAISE(retval, PM_RET_EX_TYPE);
01292         return retval;
01293     }
01294 
01295     /* Get the base, if it exists; otherwise assume 10 */
01296     base = 10;
01297     if (NATIVE_GET_NUM_ARGS() == 2)
01298     {
01299         pb = NATIVE_GET_LOCAL(1);
01300 
01301         /* Raise a TypeError if 2nd arg is not an int */
01302         if (OBJ_GET_TYPE(pb) != OBJ_TYPE_INT)
01303         {
01304             PM_RAISE(retval, PM_RET_EX_TYPE);
01305             return retval;
01306         }
01307 
01308         base = ((pPmInt_t)pb)->val;
01309 
01310         /* Raise ValueError if base is out of range */
01311         if ((base < 0) || (base == 1) || (base > 36))
01312         {
01313             PM_RAISE(retval, PM_RET_EX_VAL);
01314             return retval;
01315         }
01316     }
01317 
01318     /* Perform conversion */
01319     pend = C_NULL;
01320     pc = (char const *)&(((pPmString_t)pa)->val);
01321     i = strtol(pc, &pend, base);
01322 
01323     /* Raise ValueError if there was a conversion error */
01324     if (*pend != C_NULL)
01325     {
01326         PM_RAISE(retval, PM_RET_EX_VAL);
01327         return retval;
01328     }
01329 
01330     /* Create an int object to hold the result of the conversion */
01331     retval = int_new(i, &pi);
01332 
01333     NATIVE_SET_TOS(pi);
01334 
01335     return retval;
01336     
01337 }
01338 
01339 PmReturn_t
01340 nat_31_string_count(pPmFrame_t *ppframe)
01341 {
01342 
01343     pPmObj_t ps1;
01344     pPmObj_t ps2;
01345     uint8_t *pc1;
01346     uint8_t *pc2;
01347     uint8_t *pscan;
01348     uint16_t pc1len;
01349     uint16_t pc2len;
01350     uint16_t n;
01351     pPmObj_t pn;
01352     PmReturn_t retval = PM_RET_OK;
01353 
01354     /* Raise TypeError if it's not a string or wrong number of args, */
01355     ps1 = NATIVE_GET_LOCAL(0);
01356     ps2 = NATIVE_GET_LOCAL(1);
01357     if ((OBJ_GET_TYPE(ps1) != OBJ_TYPE_STR) || (NATIVE_GET_NUM_ARGS() != 2)
01358         || (OBJ_GET_TYPE(ps2) != OBJ_TYPE_STR))
01359     {
01360         PM_RAISE(retval, PM_RET_EX_TYPE);
01361         return retval;
01362     }
01363 
01364     pc1 = ((pPmString_t)ps1)->val;
01365     pc2 = ((pPmString_t)ps2)->val;
01366     pc1len = ((pPmString_t)ps1)->length;
01367     pc2len = ((pPmString_t)ps2)->length;
01368     n = 0;
01369 
01370     if (*pc2 != C_NULL)
01371     {
01372         pscan = (uint8_t *)strstr((const char *)pc1, (const char *)pc2);
01373         while (pscan != C_NULL)
01374         {
01375             n++;
01376             pscan += pc2len;
01377             if (pscan > pc1 + pc1len) break;
01378             pscan = (uint8_t *)strstr((const char *)pscan, (const char *)pc2);
01379         }
01380     }
01381 
01382     retval = int_new(n, &pn);
01383 
01384     NATIVE_SET_TOS(pn);
01385 
01386     return retval;
01387     
01388 }
01389 
01390 PmReturn_t
01391 nat_32_string_find(pPmFrame_t *ppframe)
01392 {
01393 
01394     pPmObj_t ps1;
01395     pPmObj_t ps2;
01396     uint8_t *pc1;
01397     uint8_t *pc2;
01398     uint8_t *pscan;
01399     int32_t n;
01400     pPmObj_t pn;
01401     PmReturn_t retval = PM_RET_OK;
01402 
01403     /* Raise TypeError if it's not a string or wrong number of args, */
01404     ps1 = NATIVE_GET_LOCAL(0);
01405     ps2 = NATIVE_GET_LOCAL(1);
01406     if ((OBJ_GET_TYPE(ps1) != OBJ_TYPE_STR) || (NATIVE_GET_NUM_ARGS() != 2)
01407         || (OBJ_GET_TYPE(ps2) != OBJ_TYPE_STR))
01408     {
01409         PM_RAISE(retval, PM_RET_EX_TYPE);
01410         return retval;
01411     }
01412 
01413     pc1 = ((pPmString_t)ps1)->val;
01414     pc2 = ((pPmString_t)ps2)->val;
01415     n = -1;
01416 
01417     /* If the strings are non-null, try to find the index of the substring */
01418     if ((*pc1 != C_NULL) && (*pc2 != C_NULL))
01419     {
01420         pscan = (uint8_t *)strstr((const char *)pc1, (const char *)pc2);
01421         if (pscan != C_NULL)
01422         {
01423             n = pscan - pc1;
01424         }
01425     }
01426 
01427     retval = int_new(n, &pn);
01428 
01429     NATIVE_SET_TOS(pn);
01430 
01431     return retval;
01432     
01433 }
01434 
01435 PmReturn_t
01436 nat_33_ipm__getImg(pPmFrame_t *ppframe)
01437 {
01438 
01439     PmReturn_t retval;
01440     uint8_t imgType;
01441     uint16_t imgSize;
01442     uint8_t *pchunk;
01443     pPmCodeImgObj_t pimg;
01444     uint16_t i;
01445     uint8_t b;
01446 
01447     /* Get the image type */
01448     retval = plat_getByte(&imgType);
01449     PM_RETURN_IF_ERROR(retval);
01450 
01451     /* Quit if a code image type was not received */
01452     if (imgType != OBJ_TYPE_CIM)
01453     {
01454         PM_RAISE(retval, PM_RET_EX_STOP);
01455         return retval;
01456     }
01457 
01458     /* Get the image size (little endien) */
01459     retval = plat_getByte(&b);
01460     PM_RETURN_IF_ERROR(retval);
01461     imgSize = b;
01462     retval = plat_getByte(&b);
01463     PM_RETURN_IF_ERROR(retval);
01464     imgSize |= (b << 8);
01465 
01466     /* Get space for CodeImgObj */
01467     retval = heap_getChunk(sizeof(PmCodeImgObj_t) + imgSize, &pchunk);
01468     PM_RETURN_IF_ERROR(retval);
01469     pimg = (pPmCodeImgObj_t)pchunk;
01470     OBJ_SET_TYPE(pimg, OBJ_TYPE_CIO);
01471 
01472     /* Start the image with the bytes that have already been received */
01473     i = 0;
01474     pimg->val[i++] = imgType;
01475     pimg->val[i++] = imgSize & 0xFF;
01476     pimg->val[i++] = (imgSize >> 8) & 0xFF;
01477 
01478     /* Get the remaining bytes in the image */
01479     for(; i < imgSize; i++)
01480     {
01481         retval = plat_getByte(&b);
01482         PM_RETURN_IF_ERROR(retval);
01483 
01484         pimg->val[i] = b;
01485     }
01486 
01487     /* Return the image as a string object on the stack */
01488     NATIVE_SET_TOS((pPmObj_t)pimg);
01489     return retval;
01490     
01491 }
01492 
01493 /* native function lookup table */
01494 PmReturn_t (* std_nat_fxn_table[])(pPmFrame_t *) =
01495 {
01496     nat_00_list_append,
01497     nat_01_list_index,
01498     nat_02_list_insert,
01499     nat_03_list_pop,
01500     nat_04_list_remove,
01501     nat_05_dict_clear,
01502     nat_06_dict_keys,
01503     nat_07_dict_values,
01504     nat_08___bi_chr,
01505     nat_09___bi_dir,
01506     nat_10___bi_eval,
01507     nat_11___bi_globals,
01508     nat_12___bi_id,
01509     nat_13___bi_len,
01510     nat_14___bi_locals,
01511     nat_15___bi_ord,
01512     nat_16___bi_range,
01513     nat_17___bi_sum,
01514     nat_18___bi_type,
01515     nat_19___bi_Co,
01516     nat_20___bi___init__,
01517     nat_21___bi_send,
01518     nat_22___bi_ismain,
01519     nat_23___bi___init__,
01520     nat_24_sys_exit,
01521     nat_25_sys_getb,
01522     nat_26_sys_heap,
01523     nat_27_sys_putb,
01524     nat_28_sys_runInThread,
01525     nat_29_sys_time,
01526     nat_30_string_atoi,
01527     nat_31_string_count,
01528     nat_32_string_find,
01529     nat_33_ipm__getImg,
01530 };