Skip to content

Commit 82ae5d9

Browse files
committed
[Tolk] Ensure that compiled wasm passes all tests
1 parent c08958e commit 82ae5d9

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

tolk-tester/tests/invalid-symbol/err-2980.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
@compilation_should_fail
77
On Linux/Mac, `realpath()` returns an error, and the error message is "cannot find file"
88
On Windows, it fails after, on reading, with a message "cannot open file"
9-
@stderr err-2980.tolk:2:7: error: Failed to import: cannot
9+
@stderr err-2980.tolk:2:7: error: Failed to import:
1010
@stderr import "unexisting.tolk";
1111
*/

tolk-tester/tolk-tester.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -500,29 +500,31 @@ function copyFromCString(mod, ptr) {
500500
function compileFile(mod, filename, experimentalOptions, withSrcLineComments) {
501501
// see tolk-wasm.cpp: typedef void (*WasmFsReadCallback)(int, char const*, char**, char**)
502502
const callbackPtr = mod.addFunction((kind, dataPtr, destContents, destError) => {
503-
if (kind === 0) { // realpath
504-
try {
505-
let relative = copyFromCString(mod, dataPtr)
506-
if (relative.startsWith('@stdlib/')) {
507-
// import "@stdlib/filename" or import "@stdlib/filename.tolk"
508-
relative = STDLIB_FOLDER + '/' + relative.substring(7)
509-
if (!relative.endsWith('.tolk')) {
510-
relative += '.tolk'
503+
switch (kind) { // enum ReadCallback::Kind in C++
504+
case 0: // realpath
505+
let relativeFilename = copyFromCString(mod, dataPtr) // from `import` statement, relative to cur file
506+
if (!relativeFilename.endsWith('.tolk')) {
507+
relativeFilename += '.tolk'
508+
}
509+
copyToCStringPtr(mod, path.normalize(relativeFilename), destContents)
510+
break
511+
case 1: // read file
512+
try {
513+
const filename = copyFromCString(mod, dataPtr) // already normalized (as returned above)
514+
if (filename.startsWith('@stdlib/')) {
515+
const contents = fs.readFileSync(STDLIB_FOLDER + '/' + filename.substring(8)).toString('utf-8');
516+
copyToCStringPtr(mod, contents, destContents)
517+
} else {
518+
const contents = fs.readFileSync(filename).toString('utf-8');
519+
copyToCStringPtr(mod, contents, destContents)
511520
}
521+
} catch (err) {
522+
copyToCStringPtr(mod, err.message || err.toString(), destError)
512523
}
513-
copyToCStringPtr(mod, fs.realpathSync(relative), destContents);
514-
} catch (err) {
515-
copyToCStringPtr(mod, 'cannot find file', destError);
516-
}
517-
} else if (kind === 1) { // read file
518-
try {
519-
const absolute = copyFromCString(mod, dataPtr) // already normalized (as returned above)
520-
copyToCStringPtr(mod, fs.readFileSync(absolute).toString('utf-8'), destContents);
521-
} catch (err) {
522-
copyToCStringPtr(mod, err.message || err.toString(), destError);
523-
}
524-
} else {
525-
copyToCStringPtr(mod, 'Unknown callback kind=' + kind, destError);
524+
break
525+
default:
526+
copyToCStringPtr(mod, 'Unknown callback kind=' + kind, destError)
527+
break
526528
}
527529
}, 'viiii');
528530

0 commit comments

Comments
 (0)