Esempio n. 1
0
        public BhpModule Convert(JavaModule _in)
        {
            this.srcModule = _in;
            //logger.Log("beginConvert.");
            this.outModule = new BhpModule(this.logger);
            foreach (var c in _in.classes.Values)
            {
                if (c.skip)
                {
                    continue;
                }
                foreach (var m in c.methods)
                {
                    if (m.Value.skip)
                    {
                        continue;
                    }
                    if (m.Key[0] == '<')
                    {
                        continue;                 //系統函數不要
                    }
                    BhpMethod nm = new BhpMethod();
                    nm.name                       = c.classfile.Name + "::" + m.Key;
                    nm.displayName                = m.Key;
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
            }

            foreach (var c in _in.classes.Values)
            {
                if (c.skip)
                {
                    continue;
                }
                foreach (var m in c.methods)
                {
                    if (m.Value.skip)
                    {
                        continue;
                    }
                    if (m.Key[0] == '<')
                    {
                        continue;                 //系統函數不要
                    }
                    var nm = this.methodLink[m.Value];
                    //try
                    {
                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                var name = key.Substring(key.IndexOf("::") + 2);
                var i    = name.IndexOf('(');
                name = name.Substring(0, i);

                if (name == ("Main"))
                {
                    var m = outModule.mapMethods[key];
                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key;
                            if (srcm.DeclaringType.superClass == "org.bhp.smartcontract.framework.SmartContract")
                            {
                                logger.Log("Find entrypoint:" + key);
                                if (mainmethod != "")
                                {
                                    throw new Exception("Have too mush EntryPoint,Check it.");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "")
            {
                throw new Exception("Can't find Main Method from SmartContract,Check it.");
            }
            outModule.mainMethod = mainmethod;
            //得找到第一个函数
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }
Esempio n. 2
0
        public JavaClass(JavaModule module, javaloader.ClassFile classfile, string[] srcfile = null)
        {
            this.module    = module;
            this.classfile = classfile;
            if (this.classfile.IsEnum)
            {
                this.IsEnum = true;
                foreach (var m in this.classfile.Methods)
                {
                    if (m.Name == javaloader.StringConstants.CLINIT)
                    {
                        _InitConsts(m.Instructions);
                    }
                }
            }
            this.srcfile = srcfile;
            if (this.srcfile == null)
            {
                this.srcfile = new string[0];
            }
            foreach (var f in this.classfile.Fields)
            {
                this.fields.Add(f.Name, f.Signature);
            }
            bool isKtObj = false;

            if (this.classfile.SourceFileAttribute.Contains(".kt"))
            {
                var sign = "L" + this.classfile.Name + ";";
                foreach (var f in this.classfile.Fields)
                {
                    if (f.Name == "INSTANCE" && f.IsStatic && f.Signature == sign)
                    {
                        isKtObj = true;
                        break;
                    }
                }
            }
            foreach (var m in this.classfile.Methods)
            {
                bool bskip = false;
                if (m.IsStatic == false && isKtObj == false)
                {
                    bskip = true;
                    //静态成员不要,除非是kotlin 的 object 对象,相当于静态
                }

                if (m.Annotations != null)
                {
                    object[] info = m.Annotations[0] as object[];
                    if (info[1] as string == "Lorg/bhp/smartcontract/framework/Appcall;" ||
                        info[1] as string == "Lorg/bhp/smartcontract/framework/Syscall;" ||
                        info[1] as string == "Lorg/bhp/smartcontract/framework/OpCode;" ||
                        info[1] as string == "Lorg/bhp/smartcontract/framework/Nonemit;")
                    {
                        //continue;
                        bskip = true;
                    }
                    //if(m.Annotations[0])
                }
                if (m.Name == "<init>")
                {
                    bskip = true;
                }
                var nm = new JavaMethod(this, m);
                nm.skip = bskip;
                //if (bskip == false && methods.ContainsKey(m.Name))
                //{
                //    throw new Exception("already have a func named:" + classfile.Name + "." + m.Name);
                //}
                this.methods[m.Name + m.Signature] = nm;
            }
            this.superClass = this.classfile.SuperClass;
        }