@@ -977,6 +977,12 @@ CheerpWriter::COMPILE_INSTRUCTION_FEEDBACK CheerpWriter::handleBuiltinCall(const
977977 stream << " new Blob([" << threadingObject << " ." << blobText << " ])" ;
978978 return COMPILE_OK;
979979 }
980+ else if (intrinsicId==Intrinsic::cheerp_thread_setup_resolve)
981+ {
982+ StringRef threadSetupResolve = namegen.getBuiltinName (NameGenerator::Builtin::THREADSETUPRESOLVE);
983+ stream << threadSetupResolve << " ();" << NewLine;
984+ return COMPILE_OK;
985+ }
980986 else if (intrinsicId==Intrinsic::abs)
981987 {
982988 // Implementing ( X >= 0 ) ? X : -X
@@ -6848,7 +6854,7 @@ void CheerpWriter::compileWasmLoader()
68486854 stream << " module.exports=" << NewLine;
68496855 else if (makeModule == MODULE_TYPE::ES6)
68506856 stream << " return " ;
6851- stream << dummyName << " .promise.then(" << shortestName << " =>{" << NewLine;
6857+ stream << dummyName << " .promise= " << dummyName << " .promise .then(" << shortestName << " =>{" << NewLine;
68526858 stream << threadObject << " .module=" << shortestName << " .module;" << NewLine;
68536859 stream << " __asm=" << shortestName << " .instance.exports;" << NewLine;
68546860 }
@@ -7131,6 +7137,13 @@ void CheerpWriter::compileEntryPoint()
71317137
71327138void CheerpWriter::compileThreadingObject ()
71337139{
7140+ // First create the promise and resolve variables for threading setup.
7141+ StringRef threadPromise = namegen.getBuiltinName (NameGenerator::Builtin::THREADSETUPPROMISE);
7142+ StringRef threadResolve = namegen.getBuiltinName (NameGenerator::Builtin::THREADSETUPRESOLVE);
7143+ stream << " var " << threadResolve << " =null;" << NewLine;
7144+ stream << " var " << threadPromise << " =new Promise((r)=>{" << threadResolve << " =r;});" << NewLine;
7145+
7146+ // Then create the rest of the threading variables.
71347147 StringRef threadObject = namegen.getBuiltinName (NameGenerator::Builtin::THREADINGOBJECT);
71357148 StringRef workerThreadObject = namegen.getBuiltinName (NameGenerator::Builtin::THREADINGOBJECTWORKER);
71367149 StringRef blobName = namegen.getBuiltinName (NameGenerator::Builtin::BLOBNAME);
@@ -7266,6 +7279,16 @@ void CheerpWriter::makeJS()
72667279
72677280 compileDefineExports ();
72687281 compileEntryPoint ();
7282+
7283+ // If -pthread is linked in, we want to return the thread setup promise here.
7284+ if (!LowerAtomics)
7285+ {
7286+ stream << " return " << namegen.getBuiltinName (NameGenerator::Builtin::THREADSETUPPROMISE) << " ;" << NewLine;
7287+ // If we are in ES6 or CommonJS module mode, chain the thread setup promise here.
7288+ if (makeModule == MODULE_TYPE::ES6 || makeModule == MODULE_TYPE::COMMONJS)
7289+ stream << " }).then(()=>{" << NewLine;
7290+ }
7291+
72697292 if (makeModule == MODULE_TYPE::COMMONJS || makeModule == MODULE_TYPE::ES6)
72707293 compileCommonJSExports ();
72717294
0 commit comments