comparison quick-test.js @ 43:90bf9d9f9c95

Rearrange and rename VamPipe -> Piper as appropriate
author Chris Cannam
date Mon, 10 Oct 2016 17:05:37 +0100
parents a734a7e976fa
children
comparison
equal deleted inserted replaced
42:873ce3bfa776 43:90bf9d9f9c95
4 4
5 // It is possible to declare both parameters and return values as 5 // It is possible to declare both parameters and return values as
6 // "string", in which case Emscripten will take care of 6 // "string", in which case Emscripten will take care of
7 // conversions. But it's not clear how one would manage memory for 7 // conversions. But it's not clear how one would manage memory for
8 // newly-constructed returned C strings -- the returned pointer from 8 // newly-constructed returned C strings -- the returned pointer from
9 // vampipeRequestJson would appear (?) to be thrown away by the 9 // piperRequestJson would appear (?) to be thrown away by the
10 // Emscripten string converter if we declare it as returning a string, 10 // Emscripten string converter if we declare it as returning a string,
11 // so we have no opportunity to pass it to vampipeFreeJson, which 11 // so we have no opportunity to pass it to piperFreeJson, which
12 // suggests this would leak memory if the string isn't static. Not 12 // suggests this would leak memory if the string isn't static. Not
13 // wholly sure though. Anyway, passing and returning pointers (as 13 // wholly sure though. Anyway, passing and returning pointers (as
14 // numbers) means we can manage the Emscripten heap memory however we 14 // numbers) means we can manage the Emscripten heap memory however we
15 // want in our request wrapper function below. 15 // want in our request wrapper function below.
16 16
17 var vampipeRequestJson = exampleModule.cwrap( 17 var piperRequestJson = exampleModule.cwrap(
18 'vampipeRequestJson', 'number', ['number'] 18 'piperRequestJson', 'number', ['number']
19 ); 19 );
20 20
21 var vampipeFreeJson = exampleModule.cwrap( 21 var piperFreeJson = exampleModule.cwrap(
22 'vampipeFreeJson', 'void', ['number'] 22 'piperFreeJson', 'void', ['number']
23 ); 23 );
24 24
25 function note(blah) { 25 function note(blah) {
26 document.getElementById("test-result").innerHTML += blah + "<br>"; 26 document.getElementById("test-result").innerHTML += blah + "<br>";
27 } 27 }
36 // Inspection reveals that intArrayFromString converts the string 36 // Inspection reveals that intArrayFromString converts the string
37 // from utf16 to utf8, which is what we want (though the docs 37 // from utf16 to utf8, which is what we want (though the docs
38 // don't mention this). Note the *Cstr values are Emscripten heap 38 // don't mention this). Note the *Cstr values are Emscripten heap
39 // pointers 39 // pointers
40 var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL); 40 var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL);
41 var outCstr = vampipeRequestJson(inCstr); 41 var outCstr = piperRequestJson(inCstr);
42 m._free(inCstr); 42 m._free(inCstr);
43 var result = m.Pointer_stringify(outCstr); 43 var result = m.Pointer_stringify(outCstr);
44 vampipeFreeJson(outCstr); 44 piperFreeJson(outCstr);
45 note("Returned JSON = " + result); 45 note("Returned JSON = " + result);
46 return result; 46 return result;
47 } 47 }
48 48
49 function test() { 49 function test() {