Скомпилируйте C++ в WASM на стороне клиента браузераJavascript

Форум по Javascript
Ответить
Anonymous
 Скомпилируйте C++ в WASM на стороне клиента браузера

Сообщение Anonymous »

Почему я не могу скомпилировать его в WASM на стороне клиента браузера, а не на сервере? Последняя ошибка:

Ошибка инициализации компилятора. Проверьте консоль для получения подробной информации.

Не удалось выполнить postMessage на «Worker»: для передачи SharedArrayBuffer требуется self.crossOriginIsolated.

редактор hello.js:
document.addEventListener('DOMContentLoaded', () => {
(async () => {
try { // Add a try block here
const { WebContainer } = await import('https://cdn.jsdelivr.net/npm/@webcontai ... t/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

Подробнее здесь: https://stackoverflow.com/questions/798 ... lient-side
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»