Skip to content

Commit

Permalink
added type promotion for nodes, tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
cnvogelg committed Dec 14, 2024
1 parent 225512e commit dcf4b7f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions amitools/vamos/libtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
# dos
from .lock import FileLock, FileHandle
from .process import CLI, Process, PathList

# type promotion
from .promote import promote_type
33 changes: 33 additions & 0 deletions amitools/vamos/libtypes/promote.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions test/unit/libtypes_promote.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dcf4b7f

Please sign in to comment.