Code

Discussions about extensions and frameworks

Code

Postby oliver » Sun Apr 10, 2016 4:52 pm

Code objects are used by the inject function and bundle up JavaScript code.

Here's the code for the type:
Code: Select all
var Code = {
   type: "code",
   isLoaded: true,

   toString: function(obj) { return obj.stringValue; },
   toHTML: function(obj) {
      var stringForType = obj.stringValue.slice(0, display.isLarge() ? 60 : 15) + " (" + obj.stringValue.length + "b)";      
      return (stringForType + calculator.HTMLforTypeBadge(obj.type, display.isLarge() ? 43 : 37));
   },
   isStringRepresentation: function(x) { return x.match(/^\/\*/); },
   fromString: function(str) {
      function codeObj() {
         this.type = Code.type;
         this.stringValue = str;
         this.toString = function() { return Code.toString(this); };
      }
      return new codeObj();
   },
   
   "==": function(a, b) { if (!(calculator.typeOf(a) == "code" && calculator.typeOf(b) == "code")) return false; return a.stringValue == b.stringValue; },
   "inject": function(name, obj) { if (calculator.typeOf(name) != "string") throw Error("bad arg"); /* use */ Code.eval(obj); /* inject */ calculator.exec("inject", obj.stringValue, calculator.unquote(name)); },

///   eval: function(obj) { return eval(obj.stringValue); }, // if we wouldn't care to provide a nicer error msg in case of syntax errors
   eval: function(obj) {
      try {
         eval(obj.stringValue);
      }
      catch(e) {
         if (e instanceof SyntaxError)
            calculator.exec("alert", "SyntaxError", "in line", e.line);
         else if (e instanceof EvalError)
            calculator.exec("alert", "EvalError", "in line", e.line);
         else
            throw e;
         return obj; // have object go back on stack
      }
   },
   
   onload: function() {
      // add a global constant
      calculator.vars["noCode"] = Code.fromString("");
   }
};
calculator.registerType(Code);                                                    


Note, isStringRepresentation looks to see if the handed string starts with /*, that is the beginnings of a C-style comment. If we see this, we assume the string is going to be JavaScript code and claim the string is ours (by returning a non-falsy response, which will be interpreted as true.

The inject function is defined on this object. It's magical and private in that it calls some native function that does the actual injection.

Note the eval function: it runs the JavaScript eval() function on the string that represents the code. That produces a function object from the string. That is, if the string is syntactically correct. This evaluation is in a try block to catch and report the two kinds of errors that can occur, if the string is not a valid function. Basically, this is how JavaScript code gets "compiled" at run-time.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to Extensions

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron