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

Feat/image generation utilities #17

Closed
Closed
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ examples/webpack/node_modules
examples/webpack/public/*.data
examples/webpack/public/*.wasm
examples/webpack/public/*.js
dist/utils.js
dist/utils.d.ts
23 changes: 23 additions & 0 deletions dist/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import SWIPL from './swipl/swipl-bundle';
import type SWIPL_TYPE from './common';
import type { } from 'emscripten';

export async function generateImage(prolog: string): Promise<Uint8Array> {
const Module = await SWIPL({
arguments: ['-q', '-f', 'prolog.pl'],
// @ts-ignore
preRun: (module: SWIPLModule) => module.FS.writeFile('prolog.pl', prolog),
});

Module.prolog.query("qsave_program('prolog.pvm')").once();
return Module.FS.readFile('prolog.pvm')
}

export async function loadImage(swipl: typeof SWIPL_TYPE) {
return (options?: Partial<EmscriptenModule> | undefined) => swipl({
...options,
arguments: ['-q', '-x', 'eye.pvm'],
// @ts-ignore
preRun: (module: SWIPLModule) => module.FS.writeFile('image.pvm', strToBuffer(EYE_PVM)),
});
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"typescript": "^4.8.4"
},
"files": [
"dist"
"dist/**/*.js",
"dist/**/*.d.ts",
"dist/**/*.data",
"dist/**/*.wasm"
],
"scripts": {
"build:wasm-docker:build": "docker build -t swipl-wasm-image docker",
Expand All @@ -36,7 +39,7 @@
"build:wasm-docker:extract:node": "docker cp swipl-wasm:/swipl-devel/build.wasm/src/swipl-web.js dist/swipl/swipl.js",
"build:wasm-docker:extract": "run-s build:wasm-docker:extract:*",
"build:wasm-docker": "run-s build:wasm-docker:build build:wasm-docker:create build:wasm-docker:extract build:wasm-docker:remove",
"build": "run-s build:wasm-docker",
"build": "run-s build:wasm-docker && tsc",
"test:serve-http": "http-server . -c-1",
"test:node": "mocha tests/node.js --timeout 20000",
"test:browser": "mocha tests/browser.js --timeout 20000",
Expand Down
16 changes: 16 additions & 0 deletions tests/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const image = require('../dist/utils');

// console.log(image.generateImage)

async function main() {
const result = await image.generateImage(`
find_max(X, Y, X) :- X >= Y, !.
find_max(X, Y, Y) :- X < Y.

find_min(X, Y, X) :- X =< Y, !.
find_min(X, Y, Y) :- X > Y.
`);
console.log(result)
}

main();
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}