1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

Add WASM FunC autotests (#673)

* feat: func wasm autotests

* fixes necessary for func wasm autotests

---------

Co-authored-by: krigga <krigga7@gmail.com>
This commit is contained in:
EmelyanenkoK 2023-04-19 21:29:41 +03:00 committed by GitHub
parent 8d919a5db9
commit 8b0d6a2665
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 172 additions and 3 deletions

View file

@ -0,0 +1,63 @@
const fsSync = require('fs');
const copyToCString = (mod, str) => {
const len = mod.lengthBytesUTF8(str) + 1;
const ptr = mod._malloc(len);
mod.stringToUTF8(str, ptr, len);
return ptr;
};
const copyToCStringPtr = (mod, str, ptr) => {
const allocated = copyToCString(mod, str);
mod.setValue(ptr, allocated, '*');
return allocated;
};
const copyFromCString = (mod, ptr) => {
return mod.UTF8ToString(ptr);
};
async function compileFile(mod, filename) {
const callbackPtr = mod.addFunction((_kind, _data, contents, error) => {
const kind = copyFromCString(mod, _kind);
const data = copyFromCString(mod, _data);
if (kind === 'realpath') {
copyToCStringPtr(mod, fsSync.realpathSync(data), contents);
} else if (kind === 'source') {
const path = fsSync.realpathSync(data);
try {
copyToCStringPtr(mod, fsSync.readFileSync(path).toString('utf-8'), contents);
} catch (err) {
copyToCStringPtr(mod, e.message, error);
}
} else {
copyToCStringPtr(mod, 'Unknown callback kind ' + kind, error);
}
}, 'viiii');
const config = {
optLevel: 2,
sources: [filename]
};
const configPtr = copyToCString(mod, JSON.stringify(config));
const responsePtr = mod._func_compile(configPtr, callbackPtr);
return JSON.parse(copyFromCString(mod, responsePtr));
}
const wasmModule = require(process.env.FUNCFIFTLIB_MODULE)
const wasmBinary = new Uint8Array(fsSync.readFileSync(process.env.FUNCFIFTLIB_WASM))
async function compileWasm() {
const mod = await wasmModule({ wasmBinary })
return mod
}
module.exports = {
compileFile,
compileWasm
}