@@ -43,22 +43,38 @@ const isGlobalImport = n => n.descr.type === "GlobalType";
4343const JS_COMPAT_TYPES = new Set ( [ "i32" , "f32" , "f64" ] ) ;
4444
4545/**
46- * @param {t.ModuleImport } moduleImport the import
46+ * @param {t.Signature } signature the func signature
4747 * @returns {null | string } the type incompatible with js types
4848 */
49- const getJsIncompatibleType = moduleImport => {
50- if ( moduleImport . descr . type !== "FuncImportDescr" ) return null ;
51- const signature = moduleImport . descr . signature ;
49+ const getJsIncompatibleType = signature => {
5250 for ( const param of signature . params ) {
53- if ( ! JS_COMPAT_TYPES . has ( param . valtype ) )
51+ if ( ! JS_COMPAT_TYPES . has ( param . valtype ) ) {
5452 return `${ param . valtype } as parameter` ;
53+ }
5554 }
5655 for ( const type of signature . results ) {
5756 if ( ! JS_COMPAT_TYPES . has ( type ) ) return `${ type } as result` ;
5857 }
5958 return null ;
6059} ;
6160
61+ /**
62+ * TODO why are there two different Signature types?
63+ * @param {t.FuncSignature } signature the func signature
64+ * @returns {null | string } the type incompatible with js types
65+ */
66+ const getJsIncompatibleTypeOfFuncSignature = signature => {
67+ for ( const param of signature . args ) {
68+ if ( ! JS_COMPAT_TYPES . has ( param ) ) {
69+ return `${ param } as parameter` ;
70+ }
71+ }
72+ for ( const type of signature . result ) {
73+ if ( ! JS_COMPAT_TYPES . has ( type ) ) return `${ type } as result` ;
74+ }
75+ return null ;
76+ } ;
77+
6278const decoderOpts = {
6379 ignoreCodeSection : true ,
6480 ignoreDataSection : true ,
@@ -96,17 +112,15 @@ class WebAssemblyParser extends Tapable {
96112 if ( descriptor . exportType === "Func" ) {
97113 const funcidx = descriptor . id . value ;
98114
115+ /** @type {t.FuncSignature } */
99116 const funcSignature = moduleContext . getFunction ( funcidx ) ;
100117
101- const hasIncompatibleArg = funcSignature . args . some (
102- t => ! JS_COMPAT_TYPES . has ( t )
103- ) ;
104- const hasIncompatibleResult = funcSignature . result . some (
105- t => ! JS_COMPAT_TYPES . has ( t )
118+ const incompatibleType = getJsIncompatibleTypeOfFuncSignature (
119+ funcSignature
106120 ) ;
107121
108- if ( hasIncompatibleArg === true || hasIncompatibleResult === true ) {
109- jsIncompatibleExports . push ( node . name ) ;
122+ if ( incompatibleType ) {
123+ jsIncompatibleExports [ node . name ] = incompatibleType ;
110124 }
111125 }
112126
@@ -151,10 +165,15 @@ class WebAssemblyParser extends Tapable {
151165 } else if ( isTableImport ( node ) === true ) {
152166 onlyDirectImport = "Table" ;
153167 } else if ( isFuncImport ( node ) === true ) {
154- const incompatibleType = getJsIncompatibleType ( node ) ;
168+ const incompatibleType = getJsIncompatibleType ( node . descr . signature ) ;
155169 if ( incompatibleType ) {
156170 onlyDirectImport = `Non-JS-compatible Func Sigurature (${ incompatibleType } )` ;
157171 }
172+ } else if ( isGlobalImport ( node ) === true ) {
173+ const type = node . descr . valtype ;
174+ if ( ! JS_COMPAT_TYPES . has ( type ) ) {
175+ onlyDirectImport = `Non-JS-compatible Global Type (${ type } )` ;
176+ }
158177 }
159178
160179 const dep = new WebAssemblyImportDependency (
0 commit comments