package com.ninty.nativee.lang.invoke;

import com.ninty.nativee.INativeMethod;
import com.ninty.nativee.NaMethodManager;
import com.ninty.runtime.NiFrame;
import com.ninty.runtime.heap.NiClass;
import com.ninty.runtime.heap.NiObject;

public class NaMethodHandleNatives {
    private final static String className = "java/lang/invoke/MethodHandleNatives";

    public static void init() {
        NaMethodManager.register(className, "getConstant", "(I)I", new
                getConstant());
        NaMethodManager.register(className, "resolve", "(Ljava/lang/invoke/MemberName;Ljava/lang/Class;)Ljava/lang/invoke/MemberName;", new
                resolve());
    }

    public static class getConstant implements INativeMethod {

        @Override
        public void invoke(NiFrame frame) {
            int which = frame.getLocalVars().getInt(0);
            frame.getOperandStack().pushInt(which == 4 ? 1 : 0);
        }
    }

    public static class resolve implements INativeMethod {

        @Override
        public void invoke(NiFrame frame) {
            NiObject memberName = frame.getLocalVars().getRef(0);
            NiObject caller = frame.getLocalVars().getRef(1);
            NiClass callerClz = (NiClass) caller.getExtra();
            int flags = memberName.getFieldInt("flags");
            memberName.setFieldInt("flags", flags & callerClz.getAccessFlags());
            frame.getOperandStack().pushRef(memberName);
        }
    }
}

@@ -227,16 +227,56 @@
 iObject;
+%0Aimport com.ninty.runtime.heap.NiString;
 %0A%0Apublic
@@ -1215,16 +1215,54 @@
 Ref(1);%0A
+            if (caller != null) %7B%0A    
         
@@ -1314,16 +1314,20 @@
 xtra();%0A
+    
         
@@ -1375,16 +1375,124 @@
 lags%22);%0A
+                String name = NiString.getString(memberName.getFieldRef(%22name%22, %22Ljava/lang/String;%22));%0A    
         
@@ -1537,9 +1537,9 @@
 ags 
-&
+%7C
  cal
@@ -1545,16 +1545,39 @@
 llerClz.
+getMethod(name, %22()V%22).
 getAcces
@@ -1583,24 +1583,38 @@
 ssFlags());%0A
+            %7D%0A
             

return member if caller is null