<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>221879</bug_id>
          
          <creation_ts>2021-02-14 14:37:54 -0800</creation_ts>
          <short_desc>Class not recognized in AudioWorklet when module is imported in two different ways</short_desc>
          <delta_ts>2024-03-05 12:08:16 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Web Audio</component>
          <version>Safari Technology Preview</version>
          <rep_platform>Mac (Intel)</rep_platform>
          <op_sys>macOS 11</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>163209</dup_id>
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Minor</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jeff Kaufman">jeff.t.kaufman</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ashvayka</cc>
    
    <cc>cdumez</cc>
    
    <cc>ggaren</cc>
    
    <cc>jeff.t.kaufman</cc>
    
    <cc>mark.lam</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1729016</commentid>
    <comment_count>0</comment_count>
    <who name="Jeff Kaufman">jeff.t.kaufman</who>
    <bug_when>2021-02-14 14:37:54 -0800</bug_when>
    <thetext>I have an AudioWorklet with:

    class AudioChunk { ... }

    function thaw_audio_chunk(o) {
      return new AudioChunk(...)
    }

This is fine in Firefox and Chrome, but in Safari it throws:

    ReferenceError: Can&apos;t find variable: AudioChunk
    thaw_audio_chunk@https://www.jefftk.com/test/safari-audio-worklet-error2/audio-worklet.js:126:24
    handle_message@https://www.jefftk.com/test/safari-audio-worklet-error2/audio-worklet.js:669:33
    @https://www.jefftk.com/test/safari-audio-worklet-error2/audio-worklet.js:521:30
    try_do@https://www.jefftk.com/test/safari-audio-worklet-error2/audio-worklet.js:544:15
    @https://www.jefftk.com/test/safari-audio-worklet-error2/audio-worklet.js:520:20

Here is a standalone version that reproduces the bug reliably on my machine: https://www.jefftk.com/test/safari-audio-worklet-error2/  After pressing &quot;start&quot;, giving mic permission, and waiting a few seconds you should see &quot;This app has crashed. We&apos;re really sorry :-(&quot; and that stack trace.

Part of why it happens is that I am loading the worklet script two different ways:

   audio-worklet.js:
     if (typeof AudioWorkletProcessor === &quot;undefined&quot;) {
       // do nothing
     } else {
       class AudioChunk { ... }
       ...
     }
 
   app.js
     import &apos;./audio-worklet.js&apos;;
     ...
     await this.audioCtx.audioWorklet.addModule(&apos;audio-worklet.js&apos;);
     ...

This is a hack that I started doing because it got me better error messages in Chrome.  Removing it is sufficient to stop triggering the bug in Safari: https://www.jefftk.com/test/safari-audio-worklet-error3/

Something else is also necessary to trigger it, however, because when I tried to make a very simple version that replicated this situation did not trigger it: https://www.jefftk.com/test/worklet-invoker  This prints &quot;success&quot; in Firefox, Chrome, and Safari, and does not throw.

This was easy to work around once I figured it out, since I can stop doing the silly load-it-both-ways thing, but I wanted to report it in case having the repro is useful.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1731997</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2021-02-21 14:38:12 -0800</bug_when>
    <thetext>&lt;rdar://problem/74575943&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2018842</commentid>
    <comment_count>2</comment_count>
    <who name="Alexey Shvayka">ashvayka</who>
    <bug_when>2024-03-05 12:02:16 -0800</bug_when>
    <thetext>Please note that both

```
    function thaw_audio_chunk(o) {
      return new AudioChunk(...)
    }
```

and the `class AudioChunk` it references are defined inside `else {` block, which subjects the function to have incorrect scope of the global environment. The root cause of it was broken Annex B function hoisting implementation, which was fixed in https://commits.webkit.org/268553@main

*** This bug has been marked as a duplicate of bug 163209 ***</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2018844</commentid>
    <comment_count>3</comment_count>
    <who name="Alexey Shvayka">ashvayka</who>
    <bug_when>2024-03-05 12:08:16 -0800</bug_when>
    <thetext>(In reply to Jeff Kaufman from comment #0)
&gt; This was easy to work around once I figured it out, since I can stop doing
&gt; the silly load-it-both-ways thing, but I wanted to report it in case having
&gt; the repro is useful.

Thank you Jeff for filing this bug along with easy-to-follow repro, that&apos;s highly appreciated!!</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>