Source: tools/codeGenUtils.js

  1. /**
  2. * Utilities for handling generated code (e.g. grammars, views etc.)
  3. *
  4. * @name CodeGenUtils
  5. * @class
  6. * @memberOf mmir.tools
  7. * @static
  8. * @hideconstructor
  9. *
  10. * @example
  11. * var codeGenUtils = mmir.require('mmirf/codeGenUtils');
  12. * var prefixCode = codeGenUtils.getCodeWrapPrefix();
  13. * //...
  14. *
  15. */
  16. define(function(){
  17. return {
  18. /**
  19. * Prefix for wrapping persisted objects:
  20. *
  21. * <ul>
  22. * <li> wraps code into a closure
  23. * </li><li> makes global namespace available as variable <code>global</code>
  24. * </li><li> makes mmirf/core available as variable <code>mmir</code> (if mmirf/core is present in global namespace)
  25. * </li><li> makes mmirf/core's require function available as <code>require</code> (if mmirf/core is present and has require function)
  26. * </ul>
  27. *
  28. * @param {Boolean} [disableStrictMode] OPTIONAL disable JavaScript strict mode in the generated closure
  29. * (i.e. allow non-strict JS code in the generated code)
  30. *
  31. * @returns {String} the prefix code for closure-wrapping generated code (i.e. prepend to generated code)
  32. *
  33. * @see #getCodeWrapSuffix
  34. *
  35. * @public
  36. * @function
  37. * @memberOf mmir.tools.CodeGenUtils#
  38. */
  39. getCodeWrapPrefix: function(disableStrictMode){
  40. return ';(function(global){\n' +
  41. (disableStrictMode? '' : '"use strict";\n')+
  42. 'var mmirName = typeof MMIR_CORE_NAME === "string"? MMIR_CORE_NAME : "mmir";\n'+
  43. 'var mmir = global? global[mmirName] : void(0);\n'+
  44. 'var require = mmir && mmir.require? mmir.require : (typeof requirejs !== "undefined"? requirejs : (global? global.require : require));\n';
  45. },
  46. /**
  47. * Suffix for wrapping generated code in a closure:
  48. *
  49. * <ul>
  50. * <li> closes and self-calls closure
  51. * </li>
  52. * <li> sets global namespace to <code>window</code> (browser) or <code>self</code> (browser webworker) or <code>global</code> (node module)
  53. * (via the closure function's argument)
  54. * </li>
  55. * </ul>
  56. *
  57. * @returns {String} the suffix code for closure-wrapping of generated code (i.e. append to generated code)
  58. *
  59. * @see #getCodeWrapPrefix
  60. *
  61. * @public
  62. * @function
  63. * @memberOf mmir.tools.CodeGenUtils#
  64. */
  65. getCodeWrapSuffix: function(){
  66. return '\n})(typeof window !== "undefined" ? window : typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : this);\n';
  67. }
  68. }
  69. });