@@ -243,24 +243,27 @@ struct StringLowering : public StringGathering {
243243
244244 Type nnExt = Type(HeapType::ext, NonNullable);
245245
246+ // Creates an imported string function, returning its name (which is equal to
247+ // the true name of the import, if there is no conflict).
248+ Name addImport (Module* module , Name trueName, Type params, Type results) {
249+ auto name = Names::getValidFunctionName (*module , trueName);
250+ auto sig = Signature (params, results);
251+ Builder builder (*module );
252+ auto * func = module ->addFunction (builder.makeFunction (name, sig, {}));
253+ func->module = " wasm:js-string" ;
254+ func->base = trueName;
255+ return name;
256+ }
257+
246258 void replaceInstructions (Module* module ) {
247259 // Add all the possible imports up front, to avoid adding them during
248260 // parallel work. Optimizations can remove unneeded ones later.
249- Builder builder (*module );
250- auto array16 = Array (Field (Field::i16 , Mutable));
261+ auto nullArray16 = Type (Array (Field (Field::i16 , Mutable)), Nullable);
251262
252- {
253- fromCharCodeArrayImport = Names::getValidFunctionName (
254- *module , " string.fromCharCodeArray" );
255- auto sig = Signature ({Type (array16, Nullable), Type::i32 , Type::i32 }, nnExt);
256- module ->addFunction (builder.makeFunction (fromCharCodeArrayImport, sig, {}));
257- }
258- {
259- fromCodePointImport = Names::getValidFunctionName (
260- *module , " string.fromCodePoint" );
261- auto sig = Signature (Type::i32 , nnExt);
262- module ->addFunction (builder.makeFunction (fromCodePointImport, sig, {}));
263- }
263+ // string.fromCharCodeArray: array, start, end -> ext
264+ fromCharCodeArrayImport = addImport (module , " fromCharCodeArray" , {nullArray16, Type::i32 , Type::i32 }, nnExt);
265+ // string.fromCodePoint: codepoint -> ext
266+ fromCodePointImport = addImport (module , " fromCodePoint" , Type::i32 , nnExt);
264267
265268 // Replace the string instructions in parallel.
266269 struct Replacer : public WalkerPass <PostWalker<Replacer>> {
0 commit comments