Esempio n. 1
0
        //Console.WriteLine("helo ha:"+args[0]); //普通输出
        //Console.WriteLine("<WARN> 这是一个严重的问题。");//警告输出,黄字
        //Console.WriteLine("<WARN|aaaa.cs(1)> 这是ee一个严重的问题。");//警告输出,带文件名行号
        //Console.WriteLine("<ERR> 这是一个严重的问题。");//错误输出,红字
        //Console.WriteLine("<ERR|aaaa.cs> 这是ee一个严重的问题。");//错误输出,带文件名
        //Console.WriteLine("SUCC");//输出这个表示编译成功
        //控制台输出约定了特别的语法
        public static void Main(string[] args)
        {
            //set console
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            var log = new DefLogger();

            log.Log("Neo.Compiler.JVM console app v" + Assembly.GetEntryAssembly().GetName().Version);
            if (args.Length == 0)
            {
                log.Log("need one param for .class filename.");
                return;
            }
            string filename = args[0];
            string onlyname = System.IO.Path.GetFileNameWithoutExtension(filename);
            //javaloader.ClassFile classFile = null;
            JavaModule module = new JavaModule();


            byte[] bytes = null;
            bool   bSucc = false;

            //convert and build
            try
            {
                var path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                path = System.IO.Path.GetDirectoryName(path);
                module.LoadJar(System.IO.Path.Combine(path, "org.neo.smartcontract.framework.jar"));
                module.LoadClass(filename);
                var conv = new ModuleConverter(log);

                AntsModule am = conv.Convert(module);
                bytes = am.Build();
                log.Log("convert succ");
            }
            catch (Exception err)
            {
                log.Log("Convert Error:" + err.ToString());
                return;
            }
            //write bytes
            try
            {
                string bytesname = onlyname + ".avm";

                System.IO.File.Delete(bytesname);
                System.IO.File.WriteAllBytes(bytesname, bytes);
                log.Log("write:" + bytesname);
                bSucc = true;
            }
            catch (Exception err)
            {
                log.Log("Write Bytes Error:" + err.ToString());
                return;
            }


            if (bSucc)
            {
                log.Log("SUCC");
            }
        }
Esempio n. 2
0
        private static async Task parseJAVA(IOwinContext context, FormData formdata)
        {
            try
            {
                string dictname  = null;
                string classname = null;
                var    file      = formdata.mapFiles["file"];
                var    code      = System.Text.Encoding.UTF8.GetString(file);

                //准备临时目录
                {
                    Random i   = new Random();
                    var    num = i.Next();

                    while (System.IO.Directory.Exists("tmp\\tmp_" + num.ToString("X08")))
                    {
                        num++;
                    }
                    dictname = "tmp\\tmp_" + num.ToString("X08");

                    var fc     = code.IndexOf("class");
                    int ibegin = -1;

                    for (int ib = fc + 6; ib < fc + 100; ib++)
                    {
                        if (ibegin < 0)
                        {
                            if (code[ib] == ' ')
                            {
                                continue;
                            }
                            else
                            {
                                ibegin = ib;
                            }
                        }
                        else
                        {
                            if (code[ib] == ' ' || code[ib] == '}')
                            {
                                classname = code.Substring(ibegin, ib - ibegin);
                                break;
                            }
                        }
                    }
                }
                System.IO.Directory.CreateDirectory(dictname);
                string filename = System.IO.Path.Combine(dictname, classname + ".java");
                System.IO.File.WriteAllText(filename, code);
                string jarfile = "AntShares.SmartContract.Framework.jar";
                System.IO.File.Copy(jarfile, System.IO.Path.Combine(dictname, jarfile));

                //编译
                try
                {
                    System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
                    info.FileName               = "cmd.exe";
                    info.UseShellExecute        = false;
                    info.RedirectStandardOutput = true;
                    info.RedirectStandardInput  = true;
                    info.RedirectStandardError  = true;
                    info.WorkingDirectory       = dictname;
                    var proc = System.Diagnostics.Process.Start(info);
                    proc.StandardInput.WriteLine("javac -cp " + jarfile + " " + classname + ".java");
                    proc.StandardInput.WriteLine("exit");


                    string        back      = proc.StandardError.ReadToEnd();
                    string        inerror   = "";
                    int           line      = -1;
                    string        tag       = "";
                    List <string> outline   = new List <string>();
                    List <int>    errorline = new List <int>();
                    List <string> errorTag  = new List <string>();
                    string[]      lines     = back.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                    for (var i = 0; i < lines.Length; i++)
                    {
                        if (inerror == "")
                        {
                            var mm = lines[i].Split(':');
                            if (mm.Length > 3)
                            {
                                line     = int.Parse(mm[1]);
                                inerror += mm[3];
                                tag      = mm[2];
                            }
                        }
                        else
                        {
                            if (lines[i].IndexOf("^") >= 0)
                            {
                                outline.Add(inerror);
                                errorline.Add(line);
                                errorTag.Add(tag);
                                inerror = "";
                            }
                            else
                            {
                                inerror += "\n" + lines[i];
                            }
                        }
                    }

                    if (outline.Count == 0)
                    {
                        //succ
                    }
                    else
                    {
                        MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                        json["tag"] = new MyJson.JsonNode_ValueNumber(-3);
                        json["msg"] = new MyJson.JsonNode_ValueString("compile fail.");

                        MyJson.JsonNode_Array errs = new MyJson.JsonNode_Array();
                        json["errors"] = errs;
                        for (var i = 0; i < outline.Count; i++)
                        {
                            MyJson.JsonNode_Object errtag = new MyJson.JsonNode_Object();
                            errs.Add(errtag);
                            errtag.SetDictValue("msg", outline[i]);
                            errtag.SetDictValue("line", errorline[i]);
                            errtag.SetDictValue("tag", errorTag[i]);
                            //errtag.SetDictValue("col", r.Errors[i].Column);
                        }
                        await context.Response.WriteAsync(json.ToString());

                        return;
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on comp.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }

                //conv
                try
                {
                    JavaModule module = new JavaModule();
                    module.LoadJar(jarfile);
                    module.LoadClass(System.IO.Path.Combine(dictname, classname + ".class"));
                    var logjson = new Log2Json();
                    var conv    = new Neo.Compiler.JVM.ModuleConverter(logjson);
                    var neomd   = conv.Convert(module);
                    var mm      = neomd.mapMethods[neomd.mainMethod];

                    var bs = neomd.Build();
                    if (bs != null)
                    {
                        MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                        json["tag"] = new MyJson.JsonNode_ValueNumber(0);
                        StringBuilder sb     = new StringBuilder();
                        StringBuilder sb2    = new StringBuilder();
                        var           hash   = System.Security.Cryptography.SHA256.Create();
                        var           hashbs = hash.ComputeHash(bs);
                        foreach (var b in bs)
                        {
                            sb.Append(b.ToString("X02"));
                        }
                        foreach (var b in hashbs)
                        {
                            sb2.Append(b.ToString("X02"));
                        }
                        json["hex"]  = new MyJson.JsonNode_ValueString(sb.ToString());
                        json["hash"] = new MyJson.JsonNode_ValueString(sb2.ToString());

                        json["returntype"] = new MyJson.JsonNode_ValueString(mm.returntype);
                        MyJson.JsonNode_Array funcparams = new MyJson.JsonNode_Array();
                        json["params"] = funcparams;
                        if (mm.paramtypes != null)
                        {
                            foreach (var v in mm.paramtypes)
                            {
                                funcparams.Add(new MyJson.JsonNode_ValueString(v.type));
                            }
                        }

                        await context.Response.WriteAsync(json.ToString());

                        return;
                    }


                    else
                    {
                        {
                            MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                            json["tag"]  = new MyJson.JsonNode_ValueNumber(-4);
                            json["msg"]  = new MyJson.JsonNode_ValueString("compile fail.");
                            json["info"] = logjson.array;
                            await context.Response.WriteAsync(json.ToString());

                            return;
                        }
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on conv.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
            catch
            {
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("parse fail.");
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
        }