Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/testWasmIntegrated/testWasmIntegrated.ts @ 1049:348866dd217c
removing classes OpenGLContextLostException and StoneOrthancException
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 10 Oct 2019 14:29:02 +0200 |
parents | f185cfcb72a0 |
children |
rev | line source |
---|---|
499 | 1 var SendMessageToCpp: Function = null; |
2 export var TestWasmIntegratedModule : any; | |
3 | |
690 | 4 import * as TestStoneCodeGen from './build-wasm/TestStoneCodeGen_generated' |
5 | |
499 | 6 /* |
7 +--------------------------------------------------+ | |
8 | install emscripten handlers | | |
9 +--------------------------------------------------+ | |
10 */ | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
11 |
499 | 12 // (<any> window).Module = { |
13 // preRun: [ | |
14 // function() { | |
15 // console.log('Loading the Stone Framework using WebAssembly'); | |
16 // } | |
17 // ], | |
18 // postRun: [ | |
19 // function() { | |
20 // // This function is called by ".js" wrapper once the ".wasm" | |
21 // // WebAssembly module has been loaded and compiled by the | |
22 // // browser | |
23 // console.log('WebAssembly is ready'); | |
24 // // window.SendMessageToCpp = (<any> window).Module.cwrap('SendMessageToCpp', 'string', ['string']); | |
25 // // window.SendFreeTextToCpp = (<any> window).Module.cwrap('SendFreeTextToCpp', 'string', ['string']); | |
26 // } | |
27 // ], | |
28 // print: function(text : string) { | |
29 // console.log(text); | |
30 // }, | |
31 // printErr: function(text : string) { | |
32 // console.error(text); | |
33 // }, | |
34 // totalDependencies: 0 | |
35 // }; | |
36 | |
37 /* | |
38 +--------------------------------------------------+ | |
39 | install handlers | | |
40 +--------------------------------------------------+ | |
41 */ | |
498 | 42 document.querySelectorAll(".TestWasm-button").forEach((e) => { |
43 (e as HTMLButtonElement).addEventListener("click", () => { | |
44 ButtonClick(e.attributes["tool-selector"].value); | |
45 }); | |
46 }); | |
47 | |
499 | 48 /* |
49 +--------------------------------------------------+ | |
50 | define stock messages | | |
51 +--------------------------------------------------+ | |
52 */ | |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
53 let schemaText: string = null; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
54 fetch("testTestStoneCodeGen.yaml").then(function(res) {return res.text();}).then(function(text) {schemaText = text;}); |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
55 |
498 | 56 let stockSerializedMessages = new Map<string,string>(); |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
57 stockSerializedMessages["Test CppHandler message2"] = null; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
58 fetch("cppHandler_test_Message2.json").then(function(res) {return res.text();}).then(function(text) {stockSerializedMessages["Test CppHandler message2"] = text;}); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
59 |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
60 stockSerializedMessages["Test 2"] = ` { |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
61 "type" : "TestStoneCodeGen.Message1", |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
62 "value" : { |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
63 "memberInt32" : -987, |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
64 "memberString" : "Salomé", |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
65 "memberEnumMonth" : "March", |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
66 "memberBool" : true, |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
67 "memberFloat32" : 0.1, |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
68 "memberFloat64" : -0.2, |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
519
diff
changeset
|
69 "extraMember" : "don't care" |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
70 } |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
71 }`; |
498 | 72 stockSerializedMessages["Test 3"] = "Test 3 stock message sdfsfsdfsdf"; |
73 stockSerializedMessages["Test 4"] = "Test 4 stock message 355345345"; | |
74 stockSerializedMessages["Test 5"] = "Test 5 stock message 34535"; | |
75 stockSerializedMessages["Test 6"] = "Test 6 stock message xcvcxvx"; | |
76 stockSerializedMessages["Test 7"] = "Test 7 stock message fgwqewqdgg"; | |
77 stockSerializedMessages["Test 8"] = "Test 8 stock message fgfsdfsdgg"; | |
78 | |
499 | 79 /* |
80 +--------------------------------------------------+ | |
81 | define handler | | |
82 +--------------------------------------------------+ | |
83 */ | |
84 | |
85 function setSerializedInputValue(text: string) { | |
86 let e : HTMLTextAreaElement = document.getElementById('TestWasm-SerializedInput') as HTMLTextAreaElement; | |
87 e.value = text; | |
88 } | |
89 | |
90 function getSerializedInputValue(): string { | |
91 let e : HTMLTextAreaElement = document.getElementById('TestWasm-SerializedInput') as HTMLTextAreaElement; | |
92 return e.value; | |
93 } | |
94 | |
95 function setCppOutputValue(text: string) { | |
96 let e : HTMLTextAreaElement = document.getElementById('TestWasm-CppOutput') as HTMLTextAreaElement; | |
97 e.value = text; | |
98 } | |
99 | |
100 function getCppOutputValue(): string { | |
101 let e : HTMLTextAreaElement = document.getElementById('TestWasm-CppOutput') as HTMLTextAreaElement; | |
102 return e.value; | |
103 } | |
104 | |
105 function SendFreeTextFromCpp(txt: string):string | |
106 { | |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
107 setCppOutputValue(getCppOutputValue() + "\n" + txt); |
499 | 108 return ""; |
109 } | |
110 (<any> window).SendFreeTextFromCpp = SendFreeTextFromCpp; | |
111 | |
690 | 112 var referenceMessages = Array<any>(); |
113 | |
114 function testTsCppTs() { | |
115 var r = new TestStoneCodeGen.Message2(); | |
116 r.memberEnumMovieType = TestStoneCodeGen.MovieType.RomCom; | |
117 r.memberStringWithDefault = "overriden"; | |
118 r.memberMapEnumFloat[TestStoneCodeGen.CrispType.CreamAndChives] = 0.5; | |
119 r.memberString = "reference-messsage2-test1"; | |
120 | |
121 referenceMessages[r.memberString] = r; | |
122 var strMsg2 = r.StoneSerialize(); | |
123 let SendMessageToCppForEchoLocal = (<any> window).Module.cwrap('SendMessageToCppForEcho', 'string', ['string']); | |
124 SendMessageToCppForEchoLocal(strMsg2); | |
125 } | |
126 | |
127 class MyEchoHandler implements TestStoneCodeGen.IHandler | |
128 { | |
129 public HandleMessage2(value: TestStoneCodeGen.Message2): boolean | |
130 { | |
131 if (value.memberString in referenceMessages) { | |
132 let r = referenceMessages[value.memberString]; | |
133 let equals = (value.memberStringWithDefault == r.memberStringWithDefault); | |
134 if (TestStoneCodeGen.CrispType.CreamAndChives in r.memberMapEnumFloat) { | |
135 equals == equals && r.memberMapEnumFloat[TestStoneCodeGen.CrispType.CreamAndChives] == value.memberMapEnumFloat[TestStoneCodeGen.CrispType.CreamAndChives]; | |
136 } | |
137 // TODO continue comparison | |
138 | |
139 if (equals) { | |
140 console.log("objects are equals after round trip"); | |
141 return true; | |
142 } | |
143 } | |
144 console.log("problem after round trip"); | |
145 return true; | |
146 } | |
147 } | |
148 | |
149 function SendMessageFromCpp(txt: string):string | |
150 { | |
151 setCppOutputValue(getCppOutputValue() + "\n" + txt); | |
152 TestStoneCodeGen.StoneDispatchToHandler(txt, new MyEchoHandler()); | |
153 return ""; | |
154 } | |
155 (<any> window).SendMessageFromCpp = SendMessageFromCpp; | |
156 | |
157 | |
499 | 158 |
498 | 159 function ButtonClick(buttonName: string) { |
160 if (buttonName.startsWith('Test ')) { | |
499 | 161 setSerializedInputValue(stockSerializedMessages[buttonName]); |
498 | 162 } |
690 | 163 else if (buttonName == "Test-ts-cpp-ts") { |
164 testTsCppTs(); | |
165 } | |
498 | 166 else if(buttonName == 'Trigger') |
167 { | |
499 | 168 let serializedInputValue:string = getSerializedInputValue(); |
169 | |
170 let SendMessageToCppLocal = (<any> window).Module.cwrap('SendMessageToCpp', 'string', ['string']); | |
171 SendMessageToCppLocal(serializedInputValue); | |
498 | 172 } |
500
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
173 else if(buttonName == 'Clear') |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
174 { |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
175 setCppOutputValue(""); |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
176 } |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
177 else if(buttonName == 'ShowSchema') |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
178 { |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
179 setCppOutputValue(schemaText); |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
180 } |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
181 else |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
182 { |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
183 throw new Error("Internal error!"); |
329f229c2794
Improvements to the integrated demo for TS <--> C++
bgo-osimis
parents:
499
diff
changeset
|
184 } |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
185 } |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
186 |
498 | 187 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
188 |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
189 // 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
|
190 // 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
|
191 function UpdateWebApplicationWithString(statusUpdateMessageString: string) { |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
192 console.log("updating web application (string): ", statusUpdateMessageString); |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
193 let statusUpdateMessage = JSON.parse(statusUpdateMessageString); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
194 |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
195 if ("event" in statusUpdateMessage) |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
196 { |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
197 let eventName = statusUpdateMessage["event"]; |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
198 if (eventName == "appStatusUpdated") |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
199 { |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
200 //ui.onAppStatusUpdated(statusUpdateMessage["data"]); |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
201 } |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
202 } |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
diff
changeset
|
203 } |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
204 |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
205 |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
206 function UpdateWebApplicationWithSerializedMessage(statusUpdateMessageString: string) { |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
207 console.log("updating web application (serialized message): ", statusUpdateMessageString); |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
208 console.log("<not supported!>"); |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
209 } |
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
500
diff
changeset
|
210 |