diff --git a/amitools/vamos/libtypes/__init__.py b/amitools/vamos/libtypes/__init__.py index ef8424c7..8746bf6b 100644 --- a/amitools/vamos/libtypes/__init__.py +++ b/amitools/vamos/libtypes/__init__.py @@ -10,3 +10,6 @@ # dos from .lock import FileLock, FileHandle from .process import CLI, Process, PathList + +# type promotion +from .promote import promote_type diff --git a/amitools/vamos/libtypes/promote.py b/amitools/vamos/libtypes/promote.py new file mode 100644 index 00000000..2c3faff9 --- /dev/null +++ b/amitools/vamos/libtypes/promote.py @@ -0,0 +1,33 @@ +from amitools.vamos.libstructs import NodeType, LibraryStruct +from .node import Node +from .task import Task +from .process import Process + +node_map = { + NodeType.NT_TASK: Task, + NodeType.NT_PROCESS: Process, + NodeType.NT_DEVICE: LibraryStruct, + NodeType.NT_LIBRARY: LibraryStruct, +} + + +def promote_type(obj): + """convert objects according to Amiga rules + + * if its a node then use the node type to derive the actual class + * if its a task but of type process + """ + if type(obj) is Node: + node_type = obj.type.get() + if node_type in node_map: + node_cls = node_map[node_type] + return obj.clone(node_cls) + + # promote a task to a process + elif type(obj) is Task: + node_type = obj.node.type.get() + if node_type == NodeType.NT_PROCESS: + return obj.clone(Process) + + # no promotion applied + return obj diff --git a/test/unit/libtypes_promote.py b/test/unit/libtypes_promote.py new file mode 100644 index 00000000..6cdf0b6a --- /dev/null +++ b/test/unit/libtypes_promote.py @@ -0,0 +1,21 @@ +from amitools.vamos.machine.mock import MockMemory +from amitools.vamos.libstructs import NodeType +from amitools.vamos.libtypes import promote_type, Node, Task, Process +from amitools.vamos.libtypes.promote import node_map + + +def libtypes_promote_node_test(): + mem = MockMemory() + for node_type, node_cls in node_map.items(): + node = Node(mem, 0x40) + node.type.set(node_type) + new_node = promote_type(node) + assert type(new_node) is node_cls + + +def libtypes_promote_task_test(): + mem = MockMemory() + task = Task(mem, 0x40) + task.node.type.set(NodeType.NT_PROCESS) + proc = promote_type(task) + assert type(proc) is Process