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:
parent
8d919a5db9
commit
8b0d6a2665
6 changed files with 172 additions and 3 deletions
63
crypto/func/auto-tests/wasm_tests_common.js
Normal file
63
crypto/func/auto-tests/wasm_tests_common.js
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue