wasmonkey
Version:
Write and run C code inside JavaScript file.
225 lines (187 loc) • 6.31 kB
JavaScript
const fs = require("fs");
const settings = require("../../settings");
const WASM_CONFIGS = {
wasmMemory: new WebAssembly.Memory({initial:256, maximum: 256}),
wasmTable: new WebAssembly.Table({
initial: 1,
maximum: 1,
element: "anyfunc"
}),
wasmLibArgs: {
__handle_stack_overflow: ()=>{},
emscripten_resize_heap: ()=>{},
__lock: ()=>{},
__unlock: ()=>{},
memory: this.wasmMemory,
table: this.wasmTable
},
wasmInfo: {
env: this.wasmLibArgs,
wasi_snapshot_preview1: this.wasmLibArgs
}
}
const load = () => {
const filePath =
settings.rootFolder+"/"+settings.execFolderName+"/"+settings.execFileName+".wasm";
let wasmBuffer;
try {
wasmBuffer = fs.readFileSync(filePath);
}
catch(err) {
console.log(err)
}
let instance = new WebAssembly.Instance(new WebAssembly.Module(wasmBuffer), WASM_CONFIGS.wasmInfo);
return instance.exports
}
module.exports = {load}
// const fs = require("fs");
// const { exec } = require("child_process");
// const CONFIG_FOLDER_NAME = "malanka";
// const PARSED_FILE_SUBSUFFIX = ".mal.js";
// const CODE_SECTION_TAG = "@malanka {"
// const EXECUTION_FILE_PATH = CONFIG_FOLDER_NAME+"/execute.c";
// const filesToParse = [];
// let resultCodeTemplate =
// `
// #include "emscripten.h"
// `;
// const checkForFile = path => {
// let isDirectory = false;
// try {
// isDirectory = fs.lstatSync(path).isDirectory();
// }
// catch(err) {
// console.log(err)
// }
// if(path.replace("./","") !== CONFIG_FOLDER_NAME) {
// if(!isDirectory) {
// let filename = path.split("/");
// filename = filename[filename.length-1];
// if(filename.includes(PARSED_FILE_SUBSUFFIX)) {
// filesToParse.push(path);
// }
// }
// else {
// iterateFiles(path);
// }
// }
// }
// const iterateFiles = path => {
// var files = fs.readdirSync(path);
// files.forEach(file => {
// checkForFile(path+"/"+file);
// });
// }
// const analazeTagSection = codeText => {
// let codeLines = codeText.split("\n");
// let isTagSpecialCodeSectionOpened = false;
// let isSingleStrLiteral = false;
// let isDoubleStrLiteral = false;
// let openedFigureBracketsCount = 0;
// codeLines.forEach(line => {
// line = line.trim();
// if(isTagSpecialCodeSectionOpened) {
// // for "}" bracket of tag special code section
// if(!isSingleStrLiteral && !isDoubleStrLiteral && openedFigureBracketsCount===1) {
// if(line.trim()==="}") {
// isTagSpecialCodeSectionOpened = false;
// openedFigureBracketsCount = 0;
// }
// }
// isSingleStrLiteral = (line.split("'").length-1)%2 ? !isSingleStrLiteral : isSingleStrLiteral;
// isDoubleStrLiteral = (line.split('"').length-1)%2 ? !isDoubleStrLiteral : isDoubleStrLiteral;
// openedFigureBracketsCount += (line.split("{").length-1)-(line.split("}").length-1);
// if(isTagSpecialCodeSectionOpened)
// resultCodeTemplate += line+"\n"
// }
// else {
// let codeSectionTagIndex = line.indexOf(CODE_SECTION_TAG);
// if (codeSectionTagIndex != -1) {
// // check for comments befor
// let appendStr = line.substring(0,codeSectionTagIndex);
// if(!appendStr.includes("//")) {
// isTagSpecialCodeSectionOpened = true;
// openedFigureBracketsCount = 1;
// }
// }
// }
// });
// }
// const parseFilesTagsCode = _ => {
// filesToParse.forEach(file => {
// try {
// const data = fs.readFileSync(file, 'utf8')
// analazeTagSection(data);
// } catch (err) {
// console.error(err)
// }
// return;
// });
// }
// const writeToExecutionFile = _ => {
// try {
// const data = fs.writeFileSync(EXECUTION_FILE_PATH, resultCodeTemplate)
// } catch (err) {
// console.error(err)
// }
// }
// const execute = _ => {
// let compileCommand = `emcc ${EXECUTION_FILE_PATH} -o ${CONFIG_FOLDER_NAME}/compiled.wasm -s WASM=1 --no-entry`;
// exec(compileCommand, (error, stdout, stderr) => {
// if (error) {
// console.log(`error: ${error.message}`);
// return;
// }
// if (stderr) {
// console.log(`stderr: ${stderr}`);
// return;
// }
// });
// }
// let wasmExports = null;
// let wasmMemory = new WebAssembly.Memory({initial: 256, maximum: 256});
// let wasmTable = new WebAssembly.Table({
// 'initial': 1,
// 'maximum': 1,
// 'element': 'anyfunc'
// });
// let asmLibraryArg = {
// "__handle_stack_overflow": ()=>{},
// "emscripten_resize_heap": ()=>{},
// "__lock": ()=>{},
// "__unlock": ()=>{},
// "memory": wasmMemory,
// "table": wasmTable
// };
// var info = {
// 'env': asmLibraryArg,
// 'wasi_snapshot_preview1': asmLibraryArg
// };
// function factorialTest(n) {
// return n !== 0 ? n*factorialTest(n-1) : 1;
// }
// function goFactorial(count, n) {
// for (let i = 0; i < count; i++)
// factorialTest(n);
// }
// function loadWasm(){
// const wasmBuffer = fs.readFileSync('./malanka/compiled.wasm');
// WebAssembly.instantiate(wasmBuffer,info).then(wasmModule => {
// let wasm_exports = wasmModule.instance.exports;
// let n = 10;
// let start_ts = Date.now();
// goFactorial(10e6,15);
// goFactorial(10e6,15);
// goFactorial(10e6,15);
// // wasm_exports["goFactorial"](10e6,15)
// // wasm_exports["goFactorial"](10e6,15)
// // wasm_exports["goFactorial"](10e6,15)
// console.log("time: " + (Date.now()-start_ts));
// });
// }
// // iterateFiles(".");
// // parseFilesTagsCode();
// // writeToExecutionFile();
// execute();
// loadWasm();
// // recurisiveFiles('parser.js');