Skip to content

Commit 29a5922

Browse files
committed
Class loader split into two files
1 parent 9b8feaf commit 29a5922

File tree

4 files changed

+62
-56
lines changed

4 files changed

+62
-56
lines changed

javoc/class/classLoader.lua

Lines changed: 0 additions & 56 deletions
This file was deleted.

javoc/class/fileClassLoader.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local javoc, fileClassLoader = require("umfal")("javoc")
2+
3+
local debugPrint = javoc.util.debug.print
4+
5+
---Loads a class from a file. Path is relative from classpath
6+
---@param file string @ Name of the class to load
7+
---@param classpath string @ Absolute path to the classes to load
8+
---@return table class @ Loaded and parsed class file
9+
function fileClassLoader.load(file, classpath)
10+
if not file:find(".class") then
11+
file = file .. ".class"
12+
end
13+
14+
local filePath = "" .. classpath .. file
15+
16+
debugPrint("Attempting to load class on path of " .. filePath)
17+
local stream = io.open(filePath, "rb")
18+
19+
if not stream then
20+
debugPrint("Failed to find the file or open it")
21+
error("Class File not found!")
22+
end
23+
24+
debugPrint("File stream created, loading class from it")
25+
return javoc.class.streamClassLoader.load(stream)
26+
end

javoc/class/streamClassLoader.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
local javoc, streamClassLoader = require("umfal")("javoc")
2+
3+
local debugPrint = javoc.util.debug.print
4+
5+
---Loads a class from the stream - usually file, but not always
6+
---@param stream file_stream @ Stream with class file
7+
---@return table class @ Load`ed and parsed class file
8+
function streamClassLoader.load(stream)
9+
local class = {}
10+
11+
if not javoc.class.subloaders.magicValue.check(stream) then
12+
error("Class loading aborted: incorrect 'magic value'")
13+
end
14+
15+
debugPrint("Correct 'magic value' is loaded")
16+
17+
class.version = javoc.class.subloaders.version.load(stream)
18+
class.constantPool = javoc.class.subloaders.constantPool.load(stream)
19+
class.accessFlags = javoc.class.subloaders.accessFlags.load(stream)
20+
class.thisClass, class.superClass = javoc.class.subloaders.classNames.load(stream, class.constantPool)
21+
22+
debugPrint("Class " .. class.thisClass.name .. " loaded successfully")
23+
24+
return class
25+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local javoc, magicValue = require("umfal")("javoc")
2+
3+
local debugPrint = javoc.util.debug.print
4+
local binaryUtils = javoc.util.binaryUtils
5+
6+
function magicValue.check(stream)
7+
debugPrint("Loading 'magic value' (0xCAFEBABE)")
8+
local magicValue = binaryUtils.readU4(stream)
9+
10+
return (magicValue == 0xCAFEBABE)
11+
end

0 commit comments

Comments
 (0)