Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.ts @ 724:5b16242cdc93
Fixed truncation warning
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 21 May 2019 10:39:14 +0200 |
parents | 17106b29ed6d |
children | 342f3e04bfa9 |
rev | line source |
---|---|
499 | 1 var SendMessageToCpp: Function = null; |
2 export var TestWasmIntegratedModule : any; | |
3 | |
4 /* | |
5 +--------------------------------------------------+ | |
6 | install emscripten handlers | | |
7 +--------------------------------------------------+ | |
8 */ | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
9 |
499 | 10 // (<any> window).Module = { |
11 // preRun: [ | |
12 // function() { | |
13 // console.log('Loading the Stone Framework using WebAssembly'); | |
14 // } | |
15 // ], | |
16 // postRun: [ | |
17 // function() { | |
18 // // This function is called by ".js" wrapper once the ".wasm" | |
19 // // WebAssembly module has been loaded and compiled by the | |
20 // // browser | |
21 // console.log('WebAssembly is ready'); | |
22 // // window.SendMessageToCpp = (<any> window).Module.cwrap('SendMessageToCpp', 'string', ['string']); | |
23 // // window.SendFreeTextToCpp = (<any> window).Module.cwrap('SendFreeTextToCpp', 'string', ['string']); | |
24 // } | |
25 // ], | |
26 // print: function(text : string) { | |
27 // console.log(text); | |
28 // }, | |
29 // printErr: function(text : string) { | |
30 // console.error(text); | |
31 // }, | |
32 // totalDependencies: 0 | |
33 // }; | |
34 | |
35 /* | |
36 +--------------------------------------------------+ | |
37 | install handlers | | |
38 +--------------------------------------------------+ | |
39 */ | |
498 | 40 document.querySelectorAll(".TestWasm-button").forEach((e) => { |
41 (e as HTMLButtonElement).addEventListener("click", () => { | |
42 ButtonClick(e.attributes["tool-selector"].value); | |
43 }); | |
44 }); | |
45 | |
499 | 46 /* |
47 +--------------------------------------------------+ | |
48 | define stock messages | | |
49 +--------------------------------------------------+ | |
50 */ | |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
51 let schemaText: string = `rootName: testWasmIntegratedCpp |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
52 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
53 struct B: |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
54 someAs: vector<A> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
55 someInts: vector<int32> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
56 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
57 struct C: |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
58 someBs: vector<B> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
59 ddd: vector<string> |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
60 definition: vector<json> |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
61 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
62 struct A: |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
63 someStrings: vector<string> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
64 someInts2: vector<int32> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
65 movies: vector<MovieType> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
66 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
67 struct Message1: |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
68 a: int32 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
69 b: string |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
70 c: EnumMonth0 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
71 d: bool |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
72 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
73 struct Message2: |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
74 toto: string |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
75 tata: vector<Message1> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
76 tutu: vector<string> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
77 titi: map<string, string> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
78 lulu: map<string, Message1> |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
79 movieType: MovieType |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
80 definition: json |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
81 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
82 enum MovieType: |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
83 - RomCom |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
84 - Horror |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
85 - ScienceFiction |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
86 - Vegetables |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
87 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
88 enum CrispType: |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
89 - SaltAndPepper |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
90 - CreamAndChives |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
91 - Paprika |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
92 - Barbecue |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
93 |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
94 enum EnumMonth0: |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
95 - January |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
96 - February |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
97 - March |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
98 `; |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
99 |
498 | 100 let stockSerializedMessages = new Map<string,string>(); |
499 | 101 stockSerializedMessages["Test 1"] = `{ |
102 "type" : "testWasmIntegratedCpp.Message2", | |
103 "value" : | |
104 { | |
105 "lulu" : | |
106 { | |
107 "54" : | |
108 { | |
109 "a" : 43, | |
110 "b" : "Sandrine", | |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
111 "c" : "March", |
499 | 112 "d" : true |
113 }, | |
114 "55" : | |
115 { | |
116 "a" : 42, | |
117 "b" : "Benjamin", | |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
118 "c" : "January", |
499 | 119 "d" : false |
120 } | |
121 }, | |
122 "tata" : | |
123 [ | |
124 { | |
125 "a" : 42, | |
126 "b" : "Benjamin", | |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
127 "c" : "March", |
499 | 128 "d" : false |
129 }, | |
130 { | |
131 "a" : 43, | |
132 "b" : "Sandrine", | |
519
17106b29ed6d
Changed the metadata system for structs. A __handler entry is now required
Benjamin Golinvaux <bgo@osimis.io>
parents:
508
diff
changeset
|
133 "c" : "January", |
499 | 134 "d" : false |
135 } | |
136 ], | |
137 "titi" : | |
138 { | |
139 "44" : "key 44", | |
140 "45" : "key 45" | |
141 }, | |
142 "toto" : "Prout zizi", | |
143 "tutu" : | |
144 [ | |
145 "Mercadet", | |
146 "Poisson" | |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
147 ], |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
148 "definition": |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
149 { |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
150 "val" : [ "berk", 42 ], |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
151 "zozo" : |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
152 { |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
153 "23": "zloutch", |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
154 "lalala": 42 |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
155 } |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
156 } |
499 | 157 } |
158 }`; | |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
159 stockSerializedMessages["Test 2"] = ` { |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
160 "type" : "testWasmIntegratedCpp.Message1", |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
161 "value" : { |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
162 "a" : -987, |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
163 "b" : "Salomé", |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
164 "c" : 2, |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
165 "d" : true |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
166 } |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
167 }`; |
498 | 168 stockSerializedMessages["Test 3"] = "Test 3 stock message sdfsfsdfsdf"; |
169 stockSerializedMessages["Test 4"] = "Test 4 stock message 355345345"; | |
170 stockSerializedMessages["Test 5"] = "Test 5 stock message 34535"; | |
171 stockSerializedMessages["Test 6"] = "Test 6 stock message xcvcxvx"; | |
172 stockSerializedMessages["Test 7"] = "Test 7 stock message fgwqewqdgg"; | |
173 stockSerializedMessages["Test 8"] = "Test 8 stock message fgfsdfsdgg"; | |
174 | |
499 | 175 /* |
176 +--------------------------------------------------+ | |
177 | define handler | | |
178 +--------------------------------------------------+ | |
179 */ | |
180 | |
181 function setSerializedInputValue(text: string) { | |
182 let e : HTMLTextAreaElement = document.getElementById('TestWasm-SerializedInput') as HTMLTextAreaElement; | |
183 e.value = text; | |
184 } | |
185 | |
186 function getSerializedInputValue(): string { | |
187 let e : HTMLTextAreaElement = document.getElementById('TestWasm-SerializedInput') as HTMLTextAreaElement; | |
188 return e.value; | |
189 } | |
190 | |
191 function setCppOutputValue(text: string) { | |
192 let e : HTMLTextAreaElement = document.getElementById('TestWasm-CppOutput') as HTMLTextAreaElement; | |
193 e.value = text; | |
194 } | |
195 | |
196 function getCppOutputValue(): string { | |
197 let e : HTMLTextAreaElement = document.getElementById('TestWasm-CppOutput') as HTMLTextAreaElement; | |
198 return e.value; | |
199 } | |
200 | |
201 function SendFreeTextFromCpp(txt: string):string | |
202 { | |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
203 setCppOutputValue(getCppOutputValue() + "\n" + txt); |
499 | 204 return ""; |
205 } | |
206 (<any> window).SendFreeTextFromCpp = SendFreeTextFromCpp; | |
207 | |
208 | |
498 | 209 function ButtonClick(buttonName: string) { |
210 if (buttonName.startsWith('Test ')) { | |
499 | 211 setSerializedInputValue(stockSerializedMessages[buttonName]); |
498 | 212 } |
213 else if(buttonName == 'Trigger') | |
214 { | |
499 | 215 let serializedInputValue:string = getSerializedInputValue(); |
216 | |
217 let SendMessageToCppLocal = (<any> window).Module.cwrap('SendMessageToCpp', 'string', ['string']); | |
218 SendMessageToCppLocal(serializedInputValue); | |
498 | 219 } |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
220 else if(buttonName == 'Clear') |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
221 { |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
222 setCppOutputValue(""); |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
223 } |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
224 else if(buttonName == 'ShowSchema') |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
225 { |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
226 setCppOutputValue(schemaText); |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
227 } |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
228 else |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
229 { |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
230 throw new Error("Internal error!"); |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
231 } |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
232 } |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
233 |
498 | 234 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
235 |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
236 // this method is called "from the C++ code" when the StoneApplication is updated. |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
237 // it can be used to update the UI of the application |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
238 function UpdateWebApplicationWithString(statusUpdateMessageString: string) { |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
239 console.log("updating web application (string): ", statusUpdateMessageString); |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
240 let statusUpdateMessage = JSON.parse(statusUpdateMessageString); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
241 |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
242 if ("event" in statusUpdateMessage) |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
243 { |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
244 let eventName = statusUpdateMessage["event"]; |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
245 if (eventName == "appStatusUpdated") |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
246 { |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
247 //ui.onAppStatusUpdated(statusUpdateMessage["data"]); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
248 } |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
249 } |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
250 } |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
251 |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
252 |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
253 function UpdateWebApplicationWithSerializedMessage(statusUpdateMessageString: string) { |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
254 console.log("updating web application (serialized message): ", statusUpdateMessageString); |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
255 console.log("<not supported!>"); |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
256 } |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
257 |