Skip to content

Regression in modularize mode (currentSrc issue) #6903

Closed

Description

@bvibber

Compiling ogv.js with emscripten sdk 1.38.10, I'm seeing a new failure mode with my demuxer module, which is loaded asynchronously into the web page context:

[Error] TypeError: null is not an object (evaluating 'ca.src')

This is in the (minified) setup code:

        if (n) {
            var da = this._currentScript || document.currentScript;
            0 !== da.src.indexOf("blob:") && (t = da.src.split("/").slice(0, -1).join("/") +
            "/")
        } else

source:

   if (ENVIRONMENT_IS_WEB) {
#if MODULARIZE
    // When MODULARIZE, this JS may be executed later, after document.currentScript is gone, so we send it
    // using this._currentScript.
#else
    var currentScript = document.currentScript;
#endif
     if (currentScript.src.indexOf('blob:') !== 0) {
       scriptDirectory = currentScript.src.split('/').slice(0, -1).join('/') + '/';
     }

Something about this isn't doing what I expect...

I notice at the end of the .js file is this which appears to be an attempt to capture the currentSource:

OGVDemuxerWebMW = OGVDemuxerWebMW.bind({
    _currentScript: typeof document !== 'undefined' ? document.currentScript : undefined
});

But that syntax for the Function.prototype.bind function doesn't appear to be documented on MDN, and it doesn't seem to do anything that I can tell on Safari 11.1.2.

Activity

  1. bvibber commented on Jul 25, 2018

    @bvibber
    CollaboratorAuthor

    That bit appears to have come from d1f303a

  2. kripken commented on Jul 25, 2018

    @kripken
    Member

    Odd, isn't that bind usage basically the simple case shown here:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

    (of binding an object which is then this in the call)? Does that MDN example not work in Safari?

  3. kripken commented on Jul 25, 2018

    @kripken
    Member

    If that's not it, is it possible that the line with OGVDemuxerWebMW = OGVDemuxerWebMW.bind({ .. }) is not being called, somehow? If so, and it's calling the unbound version, then it would see a null there, which would seem to fit the symptoms (but I have no idea why that line wouldn't be reached, very odd).

  4. nazar-pc commented on Jul 25, 2018

    @nazar-pc
    Contributor

    I suspect it is called, but asynchronously as part of a bigger bundle. You may want to adjust your build process or override Module.locateFile. On Emscripten side we should probably add a check whether currentScript is an object at all to avoid this error.

  5. kripken commented on Jul 25, 2018

    @kripken
    Member

    Hmm, we do the bind right after defining the function, so it seems like that has to happen.

    And we do check if this['_currentScript'] exists, so I think that should handle the case of not having document or document.currentScript defined during binding.

    Maybe another binding happens later, overriding this one?

  6. kripken commented on Jul 25, 2018

    @kripken
    Member

    Btw, this code slightly changed since the original landing - @Brion is this still an issue on latest incoming?

  7. bvibber commented on Jul 25, 2018

    @bvibber
    CollaboratorAuthor

    (document and document.currentScript are defined at the time that bind() is called, I checked in the debugger)

  8. bvibber commented on Jul 25, 2018

    @bvibber
    CollaboratorAuthor

    Ah, I understand what the bind() call is doing now -- it's putting the _currentScript property on the hidden this, not on the Module object... investigating further on my end.

  9. bvibber commented on Jul 25, 2018

    @bvibber
    CollaboratorAuthor

    AHA!

    My call to the OGVDemuerWebMW module function was made like this:

    return new global[className](options);
    

    which used to work fine. That "new" replaces the "this" parameter from the bind() call with a copy of the prototype, which has no _currentScript property.

    Removing the "new" from the calls should get it working... recompiling now. :)

  10. bvibber commented on Jul 25, 2018

    @bvibber
    CollaboratorAuthor

    Aaaaand it works now. :) As far as I know following documentation correctly in the initial call would have saved me here. :D

  11. kripken commented on Jul 25, 2018

    @kripken
    Member

    Great, thanks for the update @Brion!

    I added a commit to #6894 to avoid crashing in either the case of document.scriptDirectory not existing (as @nazar-pc mentioned), or doing new Module() which overrides the object containing the helper property. In both cases we can't access the scriptDirectory but we should at least not crash.

  12. tgds commented on Nov 9, 2018

    @tgds

    @kripken this introduces a new problem. document.currentScript.src is undefined if you import the script in SVG using <script href="a.out.js"> and the script crashes with Cannot read property 'indexOf' of undefined

  13. kripken commented on Nov 12, 2018

    @kripken
    Member

    Thanks @tgds. I hadn't realized that could be an issue. I filed #7494

    What's the use case for loading in an SVG, by the way?

  14. tgds commented on Nov 13, 2018

    @tgds

    Honestly, I only came across it in this Google codelab and that's how I found the issue. I suppose the use case is interactive SVGs sharable without HTML glue?

  15. kripken commented on Nov 13, 2018

    @kripken
    Member

    Interesting, thanks @tgds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions