Support includes import for circom wasm build

pull/3996/head
ioedeveloper 2 years ago
parent d4da5bc063
commit 75b011964f
  1. 45
      apps/circuit-compiler/src/app/services/circomPluginClient.ts

@ -23,36 +23,38 @@ export class CircomPluginClient extends PluginClient {
} }
async compile (path: string) { async compile (path: string) {
this.parse(path) const fileContent = await this.call('fileManager', 'readFile', path)
// const fileContent = await this.call('fileManager', 'readFile', path) let buildFiles = {
// let buildFiles = await this.resolveDependencies(path, fileContent) [path]: fileContent
}
// buildFiles = buildFiles.flat(Infinity) buildFiles = await this.resolveDependencies(path, fileContent, buildFiles)
// const compilationResult = main_browser(path, buildFiles, { prime: "bn128" }) const compilationResult = main_browser(path, buildFiles, { prime: "bn128" })
// console.log('compilation result: ' + compilationResult) console.log('compilation result: ' + compilationResult)
// // generate_witness(compilationResult, '{ "a": "3", "b": "11" }') generate_witness(compilationResult, '{ "a": "3", "b": "11" }')
generate_witness(compilationResult, '{ "a": "5", "b": "77" }')
} }
async parse (path: string) { async parse (path: string) {
const fileContent = await this.call('fileManager', 'readFile', path) const fileContent = await this.call('fileManager', 'readFile', path)
let buildFiles = await this.resolveDependencies(path, fileContent) let buildFiles = {
[path]: fileContent
}
buildFiles = buildFiles.flat(Infinity) buildFiles = await this.resolveDependencies(path, fileContent, buildFiles)
buildFiles.reverse()
const parsingResult = parse_circuit_browser(path, buildFiles, { prime: "bn128" }) const parsingResult = parse_circuit_browser(path, buildFiles, { prime: "bn128" })
console.log('parsing result: ' + parsingResult) console.log('parsing result: ' + parsingResult)
} }
async resolveDependencies (filePath: string, fileContent: string, depPath: string = '') { async resolveDependencies (filePath: string, fileContent: string, output = {}, depPath: string = '', blackPath: string[] = []) {
const includes = (fileContent.match(/include ['"].*['"]/g) || []).map(include => include.replace(/include ['"]/g, '').replace(/['"]/g, '')) const includes = (fileContent.match(/include ['"].*['"]/g) || []).map(include => include.replace(/include ['"]/g, '').replace(/['"]/g, ''))
if (includes.length === 0) { await Promise.all(includes.map(async include => {
return [fileContent] // fix for endless recursive includes
} else { if (blackPath.includes(include)) return
const depFiles = await Promise.all(includes.map(async include => {
let dependencyContent = '' let dependencyContent = ''
let path = include let path = include
// @ts-ignore // @ts-ignore
@ -93,11 +95,14 @@ export class CircomPluginClient extends PluginClient {
} }
const dependencyIncludes = (dependencyContent.match(/include ['"].*['"]/g) || []).map(include => include.replace(/include ['"]/g, '').replace(/['"]/g, '')) const dependencyIncludes = (dependencyContent.match(/include ['"].*['"]/g) || []).map(include => include.replace(/include ['"]/g, '').replace(/['"]/g, ''))
if (dependencyIncludes.length > 0) return await this.resolveDependencies(filePath, dependencyContent, path) blackPath.push(include)
else return dependencyContent if (dependencyIncludes.length > 0) {
})) await this.resolveDependencies(filePath, dependencyContent, output, path, blackPath)
depFiles.push(fileContent) output[include] = dependencyContent
return depFiles } else {
output[include] = dependencyContent
} }
}))
return output
} }
} }

Loading…
Cancel
Save