Ejemplo n.º 1
0
        public static byte[] Convert(string classfilename, ILogger logger = null)
        {
            var moduleJVMPackage = new JavaModule();

            moduleJVMPackage.LoadClass("go.class");
            moduleJVMPackage.LoadJar("AntShares.SmartContract.Framework.jar");

            var converter = new ModuleConverter(logger);
            //有异常的话在 convert 函数中会直接throw 出来
            var antmodule = converter.Convert(moduleJVMPackage);

            return(antmodule.Build());
        }
Ejemplo 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_" + num.ToString("X08")))
                    {
                        num++;
                    }
                    dictname = "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 AntShares.Compiler.JVM.ModuleConverter(logjson);
                    var bs      = conv.Convert(module).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());

                        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;
                }
            }
        }