@@ -674,9 +674,7 @@ function emitDCEGraph(ast) {
674
674
//
675
675
// or, in the minimal runtime, it looks like
676
676
//
677
- // WebAssembly.instantiate(Module["wasm"], imports).then((output) => {
678
- // var wasmExports = output.instance.exports; // may also not have "var", if
679
- // // declared outside and used elsewhere
677
+ // function assignWasmExports(wasmExports)
680
678
// ..
681
679
// _malloc = wasmExports["malloc"];
682
680
// ..
@@ -801,79 +799,45 @@ function emitDCEGraph(ast) {
801
799
emptyOut ( node ) ;
802
800
}
803
801
} else if ( node . type === 'FunctionDeclaration' ) {
804
- if ( ! specialScopes ) {
805
- defuns . push ( node ) ;
806
- const name = node . id . name ;
807
- nameToGraphName [ name ] = getGraphName ( name , 'defun' ) ;
808
- emptyOut ( node ) ; // ignore this in the second pass; we scan defuns separately
809
- }
810
- } else if ( node . type === 'ArrowFunctionExpression' ) {
811
- assert ( specialScopes > 0 ) ;
812
- specialScopes -- ;
802
+ const name = node . id . name ;
813
803
// Check if this is the minimal runtime exports function, which looks like
814
- // (output) => { var wasmExports = output.instance.exports;
804
+ // function assignWasmExports( wasmExports)
815
805
if (
806
+ name == 'assignWasmExports' &&
816
807
node . params . length === 1 &&
817
808
node . params [ 0 ] . type === 'Identifier' &&
818
- node . params [ 0 ] . name === 'output' &&
819
- node . body . type === 'BlockStatement'
809
+ node . params [ 0 ] . name === 'wasmExports'
820
810
) {
811
+ // This looks very much like what we are looking for.
821
812
const body = node . body . body ;
822
- if ( body . length >= 1 ) {
823
- const first = body [ 0 ] ;
824
- let target ;
825
- let value ; // "(var?) target = value"
826
- // Look either for var wasmExports = or just wasmExports =
827
- if ( first . type === 'VariableDeclaration' && first . declarations . length === 1 ) {
828
- const decl = first . declarations [ 0 ] ;
829
- target = decl . id ;
830
- value = decl . init ;
831
- } else if (
832
- first . type === 'ExpressionStatement' &&
833
- first . expression . type === 'AssignmentExpression'
813
+ assert ( ! foundMinimalRuntimeExports ) ;
814
+ foundMinimalRuntimeExports = true ;
815
+ for ( let i = 0 ; i < body . length ; i ++ ) {
816
+ const item = body [ i ] ;
817
+ if (
818
+ item . type === 'ExpressionStatement' &&
819
+ item . expression . type === 'AssignmentExpression' &&
820
+ item . expression . operator === '=' &&
821
+ item . expression . left . type === 'Identifier' &&
822
+ item . expression . right . type === 'MemberExpression' &&
823
+ item . expression . right . object . type === 'Identifier' &&
824
+ item . expression . right . object . name === 'wasmExports' &&
825
+ item . expression . right . property . type === 'Literal'
834
826
) {
835
- const assign = first . expression ;
836
- if ( assign . operator === '=' ) {
837
- target = assign . left ;
838
- value = assign . right ;
839
- }
840
- }
841
- if ( target && target . type === 'Identifier' && target . name === 'wasmExports' && value ) {
842
- if (
843
- value . type === 'MemberExpression' &&
844
- value . object . type === 'MemberExpression' &&
845
- value . object . object . type === 'Identifier' &&
846
- value . object . object . name === 'output' &&
847
- value . object . property . type === 'Identifier' &&
848
- value . object . property . name === 'instance' &&
849
- value . property . type === 'Identifier' &&
850
- value . property . name === 'exports'
851
- ) {
852
- // This looks very much like what we are looking for.
853
- assert ( ! foundMinimalRuntimeExports ) ;
854
- for ( let i = 1 ; i < body . length ; i ++ ) {
855
- const item = body [ i ] ;
856
- if (
857
- item . type === 'ExpressionStatement' &&
858
- item . expression . type === 'AssignmentExpression' &&
859
- item . expression . operator === '=' &&
860
- item . expression . left . type === 'Identifier' &&
861
- item . expression . right . type === 'MemberExpression' &&
862
- item . expression . right . object . type === 'Identifier' &&
863
- item . expression . right . object . name === 'wasmExports' &&
864
- item . expression . right . property . type === 'Literal'
865
- ) {
866
- const name = item . expression . left . name ;
867
- const asmName = item . expression . right . property . value ;
868
- saveAsmExport ( name , asmName ) ;
869
- emptyOut ( item ) ; // ignore all this in the second pass; this does not root
870
- }
871
- }
872
- foundMinimalRuntimeExports = true ;
873
- }
827
+ const name = item . expression . left . name ;
828
+ const asmName = item . expression . right . property . value ;
829
+ saveAsmExport ( name , asmName ) ;
830
+ emptyOut ( item ) ; // ignore all this in the second pass; this does not root
874
831
}
875
832
}
833
+ } else if ( ! specialScopes ) {
834
+ defuns . push ( node ) ;
835
+ nameToGraphName [ name ] = getGraphName ( name , 'defun' ) ;
836
+ emptyOut ( node ) ; // ignore this in the second pass; we scan defuns separately
876
837
}
838
+ } else if ( node . type === 'ArrowFunctionExpression' ) {
839
+ assert ( specialScopes > 0 ) ;
840
+ specialScopes -- ;
877
841
} else if ( node . type === 'Property' && node . method ) {
878
842
assert ( specialScopes > 0 ) ;
879
843
specialScopes -- ;
0 commit comments