_Executor.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.calc._Executor"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.calc._Executor"] = true;
  8. dojo.provide("dojox.calc._Executor");
  9. dojo.require("dijit._Templated");
  10. dojo.require("dojox.math._base");
  11. dojo.experimental("dojox.calc._Executor");
  12. (function(){
  13. var calcEnv; // private
  14. // do not override toFrac's pow function if it won the race
  15. if(!("pow" in dojox.calc)){
  16. dojox.calc.pow = function(/*Number*/ base, /*Number*/ exponent){
  17. // summary:
  18. // Computes base ^ exponent
  19. // Wrapper to Math.pow(base, exponent) to handle (-27) ^ (1/3)
  20. function isInt(n){
  21. return Math.floor(n) == n;
  22. }
  23. if(base >= 0 || isInt(exponent)){
  24. return Math.pow(base, exponent);
  25. }else{ // e.g. (1/3) root of -27 = -3
  26. var inv = 1 / exponent;
  27. // e.g. 1 / (1/3) must be an odd integer
  28. return (isInt(inv) && (inv & 1)) ? -Math.pow(-base, exponent) : NaN;
  29. }
  30. };
  31. }
  32. dojo.declare(
  33. "dojox.calc._Executor",
  34. [dijit._Widget, dijit._Templated],
  35. {
  36. // summary:
  37. // A graphing, scientific calculator
  38. //
  39. templateString: '<iframe src="' +
  40. dojo.moduleUrl("dojox.calc","_ExecutorIframe.html") +
  41. '" style="display:none;" onload="if(arguments[0] && arguments[0].Function)'+dijit._scopeName+'.byNode(this)._onLoad(arguments[0])"></iframe>',
  42. _onLoad: function(env){
  43. // summary
  44. // prepare for communications between the user and the calculator by saving the calculator environment, storing the prompt function locally, and making dojox.math available
  45. //
  46. calcEnv = env;
  47. env.outerPrompt = window.prompt; // for IE who can't execute the iframe's prompt method without notifying the user first
  48. // let the user call dojo math functions
  49. env.dojox = {math: {}};
  50. for(var f in dojox.math){ env.dojox.math[f] = dojo.hitch(dojox.math, f); }
  51. if("toFrac" in dojox.calc){
  52. env.toFracCall = dojo.hitch(dojox.calc, 'toFrac');
  53. this.Function('toFrac', 'x', "return toFracCall(x)");
  54. }
  55. env.isJavaScriptLanguage = dojo.number.format(1.5, {pattern:'#.#'}) == "1.5";
  56. env.Ans = 0;
  57. env.pi = Math.PI;
  58. env.eps = Math.E;
  59. env.powCall = dojo.hitch(dojox.calc, 'pow');
  60. // TODO add Degrees support to trig functions
  61. //this.normalizedFunction('toString', 'number, radix', "return number.toString(radix)");
  62. this.normalizedFunction('sqrt', 'x', "return Math.sqrt(x)");
  63. this.normalizedFunction('sin', 'x', "return Math.sin(x)");
  64. this.normalizedFunction('cos', 'x', "return Math.cos(x)");
  65. this.normalizedFunction('tan', 'x', "return Math.tan(x)");
  66. this.normalizedFunction('asin', 'x', "return Math.asin(x)");
  67. this.normalizedFunction('acos', 'x', "return Math.acos(x)");
  68. this.normalizedFunction('atan', 'x', "return Math.atan(x)");
  69. this.normalizedFunction('atan2', 'y, x', "return Math.atan2(y, x)");
  70. this.normalizedFunction('Round', 'x', "return Math.round(x)");
  71. this.normalizedFunction('Int', 'x', "return Math.floor(x)");
  72. this.normalizedFunction('Ceil', 'x', "return Math.ceil(x)");
  73. this.normalizedFunction('ln', 'x', "return Math.log(x)");
  74. this.normalizedFunction('log', 'x', "return Math.log(x)/Math.log(10)");
  75. this.normalizedFunction('pow', 'x, y', "return powCall(x,y)");
  76. this.normalizedFunction('permutations', 'n, r', "return dojox.math.permutations(n, r);");
  77. this.normalizedFunction('P', 'n, r', "return dojox.math.permutations(n, r);");
  78. this.normalizedFunction('combinations', 'n, r', "return dojox.math.combinations(n, r);");
  79. this.normalizedFunction('C', 'n, r', "return dojox.math.combinations(n, r)");
  80. this.normalizedFunction('toRadix', 'number, baseOut', "if(!baseOut){ baseOut = 10; } if(typeof number == 'string'){ number = parseFloat(number); }return number.toString(baseOut);");
  81. this.normalizedFunction('toBin', 'number', "return toRadix(number, 2)");
  82. this.normalizedFunction('toOct', 'number', "return toRadix(number, 8)");
  83. this.normalizedFunction('toHex', 'number', "return toRadix(number, 16)");
  84. this.onLoad();
  85. },
  86. onLoad: function(){
  87. // summary:
  88. // this should be overwritten and become a great place for making user predefined functions
  89. //
  90. },
  91. Function: function(name, args, body){
  92. // summary
  93. // create an anonymous function to run the code the parser generates from the user input.
  94. // params
  95. // name: this argument is simply a String that represents the name of the function being evaluated. It can be undefined, but in that case the function is a one time use.
  96. // args: the function arguments (a String)
  97. // body: the function body, also a String
  98. //
  99. return dojo.hitch(calcEnv, calcEnv.Function.apply(calcEnv, arguments));
  100. },
  101. normalizedFunction: function(name, args, body){
  102. return dojo.hitch(calcEnv, calcEnv.normalizedFunction.apply(calcEnv, arguments));
  103. },
  104. deleteFunction: function(name){
  105. calcEnv[name] = undefined;
  106. delete calcEnv[name];
  107. },
  108. eval: function(text){
  109. // summary
  110. // create an anonymous function to run the code the parser generates from the user input.
  111. // params
  112. // text, type String, is the user input that needs to be parsed
  113. //
  114. return calcEnv.eval.apply(calcEnv, arguments);
  115. },
  116. destroy: function(){
  117. this.inherited(arguments);
  118. calcEnv = null; // assist garbage collection
  119. }
  120. });
  121. })();
  122. (function(){
  123. var magicBigInt = (1 << 30) - 35; // 2^30 - 35 is a prime that ensures approx(n/(2^k)) != n/(2^k) for k >= 1 and n < 2^k
  124. dojo.mixin(dojox.calc, {
  125. approx: function(r){
  126. // summary:
  127. // Return a less exact approximation of r such that approx(r * (1 +- eps)) == approx(r)
  128. if(typeof r == "number"){
  129. return Math.round(r * magicBigInt) / magicBigInt;
  130. }
  131. return r;
  132. }
  133. });
  134. })();
  135. }