@@ -36,12 +36,9 @@ function ptrToString(ptr) {
3636 return wasmModule . UTF8ToString ( ptr ) ;
3737}
3838
39- const protobufCache = new WeakMap ( ) ;
40-
4139const 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
8360const 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
214184function 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
259208function 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 }
0 commit comments