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

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

ファイルヘッダーコメントを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csharp
File size: 7.3 KB
Line 
1/*
2 * TOPPERS/ECNL Prototyping tool
3 *
4 * Copyright (C) 2017 Cores Co., Ltd. Japan
5 *
6 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
7 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
8 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
9 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
10 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
11 * スコード中に含まれていること.
12 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
13 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
14 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
15 * の無保証規定を掲載すること.
16 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
17 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
18 * と.
19 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
20 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
21 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
22 * 報告すること.
23 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
24 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
25 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
26 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
27 * 免責すること.
28 *
29 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
30 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
31 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
32 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
33 * の責任を負わない.
34 *
35 * @(#) $Id: Mruby.cs 287 2017-05-05 14:22:23Z coas-nagasima $
36 */
37using System;
38using System.Collections.Generic;
39using System.Linq;
40using Bridge;
41using Bridge.Html5;
42using Bridge.Text.RegularExpressions;
43
44namespace WebMrbc
45{
46 public class Mruby
47 {
48 public object[] preRun = new object[0];
49 public object[] postRun = new object[0];
50
51 public Mruby()
52 {
53 /*canvas = Document.GetElementById("canvas");
54
55 // As a default initial behavior, pop up an alert when webgl context is lost. To make your
56 // application robust, you may want to override this behavior before shipping!
57 // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
58 canvas.AddEventListener("webglcontextlost", new Action<Event>((Event e) => {
59 Window.Alert("WebGL context lost. You will need to reload the page."); e.PreventDefault();
60 }), false);*/
61 setStatus = new Action<string>(_setStatus);
62 }
63
64 public void print(object arg)
65 {
66 string text;
67 Array args;
68 if (arg.GetType() == typeof(string))
69 text = (string)arg;
70 else if (arg.GetType().Is<Array>() && (args = (Array)arg).Length > 1) {
71 var texts = new string[0];
72 foreach (var ele in args) {
73 texts.Push(ele.ToString());
74 }
75 text = texts.Join(" ");
76 }
77 else
78 text = arg.ToString();
79
80 App.WriteLine(text);
81 }
82
83 public void printErr(object arg)
84 {
85 string text;
86 Array args;
87 if (arg.GetType() == typeof(string))
88 text = (string)arg;
89 else if (arg.GetType().Is<Array>() && (args = (Array)arg).Length > 1) {
90 var texts = new string[0];
91 foreach (var ele in args) {
92 texts.Push(ele.ToString());
93 }
94 text = texts.Join(" ");
95 }
96 else
97 text = arg.ToString();
98
99 App.WriteLine(text);
100 }
101
102 internal void run(string[] args)
103 {
104 App.Write("$ " + args.Join(" ") + "\r\n");
105
106 var exe = (string)args.Shift();
107 this["arguments"] = args;
108
109 var onerror = Window.OnError;
110 Window.OnError = new ErrorEventHandler((string message, string url, int lineNumber, int columnNumber, object error) => {
111 var spinnerElement = Document.GetElementById("spinner");
112 // TODO: do not warn on ok events like simulating an infinite loop or exitStatus
113 setStatus("Exception thrown, see JavaScript console");
114 spinnerElement.Style.Display = Display.None;
115 setStatus = new Action<string>((text) => {
116 if (!string.IsNullOrEmpty(text))
117 printErr(new[] { "[post-exception status] " + text });
118 });
119 onerror(message, url, lineNumber, columnNumber, error);
120 return false;
121 });
122 try {
123 Script.Eval(exe + "(this)");
124 }
125 finally {
126 Window.OnError = onerror;
127 }
128 }
129
130 public HTMLElement canvas;
131 public Action<string> setStatus;
132
133 object _last;
134 string _text;
135
136 private void _setStatus(string text)
137 {
138 if (_last == null) _last = new { time = DateTime.Now.Ticks, text = "" };
139 if (text == _text) return;
140 _text = text;
141 var progressElement = (HTMLProgressElement)Document.GetElementById("progress");
142 var spinnerElement = (HTMLDivElement)Document.GetElementById("spinner");
143 var m = text.Match(new Regex(@"([^(]+)\((\d+(\.\d+)?)\/(\d+)\)"));
144 var now = DateTime.Now.Ticks;
145 if (m != null && now - DateTime.Now.Ticks < 30) return; // if this is a progress update, skip it if too soon
146 if (m != null) {
147 text = m[1];
148 progressElement.Value = Script.ParseInt(m[2]) * 100;
149 progressElement.Max = Script.ParseInt(m[4]) * 100;
150 progressElement.SetAttribute("hidden", "false");
151 spinnerElement.SetAttribute("hidden", "false");
152 }
153 else {
154 progressElement.Value = 0.0;
155 progressElement.Max = 0.0;
156 progressElement.SetAttribute("hidden", "true");
157 if (!string.IsNullOrEmpty(text))
158 spinnerElement.Style.Display = Display.None;
159 }
160 var statusElement = (HTMLDivElement)Document.GetElementById("status");
161 statusElement.InnerHTML = text;
162 }
163
164 public int totalDependencies = 0;
165
166 public void monitorRunDependencies(int left)
167 {
168 totalDependencies = System.Math.Max(totalDependencies, left);
169 setStatus((left != 0) ? "Preparing... (" + (totalDependencies - left) + "/" + totalDependencies + ")" : "All downloads complete.");
170 }
171
172 [External, Name(false)]
173 internal string UTF8ArrayToString(Uint8Array buf, int index)
174 {
175 throw new NotImplementedException();
176 }
177
178 [External]
179 internal string intArrayToString(Uint8Array buf)
180 {
181 throw new NotImplementedException();
182 }
183
184 [External]
185 internal int stringToUTF8Array(string str, Uint8Array outU8Array, int outIdx, int maxBytesToWrite)
186 {
187 throw new NotImplementedException();
188 }
189
190 [External]
191 internal int lengthBytesUTF8(string str)
192 {
193 throw new NotImplementedException();
194 }
195
196 [Name(false)]
197 internal Uint8Array UTF8StringToArray(string str)
198 {
199 var len = lengthBytesUTF8(str);
200 var result = new Uint8Array(len);
201 stringToUTF8Array(str, result, 0, len);
202 return result;
203 }
204
205 [Script("return this.FS;")]
206 internal FileSystem GetFileSystem()
207 {
208 return (FileSystem)this["FS"];
209 }
210
211 [External]
212 internal void stdin(object data)
213 {
214 throw new NotImplementedException();
215 }
216 }
217}
Note: See TracBrowser for help on using the repository browser.