jswrap_functions.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 methods and functions in the global namespace
  14. * ----------------------------------------------------------------------------
  15. */
  16. #include "jswrap_functions.h"
  17. #include "jslex.h"
  18. #include "jsparse.h"
  19. #include "jsinteractive.h"
  20. /*JSON{ "type":"variable", "name" : "arguments",
  21. "description" : "A variable containing the arguments given to the function",
  22. "generate" : "jswrap_arguments",
  23. "return" : ["JsVar", "An array containing all the arguments given to the function"]
  24. }*/
  25. extern JsExecInfo execInfo;
  26. JsVar *jswrap_arguments() {
  27. JsVar *scope = 0;
  28. if (execInfo.scopeCount>0)
  29. scope = jsvLock(execInfo.scopes[execInfo.scopeCount-1]);
  30. if (!jsvIsFunction(scope)) {
  31. jsvUnLock(scope);
  32. jsError("Can only use 'arguments' variable inside a function");
  33. return 0;
  34. }
  35. JsVar *args = jsvNewWithFlags(JSV_ARRAY);
  36. if (!args) return 0; // out of memory
  37. JsObjectIterator it;
  38. jsvObjectIteratorNew(&it, scope);
  39. while (jsvObjectIteratorHasElement(&it)) {
  40. JsVar *idx = jsvObjectIteratorGetKey(&it);
  41. if (jsvIsFunctionParameter(idx)) {
  42. JsVar *val = jsvSkipOneName(idx);
  43. jsvArrayPushAndUnLock(args, val);
  44. }
  45. jsvUnLock(idx);
  46. jsvObjectIteratorNext(&it);
  47. }
  48. jsvObjectIteratorFree(&it);
  49. jsvUnLock(scope);
  50. return args;
  51. }
  52. /*JSON{ "type":"function", "name" : "eval",
  53. "description" : "Evaluate a string containing JavaScript code",
  54. "generate" : "jswrap_eval",
  55. "params" : [ [ "code", "JsVar", ""] ],
  56. "return" : ["JsVar", "The result of evaluating the string"]
  57. }*/
  58. JsVar *jswrap_eval(JsVar *v) {
  59. if (!v) return 0;
  60. JsVar *s = jsvAsString(v, false); // get as a string
  61. JsVar *result = jspEvaluateVar(jsiGetParser(), s, 0);
  62. jsvUnLock(s);
  63. return result;
  64. }
  65. /*JSON{ "type":"function", "name" : "parseInt",
  66. "description" : "Convert a string representing a number into an integer",
  67. "generate" : "jswrap_parseInt",
  68. "params" : [ [ "string", "JsVar", ""],
  69. [ "radix", "JsVar", "The Radix of the string (optional)"] ],
  70. "return" : ["JsVar", "The integer value of the string (or NaN)"]
  71. }*/
  72. JsVar *jswrap_parseInt(JsVar *v, JsVar *radixVar) {
  73. int radix = 0/*don't force radix*/;
  74. if (jsvIsNumeric(radixVar))
  75. radix = (int)jsvGetInteger(radixVar);
  76. char buffer[JS_NUMBER_BUFFER_SIZE];
  77. jsvGetString(v, buffer, JS_NUMBER_BUFFER_SIZE);
  78. bool hasError;
  79. JsVarInt i = stringToIntWithRadix(buffer, radix, &hasError);
  80. if (hasError) return jsvNewFromFloat(NAN);
  81. return jsvNewFromInteger(i);
  82. }
  83. /*JSON{ "type":"function", "name" : "parseFloat",
  84. "description" : "Convert a string representing a number into an float",
  85. "generate" : "jswrap_parseFloat",
  86. "params" : [ [ "string", "JsVar", ""] ],
  87. "return" : ["float", "The value of the string"]
  88. }*/
  89. JsVarFloat jswrap_parseFloat(JsVar *v) {
  90. char buffer[JS_NUMBER_BUFFER_SIZE];
  91. jsvGetString(v, buffer, JS_NUMBER_BUFFER_SIZE);
  92. return stringToFloat(buffer);
  93. }