c_stdlib.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //#include "user_interface.h"
  2. #include "user_config.h"
  3. #ifdef LUA_CROSS_COMPILER
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7. #define ICACHE_RODATA_ATTR
  8. #define TRUE 1
  9. #define FALSE 0
  10. #else
  11. #include "c_stdlib.h"
  12. #include "c_types.h"
  13. #include "c_string.h"
  14. // const char *lua_init_value = "print(\"Hello world\")";
  15. const char *lua_init_value = "@init.lua";
  16. // int c_abs(int x){
  17. // return x>0?x:0-x;
  18. // }
  19. // void c_exit(int e){
  20. // }
  21. const char *c_getenv(const char *__string)
  22. {
  23. if (c_strcmp(__string, "LUA_INIT") == 0)
  24. {
  25. return lua_init_value;
  26. }
  27. return NULL;
  28. }
  29. // make sure there is enough memory before real malloc, otherwise malloc will panic and reset
  30. // void *c_malloc(size_t __size){
  31. // if(__size>system_get_free_heap_size()){
  32. // NODE_ERR("malloc: not enough memory\n");
  33. // return NULL;
  34. // }
  35. // return (void *)os_malloc(__size);
  36. // }
  37. // void *c_zalloc(size_t __size){
  38. // if(__size>system_get_free_heap_size()){
  39. // NODE_ERR("zalloc: not enough memory\n");
  40. // return NULL;
  41. // }
  42. // return (void *)os_zalloc(__size);
  43. // }
  44. // void c_free(void *p){
  45. // // NODE_ERR("free1: %d\n", system_get_free_heap_size());
  46. // os_free(p);
  47. // // NODE_ERR("-free1: %d\n", system_get_free_heap_size());
  48. // }c_stdlib.s
  49. // int c_rand(void){
  50. // }
  51. // void c_srand(unsigned int __seed){
  52. // }
  53. // int c_atoi(const char *__nptr){
  54. // }
  55. #include <_ansi.h>
  56. //#include <reent.h>
  57. //#include "mprec.h"
  58. #endif
  59. double powersOf10[] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR = /* Table giving binary powers of 10. Entry */
  60. {
  61. 10., /* is 10^2^i. Used to convert decimal */
  62. 100., /* exponents into floating-point numbers. */
  63. 1.0e4,
  64. 1.0e8,
  65. 1.0e16,
  66. 1.0e32,
  67. 1.0e64,
  68. 1.0e128,
  69. 1.0e256
  70. };
  71. double c_strtod(const char *string, char **endPtr)
  72. {
  73. int maxExponent = 511; /* Largest possible base 10 exponent. Any
  74. * exponent larger than this will already
  75. * produce underflow or overflow, so there's
  76. * no need to worry about additional digits.
  77. */
  78. int sign, expSign = FALSE;
  79. double fraction, dblExp, *d;
  80. register const char *p;
  81. register int c;
  82. int exp = 0; /* Exponent read from "EX" field. */
  83. int fracExp = 0; /* Exponent that derives from the fractional
  84. * part. Under normal circumstatnces, it is
  85. * the negative of the number of digits in F.
  86. * However, if I is very long, the last digits
  87. * of I get dropped (otherwise a long I with a
  88. * large negative exponent could cause an
  89. * unnecessary overflow on I alone). In this
  90. * case, fracExp is incremented one for each
  91. * dropped digit. */
  92. int mantSize; /* Number of digits in mantissa. */
  93. int decPt; /* Number of mantissa digits BEFORE decimal
  94. * point. */
  95. const char *pExp; /* Temporarily holds location of exponent
  96. * in string. */
  97. /*
  98. * Strip off leading blanks and check for a sign.
  99. */
  100. p = string;
  101. while (isspace((unsigned char)(*p)))
  102. {
  103. p += 1;
  104. }
  105. if (*p == '-')
  106. {
  107. sign = TRUE;
  108. p += 1;
  109. }
  110. else
  111. {
  112. if (*p == '+')
  113. {
  114. p += 1;
  115. }
  116. sign = FALSE;
  117. }
  118. /*
  119. * Count the number of digits in the mantissa (including the decimal
  120. * point), and also locate the decimal point.
  121. */
  122. decPt = -1;
  123. for (mantSize = 0; ; mantSize += 1)
  124. {
  125. c = *p;
  126. if (!isdigit(c))
  127. {
  128. if ((c != '.') || (decPt >= 0))
  129. {
  130. break;
  131. }
  132. decPt = mantSize;
  133. }
  134. p += 1;
  135. }
  136. /*
  137. * Now suck up the digits in the mantissa. Use two integers to
  138. * collect 9 digits each (this is faster than using floating-point).
  139. * If the mantissa has more than 18 digits, ignore the extras, since
  140. * they can't affect the value anyway.
  141. */
  142. pExp = p;
  143. p -= mantSize;
  144. if (decPt < 0)
  145. {
  146. decPt = mantSize;
  147. }
  148. else
  149. {
  150. mantSize -= 1; /* One of the digits was the point. */
  151. }
  152. if (mantSize > 18)
  153. {
  154. fracExp = decPt - 18;
  155. mantSize = 18;
  156. }
  157. else
  158. {
  159. fracExp = decPt - mantSize;
  160. }
  161. if (mantSize == 0)
  162. {
  163. fraction = 0.0;
  164. p = string;
  165. goto done;
  166. }
  167. else
  168. {
  169. int frac1, frac2;
  170. frac1 = 0;
  171. for ( ; mantSize > 9; mantSize -= 1)
  172. {
  173. c = *p;
  174. p += 1;
  175. if (c == '.')
  176. {
  177. c = *p;
  178. p += 1;
  179. }
  180. frac1 = 10 * frac1 + (c - '0');
  181. }
  182. frac2 = 0;
  183. for (; mantSize > 0; mantSize -= 1)
  184. {
  185. c = *p;
  186. p += 1;
  187. if (c == '.')
  188. {
  189. c = *p;
  190. p += 1;
  191. }
  192. frac2 = 10 * frac2 + (c - '0');
  193. }
  194. fraction = (1.0e9 * frac1) + frac2;
  195. }
  196. /*
  197. * Skim off the exponent.
  198. */
  199. p = pExp;
  200. if ((*p == 'E') || (*p == 'e'))
  201. {
  202. p += 1;
  203. if (*p == '-')
  204. {
  205. expSign = TRUE;
  206. p += 1;
  207. }
  208. else
  209. {
  210. if (*p == '+')
  211. {
  212. p += 1;
  213. }
  214. expSign = FALSE;
  215. }
  216. if (!isdigit((unsigned char)(*p)))
  217. {
  218. p = pExp;
  219. goto done;
  220. }
  221. while (isdigit((unsigned char)(*p)))
  222. {
  223. exp = exp * 10 + (*p - '0');
  224. p += 1;
  225. }
  226. }
  227. if (expSign)
  228. {
  229. exp = fracExp - exp;
  230. }
  231. else
  232. {
  233. exp = fracExp + exp;
  234. }
  235. /*
  236. * Generate a floating-point number that represents the exponent.
  237. * Do this by processing the exponent one bit at a time to combine
  238. * many powers of 2 of 10. Then combine the exponent with the
  239. * fraction.
  240. */
  241. if (exp < 0)
  242. {
  243. expSign = TRUE;
  244. exp = -exp;
  245. }
  246. else
  247. {
  248. expSign = FALSE;
  249. }
  250. if (exp > maxExponent)
  251. {
  252. exp = maxExponent;
  253. // errno = ERANGE;
  254. }
  255. dblExp = 1.0;
  256. for (d = powersOf10; exp != 0; exp >>= 1, d += 1)
  257. {
  258. if (exp & 01)
  259. {
  260. dblExp *= *d;
  261. }
  262. }
  263. if (expSign)
  264. {
  265. fraction /= dblExp;
  266. }
  267. else
  268. {
  269. fraction *= dblExp;
  270. }
  271. done:
  272. if (endPtr != NULL)
  273. {
  274. *endPtr = (char *) p;
  275. }
  276. if (sign)
  277. {
  278. return -fraction;
  279. }
  280. return fraction;
  281. }
  282. // long c_strtol(const char *__n, char **__end_PTR, int __base){
  283. // }
  284. // unsigned long c_strtoul(const char *__n, char **__end_PTR, int __base){
  285. // }
  286. // long long c_strtoll(const char *__n, char **__end_PTR, int __base){
  287. // }