Skip to content

Commit 2bd8c4c

Browse files
committed
remove protobuf from parse
1 parent 743f0ff commit 2bd8c4c

File tree

3 files changed

+20
-134
lines changed

3 files changed

+20
-134
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
}
2222
},
2323
"scripts": {
24-
"clean": "rimraf build",
24+
"clean": "yarn wasm:clean",
25+
"build": "yarn wasm:clean && yarn wasm:build",
2526
"wasm:make": "docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emmake make",
2627
"wasm:build": "yarn wasm:make build",
2728
"wasm:rebuild": "yarn wasm:make rebuild",

wasm/index.cjs

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ function ptrToString(ptr) {
3636
return wasmModule.UTF8ToString(ptr);
3737
}
3838

39-
const protobufCache = new WeakMap();
40-
4139
const parseQuery = awaitInit(async (query) => {
4240
const queryPtr = stringToPtr(query);
4341
let resultPtr;
44-
let protobufPtr;
4542

4643
try {
4744
resultPtr = wasmModule._wasm_parse_query(queryPtr);
@@ -51,48 +48,21 @@ const parseQuery = awaitInit(async (query) => {
5148
throw new Error(resultStr);
5249
}
5350

54-
const parseResult = JSON.parse(resultStr);
55-
56-
const protobufLen = wasmModule._wasm_get_protobuf_len(queryPtr);
57-
if (protobufLen > 0) {
58-
const lenPtr = wasmModule._malloc(4);
59-
wasmModule.HEAPU32[lenPtr >> 2] = 0;
60-
protobufPtr = wasmModule._wasm_parse_query_protobuf(queryPtr, lenPtr);
61-
const actualLen = wasmModule.HEAPU32[lenPtr >> 2];
62-
wasmModule._free(lenPtr);
63-
64-
if (actualLen > 0) {
65-
const protobufData = new Uint8Array(wasmModule.HEAPU8.buffer, protobufPtr, actualLen);
66-
const protobufCopy = new Uint8Array(protobufData);
67-
protobufCache.set(parseResult, protobufCopy);
68-
}
69-
}
70-
71-
return parseResult;
51+
return JSON.parse(resultStr);
7252
} finally {
7353
wasmModule._free(queryPtr);
7454
if (resultPtr) {
7555
wasmModule._wasm_free_string(resultPtr);
7656
}
77-
if (protobufPtr) {
78-
wasmModule._wasm_free_string(protobufPtr);
79-
}
8057
}
8158
});
8259

8360
const deparse = awaitInit(async (parseTree) => {
84-
const protobufData = protobufCache.get(parseTree);
85-
86-
if (!protobufData) {
87-
throw new Error('deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.');
88-
}
89-
90-
const dataPtr = wasmModule._malloc(protobufData.length);
61+
const queryPtr = stringToPtr(JSON.stringify(parseTree));
9162
let resultPtr;
9263

9364
try {
94-
wasmModule.HEAPU8.set(protobufData, dataPtr);
95-
resultPtr = wasmModule._wasm_deparse_protobuf(dataPtr, protobufData.length);
65+
resultPtr = wasmModule._wasm_deparse_protobuf(queryPtr, 0);
9666
const resultStr = ptrToString(resultPtr);
9767

9868
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
@@ -101,7 +71,7 @@ const deparse = awaitInit(async (parseTree) => {
10171

10272
return resultStr;
10373
} finally {
104-
wasmModule._free(dataPtr);
74+
wasmModule._free(queryPtr);
10575
if (resultPtr) {
10676
wasmModule._wasm_free_string(resultPtr);
10777
}
@@ -210,14 +180,13 @@ const parseQueryDetailed = awaitInit(async (query) => {
210180
}
211181
});
212182

213-
// Sync versions that assume WASM module is already initialized
183+
// Sync versions
214184
function parseQuerySync(query) {
215185
if (!wasmModule) {
216186
throw new Error('WASM module not initialized. Call loadModule() first.');
217187
}
218188
const queryPtr = stringToPtr(query);
219189
let resultPtr;
220-
let protobufPtr;
221190

222191
try {
223192
resultPtr = wasmModule._wasm_parse_query(queryPtr);
@@ -227,51 +196,24 @@ function parseQuerySync(query) {
227196
throw new Error(resultStr);
228197
}
229198

230-
const parseResult = JSON.parse(resultStr);
231-
232-
const protobufLen = wasmModule._wasm_get_protobuf_len(queryPtr);
233-
if (protobufLen > 0) {
234-
const lenPtr = wasmModule._malloc(4);
235-
wasmModule.HEAPU32[lenPtr >> 2] = 0;
236-
protobufPtr = wasmModule._wasm_parse_query_protobuf(queryPtr, lenPtr);
237-
const actualLen = wasmModule.HEAPU32[lenPtr >> 2];
238-
wasmModule._free(lenPtr);
239-
240-
if (actualLen > 0) {
241-
const protobufData = new Uint8Array(wasmModule.HEAPU8.buffer, protobufPtr, actualLen);
242-
const protobufCopy = new Uint8Array(protobufData);
243-
protobufCache.set(parseResult, protobufCopy);
244-
}
245-
}
246-
247-
return parseResult;
199+
return JSON.parse(resultStr);
248200
} finally {
249201
wasmModule._free(queryPtr);
250202
if (resultPtr) {
251203
wasmModule._wasm_free_string(resultPtr);
252204
}
253-
if (protobufPtr) {
254-
wasmModule._wasm_free_string(protobufPtr);
255-
}
256205
}
257206
}
258207

259208
function deparseSync(parseTree) {
260209
if (!wasmModule) {
261210
throw new Error('WASM module not initialized. Call loadModule() first.');
262211
}
263-
const protobufData = protobufCache.get(parseTree);
264-
265-
if (!protobufData) {
266-
throw new Error('deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.');
267-
}
268-
269-
const dataPtr = wasmModule._malloc(protobufData.length);
212+
const queryPtr = stringToPtr(JSON.stringify(parseTree));
270213
let resultPtr;
271214

272215
try {
273-
wasmModule.HEAPU8.set(protobufData, dataPtr);
274-
resultPtr = wasmModule._wasm_deparse_protobuf(dataPtr, protobufData.length);
216+
resultPtr = wasmModule._wasm_deparse_protobuf(queryPtr, 0);
275217
const resultStr = ptrToString(resultPtr);
276218

277219
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
@@ -280,7 +222,7 @@ function deparseSync(parseTree) {
280222

281223
return resultStr;
282224
} finally {
283-
wasmModule._free(dataPtr);
225+
wasmModule._free(queryPtr);
284226
if (resultPtr) {
285227
wasmModule._wasm_free_string(resultPtr);
286228
}

wasm/index.js

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ function ptrToString(ptr) {
3636
return wasmModule.UTF8ToString(ptr);
3737
}
3838

39-
const protobufCache = new WeakMap();
40-
4139
export const parseQuery = awaitInit(async (query) => {
4240
const queryPtr = stringToPtr(query);
4341
let resultPtr;
44-
let protobufPtr;
4542

4643
try {
4744
resultPtr = wasmModule._wasm_parse_query(queryPtr);
@@ -51,48 +48,21 @@ export const parseQuery = awaitInit(async (query) => {
5148
throw new Error(resultStr);
5249
}
5350

54-
const parseResult = JSON.parse(resultStr);
55-
56-
const protobufLen = wasmModule._wasm_get_protobuf_len(queryPtr);
57-
if (protobufLen > 0) {
58-
const lenPtr = wasmModule._malloc(4);
59-
wasmModule.HEAPU32[lenPtr >> 2] = 0;
60-
protobufPtr = wasmModule._wasm_parse_query_protobuf(queryPtr, lenPtr);
61-
const actualLen = wasmModule.HEAPU32[lenPtr >> 2];
62-
wasmModule._free(lenPtr);
63-
64-
if (actualLen > 0) {
65-
const protobufData = new Uint8Array(wasmModule.HEAPU8.buffer, protobufPtr, actualLen);
66-
const protobufCopy = new Uint8Array(protobufData);
67-
protobufCache.set(parseResult, protobufCopy);
68-
}
69-
}
70-
71-
return parseResult;
51+
return JSON.parse(resultStr);
7252
} finally {
7353
wasmModule._free(queryPtr);
7454
if (resultPtr) {
7555
wasmModule._wasm_free_string(resultPtr);
7656
}
77-
if (protobufPtr) {
78-
wasmModule._wasm_free_string(protobufPtr);
79-
}
8057
}
8158
});
8259

8360
export const deparse = awaitInit(async (parseTree) => {
84-
const protobufData = protobufCache.get(parseTree);
85-
86-
if (!protobufData) {
87-
throw new Error('deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.');
88-
}
89-
90-
const dataPtr = wasmModule._malloc(protobufData.length);
61+
const queryPtr = stringToPtr(JSON.stringify(parseTree));
9162
let resultPtr;
9263

9364
try {
94-
wasmModule.HEAPU8.set(protobufData, dataPtr);
95-
resultPtr = wasmModule._wasm_deparse_protobuf(dataPtr, protobufData.length);
65+
resultPtr = wasmModule._wasm_deparse_protobuf(queryPtr, 0);
9666
const resultStr = ptrToString(resultPtr);
9767

9868
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
@@ -101,7 +71,7 @@ export const deparse = awaitInit(async (parseTree) => {
10171

10272
return resultStr;
10373
} finally {
104-
wasmModule._free(dataPtr);
74+
wasmModule._free(queryPtr);
10575
if (resultPtr) {
10676
wasmModule._wasm_free_string(resultPtr);
10777
}
@@ -210,13 +180,13 @@ export const parseQueryDetailed = awaitInit(async (query) => {
210180
}
211181
});
212182

183+
// Sync versions
213184
export function parseQuerySync(query) {
214185
if (!wasmModule) {
215186
throw new Error('WASM module not initialized. Call loadModule() first.');
216187
}
217188
const queryPtr = stringToPtr(query);
218189
let resultPtr;
219-
let protobufPtr;
220190

221191
try {
222192
resultPtr = wasmModule._wasm_parse_query(queryPtr);
@@ -226,51 +196,24 @@ export function parseQuerySync(query) {
226196
throw new Error(resultStr);
227197
}
228198

229-
const parseResult = JSON.parse(resultStr);
230-
231-
const protobufLen = wasmModule._wasm_get_protobuf_len(queryPtr);
232-
if (protobufLen > 0) {
233-
const lenPtr = wasmModule._malloc(4);
234-
wasmModule.HEAPU32[lenPtr >> 2] = 0;
235-
protobufPtr = wasmModule._wasm_parse_query_protobuf(queryPtr, lenPtr);
236-
const actualLen = wasmModule.HEAPU32[lenPtr >> 2];
237-
wasmModule._free(lenPtr);
238-
239-
if (actualLen > 0) {
240-
const protobufData = new Uint8Array(wasmModule.HEAPU8.buffer, protobufPtr, actualLen);
241-
const protobufCopy = new Uint8Array(protobufData);
242-
protobufCache.set(parseResult, protobufCopy);
243-
}
244-
}
245-
246-
return parseResult;
199+
return JSON.parse(resultStr);
247200
} finally {
248201
wasmModule._free(queryPtr);
249202
if (resultPtr) {
250203
wasmModule._wasm_free_string(resultPtr);
251204
}
252-
if (protobufPtr) {
253-
wasmModule._wasm_free_string(protobufPtr);
254-
}
255205
}
256206
}
257207

258208
export function deparseSync(parseTree) {
259209
if (!wasmModule) {
260210
throw new Error('WASM module not initialized. Call loadModule() first.');
261211
}
262-
const protobufData = protobufCache.get(parseTree);
263-
264-
if (!protobufData) {
265-
throw new Error('deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.');
266-
}
267-
268-
const dataPtr = wasmModule._malloc(protobufData.length);
212+
const queryPtr = stringToPtr(JSON.stringify(parseTree));
269213
let resultPtr;
270214

271215
try {
272-
wasmModule.HEAPU8.set(protobufData, dataPtr);
273-
resultPtr = wasmModule._wasm_deparse_protobuf(dataPtr, protobufData.length);
216+
resultPtr = wasmModule._wasm_deparse_protobuf(queryPtr, 0);
274217
const resultStr = ptrToString(resultPtr);
275218

276219
if (resultStr.startsWith('syntax error') || resultStr.startsWith('deparse error') || resultStr.includes('ERROR')) {
@@ -279,7 +222,7 @@ export function deparseSync(parseTree) {
279222

280223
return resultStr;
281224
} finally {
282-
wasmModule._free(dataPtr);
225+
wasmModule._free(queryPtr);
283226
if (resultPtr) {
284227
wasmModule._wasm_free_string(resultPtr);
285228
}

0 commit comments

Comments
 (0)