Fetch compile wasm binaries

pull/5370/head
ioedeveloper 2 years ago
parent a5bbf89e3e
commit cbc598cf98
  1. 117
      apps/circuit-compiler/src/app/services/circomPluginClient.ts

@ -1,8 +1,8 @@
import {PluginClient} from '@remixproject/plugin' import { PluginClient } from '@remixproject/plugin'
import {createClient} from '@remixproject/plugin-webview' import { createClient } from '@remixproject/plugin-webview'
import EventManager from 'events' import EventManager from 'events'
import pathModule from 'path' import pathModule from 'path'
import {parse} from 'circom_wasm' import { parse, compile } from 'circom_wasm'
export class CircomPluginClient extends PluginClient { export class CircomPluginClient extends PluginClient {
public internalEvents: EventManager public internalEvents: EventManager
@ -11,7 +11,7 @@ export class CircomPluginClient extends PluginClient {
super() super()
createClient(this) createClient(this)
this.internalEvents = new EventManager() this.internalEvents = new EventManager()
this.methods = ['init', 'parse'] this.methods = ['init', 'parse', 'compile']
this.onload() this.onload()
} }
@ -30,7 +30,7 @@ export class CircomPluginClient extends PluginClient {
async parse(path: string, fileContent: string): Promise<void> { async parse(path: string, fileContent: string): Promise<void> {
let buildFiles = { let buildFiles = {
[path]: fileContent [path]: fileContent,
} }
buildFiles = await this.resolveDependencies(path, fileContent, buildFiles) buildFiles = await this.resolveDependencies(path, fileContent, buildFiles)
@ -49,16 +49,14 @@ export class CircomPluginClient extends PluginClient {
for (const label in report.labels) { for (const label in report.labels) {
if (report.labels[label].file_id === '0') { if (report.labels[label].file_id === '0') {
// @ts-ignore // @ts-ignore
const startPosition: {lineNumber: number; column: number} = const startPosition: { lineNumber: number; column: number } = await this.call(
await this.call(
'editor', 'editor',
// @ts-ignore // @ts-ignore
'getPositionAt', 'getPositionAt',
report.labels[label].range.start report.labels[label].range.start
) )
// @ts-ignore // @ts-ignore
const endPosition: {lineNumber: number; column: number} = const endPosition: { lineNumber: number; column: number } = await this.call(
await this.call(
'editor', 'editor',
// @ts-ignore // @ts-ignore
'getPositionAt', 'getPositionAt',
@ -71,14 +69,14 @@ export class CircomPluginClient extends PluginClient {
position: { position: {
start: { start: {
line: startPosition.lineNumber, line: startPosition.lineNumber,
column: startPosition.column column: startPosition.column,
}, },
end: { end: {
line: endPosition.lineNumber, line: endPosition.lineNumber,
column: endPosition.column column: endPosition.column,
} },
}, },
file: path file: path,
}) })
} }
} }
@ -97,17 +95,21 @@ export class CircomPluginClient extends PluginClient {
} }
} }
async resolveDependencies( async compile(path: string): Promise<void> {
filePath: string, const fileContent = await this.call('fileManager', 'readFile', path)
fileContent: string, let buildFiles = {
output = {}, [path]: fileContent,
depPath: string = '', }
blackPath: string[] = []
): Promise<Record<string, string>> { buildFiles = await this.resolveDependencies(path, fileContent, buildFiles)
const compiledOutput = compile(path, buildFiles, { prime: 'bn128' })
console.log('compiled wasm binaries: ', compiledOutput)
}
async resolveDependencies(filePath: string, fileContent: string, output = {}, depPath: string = '', blackPath: string[] = []): Promise<Record<string, string>> {
// extract all includes // extract all includes
const includes = (fileContent.match(/include ['"].*['"]/g) || []).map( const includes = (fileContent.match(/include ['"].*['"]/g) || []).map((include) => include.replace(/include ['"]/g, '').replace(/['"]/g, ''))
(include) => include.replace(/include ['"]/g, '').replace(/['"]/g, '')
)
await Promise.all( await Promise.all(
includes.map(async (include) => { includes.map(async (include) => {
@ -123,12 +125,8 @@ export class CircomPluginClient extends PluginClient {
dependencyContent = await this.call('fileManager', 'readFile', path) dependencyContent = await this.call('fileManager', 'readFile', path)
} else { } else {
// if include import (path) does not exist, try to construct relative path using the original file path (current file opened in editor) // if include import (path) does not exist, try to construct relative path using the original file path (current file opened in editor)
let relativePath = pathModule.resolve( let relativePath = pathModule.resolve(filePath.slice(0, filePath.lastIndexOf('/')), include)
filePath.slice(0, filePath.lastIndexOf('/')), if (relativePath.indexOf('/') === 0) relativePath = relativePath.slice(1)
include
)
if (relativePath.indexOf('/') === 0)
relativePath = relativePath.slice(1)
const relativePathExists = await this.call( const relativePathExists = await this.call(
'fileManager', 'fileManager',
// @ts-ignore // @ts-ignore
@ -138,25 +136,13 @@ export class CircomPluginClient extends PluginClient {
if (relativePathExists) { if (relativePathExists) {
// fetch file content if include import exists as a relative path // fetch file content if include import exists as a relative path
dependencyContent = await this.call( dependencyContent = await this.call('fileManager', 'readFile', relativePath)
'fileManager',
'readFile',
relativePath
)
} else { } else {
if (depPath) { if (depPath) {
// if depPath is provided, try to resolve include import from './deps' folder in remix // if depPath is provided, try to resolve include import from './deps' folder in remix
path = pathModule.resolve( path = pathModule.resolve(depPath.slice(0, depPath.lastIndexOf('/')), include)
depPath.slice(0, depPath.lastIndexOf('/')),
include
)
if (path.indexOf('/') === 0) path = path.slice(1) if (path.indexOf('/') === 0) path = path.slice(1)
dependencyContent = await this.call( dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
'contentImport',
'resolveAndSave',
path,
null
)
} else { } else {
if (include.startsWith('circomlib')) { if (include.startsWith('circomlib')) {
// try to resolve include import from github if it is a circomlib dependency // try to resolve include import from github if it is a circomlib dependency
@ -164,55 +150,26 @@ export class CircomPluginClient extends PluginClient {
const version = splitInclude[1].match(/v[0-9]+.[0-9]+.[0-9]+/g) const version = splitInclude[1].match(/v[0-9]+.[0-9]+.[0-9]+/g)
if (version && version[0]) { if (version && version[0]) {
path = `https://raw.githubusercontent.com/iden3/circomlib/${ path = `https://raw.githubusercontent.com/iden3/circomlib/${version[0]}/circuits/${splitInclude.slice(2).join('/')}`
version[0] dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
}/circuits/${splitInclude.slice(2).join('/')}`
dependencyContent = await this.call(
'contentImport',
'resolveAndSave',
path,
null
)
} else { } else {
path = `https://raw.githubusercontent.com/iden3/circomlib/master/circuits/${splitInclude path = `https://raw.githubusercontent.com/iden3/circomlib/master/circuits/${splitInclude.slice(1).join('/')}`
.slice(1) dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
.join('/')}`
dependencyContent = await this.call(
'contentImport',
'resolveAndSave',
path,
null
)
} }
} else { } else {
// If all import cases are not true, use the default import to try fetching from node_modules and unpkg // If all import cases are not true, use the default import to try fetching from node_modules and unpkg
dependencyContent = await this.call( dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
'contentImport',
'resolveAndSave',
path,
null
)
} }
} }
} }
} }
// extract all includes from the dependency content // extract all includes from the dependency content
const dependencyIncludes = ( const dependencyIncludes = (dependencyContent.match(/include ['"].*['"]/g) || []).map((include) => include.replace(/include ['"]/g, '').replace(/['"]/g, ''))
dependencyContent.match(/include ['"].*['"]/g) || []
).map((include) =>
include.replace(/include ['"]/g, '').replace(/['"]/g, '')
)
blackPath.push(include) blackPath.push(include)
// recursively resolve all dependencies of the dependency // recursively resolve all dependencies of the dependency
if (dependencyIncludes.length > 0) { if (dependencyIncludes.length > 0) {
await this.resolveDependencies( await this.resolveDependencies(filePath, dependencyContent, output, path, blackPath)
filePath,
dependencyContent,
output,
path,
blackPath
)
output[include] = dependencyContent output[include] = dependencyContent
} else { } else {
output[include] = dependencyContent output[include] = dependencyContent

Loading…
Cancel
Save