Skip to main content

understand

Function understand 

Source
pub fn understand(language: Language, code: &str) -> Understanding
Expand description

Whether code is read whole: nothing in it is outside the allow-list.

The code is held to a strict subset, for Python and JavaScript alike:

  • it parses, and holds at least one statement;
  • its statements are expression statements, assignments to a plain name and if/else, so the code runs straight through;
  • its calls are on a short list that only computes (print, len, str, time.time, console.log, Math.max, the methods of a string or a list), or open in Python, which the reader judges by its path and mode;
  • its imports are the modules built into the interpreter binary (sys, errno, itertools, time) in Python 3, and sys and errno alone in Python 2 (see understand_python2), which no file in the folder the code runs in can stand in for, and it has no import, require or eval in JavaScript;
  • it names nothing that is not a name it set, a call on the list or a module on the list that it imported, and sets none of those;
  • it reaches into nothing: no attribute that starts with an underscore, and in JavaScript no member other than length and the methods on the list.

The list is an allow-list of what the grammar may hold, never a list of what is known to be dangerous: code with anything else in it is Understanding::NotUnderstood because the reader has no row for what it does, whatever read found in it.