diff --git a/js/soljson_interface.js b/js/soljson_interface.js index fc9738f..1943564 100644 --- a/js/soljson_interface.js +++ b/js/soljson_interface.js @@ -13,10 +13,40 @@ mergeInto(LibraryManager.library, { const deasync = require('deasync'); 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) { return new Promise((resolve, reject) => { // Create a new Worker - const worker = new Worker('./worker.js'); + const worker = new Worker(workerCode, { eval: true }); // Listen for messages from the worker worker.on('message', (message) => { diff --git a/js/worker.js b/js/worker.js deleted file mode 100644 index 642fa69..0000000 --- a/js/worker.js +++ /dev/null @@ -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 }); - } -});