swipl-wasm 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/bin/index.js +25 -0
- package/dist/generateImage.d.ts +7 -0
- package/dist/generateImage.js +78 -0
- package/dist/loadImage.d.ts +4 -0
- package/dist/loadImage.js +11 -0
- package/dist/loadImageDefault.d.ts +4 -0
- package/dist/loadImageDefault.js +11 -0
- package/dist/strToBuffer.d.ts +8 -0
- package/dist/strToBuffer.js +41 -0
- package/dist/swipl/swipl-bundle-no-data.d.ts +2 -0
- package/dist/swipl/swipl-bundle-no-data.js +21 -0
- package/dist/swipl/swipl-bundle.js +3 -3
- package/dist/swipl/swipl-web.data +0 -0
- package/dist/swipl/swipl-web.js +3 -3
- package/dist/swipl/swipl-web.wasm +0 -0
- package/dist/swipl/swipl.js +3 -3
- package/package.json +55 -14
package/README.md
CHANGED
|
@@ -66,6 +66,26 @@ the `.js` file instead.
|
|
|
66
66
|
</script>
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
+
## Generating an image
|
|
70
|
+
|
|
71
|
+
Often you will want to bundle a pre-built image of your prolog file. The easiest way to do this is using the `swipl-generate` command to generate the image. For example in `./examples/generation` the script: `swipl-generate ./max.pl ./dist/max.ts` will generate a file `./dist/max.ts` which contains the image of `./max.pl`. This file can then be imported into your project and used as follows:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import SWIPL from './max';
|
|
75
|
+
|
|
76
|
+
async function main() {
|
|
77
|
+
const Module = await SWIPL();
|
|
78
|
+
const res = Module.prolog.query('max(1, 2, 3).');
|
|
79
|
+
console.log(res.next())
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main();
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Note that this procedure generates a file which imports directly from `swipl-wasm/dist/loadImageDefault`, so make sure that `swipl-wasm` is a direct dependency in your project rather than a dev dependency.
|
|
86
|
+
|
|
87
|
+
To generate the image data without it pre-loaded into `SWIPL` use the `--image-only` flag.
|
|
88
|
+
|
|
69
89
|
## Running JavaScript from Prolog
|
|
70
90
|
|
|
71
91
|
This uses `eval`:
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { generateLoadedImageFile, generateImageFile } = require('../generateImage')
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
function getPath(pth) {
|
|
7
|
+
return pth.startsWith('http://') || pth.startsWith('https://')
|
|
8
|
+
? pth
|
|
9
|
+
: path.join(process.cwd(), pth)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function mainFunc() {
|
|
13
|
+
let args = process.argv.slice(2);
|
|
14
|
+
|
|
15
|
+
const fn = process.argv.includes('--image-only') ? generateImageFile : generateLoadedImageFile;
|
|
16
|
+
args = args.filter(elem => elem !== '--image-only');
|
|
17
|
+
|
|
18
|
+
if (args.length !== 2) {
|
|
19
|
+
throw new Error('Expected exactly 2 inputs, input prolog (.pl) file and output file');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
await fn(getPath(args[0]), getPath(args[1]));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
mainFunc();
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare function generateImageBuffer(prolog: string | Buffer): Promise<Uint8Array>;
|
|
3
|
+
export declare function generateImageString(prolog: string | Buffer): Promise<string>;
|
|
4
|
+
export declare function generateImageFileString(prolog: string | Buffer): Promise<string>;
|
|
5
|
+
export declare function generateLoadedImageFileString(prolog: string | Buffer): Promise<string>;
|
|
6
|
+
export declare function generateImageFile(prologPath: string, jsPath: string): Promise<void>;
|
|
7
|
+
export declare function generateLoadedImageFile(prologPath: string, jsPath: string): Promise<void>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// <reference types="emscripten" />
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.generateLoadedImageFile = exports.generateImageFile = exports.generateLoadedImageFileString = exports.generateImageFileString = exports.generateImageString = exports.generateImageBuffer = void 0;
|
|
17
|
+
const swipl_bundle_1 = __importDefault(require("./swipl/swipl-bundle"));
|
|
18
|
+
const fs_1 = __importDefault(require("fs"));
|
|
19
|
+
const cross_fetch_1 = require("cross-fetch");
|
|
20
|
+
function Uint8ToString(u8a) {
|
|
21
|
+
const CHUNK_SZ = 0x8000;
|
|
22
|
+
const c = [];
|
|
23
|
+
for (let i = 0; i < u8a.length; i += CHUNK_SZ) {
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
+
c.push(String.fromCharCode.apply(null, u8a.subarray(i, i + CHUNK_SZ)));
|
|
26
|
+
}
|
|
27
|
+
return c.join('');
|
|
28
|
+
}
|
|
29
|
+
function generateImageBuffer(prolog) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
const Module = yield (0, swipl_bundle_1.default)({
|
|
32
|
+
arguments: ['-q', '-f', 'prolog.pl'],
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
preRun: [(module) => { module.FS.writeFile('prolog.pl', prolog); }],
|
|
36
|
+
});
|
|
37
|
+
Module.prolog.query("qsave_program('prolog.pvm')").once();
|
|
38
|
+
return Module.FS.readFile('prolog.pvm');
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
exports.generateImageBuffer = generateImageBuffer;
|
|
42
|
+
function generateImageString(prolog) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
return btoa(Uint8ToString(yield generateImageBuffer(prolog)));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
exports.generateImageString = generateImageString;
|
|
48
|
+
function generateImageFileString(prolog) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
return `export default "${yield generateImageString(prolog)}"\n`;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
exports.generateImageFileString = generateImageFileString;
|
|
54
|
+
function generateLoadedImageFileString(prolog) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
return 'import loadImage from "swipl-wasm/dist/loadImageDefault"\n' +
|
|
57
|
+
'import strToBuffer from "swipl-wasm/dist/strToBuffer"\n\n' +
|
|
58
|
+
`export default loadImage(strToBuffer("${yield generateImageString(prolog)}"))\n`;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
exports.generateLoadedImageFileString = generateLoadedImageFileString;
|
|
62
|
+
function dereference(prologPath) {
|
|
63
|
+
return (prologPath.startsWith('http://') || prologPath.startsWith('https://'))
|
|
64
|
+
? (0, cross_fetch_1.fetch)(prologPath).then((res) => res.text())
|
|
65
|
+
: fs_1.default.readFileSync(prologPath);
|
|
66
|
+
}
|
|
67
|
+
function generateImageFile(prologPath, jsPath) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
fs_1.default.writeFileSync(jsPath, yield generateImageFileString(yield dereference(prologPath)));
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
exports.generateImageFile = generateImageFile;
|
|
73
|
+
function generateLoadedImageFile(prologPath, jsPath) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
fs_1.default.writeFileSync(jsPath, yield generateLoadedImageFileString(yield dereference(prologPath)));
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
exports.generateLoadedImageFile = generateLoadedImageFile;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="emscripten" />
|
|
3
|
+
import type SWIPL_TYPE from './common';
|
|
4
|
+
export declare function loadImage(image: string | Buffer | Uint8Array, swipl: typeof SWIPL_TYPE): (options?: Partial<EmscriptenModule> | undefined) => Promise<SWIPL_TYPE.SWIPLModule>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// <reference types="emscripten" />
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.loadImage = void 0;
|
|
5
|
+
function loadImage(image, swipl) {
|
|
6
|
+
return (options) => swipl(Object.assign(Object.assign({}, options), { arguments: ['-q', '-x', 'image.pvm'],
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
preRun: [(module) => module.FS.writeFile('image.pvm', image)] }));
|
|
10
|
+
}
|
|
11
|
+
exports.loadImage = loadImage;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const swipl_bundle_no_data_1 = __importDefault(require("./swipl/swipl-bundle-no-data"));
|
|
7
|
+
const loadImage_1 = require("./loadImage");
|
|
8
|
+
function default_1(image) {
|
|
9
|
+
return (0, loadImage_1.loadImage)(image, swipl_bundle_no_data_1.default);
|
|
10
|
+
}
|
|
11
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that converts a string into a buffer.
|
|
3
|
+
* This is required *only* for the conversion of the inlined
|
|
4
|
+
* EYE_PVM string into a buffer
|
|
5
|
+
* @param string The string to convert
|
|
6
|
+
* @returns A Uint8Array Buffer
|
|
7
|
+
*/
|
|
8
|
+
export default function strToBuffer(data: string): Uint8Array;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/* eslint-disable no-param-reassign,no-plusplus,no-bitwise,no-use-before-define,no-multi-assign */
|
|
4
|
+
/* istanbul ignore file */
|
|
5
|
+
/**
|
|
6
|
+
* A function that converts a string into a buffer.
|
|
7
|
+
* This is required *only* for the conversion of the inlined
|
|
8
|
+
* EYE_PVM string into a buffer
|
|
9
|
+
* @param string The string to convert
|
|
10
|
+
* @returns A Uint8Array Buffer
|
|
11
|
+
*/
|
|
12
|
+
function strToBuffer(data) {
|
|
13
|
+
if (data.length % 4 === 0) {
|
|
14
|
+
data = data.replace(/==?$/, '');
|
|
15
|
+
}
|
|
16
|
+
const output = [];
|
|
17
|
+
let buffer = 0;
|
|
18
|
+
let accumulatedBits = 0;
|
|
19
|
+
for (let i = 0; i < data.length; i++) {
|
|
20
|
+
buffer = (buffer << 6) | keystr.indexOf(data[i]);
|
|
21
|
+
accumulatedBits += 1;
|
|
22
|
+
if (accumulatedBits === 4) {
|
|
23
|
+
output.push((buffer & 0xff0000) >> 16, (buffer & 0xff00) >> 8, buffer & 0xff);
|
|
24
|
+
buffer = accumulatedBits = 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (accumulatedBits === 2) {
|
|
28
|
+
output.push(buffer >> 4);
|
|
29
|
+
}
|
|
30
|
+
else if (accumulatedBits === 3) {
|
|
31
|
+
output.push(((buffer >>= 2) & 0xff00) >> 8, buffer & 0xff);
|
|
32
|
+
}
|
|
33
|
+
// "Return output."
|
|
34
|
+
return new Uint8Array(output);
|
|
35
|
+
}
|
|
36
|
+
exports.default = strToBuffer;
|
|
37
|
+
/**
|
|
38
|
+
* A lookup table for atob(), which converts an ASCII character to the
|
|
39
|
+
* corresponding six-bit number.
|
|
40
|
+
*/
|
|
41
|
+
const keystr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|