Skip to content

Commit 2061d6d

Browse files
Merge pull request #151 from leti367/dev/leti/fixwasm
use malloc instead of stackAlloc
2 parents 4e1359b + 9053a54 commit 2061d6d

File tree

3 files changed

+94
-35
lines changed

3 files changed

+94
-35
lines changed

wasm/blingfire.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm/blingfire.wasm

100644100755
-5.47 KB
Binary file not shown.

wasm/blingfire_wrapper.js

Lines changed: 93 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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,47 @@ 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

114-
// allocate space for ids on stack (it is faster and we don't need to delete it manualy)
115144
var MaxOutLength = max_len;
116-
var IdsOut = Module["stackAlloc"](MaxOutLength * 4); // sizeof(int)
145+
var IdsOut = Module["_malloc"](MaxOutLength * 4); // sizeof(int)
117146

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-
}
147+
try
148+
{
149+
// get the IDS from BlingFire
150+
var actualLen = Module["_TextToIds"](h, inUtf8, len, IdsOut, MaxOutLength, unk);
151+
if(0 >= actualLen) {
152+
return null;
153+
}
123154

124-
// get the smallest between actualLen and MaxOutLength
125-
var actualLenOrMax = actualLen < MaxOutLength ? actualLen : MaxOutLength;
155+
// get the smallest between actualLen and MaxOutLength
156+
var actualLenOrMax = actualLen < MaxOutLength ? actualLen : MaxOutLength;
126157

127-
// read bytes
128-
var tmp = Module["HEAPU8"].subarray(IdsOut, IdsOut + (actualLenOrMax * 4));
158+
// read bytes
159+
var tmp = Module["HEAPU8"].subarray(IdsOut, IdsOut + (actualLenOrMax * 4));
129160

130-
// allocate JS array
131-
var ids = new Int32Array(actualLenOrMax);
161+
// allocate JS array
162+
var ids = new Int32Array(actualLenOrMax);
132163

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);
164+
// decode bytes into int array (I could not find how to make a cast here)
165+
var i = 0;
166+
var j = 0;
167+
for(; i < actualLenOrMax; i++, j+=4) {
168+
ids[i] = tmp[j] + (tmp[j + 1] * 256) + (tmp[j + 2] * 65536) + (tmp[j + 3] * 16777216);
169+
}
170+
}
171+
finally
172+
{
173+
if (inUtf8 != 0)
174+
{
175+
Module["_free"](inUtf8);
176+
}
177+
178+
if (IdsOut != 0)
179+
{
180+
Module["_free"](IdsOut);
181+
}
138182
}
139183

140184
return ids;
@@ -153,15 +197,30 @@ export function WordHyphenation(handle, s, hyp = 0x2D) {
153197

154198
var len = Module["lengthBytesUTF8"](s);
155199

156-
var inUtf8 = Module["stackAlloc"](len + 1); // if we don't do +1 this library won't copy the last character
200+
var inUtf8 = Module["_malloc"](len + 1); // if we don't do +1 this library won't copy the last character
157201
Module["stringToUTF8"](s, inUtf8, len + 1); // since it always also needs a space for a 0-char
158202

159203
var MaxOutLength = (len << 1) + 1; // worst case hyphen after every character
160-
var outUtf8 = Module["stackAlloc"](MaxOutLength);
204+
var outUtf8 = Module["_malloc"](MaxOutLength);
161205

162-
var actualLen = Module["_WordHyphenationWithModel"](inUtf8, len, outUtf8, MaxOutLength, h, hyp);
163-
if(0 > actualLen || actualLen > MaxOutLength) {
164-
return null;
206+
try
207+
{
208+
var actualLen = Module["_WordHyphenationWithModel"](inUtf8, len, outUtf8, MaxOutLength, h, hyp);
209+
if(0 > actualLen || actualLen > MaxOutLength) {
210+
return null;
211+
}
212+
}
213+
finally
214+
{
215+
if (inUtf8 != 0)
216+
{
217+
Module["_free"](inUtf8);
218+
}
219+
220+
if (outUtf8 != 0)
221+
{
222+
Module["_free"](outUtf8);
223+
}
165224
}
166225

167226
return Module["UTF8ToString"](outUtf8);

0 commit comments

Comments
 (0)