def initialize_classes(self): asm = assembler.CodeAssembler([]) clstraits = [] for name in self.order: rib, bases = self.tracking[name] if constants.QName("Object") not in bases: bases.append(constants.QName("Object")) asm.emit('getscopeobject', 0) for base in reversed(bases): asm.emit('getlex', base) asm.emit('pushscope') asm.emit('getlex', rib.super_name) asm.emit('newclass', rib.index) asm.emit('initproperty', rib.name) clstraits.append(traits.ClassTrait(name, rib.classobj)) for base in bases: asm.emit('popscope') return asm, clstraits
def __init__(self, name, params, rettype, trait_type=traits.MethodTrait, static=False, override=False, prologue=True): self.name = IMultiname(name) self.param_types, self.param_names = zip(*params) or ([], []) self.rettype = rettype self.static, self.override = static, override self.exceptions = [] self.asm = assembler.CodeAssembler(['this'] + list(self.param_names)) if prologue: self.asm.emit('getlocal0') self.asm.emit('pushscope') self.method_info = abc.MethodInfo(str(name), self.param_types, rettype, param_names=self.param_names) self.method_body = abc.MethodBodyInfo(self.method_info, self.asm, exceptions=self.exceptions) self.trait = traits.MethodTrait(self.name, self.method_info, override=self.override)
def test_nothing_function(): asm = assembler.CodeAssembler([]) asm.emit("getlocal0") asm.emit("pushscope") asm.emit("returnvoid") _, bytes = assemble(asm) assert bytes == "\xd0\x30\x47"
def test_multinames(): asm = assembler.CodeAssembler([]) asm.emit("getlex", "String") const = constants.ConstantPool() const.write(asm) assert const.multiname.value_at(1) == constants.QName("String")
def test_simple(): asm = assembler.CodeAssembler([]) asm.emit("pushint", 25) asm.emit("pushstring", "Hello") const = constants.ConstantPool() const.write(asm) assert const.int.value_at(1) == 25 assert const.utf8.value_at(1) == "Hello"
def test_conflicts(): asm = assembler.CodeAssembler([]) inst1 = asm.emit("pushint", 25) inst2 = asm.emit("pushint", 25) assert inst1 is not inst2 const = constants.ConstantPool() const.write(asm) assert inst1._arg_index == 1 assert inst2._arg_index == 1
def test_serialization(): asm = assembler.CodeAssembler([]) asm.emit("pushint", 25) asm.emit("pushstring", "Hello") const = constants.ConstantPool() const.write(asm) bytes = const.serialize() assert bytes == ( "\x02\x19" # int "\x01\x01" # uint, double "\x02\x05Hello" # utf8 "\x01\x01\x01") # namespace, nsset, mn
def test_branch_optimizer(): asm = assembler.CodeAssembler([]) asm.emit("getlocal0") asm.emit("pushscope") asm.emit("pushtrue") asm.emit("not") asm.emit("iftrue", "blank") opz = optimizer.BranchOptimizer() instructions = opz.optimize(asm.instructions) names = [inst.name for inst in instructions] assert names == ["getlocal0", "pushscope", "pushtrue", "iffalse"]
def test_defaults(): asm = assembler.CodeAssembler([]) pi = asm.emit("pushint", 0) pu = asm.emit("pushuint", 0) pd = asm.emit("pushdouble", float('nan')) ps = asm.emit("pushstring", "") const = constants.ConstantPool() const.write(asm) assert pi._arg_index == 0 assert pu._arg_index == 0 # NaN is a tricky situation in ValuePools. assert pd._arg_index == 0 # "" should be assigned to 1 in the case of pushstring. assert ps._arg_index == 1
def test_push_pool(): asm = assembler.CodeAssembler([]) asm.emit("getlocal0") asm.emit("pushscope") asm.emit("pushstring", "This is the string that never ends...") asm.emit("pushint", -16384) asm.emit("pushuint", 32768) asm.emit("pushdouble", 5000.0) asm.emit("returnvoid") # These instructions should all be index 1. _, bytes = assemble(asm) assert bytes == ( "\xd0\x30" # getlocal0, pushscope "\x2c\x01" # pushstring, index 1 "\x2d\x01" # pushint , index 1 "\x2e\x01" # pushuint , index 1 "\x2f\x01" # pushdouble, index 1 "\x47") # returnvoid
def test_jumping(): asm = assembler.CodeAssembler([])