@@ -12,15 +12,30 @@ export function TextToWords(s) {
1212
1313 var len = Module [ "lengthBytesUTF8" ] ( s ) ;
1414
15- var inUtf8 = Module [ "stackAlloc " ] ( len + 1 ) ; // if we don't do +1 this library won't copy the last character
15+ var inUtf8 = Module [ "_malloc " ] ( len + 1 ) ; // if we don't do +1 this library won't copy the last character
1616 Module [ "stringToUTF8" ] ( s , inUtf8 , len + 1 ) ; // since it always also needs a space for a 0-char
1717
1818 var MaxOutLength = ( len << 1 ) + 1 ; // worst case every character is a token
19- var outUtf8 = Module [ "stackAlloc " ] ( MaxOutLength ) ;
19+ var outUtf8 = Module [ "_malloc " ] ( MaxOutLength ) ;
2020
21- var actualLen = Module [ "_TextToWords" ] ( inUtf8 , len , outUtf8 , MaxOutLength ) ;
22- if ( 0 > actualLen || actualLen > MaxOutLength ) {
23- return null ;
21+ try
22+ {
23+ var actualLen = Module [ "_TextToWords" ] ( inUtf8 , len , outUtf8 , MaxOutLength ) ;
24+ if ( 0 > actualLen || actualLen > MaxOutLength ) {
25+ return null ;
26+ }
27+ }
28+ finally
29+ {
30+ if ( inUtf8 != 0 )
31+ {
32+ Module [ "_free" ] ( inUtf8 ) ;
33+ }
34+
35+ if ( outUtf8 != 0 )
36+ {
37+ Module [ "_free" ] ( outUtf8 ) ;
38+ }
2439 }
2540
2641 return Module [ "UTF8ToString" ] ( outUtf8 ) ;
@@ -31,15 +46,30 @@ export function TextToSentences(s) {
3146
3247 var len = Module [ "lengthBytesUTF8" ] ( s ) ;
3348
34- var inUtf8 = Module [ "stackAlloc " ] ( len + 1 ) ; // if we don't do +1 this library won't copy the last character
49+ var inUtf8 = Module [ "_malloc " ] ( len + 1 ) ; // if we don't do +1 this library won't copy the last character
3550 Module [ "stringToUTF8" ] ( s , inUtf8 , len + 1 ) ; // since it always also needs a space for a 0-char
3651
3752 var MaxOutLength = ( len << 1 ) + 1 ; // worst case every character is a token
38- var outUtf8 = Module [ "stackAlloc " ] ( MaxOutLength ) ;
53+ var outUtf8 = Module [ "_malloc " ] ( MaxOutLength ) ;
3954
40- var actualLen = Module [ "_TextToSentences" ] ( inUtf8 , len , outUtf8 , MaxOutLength ) ;
41- if ( 0 > actualLen || actualLen > MaxOutLength ) {
42- return null ;
55+ try
56+ {
57+ var actualLen = Module [ "_TextToSentences" ] ( inUtf8 , len , outUtf8 , MaxOutLength ) ;
58+ if ( 0 > actualLen || actualLen > MaxOutLength ) {
59+ return null ;
60+ }
61+ }
62+ finally
63+ {
64+ if ( inUtf8 != 0 )
65+ {
66+ Module [ "_free" ] ( inUtf8 ) ;
67+ }
68+
69+ if ( outUtf8 != 0 )
70+ {
71+ Module [ "_free" ] ( outUtf8 ) ;
72+ }
4373 }
4474
4575 return Module [ "UTF8ToString" ] ( outUtf8 ) ;
@@ -108,33 +138,48 @@ export function TextToIds(handle, s, max_len, unk = 0) {
108138
109139 // convert input JS string to UTF-8
110140 var len = Module [ "lengthBytesUTF8" ] ( s ) ;
111- var inUtf8 = Module [ "stackAlloc " ] ( len + 1 ) ; // if we don't do +1 this library won't copy the last character
141+ var inUtf8 = Module [ "_malloc " ] ( len + 1 ) ; // if we don't do +1 this library won't copy the last character
112142 Module [ "stringToUTF8" ] ( s , inUtf8 , len + 1 ) ; // since it always also needs a space for a 0-char
113143
114144 // allocate space for ids on stack (it is faster and we don't need to delete it manualy)
115145 var MaxOutLength = max_len ;
116- var IdsOut = Module [ "stackAlloc " ] ( MaxOutLength * 4 ) ; // sizeof(int)
146+ var IdsOut = Module [ "_malloc " ] ( MaxOutLength * 4 ) ; // sizeof(int)
117147
118- // get the IDS from BlingFire
119- var actualLen = Module [ "_TextToIds" ] ( h , inUtf8 , len , IdsOut , MaxOutLength , unk ) ;
120- if ( 0 >= actualLen ) {
121- return null ;
122- }
148+ try
149+ {
150+ // get the IDS from BlingFire
151+ var actualLen = Module [ "_TextToIds" ] ( h , inUtf8 , len , IdsOut , MaxOutLength , unk ) ;
152+ if ( 0 >= actualLen ) {
153+ return null ;
154+ }
123155
124- // get the smallest between actualLen and MaxOutLength
125- var actualLenOrMax = actualLen < MaxOutLength ? actualLen : MaxOutLength ;
156+ // get the smallest between actualLen and MaxOutLength
157+ var actualLenOrMax = actualLen < MaxOutLength ? actualLen : MaxOutLength ;
126158
127- // read bytes
128- var tmp = Module [ "HEAPU8" ] . subarray ( IdsOut , IdsOut + ( actualLenOrMax * 4 ) ) ;
159+ // read bytes
160+ var tmp = Module [ "HEAPU8" ] . subarray ( IdsOut , IdsOut + ( actualLenOrMax * 4 ) ) ;
129161
130- // allocate JS array
131- var ids = new Int32Array ( actualLenOrMax ) ;
162+ // allocate JS array
163+ var ids = new Int32Array ( actualLenOrMax ) ;
132164
133- // decode bytes into int array (I could not find how to make a cast here)
134- var i = 0 ;
135- var j = 0 ;
136- for ( ; i < actualLenOrMax ; i ++ , j += 4 ) {
137- ids [ i ] = tmp [ j ] + ( tmp [ j + 1 ] * 256 ) + ( tmp [ j + 2 ] * 65536 ) + ( tmp [ j + 3 ] * 16777216 ) ;
165+ // decode bytes into int array (I could not find how to make a cast here)
166+ var i = 0 ;
167+ var j = 0 ;
168+ for ( ; i < actualLenOrMax ; i ++ , j += 4 ) {
169+ ids [ i ] = tmp [ j ] + ( tmp [ j + 1 ] * 256 ) + ( tmp [ j + 2 ] * 65536 ) + ( tmp [ j + 3 ] * 16777216 ) ;
170+ }
171+ }
172+ finally
173+ {
174+ if ( inUtf8 != 0 )
175+ {
176+ Module [ "_free" ] ( inUtf8 ) ;
177+ }
178+
179+ if ( IdsOut != 0 )
180+ {
181+ Module [ "_free" ] ( IdsOut ) ;
182+ }
138183 }
139184
140185 return ids ;
@@ -153,15 +198,30 @@ export function WordHyphenation(handle, s, hyp = 0x2D) {
153198
154199 var len = Module [ "lengthBytesUTF8" ] ( s ) ;
155200
156- var inUtf8 = Module [ "stackAlloc " ] ( len + 1 ) ; // if we don't do +1 this library won't copy the last character
201+ var inUtf8 = Module [ "_malloc " ] ( len + 1 ) ; // if we don't do +1 this library won't copy the last character
157202 Module [ "stringToUTF8" ] ( s , inUtf8 , len + 1 ) ; // since it always also needs a space for a 0-char
158203
159204 var MaxOutLength = ( len << 1 ) + 1 ; // worst case hyphen after every character
160- var outUtf8 = Module [ "stackAlloc " ] ( MaxOutLength ) ;
205+ var outUtf8 = Module [ "_malloc " ] ( MaxOutLength ) ;
161206
162- var actualLen = Module [ "_WordHyphenationWithModel" ] ( inUtf8 , len , outUtf8 , MaxOutLength , h , hyp ) ;
163- if ( 0 > actualLen || actualLen > MaxOutLength ) {
164- return null ;
207+ try
208+ {
209+ var actualLen = Module [ "_WordHyphenationWithModel" ] ( inUtf8 , len , outUtf8 , MaxOutLength , h , hyp ) ;
210+ if ( 0 > actualLen || actualLen > MaxOutLength ) {
211+ return null ;
212+ }
213+ }
214+ finally
215+ {
216+ if ( inUtf8 != 0 )
217+ {
218+ Module [ "_free" ] ( inUtf8 ) ;
219+ }
220+
221+ if ( outUtf8 != 0 )
222+ {
223+ Module [ "_free" ] ( outUtf8 ) ;
224+ }
165225 }
166226
167227 return Module [ "UTF8ToString" ] ( outUtf8 ) ;
0 commit comments