Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught ReferenceError: org is not defined #245

Closed
jgranick opened this issue May 22, 2018 · 7 comments
Closed

Uncaught ReferenceError: org is not defined #245

jgranick opened this issue May 22, 2018 · 7 comments

Comments

@jgranick
Copy link

jgranick commented May 22, 2018

Building a large codebase, multiple classes rely upon the org.apache.royale.utils.Language.Vector module.

It appears that at first, all files are compiled to JavaScript, but then a second (optimization? circular dependency?) pass runs on the generated output, and trims the goog.require statements and moves some of them to different files. Unfortunately, the org.apache.royale.utils.Language dependency is showing up much later than other dependencies, which affects code which relies upon it in statics, const values and other things that might execute immediately.

Perhaps the optimization step should make an exception and not remove the Language module, as I assume that module does not require any further dependencies, so it will not affect circular references?

Thank you 😄

I'm trying to compile Starling using Apache Royale, and I've gotten past the compile errors and now am working on runtime

@aharui
Copy link
Contributor

aharui commented May 23, 2018

The -remove-circulars compiler option is on by default. The Google Closure Compiler does not allow circular dependencies, even simple things like Parent referencing Child and Child referencing Parent. If your code can be written without circular references, you can try -remove-circulars=false.

If you need to allow circular dependencies in your code, see if you can create a two- or three-file test case so we can debug through it.

One workaround may be to write some code that uses Language in the main class. That might pull Language in early enough to prevent your current problem.

@aharui
Copy link
Contributor

aharui commented May 23, 2018

Maybe we don't need a test case. Can you provide one or two .js files so we can see how Language was used in the output?

@jgranick
Copy link
Author

After removing circular dependencies, we are ending up with some stuff like:

/**
 * Generated by Apache Royale Compiler from starling\rendering\VertexDataFormat.as
 * starling.rendering.VertexDataFormat
 *
 * @fileoverview
 *
 * @suppress {missingRequire|checkTypes|accessControls}
 */

goog.provide('starling.rendering.VertexDataFormat');
/* Royale Dependency List: flash.display3D.VertexBuffer3D,flash.errors.ArgumentError,starling.core.Starling,starling.rendering.VertexDataAttribute,starling.utils.StringUtil*/
/* Royale Static Dependency List: flash.utils.Dictionary*/

goog.require('flash.utils.Dictionary');



/** Don't use the constructor, but call <code>VertexDataFormat.fromString</code> instead.
 * @constructor
 */
starling.rendering.VertexDataFormat = function() {
  this._attributes = org.apache.royale.utils.Language.Vector();
};


/**
 * Prevent renaming of class. Needed for reflection.
 */
goog.exportSymbol('starling.rendering.VertexDataFormat', starling.rendering.VertexDataFormat);


/**
 * @private
 * @type {string}
 */
starling.rendering.VertexDataFormat.prototype._format;


/**
 * @private
 * @type {number}
 */
starling.rendering.VertexDataFormat.prototype._vertexSize = 0;


/**
 * @private
 * @type {Array}
 */
starling.rendering.VertexDataFormat.prototype._attributes;


/**
 * @private
 * @type {flash.utils.Dictionary}
 */
starling.rendering.VertexDataFormat.sFormats = new flash.utils.Dictionary();


/** Creates a new VertexDataFormat instance from the given String, or returns one from
 *  the cache (if an equivalent String has already been used before).
 *
 *  @asparam format
 *
 *  Describes the attributes of each vertex, consisting of a comma-separated
 *  list of attribute names and their format, e.g.:
 *
 *  <pre>"position:float2, texCoords:float2, color:bytes4"</pre>
 *
 *  <p>This set of attributes will be allocated for each vertex, and they will be
 *  stored in exactly the given order.</p>
 *
 *  <ul>
 *    <li>Names are used to access the specific attributes of a vertex. They are
 *        completely arbitrary.</li>
 *    <li>The available formats can be found in the <code>Context3DVertexBufferFormat</code>
 *        class in the <code>flash.display3D</code> package.</li>
 *    <li>Both names and format strings are case-sensitive.</li>
 *    <li>Always use <code>bytes4</code> for color data that you want to access with the
 *        respective methods.</li>
 *    <li>Furthermore, the attribute names of colors should include the string "color"
 *        (or the uppercase variant). If that's the case, the "alpha" channel of the color
 *        will automatically be initialized with "1.0" when the VertexData object is
 *        created or resized.</li>
 *  </ul>
 * @export
 * @param {string} format
 * @return {starling.rendering.VertexDataFormat}
 */
starling.rendering.VertexDataFormat.fromString = function(format) {
  if (format in starling.rendering.VertexDataFormat.sFormats)
    return starling.rendering.VertexDataFormat.sFormats[format]; else {
    var /** @type {starling.rendering.VertexDataFormat} */ instance = new starling.rendering.VertexDataFormat();
    instance.parseFormat(format);
    var /** @type {string} */ normalizedFormat = instance._format;
    if (normalizedFormat in starling.rendering.VertexDataFormat.sFormats)
      instance = starling.rendering.VertexDataFormat.sFormats[normalizedFormat];
    starling.rendering.VertexDataFormat.sFormats[format] = instance;
    starling.rendering.VertexDataFormat.sFormats[normalizedFormat] = instance;
    return instance;
  }
};


/** Creates a new VertexDataFormat instance by appending the given format string
 * @export
 * @param {string} format
 * @return {starling.rendering.VertexDataFormat}
 */
starling.rendering.VertexDataFormat.prototype.extend = function(format) {
  return starling.rendering.VertexDataFormat.fromString(this._format + ", " + format);
};


/** Returns the size of a certain vertex attribute in bytes. 
 * @export
 * @param {string} attrName
 * @return {number}
 */
starling.rendering.VertexDataFormat.prototype.getSize = function(attrName) {
  return this.getAttribute(attrName).size;
};


/** Returns the size of a certain vertex attribute in 32 bit units. 
 * @export
 * @param {string} attrName
 * @return {number}
 */
starling.rendering.VertexDataFormat.prototype.getSizeIn32Bits = function(attrName) {
  return this.getAttribute(attrName).size / 4;
};


/** Returns the offset (in bytes) of an attribute within a vertex. 
 * @export
 * @param {string} attrName
 * @return {number}
 */
starling.rendering.VertexDataFormat.prototype.getOffset = function(attrName) {
  return this.getAttribute(attrName).offset;
};


/** Returns the offset (in 32 bit units) of an attribute within a vertex. 
 * @export
 * @param {string} attrName
 * @return {number}
 */
starling.rendering.VertexDataFormat.prototype.getOffsetIn32Bits = function(attrName) {
  return this.getAttribute(attrName).offset / 4;
};


/** Returns the format of a certain vertex attribute, identified by its name.
 * @export
 * @param {string} attrName
 * @return {string}
 */
starling.rendering.VertexDataFormat.prototype.getFormat = function(attrName) {
  return this.getAttribute(attrName).format;
};


/** Returns the name of the attribute at the given position within the vertex format. 
 * @export
 * @param {number} attrIndex
 * @return {string}
 */
starling.rendering.VertexDataFormat.prototype.getName = function(attrIndex) {
  return this._attributes[attrIndex].name;
};


/** Indicates if the format contains an attribute with the given name. 
 * @export
 * @param {string} attrName
 * @return {boolean}
 */
starling.rendering.VertexDataFormat.prototype.hasAttribute = function(attrName) {
  var /** @type {number} */ numAttributes = this._attributes.length;
  for (var /** @type {number} */ i = 0; i < numAttributes; ++i)
    if (this._attributes[i].name == attrName)
      return true;
  return false;
};


/** Specifies which vertex data attribute corresponds to a single vertex shader
 *  program input. This wraps the <code>Context3D</code>-method with the same name,
 *  automatically replacing <code>attrName</code> with the corresponding values for
 * @export
 * @param {number} index
 * @param {flash.display3D.VertexBuffer3D} buffer
 * @param {string} attrName
 */
starling.rendering.VertexDataFormat.prototype.setVertexBufferAt = function(index, buffer, attrName) {
  var /** @type {starling.rendering.VertexDataAttribute} */ attribute = this.getAttribute(attrName);
  starling.core.Starling["context"].setVertexBufferAt(index, buffer, attribute.offset / 4, attribute.format);
};


/**
 * @private
 * @param {string} format
 */
starling.rendering.VertexDataFormat.prototype.parseFormat = function(format) {
  if (format != null && format != "") {
    this._attributes.length = 0;
    this._format = "";
    var /** @type {Array} */ parts = format.split(",");
    var /** @type {number} */ numParts = parts.length;
    var /** @type {number} */ offset = 0;
    for (var /** @type {number} */ i = 0; i < numParts; ++i) {
      var /** @type {string} */ attrDesc = org.apache.royale.utils.Language.string(parts[i]);
      var /** @type {Array} */ attrParts = attrDesc.split(":");
      if (attrParts.length != 2)
        throw new flash.errors.ArgumentError("Missing colon: " + attrDesc);
      var /** @type {string} */ attrName = starling.utils.StringUtil.trim(attrParts[0]);
      var /** @type {string} */ attrFormat = starling.utils.StringUtil.trim(attrParts[1]);
      if (attrName.length == 0 || attrFormat.length == 0)
        throw new flash.errors.ArgumentError(("Invalid format string: " + attrDesc));
      var /** @type {starling.rendering.VertexDataAttribute} */ attribute = new starling.rendering.VertexDataAttribute(attrName, attrFormat, offset);
      offset += attribute.size;
      this._format += (i == 0 ? "" : ", ") + attribute.name + ":" + attribute.format;
      this._attributes[this._attributes.length] = attribute;
    }
    this._vertexSize = offset;
  } else {
    this._format = "";
  }
};


/** Returns the normalized format string. 
 * @export
 * @return {string}
 */
starling.rendering.VertexDataFormat.prototype.toString = function() {
  return this._format;
};


/** @asprivate 
 * @param {string} attrName
 * @return {starling.rendering.VertexDataAttribute}
 */
starling.rendering.VertexDataFormat.prototype.getAttribute = function(attrName) {
  var /** @type {number} */ i = 0, /** @type {starling.rendering.VertexDataAttribute} */ attribute;
  var /** @type {number} */ numAttributes = this._attributes.length;
  for (i = 0; i < numAttributes; ++i) {
    attribute = this._attributes[i];
    if (attribute.name == attrName)
      return attribute;
  }
  return null;
};


starling.rendering.VertexDataFormat.prototype.get__attributes = function() {
  return this._attributes;
};


starling.rendering.VertexDataFormat.prototype.get__formatString = function() {
  return this._format;
};


starling.rendering.VertexDataFormat.prototype.get__vertexSize = function() {
  return this._vertexSize;
};


starling.rendering.VertexDataFormat.prototype.get__vertexSizeIn32Bits = function() {
  return this._vertexSize / 4;
};


starling.rendering.VertexDataFormat.prototype.get__numAttributes = function() {
  return this._attributes.length;
};


Object.defineProperties(starling.rendering.VertexDataFormat.prototype, /** @lends {starling.rendering.VertexDataFormat.prototype} */ {
/**
  * @export
  * @type {Vector.<VertexDataAttribute>} */
attributes: {
get: starling.rendering.VertexDataFormat.prototype.get__attributes},
/**
  * @export
  * @type {string} */
formatString: {
get: starling.rendering.VertexDataFormat.prototype.get__formatString},
/**
  * @export
  * @type {number} */
vertexSize: {
get: starling.rendering.VertexDataFormat.prototype.get__vertexSize},
/**
  * @export
  * @type {number} */
vertexSizeIn32Bits: {
get: starling.rendering.VertexDataFormat.prototype.get__vertexSizeIn32Bits},
/**
  * @export
  * @type {number} */
numAttributes: {
get: starling.rendering.VertexDataFormat.prototype.get__numAttributes}}
);


/**
 * Metadata
 *
 * @type {Object.<string, Array.<Object>>}
 */
starling.rendering.VertexDataFormat.prototype.ROYALE_CLASS_INFO = { names: [{ name: 'VertexDataFormat', qName: 'starling.rendering.VertexDataFormat', kind: 'class' }] };



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
starling.rendering.VertexDataFormat.prototype.ROYALE_REFLECTION_INFO = function () {
  return {
    variables: function () {return {};},
    accessors: function () {
      return {
        'formatString': { type: 'String', access: 'readonly', declaredBy: 'starling.rendering.VertexDataFormat'},
        'vertexSize': { type: 'int', access: 'readonly', declaredBy: 'starling.rendering.VertexDataFormat'},
        'vertexSizeIn32Bits': { type: 'int', access: 'readonly', declaredBy: 'starling.rendering.VertexDataFormat'},
        'numAttributes': { type: 'int', access: 'readonly', declaredBy: 'starling.rendering.VertexDataFormat'}
      };
    },
    methods: function () {
      return {
        'VertexDataFormat': { type: '', declaredBy: 'starling.rendering.VertexDataFormat'},
        '|fromString': { type: 'starling.rendering.VertexDataFormat', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'String', optional: false } ]; }},
        'extend': { type: 'starling.rendering.VertexDataFormat', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'String', optional: false } ]; }},
        'getSize': { type: 'int', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'String', optional: false } ]; }},
        'getSizeIn32Bits': { type: 'int', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'String', optional: false } ]; }},
        'getOffset': { type: 'int', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'String', optional: false } ]; }},
        'getOffsetIn32Bits': { type: 'int', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'String', optional: false } ]; }},
        'getFormat': { type: 'String', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'String', optional: false } ]; }},
        'getName': { type: 'String', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'int', optional: false } ]; }},
        'hasAttribute': { type: 'Boolean', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'String', optional: false } ]; }},
        'setVertexBufferAt': { type: 'void', declaredBy: 'starling.rendering.VertexDataFormat', parameters: function () { return [  { index: 1, type: 'int', optional: false },{ index: 2, type: 'flash.display3D.VertexBuffer3D', optional: false },{ index: 3, type: 'String', optional: false } ]; }},
        'toString': { type: 'String', declaredBy: 'starling.rendering.VertexDataFormat'}
      };
    }
  };
};

A number of classes define static const instances of VertexDataFormat, so they call new VertexDataFormat (or more specifically, VertexDataFormat.fromString, which calls new) but we don't have access to the Language module for Vector.<T> support yet

@aharui
Copy link
Contributor

aharui commented May 23, 2018

Is VertexDataFormat in a SWC? If so, please manually remove the bin/js-debug folder and try again. The compiler does not copy over existing JS files from SWCs in order to try to save time (under the assumption that the JS files in the SWC have not changed and when remove-circulars is on, it also stores some dependency information in the JS files.

@jgranick
Copy link
Author

It is not coming from an SWC, currently.

@aharui
Copy link
Contributor

aharui commented May 24, 2018

Hah, well, turns out that we weren't setting the flag to goog.require Language when outputting Vector. I just pushed a change for that.

@jgranick
Copy link
Author

Awesome, thank you 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants