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

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

Visual Studio 2019 と Bridge.NET でビルドできるよう更新。

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