variable.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var node_1 = tslib_1.__importDefault(require("./node"));
  5. var call_1 = tslib_1.__importDefault(require("./call"));
  6. var Variable = function (name, index, currentFileInfo) {
  7. this.name = name;
  8. this._index = index;
  9. this._fileInfo = currentFileInfo;
  10. };
  11. Variable.prototype = Object.assign(new node_1.default(), {
  12. type: 'Variable',
  13. eval: function (context) {
  14. var variable, name = this.name;
  15. if (name.indexOf('@@') === 0) {
  16. name = "@" + new Variable(name.slice(1), this.getIndex(), this.fileInfo()).eval(context).value;
  17. }
  18. if (this.evaluating) {
  19. throw { type: 'Name',
  20. message: "Recursive variable definition for " + name,
  21. filename: this.fileInfo().filename,
  22. index: this.getIndex() };
  23. }
  24. this.evaluating = true;
  25. variable = this.find(context.frames, function (frame) {
  26. var v = frame.variable(name);
  27. if (v) {
  28. if (v.important) {
  29. var importantScope = context.importantScope[context.importantScope.length - 1];
  30. importantScope.important = v.important;
  31. }
  32. // If in calc, wrap vars in a function call to cascade evaluate args first
  33. if (context.inCalc) {
  34. return (new call_1.default('_SELF', [v.value])).eval(context);
  35. }
  36. else {
  37. return v.value.eval(context);
  38. }
  39. }
  40. });
  41. if (variable) {
  42. this.evaluating = false;
  43. return variable;
  44. }
  45. else {
  46. throw { type: 'Name',
  47. message: "variable " + name + " is undefined",
  48. filename: this.fileInfo().filename,
  49. index: this.getIndex() };
  50. }
  51. },
  52. find: function (obj, fun) {
  53. for (var i = 0, r = void 0; i < obj.length; i++) {
  54. r = fun.call(obj, obj[i]);
  55. if (r) {
  56. return r;
  57. }
  58. }
  59. return null;
  60. }
  61. });
  62. exports.default = Variable;
  63. //# sourceMappingURL=variable.js.map