Skip to content

Commit c66a86f

Browse files
Nakshatra SharmaNakshatra Sharma
authored andcommitted
Fix Tauri Verilog compilation: use HTTP plugin to bypass CORS and improve error handling
1 parent 5fb1a1b commit c66a86f

File tree

4 files changed

+85
-61
lines changed

4 files changed

+85
-61
lines changed

package-lock.json

Lines changed: 30 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@mdi/font": "5.9.55",
1919
"@mdi/js": "^7.4.47",
2020
"@tauri-apps/api": "2.3.0",
21+
"@tauri-apps/plugin-http": "^2.5.7",
2122
"@tauri-apps/plugin-fs": "2.2.0",
2223
"@tiptap/core": "^2.0.3",
2324
"@tiptap/extension-character-count": "^2.0.3",
@@ -55,7 +56,7 @@
5556
"@typescript-eslint/parser": "^8.52.0",
5657
"@vitejs/plugin-vue": "^2.3.3",
5758
"@vue/test-utils": "^2.4.6",
58-
"cross-env": "^10.1.0",
59+
"cross-env": "^7.0.3",
5960
"eslint": "^8.17.0",
6061
"eslint-config-prettier": "^8.5.0",
6162
"eslint-plugin-vue": "^9.1.0",

src-tauri/capabilities/default.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
},
1818
{
1919
"url": "https://circuitverse.org/api/v1/auth/signup"
20+
},
21+
{
22+
"url": "https://circuitverse.org/api/v1/simulator/verilogcv"
2023
}
2124
]
2225
},

src/simulator/src/Verilog2CV.js

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { showError, showMessage } from './utils'
3434
import { showProperties } from './ux'
3535
import { useSimulatorMobileStore } from '#/store/simulatorMobileStore'
3636
import { toRefs } from 'vue'
37+
import { isTauri } from '@tauri-apps/api/core'
3738

3839
var editor
3940
var verilogMode = false
@@ -257,56 +258,61 @@ export function YosysJSON2CV(
257258
}
258259
}
259260

260-
export default function generateVerilogCircuit(
261+
export default async function generateVerilogCircuit(
261262
verilogCode,
262263
scope = globalScope
263264
) {
264265
clearVerilogOutput()
265266
setVerilogOutput('Compiling Verilog code...', 'info')
266-
267-
var params = { code: verilogCode }
268-
fetch('/api/v1/simulator/verilogcv', {
269-
method: 'POST',
270-
headers: {
271-
'Content-Type': 'application/json',
272-
},
273-
body: JSON.stringify(params),
274-
})
275-
.then((response) => {
276-
if (!response.ok) {
277-
throw response
278-
}
279-
return response.json()
280-
})
281-
.then((circuitData) => {
282-
scope.initialize()
283-
for (var id in scope.verilogMetadata.subCircuitScopeIds)
284-
delete scopeList[id]
285-
scope.verilogMetadata.subCircuitScopeIds = []
286-
scope.verilogMetadata.code = verilogCode
287-
var subCircuitScope = {}
288-
YosysJSON2CV(
289-
circuitData,
290-
globalScope,
291-
'verilogCircuit',
292-
subCircuitScope,
293-
true
294-
)
295-
changeCircuitName(circuitData.name)
296-
showMessage('Verilog Circuit Successfully Created')
297-
setVerilogOutput('Verilog Circuit Successfully Created', 'success')
298-
})
299-
.catch((error) => {
300-
if (error.status == 500) {
301-
showError('Could not connect to Yosys')
302-
setVerilogOutput('Could not connect to Yosys server', 'error')
303-
} else {
304-
showError('There is some issue with the code')
305-
error.json().then((errorMessage) => {
306-
setVerilogOutput(errorMessage.message, 'error')
307-
})
308-
}
267+
268+
let apiUrl = '/api/v1/simulator/verilogcv'
269+
let fetchFn = window.fetch
270+
271+
if (isTauri()) {
272+
apiUrl = `https://circuitverse.org${apiUrl}`
273+
try { fetchFn = (await import('@tauri-apps/plugin-http')).fetch } catch (e) {}
274+
}
275+
276+
try {
277+
const response = await fetchFn(apiUrl, {
278+
method: 'POST',
279+
headers: { 'Content-Type': 'application/json' },
280+
body: JSON.stringify({ code: verilogCode }),
309281
})
282+
283+
if (!response.ok) throw response
284+
if (!response.headers.get('content-type')?.includes('application/json'))
285+
throw new Error('Invalid JSON response')
286+
287+
const circuitData = await response.json()
288+
289+
scope.initialize()
290+
for (var id in scope.verilogMetadata.subCircuitScopeIds)
291+
delete scopeList[id]
292+
scope.verilogMetadata.subCircuitScopeIds = []
293+
scope.verilogMetadata.code = verilogCode
294+
var subCircuitScope = {}
295+
YosysJSON2CV(
296+
circuitData,
297+
globalScope,
298+
'verilogCircuit',
299+
subCircuitScope,
300+
true
301+
)
302+
changeCircuitName(circuitData.name)
303+
showMessage('Verilog Circuit Successfully Created')
304+
setVerilogOutput('Verilog Circuit Successfully Created', 'success')
305+
} catch (error) {
306+
console.error('Verilog compilation error:', error)
307+
showError('Verilog compilation failed')
308+
309+
let msg = error.message || 'Unknown error'
310+
if (error.status === 500) msg = 'Could not connect to Yosys server'
311+
else if (typeof error.json === 'function') {
312+
try { msg = (await error.json()).message || msg } catch (e) {}
313+
}
314+
setVerilogOutput(msg, 'error')
315+
}
310316
}
311317

312318
export function setupCodeMirrorEnvironment() {

0 commit comments

Comments
 (0)