jswrap_process.c 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
  3. *
  4. * Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * ----------------------------------------------------------------------------
  11. * This file is designed to be parsed during the build process
  12. *
  13. * JavaScript 'process' object - for information about the Espruino board
  14. * ----------------------------------------------------------------------------
  15. */
  16. #include "jsvar.h"
  17. #include "jswrap_process.h"
  18. #include "jsinteractive.h"
  19. /*JSON{ "type":"class",
  20. "class" : "process",
  21. "description" : "This class contains information about Espruino itself"
  22. }*/
  23. /*JSON{ "type":"staticproperty",
  24. "class" : "process", "name" : "version",
  25. "description" : "Returns the version of Espruino as a String",
  26. "generate_full" : "jsvNewFromString(JS_VERSION)",
  27. "return" : ["JsVar", "The version of Espruino"]
  28. }*/
  29. /*JSON{ "type":"staticproperty",
  30. "class" : "process", "name" : "env",
  31. "description" : "Returns an Object containing various pre-defined variables. standard ones are BOARD, VERSION",
  32. "generate" : "jswrap_process_env",
  33. "return" : ["JsVar", "An object"]
  34. }*/
  35. JsVar *jswrap_process_env() {
  36. JsVar *obj = jsvNewWithFlags(JSV_OBJECT);
  37. jsvUnLock(jsvObjectSetChild(obj, "VERSION", jsvNewFromString(JS_VERSION)));
  38. jsvUnLock(jsvObjectSetChild(obj, "BUILD_DATE", jsvNewFromString(__DATE__)));
  39. jsvUnLock(jsvObjectSetChild(obj, "BUILD_TIME", jsvNewFromString(__TIME__)));
  40. jsvUnLock(jsvObjectSetChild(obj, "BOARD", jsvNewFromString(PC_BOARD_ID)));
  41. jsvUnLock(jsvObjectSetChild(obj, "CHIP", jsvNewFromString(PC_BOARD_CHIP)));
  42. jsvUnLock(jsvObjectSetChild(obj, "CHIP_FAMILY", jsvNewFromString(PC_BOARD_CHIP_FAMILY)));
  43. jsvUnLock(jsvObjectSetChild(obj, "FLASH", jsvNewFromInteger(FLASH_TOTAL)));
  44. jsvUnLock(jsvObjectSetChild(obj, "RAM", jsvNewFromInteger(RAM_TOTAL)));
  45. return obj;
  46. }
  47. /*JSON{ "type":"staticmethod",
  48. "class" : "process", "name" : "memory",
  49. "description" : ["Run a Garbage Collection pass, and return an object containing information on memory usage.",
  50. "free : Memory that is available to be used",
  51. "usage : Memory that has been used",
  52. "total : Total memory",
  53. "history : Memory used for command history - that is freed if memory is low. Note that this is INCLUDED in the figure for 'free'.",
  54. "On ARM, stackEndAddress is the address (that can be used with peek/poke/etc) of the END of the stack. The stack grows down, so unless you do a lot of recursion, the bytes above this can be used."],
  55. "generate" : "jswrap_process_memory",
  56. "return" : ["JsVar", "Information about memory usage"]
  57. }*/
  58. #ifdef ARM
  59. extern int _end; // end of ram used (variables)
  60. extern int _etext; // end of flash text (binary) section
  61. #endif
  62. JsVar *jswrap_process_memory() {
  63. jsvGarbageCollect();
  64. JsVar *obj = jsvNewWithFlags(JSV_OBJECT);
  65. if (obj) {
  66. unsigned int history = 0;
  67. JsVar *historyVar = jsvObjectGetChild(jsiGetParser()->root, JSI_HISTORY_NAME, 0);
  68. if (historyVar) {
  69. history = (unsigned int)jsvCountJsVarsUsed(historyVar); // vars used to store history
  70. jsvUnLock(historyVar);
  71. }
  72. unsigned int usage = jsvGetMemoryUsage() - history;
  73. unsigned int total = jsvGetMemoryTotal();
  74. jsvUnLock(jsvObjectSetChild(obj, "free", jsvNewFromInteger(total-usage)));
  75. jsvUnLock(jsvObjectSetChild(obj, "usage", jsvNewFromInteger(usage)));
  76. jsvUnLock(jsvObjectSetChild(obj, "total", jsvNewFromInteger(total)));
  77. jsvUnLock(jsvObjectSetChild(obj, "history", jsvNewFromInteger(history)));
  78. #ifdef ARM
  79. jsvUnLock(jsvObjectSetChild(obj, "stackEndAddress", jsvNewFromInteger((JsVarInt)(unsigned int)&_end)));
  80. jsvUnLock(jsvObjectSetChild(obj, "flash_start", jsvNewFromInteger((JsVarInt)FLASH_START)));
  81. jsvUnLock(jsvObjectSetChild(obj, "flash_binary_end", jsvNewFromInteger((JsVarInt)(unsigned int)&_etext)));
  82. jsvUnLock(jsvObjectSetChild(obj, "flash_code_start", jsvNewFromInteger((JsVarInt)FLASH_SAVED_CODE_START)));
  83. jsvUnLock(jsvObjectSetChild(obj, "flash_length", jsvNewFromInteger((JsVarInt)FLASH_TOTAL)));
  84. #endif
  85. }
  86. return obj;
  87. }