Моя последняя ошибка:
Ошибка инициализации компилятора. Проверьте консоль для получения подробной информации.
Не удалось выполнить «postMessage» на «Worker»: для передачи SharedArrayBuffer требуется self.crossOriginIsolated.
Если эта цель невозможна с тем, что я сейчас делаю (используя веб-контейнеры), может ли кто-нибудь рассказать мне о каком-либо пакете или инструменте, который может скомпилировать C++ в WASM в браузере, только на стороне клиента.
Я пробовал несколько вариантов уже есть, включая Emception, Jupyter и Cib.
Код: Выделить всё
hello.js-editorКод: Выделить всё
document.addEventListener('DOMContentLoaded', () => {
(async () => {
try { // Add a try block here
const { WebContainer } = await import('https://cdn.jsdelivr.net/npm/@webcontainer/api/dist/index.js');
await WebContainer.boot();
const out = document.getElementById('out');
const codeEl = document.getElementById('code-editor');
const compiler = new Worker('./compile-worker.js', { type: 'module' });
const runner = new Worker('./run-worker.js', { type: 'module' });
compiler.onmessage = (e) => {
if (e.data.type === 'error') {
out.textContent += 'Compile error: ' + e.data.message + '\n';
return;
}
out.textContent += 'Running...\n';
runner.onmessage = (m) => {
const { stdout, stderr, exitCode } = m.data || {};
out.textContent += stdout + (stderr ? '\n[stderr]\n' + stderr : '') + '\n[exit ' + exitCode + ']';
};
runner.postMessage({ type: 'runWasi', wasm: e.data.wasm });
};
document.getElementById('run').onclick = () => {
out.textContent = 'Compiling...\n';
compiler.postMessage({ code: codeEl.value });
};
} catch (error) { // And a catch block here
console.error("Error during WebContainer initialization or setup:", error);
const out = document.getElementById('out');
if (out) {
out.textContent = "Error initializing compiler. Check console for details. " + error.message;
}
}
})();
});
Код: Выделить всё
compile-worker.jsКод: Выделить всё
import { WebContainer } from '@webcontainer/api';
let wc;
self.onmessage = async (e) => {
const code = e.data?.code || '#include \nint main(){std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79817127/c-to-wasm-in-browser-client-side-dificultys[/url]
Мобильная версия