Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-6573] iOS: Some modules designed for CommonJS not exporting, even with the "export" variable set

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2011-12-13T14:05:49.000+0000
Affected Version/sRelease 1.8.0.1
Fix Version/sSprint 2011-50, Release 1.8.0.1, Release 2.0.0
ComponentsiOS
Labelsparity
ReporterStephen Tramer
AssigneeBlain Hamon
Created2011-12-12T10:53:40.000+0000
Updated2012-03-04T22:08:00.000+0000

Description

From the Titans mailing list: Using the CommonJS module "underscore" (available at http://documentcloud.github.com/underscore/) does not properly export when using require(). The relevant snippet from the code seems to be:
  if (typeof exports !== 'undefined') {
    if (typeof module !== 'undefined' && module.exports) {
      exports = module.exports = _;
    }
    exports._ = _;
  } else if (typeof define === 'function' && define.amd) {
    // Register as a named module with AMD.
    define('underscore', function() {
      return _;
    });
  } else {
    // Exported as a string, for Closure Compiler "advanced" mode.
    root['_'] = _;
  }
And the developer is trying the following:
var _ = require('underscore')._;
This is leading to the following error:
[ERROR] Script Error = -[KrollCallback countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x8c27fb0.

Comments

  1. Stephen Tramer 2011-12-12

    This issue may be related to TIMOB-6458, TIMOB-6457, and our other require processing mechanisms. While performing work on require() these other issues should be examined (but maybe not resolved yet).
  2. Blain Hamon 2011-12-12

    Note to self: This is because, for some reason, evaluating the module is returning a KrollCallback, not an NSDictionary.
  3. Stephen Tramer 2011-12-12

    Android is compliant with 1.1 CommonJS spec, which might give some clues as to the nature of this bug: http://wiki.commonjs.org/wiki/Modules/1.1
  4. Blain Hamon 2011-12-12

    Adding test code, just to test not only the module is generated, but it's usable as well. Note that this sample test defines _ to be the module/function itself, so we can inspect the read-only URI and id properties.
       
       var _ = require('underscore');
       var stooges = [{name : 'curly', age : 25}, {name : 'moe', age : 21}, {name : 'larry', age : 23}];
       var youngest = _(stooges).chain()
         .sortBy(function(stooge){ return stooge.age; })
         .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
         .first()
         .value();
       
       Ti.API.debug(youngest);
       Ti.API.debug(_.uri);
       
       
    Turns out these are unrelated to linked issues, but the link is still useful.
  5. Natalie Huynh 2011-12-14

    Tested with 1.8.0.1.v20111214093905 on Simulator 5.0 iPhone 4 (5.0.1) iPod 3g (4.0.2) iPad 1 (4.3.5) Returns: : [INFO] moe is 21 : [INFO] app://underscore.js

JSON Source