source: EcnlProtoTool/trunk/webapp/webmrbc/Mruby.cs@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csharp
File size: 4.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Bridge;
5using Bridge.Html5;
6using Bridge.Text.RegularExpressions;
7
8namespace WebMrbc
9{
10 public class Mruby
11 {
12 public object[] preRun = new object[0];
13 public object[] postRun = new object[0];
14
15 public Mruby()
16 {
17 /*canvas = Document.GetElementById("canvas");
18
19 // As a default initial behavior, pop up an alert when webgl context is lost. To make your
20 // application robust, you may want to override this behavior before shipping!
21 // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
22 canvas.AddEventListener("webglcontextlost", new Action<Event>((Event e) => {
23 Window.Alert("WebGL context lost. You will need to reload the page."); e.PreventDefault();
24 }), false);*/
25 setStatus = new Action<string>(_setStatus);
26 }
27
28 public void print(object arg)
29 {
30 string text;
31 Array args;
32 if (arg.GetType() == typeof(string))
33 text = (string)arg;
34 else if (arg.GetType().Is<Array>() && (args = (Array)arg).Length > 1) {
35 var texts = new string[0];
36 foreach (var ele in args) {
37 texts.Push(ele.ToString());
38 }
39 text = texts.Join(" ");
40 }
41 else
42 text = arg.ToString();
43
44 App.WriteLine(text);
45 }
46
47 public void printErr(object arg)
48 {
49 string text;
50 Array args;
51 if (arg.GetType() == typeof(string))
52 text = (string)arg;
53 else if (arg.GetType().Is<Array>() && (args = (Array)arg).Length > 1) {
54 var texts = new string[0];
55 foreach (var ele in args) {
56 texts.Push(ele.ToString());
57 }
58 text = texts.Join(" ");
59 }
60 else
61 text = arg.ToString();
62
63 App.WriteLine(text);
64 }
65
66 internal void run(string[] args)
67 {
68 App.Write("$ " + args.Join(" ") + "\r\n");
69
70 var exe = (string)args.Shift();
71 this["arguments"] = args;
72
73 var onerror = Window.OnError;
74 Window.OnError = new ErrorEventHandler((string message, string url, int lineNumber, int columnNumber, object error) => {
75 var spinnerElement = Document.GetElementById("spinner");
76 // TODO: do not warn on ok events like simulating an infinite loop or exitStatus
77 setStatus("Exception thrown, see JavaScript console");
78 spinnerElement.Style.Display = Display.None;
79 setStatus = new Action<string>((text) => {
80 if (!string.IsNullOrEmpty(text))
81 printErr(new[] { "[post-exception status] " + text });
82 });
83 onerror(message, url, lineNumber, columnNumber, error);
84 return false;
85 });
86 try {
87 Script.Eval(exe + "(this)");
88 }
89 finally {
90 Window.OnError = onerror;
91 }
92 }
93
94 public HTMLElement canvas;
95 public Action<string> setStatus;
96
97 object _last;
98 string _text;
99
100 private void _setStatus(string text)
101 {
102 if (_last == null) _last = new { time = DateTime.Now.Ticks, text = "" };
103 if (text == _text) return;
104 _text = text;
105 var progressElement = (HTMLProgressElement)Document.GetElementById("progress");
106 var spinnerElement = (HTMLDivElement)Document.GetElementById("spinner");
107 var m = text.Match(new Regex(@"([^(]+)\((\d+(\.\d+)?)\/(\d+)\)"));
108 var now = DateTime.Now.Ticks;
109 if (m != null && now - DateTime.Now.Ticks < 30) return; // if this is a progress update, skip it if too soon
110 if (m != null) {
111 text = m[1];
112 progressElement.Value = Script.ParseInt(m[2]) * 100;
113 progressElement.Max = Script.ParseInt(m[4]) * 100;
114 progressElement.SetAttribute("hidden", "false");
115 spinnerElement.SetAttribute("hidden", "false");
116 }
117 else {
118 progressElement.Value = 0.0;
119 progressElement.Max = 0.0;
120 progressElement.SetAttribute("hidden", "true");
121 if (!string.IsNullOrEmpty(text))
122 spinnerElement.Style.Display = Display.None;
123 }
124 var statusElement = (HTMLDivElement)Document.GetElementById("status");
125 statusElement.InnerHTML = text;
126 }
127
128 public int totalDependencies = 0;
129
130 public void monitorRunDependencies(int left)
131 {
132 totalDependencies = System.Math.Max(totalDependencies, left);
133 setStatus((left != 0) ? "Preparing... (" + (totalDependencies - left) + "/" + totalDependencies + ")" : "All downloads complete.");
134 }
135
136 [External, Name(false)]
137 internal string UTF8ArrayToString(Uint8Array buf, int index)
138 {
139 throw new NotImplementedException();
140 }
141
142 [External]
143 internal string intArrayToString(Uint8Array buf)
144 {
145 throw new NotImplementedException();
146 }
147
148 [External]
149 internal int stringToUTF8Array(string str, Uint8Array outU8Array, int outIdx, int maxBytesToWrite)
150 {
151 throw new NotImplementedException();
152 }
153
154 [External]
155 internal int lengthBytesUTF8(string str)
156 {
157 throw new NotImplementedException();
158 }
159
160 [Name(false)]
161 internal Uint8Array UTF8StringToArray(string str)
162 {
163 var len = lengthBytesUTF8(str);
164 var result = new Uint8Array(len);
165 stringToUTF8Array(str, result, 0, len);
166 return result;
167 }
168
169 [Script("return this.FS;")]
170 internal FileSystem GetFileSystem()
171 {
172 return (FileSystem)this["FS"];
173 }
174
175 [External]
176 internal void stdin(object data)
177 {
178 throw new NotImplementedException();
179 }
180 }
181}
Note: See TracBrowser for help on using the repository browser.