Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: multi file generation #187

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function mainFunc() {
let args = process.argv.slice(2);

const fn = process.argv.includes('--image-only') ? generateImageFile : generateLoadedImageFile;
args = args.filter(elem => elem !== '--image-only');
args = args.filter(elem => elem !== '--image-only' && !elem.startsWith('--copy-dir'));

if (args.length !== 2) {
throw new Error('Expected exactly 2 inputs, input prolog (.pl) file and output file');
Expand Down
16 changes: 13 additions & 3 deletions dist/generateImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import SWIPL from './swipl/swipl-bundle';
import fs from 'fs';
import { fetch } from '@inrupt/universal-fetch';
import { readFileSync } from 'fs-extra';

function Uint8ToString(u8a: Uint8Array) {
const CHUNK_SZ = 0x8000;
Expand All @@ -14,12 +15,21 @@ function Uint8ToString(u8a: Uint8Array) {
return c.join('');
}

export async function generateImageBuffer(prolog: string | Buffer): Promise<Uint8Array> {
interface Options {
name?: string;
copyDir?: string;
}

export async function generateImageBuffer(prolog: string | Buffer, options?: Options): Promise<Uint8Array> {
const Module = await SWIPL({
arguments: ['-q', '-f', 'prolog.pl'],
arguments: ['-q', '-f', options?.name ?? 'prolog.pl'],
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
preRun: [(module: SWIPLModule) => { module.FS.writeFile('prolog.pl', prolog) }],
preRun: [(module: SWIPLModule) => {
module.FS.

module.FS.writeFile(options?.name ?? 'prolog.pl', prolog)
}],
});

Module.prolog.query("qsave_program('prolog.pvm')").once();
Expand Down
2 changes: 1 addition & 1 deletion examples/generation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"keywords": [],
"author": "",
"license": "ISC",
"license": "MIT",
"dependencies": {
"swipl-wasm": "file:../.."
},
Expand Down
1 change: 1 addition & 0 deletions examples/multi-file-generation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/max.ts
11 changes: 11 additions & 0 deletions examples/multi-file-generation/dist/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import SWIPL from './max';

async function main() {
const Module = await SWIPL();
const res = Module.prolog.query('myMax(A, B, C).', { A: 1, B: 2 });
if ((res.once() as { C: number }).C !== 2) {
throw new Error('Failed to find max')
}
}

main();
Loading