Commit 86d632ba authored by Tobias Pape's avatar Tobias Pape Committed by Stefan Marr

Make prmitive loading semi-dynamic

parent f0a645a8
#
# Captures the known primitives
#
class PrimitivesNotFound(Exception): pass
def _is_primitives_class(e):
"NOT_RPYTHON"
from som.primitives.primitives import Primitives
import inspect
_, entry = e
return (inspect.isclass(entry) and
issubclass(entry, Primitives)
and entry is not Primitives)
def _setup_primitives():
"NOT_RPYTHON"
from importlib import import_module
import inspect
import py
from pprint import pformat
directory = py.path.local(__file__).dirpath()
files = filter(lambda ent: ent.basename.endswith("_primitives.py"),
directory.listdir())
mods = map(lambda mod: import_module("som.primitives." + mod.purebasename),
files)
all_members = map(lambda module: inspect.getmembers(module),
mods)
all_members = reduce(lambda all, each: all + each, all_members)
all_prims = filter(_is_primitives_class, all_members)
prim_pairs = map(lambda (name, cls):
(name[:name.find("Primitives")], cls),
all_prims)
return dict(prim_pairs)
_primitives = _setup_primitives()
def primitives_for_class(cls):
key = cls.get_name().get_string()
res = _primitives.get(key, None)
if res is None:
raise PrimitivesNotFound
return res
\ No newline at end of file
from som.vmobjects.object import Object
from som.primitives.primitives import Primitives
from importlib import import_module
import inspect
class Class(Object):
# Static field indices and number of class fields
......@@ -31,7 +28,6 @@ class Class(Object):
# Check whether or not this class has a super class
return self.get_field(self.SUPER_CLASS_INDEX) != self._universe.nilObject
def get_name(self):
# Get the name of this class by reading the field with name index
return self.get_field(self.NAME_INDEX)
......@@ -172,47 +168,18 @@ class Class(Object):
# Get the next invokable in the instance invokable array
if self.get_instance_invokable(i).is_primitive():
return True
return False
def load_primitives(self):
## We currently do not support dynamic loading of primitives.
## Dynamic loading might come later, based on the SPy solution of Lars.
# TODO: add support for dynamic loading!
# The hack for now hard codes the primitives for the classes of the
# standard library.
if self.get_name().get_string() == "Array":
from som.primitives.array_primitives import ArrayPrimitives
ArrayPrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "BigInteger":
from som.primitives.biginteger_primitives import BigIntegerPrimitives
BigIntegerPrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "Block":
from som.primitives.block_primitives import BlockPrimitives
BlockPrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "Class":
from som.primitives.class_primitives import ClassPrimitives
ClassPrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "Double":
from som.primitives.double_primitives import DoublePrimitives
DoublePrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "Integer":
from som.primitives.integer_primitives import IntegerPrimitives
IntegerPrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "Object":
from som.primitives.object_primitives import ObjectPrimitives
ObjectPrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "String":
from som.primitives.string_primitives import StringPrimitives
StringPrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "Symbol":
from som.primitives.symbol_primitives import SymbolPrimitives
SymbolPrimitives(self._universe).install_primitives_in(self)
elif self.get_name().get_string() == "System":
from som.primitives.system_primitives import SystemPrimitives
SystemPrimitives(self._universe).install_primitives_in(self)
from som.primitives.known import (primitives_for_class,
PrimitivesNotFound)
try:
prims = primitives_for_class(self)
except PrimitivesNotFound:
prims = None
assert prims is not None, "We yet only support prims for known classes"
prims(self._universe).install_primitives_in(self)
def replace_bytecodes(self):
cnt = self.get_number_of_instance_invokables()
for i in range(0, cnt):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment