mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-14 01:51:03 +00:00
Add worker code to resolc.js
This commit is contained in:
+31
-1
@@ -13,10 +13,40 @@ mergeInto(LibraryManager.library, {
|
|||||||
const deasync = require('deasync');
|
const deasync = require('deasync');
|
||||||
|
|
||||||
var inputJson = UTF8ToString(inputPtr, inputLen);
|
var inputJson = UTF8ToString(inputPtr, inputLen);
|
||||||
|
|
||||||
|
// Inline worker code
|
||||||
|
const workerCode = `
|
||||||
|
// worker.js
|
||||||
|
// nodejs version
|
||||||
|
const { parentPort } = require('worker_threads');
|
||||||
|
|
||||||
|
parentPort.on('message', async (inputJson) => {
|
||||||
|
const { default: ModuleFactory } = await import('./resolc.js');
|
||||||
|
const newModule = await ModuleFactory();
|
||||||
|
|
||||||
|
// Create a virtual file for stdin
|
||||||
|
newModule.FS.writeFile('/in', inputJson);
|
||||||
|
|
||||||
|
// Call main on the new instance
|
||||||
|
const output = newModule.callMain(['--recursive-process']);
|
||||||
|
|
||||||
|
// Check the /err file content
|
||||||
|
const errorMessage = newModule.FS.readFile('/err', { encoding: 'utf8' });
|
||||||
|
|
||||||
|
if (errorMessage.length > 0) {
|
||||||
|
// If /err is not empty, throw an error with its content
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
} else {
|
||||||
|
// If no error, read the output file
|
||||||
|
let outputFile = newModule.FS.readFile('/out', { encoding: 'utf8' });
|
||||||
|
parentPort.postMessage({ output: outputFile });
|
||||||
|
}
|
||||||
|
});`
|
||||||
|
|
||||||
function compileWithWorker(inputJson, callback) {
|
function compileWithWorker(inputJson, callback) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// Create a new Worker
|
// Create a new Worker
|
||||||
const worker = new Worker('./worker.js');
|
const worker = new Worker(workerCode, { eval: true });
|
||||||
|
|
||||||
// Listen for messages from the worker
|
// Listen for messages from the worker
|
||||||
worker.on('message', (message) => {
|
worker.on('message', (message) => {
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
// worker.js
|
|
||||||
// nodejs version
|
|
||||||
import { parentPort } from 'worker_threads';
|
|
||||||
|
|
||||||
parentPort.on('message', async (inputJson) => {
|
|
||||||
const { default: ModuleFactory } = await import('./resolc.js');
|
|
||||||
const newModule = await ModuleFactory();
|
|
||||||
|
|
||||||
// Create a virtual file for stdin
|
|
||||||
newModule.FS.writeFile('/in', inputJson);
|
|
||||||
|
|
||||||
// Call main on the new instance
|
|
||||||
const output = newModule.callMain(['--recursive-process']);
|
|
||||||
|
|
||||||
// Check the /err file content
|
|
||||||
const errorMessage = newModule.FS.readFile('/err', { encoding: 'utf8' });
|
|
||||||
|
|
||||||
if (errorMessage.length > 0) {
|
|
||||||
// If /err is not empty, throw an error with its content
|
|
||||||
throw new Error(errorMessage);
|
|
||||||
} else {
|
|
||||||
// If no error, read the output file
|
|
||||||
let outputFile = newModule.FS.readFile('/out', { encoding: 'utf8' });
|
|
||||||
parentPort.postMessage({ output: outputFile });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user