Friday, October 14, 2011

MAX-Javascript Includes I

Some method for using code from external files is needed when working with the MAX/MSP Javascript implementation.  At present there is a 'static' file inclusion method, in which any .js file within the jsextensions folder is interpreted when MAX initialises, but the limitation of that is that the included files are only interpreted when MAX initialises.
A more dynamic runtime-based system is needed, one which permits a more modular style of code development for Javascript MAX-objects, and which provides a mechanism to support reusable code libraries.   Browser-based Javascript permits the use of libraries (like Google Closure) via  <script src=""> tags that leverage the dynamic nature of an HTML document, but this has no real equivalent inside a MAX/MSP patch. 
The obvious model for file inclusion is the classic C #include file directive, but as there is no pre-processing system within MAX-Javascript, we need to construct an alternative.  Fortunately, Javascript is capable of dynamic code evaluation, via eval(); unfortunately eval() is incredibly slow and much frowned-upon.
A useful file-inclusion system would be designed to achieve the following:
  • simple syntax
  • file inclusion relative to the 'main' code or to a given library location (as per C preprocessor)
  • minimised eval() overhead
    • optimise to single eval
    • remove re-eval of unchanged code
  • dynamic control over inclusion mechanism
  • possible compatibility with other methods (ie could be useable to override goog.global.CLOSURE_IMPORT_SCRIPT in Google Closure)

No comments:

Post a Comment