11package dev .ultreon .pythonc ;
22
33import org .objectweb .asm .MethodVisitor ;
4+ import org .objectweb .asm .Opcodes ;
45import org .objectweb .asm .Type ;
56
67import static org .objectweb .asm .Opcodes .GETSTATIC ;
@@ -23,6 +24,59 @@ public Type type(PythonCompiler compiler) {
2324
2425 @ Override
2526 public void set (MethodVisitor mv , PythonCompiler compiler , PyExpr visit ) {
27+ if (type .getSort () == Type .OBJECT ) {
28+ visit .load (mv , compiler , visit .preload (mv , compiler , true ), true );
29+ compiler .writer .getContext ().pop ();
2630
31+ mv .visitFieldInsn (Opcodes .PUTFIELD , owner , name , type .getDescriptor ());
32+ } else {
33+ visit .load (mv , compiler , visit .preload (mv , compiler , false ), false );
34+ if (compiler .writer .getContext ().pop () != type ) {
35+ int sort = visit .type (compiler ).getSort ();
36+ if (sort == Type .OBJECT ) {
37+ throw new RuntimeException ("Cannot smart cast " + visit .type (compiler ) + " to " + type );
38+ } else if (sort == Type .ARRAY ) {
39+ throw new RuntimeException ("Cannot smart cast " + visit .type (compiler ) + " to " + type );
40+ } else if (sort == Type .VOID ) {
41+ throw new RuntimeException ("Cannot smart cast " + visit .type (compiler ) + " to " + type );
42+ } else if (sort == Type .LONG ) {
43+ if (type .getSort () == Type .DOUBLE ) {
44+ mv .visitInsn (Opcodes .D2L );
45+ } else if (type .getSort () == Type .INT ) {
46+ mv .visitInsn (Opcodes .L2I );
47+ } else {
48+ throw new RuntimeException ("Cannot smart cast " + visit .type (compiler ) + " to " + type );
49+ }
50+ } else if (sort == Type .DOUBLE ) {
51+ if (type .getSort () == Type .LONG ) {
52+ mv .visitInsn (Opcodes .L2D );
53+ } else if (type .getSort () == Type .INT ) {
54+ mv .visitInsn (Opcodes .I2D );
55+ } else {
56+ throw new RuntimeException ("Cannot smart cast " + visit .type (compiler ) + " to " + type );
57+ }
58+ } else if (sort == Type .INT ) {
59+ if (type .getSort () == Type .LONG ) {
60+ mv .visitInsn (Opcodes .I2L );
61+ } else if (type .getSort () == Type .DOUBLE ) {
62+ mv .visitInsn (Opcodes .I2D );
63+ } else {
64+ throw new RuntimeException ("Cannot smart cast " + visit .type (compiler ) + " to " + type );
65+ }
66+ } else if (sort == Type .FLOAT ) {
67+ if (type .getSort () == Type .DOUBLE ) {
68+ mv .visitInsn (Opcodes .F2D );
69+ } else if (type .getSort () == Type .INT ) {
70+ mv .visitInsn (Opcodes .F2I );
71+ } else {
72+ throw new RuntimeException ("Cannot smart cast " + visit .type (compiler ) + " to " + type );
73+ }
74+ } else {
75+ throw new RuntimeException ("Cannot smart cast " + visit .type (compiler ) + " to " + type );
76+ }
77+ }
78+
79+ mv .visitFieldInsn (Opcodes .PUTFIELD , owner , name , type .getDescriptor ());
80+ }
2781 }
2882}
0 commit comments