33// META: script=/wasm/jsapi/wasm-module-builder.js
44// META: script=/wasm/jsapi/js-string/polyfill.js
55
6+ // The list of builtins and their signatures.
7+ let builtins ;
8+
69// Generate two sets of exports, one from a polyfill implementation and another
710// from the builtins provided by the host.
811let polyfillExports ;
@@ -14,7 +17,7 @@ setup(() => {
1417 // a known builtin function from wasm.
1518 const builder = new WasmModuleBuilder ( ) ;
1619 const arrayIndex = builder . addArray ( kWasmI16 , true , kNoSuperType , true ) ;
17- const builtins = [
20+ builtins = [
1821 {
1922 name : "test" ,
2023 params : [ kWasmExternRef ] ,
@@ -381,3 +384,35 @@ test(() => {
381384 }
382385 }
383386} ) ;
387+
388+ // Test that incorrect import types are rejected, even if they have correct
389+ // signatures.
390+ test ( ( ) => {
391+ for ( let builtin of builtins ) {
392+ const builder = new WasmModuleBuilder ( ) ;
393+ // The type is wrong because it is in a nontrivial rec group.
394+ const typeIndex = builder . nextTypeIndex ( ) ;
395+ builder . startRecGroup ( ) ;
396+ builder . addType ( {
397+ params : builtin . params ,
398+ results : builtin . results
399+ } ) ;
400+ builder . addStruct ( [ ] ) ;
401+ builder . endRecGroup ( ) ;
402+
403+ builder . addImport (
404+ "wasm:js-string" ,
405+ builtin . name ,
406+ typeIndex ) ;
407+
408+ const buffer = builder . toBuffer ( ) ;
409+
410+ // Validation should fail.
411+ assert_false ( WebAssembly . validate ( buffer , { builtins : [ "js-string" ] } ) ) ;
412+
413+ // Compilation should fail.
414+ assert_throws_js ( WebAssembly . CompileError , ( ) => {
415+ new WebAssembly . Module ( buffer , { builtins : [ "js-string" ] } ) ;
416+ } ) ;
417+ }
418+ } , "Incorrect types" ) ;
0 commit comments