fix crash when attempting to import non-existing module

This commit is contained in:
Irmen de Jong 2021-10-11 20:27:51 +02:00
parent c55ac0450f
commit 2c2d474059
5 changed files with 38 additions and 16 deletions

View File

@ -178,7 +178,7 @@ fun parseImports(filepath: Path,
val programAst = Program(filepath.nameWithoutExtension, bf, compTarget)
bf.program = programAst
val importer = ModuleImporter(programAst, compTarget.name, libdirs)
val importer = ModuleImporter(programAst, compTarget.name, errors, libdirs)
importer.importModule(filepath)
errors.report()
@ -351,24 +351,39 @@ fun printAst(programAst: Program) {
println()
}
internal fun loadAsmIncludeFile(filename: String, sourcePath: Path): String {
return if (filename.startsWith("library:")) {
val resource = tryGetEmbeddedResource(filename.substring(8))
?: throw IllegalArgumentException("library file '$filename' not found")
resource.bufferedReader().use { it.readText() }
internal fun loadAsmIncludeFile(filename: String, sourcePath: Path): Result<String> {
if (filename.startsWith("library:")) {
val resource = getEmbeddedResource(filename.substring(8))
resource.fold(
onSuccess = { resource ->
val text = resource.bufferedReader().use { it.readText() }
return Result.success(text)
},
onFailure = {
return Result.failure(IllegalArgumentException("library file '$filename' not found")) // TODO FileNotFoundException instead?
}
)
} else {
// first try in the isSameAs folder as where the containing file was imported from
val sib = sourcePath.resolveSibling(filename)
if (sib.toFile().isFile)
sib.toFile().readText()
return if (sib.toFile().isFile)
Result.success(sib.toFile().readText())
else
File(filename).readText()
Result.success(File(filename).readText())
}
}
/**
* Handle via SourceCode
*/
internal fun tryGetEmbeddedResource(name: String): InputStream? {
return object{}.javaClass.getResourceAsStream("/prog8lib/$name")
internal fun getEmbeddedResource(name: String): Result<InputStream> {
return try {
val stream = object {}.javaClass.getResourceAsStream("/prog8lib/$name")
if(stream!=null)
Result.success(stream)
else
Result.failure(NoSuchFileException(File(name)))
} catch(ex: Exception) {
Result.failure(ex)
}
}

View File

@ -15,6 +15,7 @@ import kotlin.io.path.*
class ModuleImporter(private val program: Program,
private val compilationTargetName: String,
private val errors: IErrorReporter,
libdirs: List<String>) {
private val libpaths: List<Path> = libdirs.map { Path(it) }
@ -83,8 +84,11 @@ class ModuleImporter(private val program: Program,
importModule(srcCode)
} else {
srcCode = tryGetModuleFromFile(moduleName, importingModule)
if (srcCode == null)
throw NoSuchFileException(File("$moduleName.p8"))
if (srcCode == null) {
errors.err("imported file not found: $moduleName.p8", import.position)
return null
//throw NoSuchFileException(File("$moduleName.p8"))
}
importModule(srcCode)
}

View File

@ -1317,7 +1317,7 @@ $repeatLabel lda $counterVar
// TODO: handle %asminclude with SourceCode
val includedName = stmt.args[0].str!!
val sourcePath = Path(stmt.definingModule.source!!.pathString()) // FIXME: %asminclude inside non-library, non-filesystem module
val sourcecode = loadAsmIncludeFile(includedName, sourcePath)
val sourcecode = loadAsmIncludeFile(includedName, sourcePath).getOrThrow()
assemblyLines.add(sourcecode.trimEnd().trimStart('\n'))
}
"%asmbinary" -> {

View File

@ -5,6 +5,7 @@ import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.*
import prog8.ast.Program
import prog8.compiler.ErrorReporter
import prog8.compiler.ModuleImporter
import prog8.parser.ParseError
import prog8tests.helpers.*
@ -25,7 +26,7 @@ class TestModuleImporter {
private fun makeImporter(vararg searchIn: String): ModuleImporter = makeImporter(searchIn.asList())
private fun makeImporter(searchIn: Iterable<String>) =
ModuleImporter(program, "blah", searchIn.toList())
ModuleImporter(program, "blah", ErrorReporter(), searchIn.toList())
@Nested
inner class Constructor {

View File

@ -1,9 +1,11 @@
%import textio
%import textio2
%import test_stack
%zeropage basicsafe
main {
sub start() {
%asminclude "fozsdfsdf.asm"
txt.print("ok")
test_stack.test()
}