Skip to content

Commit d9b5082

Browse files
committed
feat: Support passing filenames in options for each output type
This adds `cSourceFileName`, `dataFileName`, `wasmFileName`, and `watFileName` to the Compiler options. It also moves the fastfile creation into the Compiler itself (instead of the CLI) and will call fastfile only if the new *FileName options are passed.
1 parent c311ae3 commit d9b5082

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

cli.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const fs = require("fs");
2525
const path = require("path");
2626
const Scalar = require("ffjavascript").Scalar;
2727
const stringifyBigInts = require("ffjavascript").utils.stringifyBigInts;
28-
const fastFile = require("fastfile");
2928

3029
const compiler = require("./src/compiler");
3130

@@ -87,15 +86,15 @@ async function run() {
8786
options.sanityCheck = argv.sanitycheck;
8887

8988
if (argv.csource) {
90-
options.cSourceFile = await fastFile.createOverride(cSourceName);
89+
options.cSourceFileName = cSourceName;
9190
const noExt = cSourceName.substr(0, cSourceName.lastIndexOf(".")) || cSourceName;
92-
options.dataFile = await fastFile.createOverride(noExt+".dat");
91+
options.dataFileName = noExt+".dat";
9392
}
9493
if (argv.wasm) {
95-
options.wasmFile = await fastFile.createOverride(wasmName);
94+
options.wasmFileName = wasmName;
9695
}
9796
if (argv.wat) {
98-
options.watFile = await fastFile.createOverride(watName);
97+
options.watFileName = watName;
9998
}
10099
if (argv.r1cs) {
101100
options.r1csFileName = r1csName;
@@ -119,10 +118,6 @@ async function run() {
119118

120119
await compiler(fullFileName, options);
121120

122-
if (options.cSourceFile) await options.cSourceFile.close();
123-
if (options.dataFile) await options.dataFile.close();
124-
if (options.wasmFile) await options.wasmFile.close();
125-
if (options.watFile) await options.watFile.close();
126121
let symDone = false;
127122
if (options.symWriteStream) {
128123
options.symWriteStream.on("finish", () => {

ports/c/tester.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const utils = require("../../src/utils");
1313
const loadR1cs = require("r1csfile").load;
1414
const ZqField = require("ffjavascript").ZqField;
1515
const buildZqField = require("ffiasm").buildZqField;
16-
const fastFile = require("fastfile");
1716

1817
const {stringifyBigInts, unstringifyBigInts } = require("ffjavascript").utils;
1918

@@ -30,17 +29,14 @@ async function c_tester(circomFile, _options) {
3029
const baseName = path.basename(circomFile, ".circom");
3130
const options = Object.assign({}, _options);
3231

33-
options.cSourceFile = await fastFile.createOverride(path.join(dir.path, baseName + ".cpp"));
34-
options.dataFile = await fastFile.createOverride(path.join(dir.path, baseName + ".dat"));
32+
options.cSourceFileName = path.join(dir.path, baseName + ".cpp");
33+
options.dataFileName = path.join(dir.path, baseName + ".dat");
3534
options.symWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".sym"));
3635
options.r1csFileName = path.join(dir.path, baseName + ".r1cs");
3736

3837
options.p = options.p || Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
3938
await compiler(circomFile, options);
4039

41-
await options.cSourceFile.close();
42-
await options.dataFile.close();
43-
4440
const source = await buildZqField(options.p, "Fr");
4541

4642
// console.log(dir.path);

ports/wasm/tester.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const compiler = require("../../src/compiler");
99
const utils = require("../../src/utils");
1010
const loadR1cs = require("r1csfile").load;
1111
const ZqField = require("ffjavascript").ZqField;
12-
const fastFile = require("fastfile");
1312

1413
const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
1514

@@ -25,15 +24,13 @@ async function wasm_tester(circomFile, _options) {
2524
const baseName = path.basename(circomFile, ".circom");
2625
const options = Object.assign({}, _options);
2726

28-
options.wasmFile = await fastFile.createOverride(path.join(dir.path, baseName + ".wasm"));
27+
options.wasmFileName = path.join(dir.path, baseName + ".wasm");
2928

3029
options.symWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".sym"));
3130
options.r1csFileName = path.join(dir.path, baseName + ".r1cs");
3231

3332
await compiler(circomFile, options);
3433

35-
await options.wasmFile.close();
36-
3734
const wasm = await fs.promises.readFile(path.join(dir.path, baseName + ".wasm"));
3835

3936
const wc = await WitnessCalculatorBuilder(wasm);

src/compiler.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const buildR1cs = require("./r1csfile").buildR1cs;
2929
const BigArray = require("./bigarray");
3030
const buildSyms = require("./buildsyms");
3131
const {performance} = require("perf_hooks");
32+
const fastFile = require("fastfile");
3233

3334
module.exports = compile;
3435
const measures = {};
@@ -129,6 +130,14 @@ async function compile(srcFile, options) {
129130

130131
delete ctx.constraints; // Liberate memory.
131132

133+
if (options.cSourceFileName) {
134+
options.cSourceFile = await fastFile.createOverride(options.cSourceFileName);
135+
}
136+
137+
if (options.dataFileName) {
138+
options.dataFile = await fastFile.createOverride(options.dataFileName);
139+
}
140+
132141
if (options.cSourceFile) {
133142
if (ctx.verbose) console.log("Generating c...");
134143
measures.generateC = -performance.now();
@@ -140,6 +149,14 @@ async function compile(srcFile, options) {
140149

141150
if (ctx.error) throw(ctx.error);
142151

152+
if (options.wasmFileName) {
153+
options.wasmFile = await fastFile.createOverride(options.wasmFileName);
154+
}
155+
156+
if (options.watFileName) {
157+
options.watFile = await fastFile.createOverride(options.watFileName);
158+
}
159+
143160
if ((options.wasmFile)||(options.watFile)) {
144161
if (ctx.verbose) console.log("Generating wasm...");
145162
measures.generateWasm = -performance.now();
@@ -173,6 +190,11 @@ async function compile(srcFile, options) {
173190
console.log(mStr + ": " + ms2String(mValue));
174191
}
175192
}
193+
194+
if (options.cSourceFile) await options.cSourceFile.close();
195+
if (options.dataFile) await options.dataFile.close();
196+
if (options.wasmFile) await options.wasmFile.close();
197+
if (options.watFile) await options.watFile.close();
176198
}
177199

178200

0 commit comments

Comments
 (0)