swipl-wasm 1.1.0 → 2.0.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 +61 -27
- package/dist/common.d.ts +88 -0
- package/dist/swipl/swipl-web.d.ts +2 -0
- package/dist/swipl/swipl-web.data +0 -0
- package/dist/swipl/swipl-web.js +1 -1
- package/dist/swipl/swipl-web.wasm +0 -0
- package/dist/swipl/swipl.d.ts +2 -17
- package/dist/swipl/swipl.js +1 -1
- package/dist/swipl-node.d.ts +2 -0
- package/dist/swipl-node.js +17 -0
- package/package.json +22 -56
- package/dist/index.d.ts +0 -9
- package/dist/index.js +0 -34
- package/dist/locateFile-browser.d.ts +0 -1
- package/dist/locateFile-browser.js +0 -13
- package/dist/locateFile.d.ts +0 -1
- package/dist/locateFile.js +0 -21
- package/dist/swipl/.gitkeep +0 -0
package/README.md
CHANGED
|
@@ -14,63 +14,97 @@ In browser:
|
|
|
14
14
|
<script>
|
|
15
15
|
(async () => {
|
|
16
16
|
const swipl = await SWIPL({
|
|
17
|
-
noInitialRun: true,
|
|
18
17
|
arguments: ["-q"],
|
|
19
|
-
locateFile: (
|
|
20
|
-
|
|
21
|
-
return "/dist/swipl/swipl-web.data";
|
|
22
|
-
} else if (url === "swipl-web.wasm") {
|
|
23
|
-
return "/dist/swipl/swipl-web.wasm";
|
|
24
|
-
}
|
|
25
|
-
return url;
|
|
18
|
+
locateFile: (path) => {
|
|
19
|
+
return `/dist/swipl/${path}`;
|
|
26
20
|
},
|
|
27
21
|
});
|
|
28
22
|
const query = "member(X, [a, b, c]).";
|
|
29
23
|
const solutionElement = document.getElementById("solution");
|
|
30
|
-
// See https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650/1
|
|
31
24
|
const firstSolution = swipl.prolog.query(query).once().X;
|
|
32
25
|
solutionElement.textContent = firstSolution;
|
|
33
26
|
})();
|
|
34
27
|
</script>
|
|
35
28
|
```
|
|
36
29
|
|
|
30
|
+
The function `locateFile` will help the browser to find the necessary
|
|
31
|
+
files (`swipl-web.wasm` and `swipl-web.data`). In this case the files
|
|
32
|
+
should be served along with `swipl-web.js` under the `/dist/swipl`
|
|
33
|
+
directory in the web server.
|
|
34
|
+
|
|
37
35
|
You can run this example by executing `npm run test:serve-http` and
|
|
38
36
|
visiting <http://localhost:8080/examples/browser.html>.
|
|
39
37
|
|
|
40
38
|
In Nodejs:
|
|
41
39
|
|
|
42
40
|
```js
|
|
43
|
-
|
|
44
|
-
// .data and .wasm files.
|
|
45
|
-
const swiplModuleLocation = require.resolve("swipl-wasm/swipl");
|
|
46
|
-
const swipl = await SWIPL({
|
|
47
|
-
noInitialRun: true,
|
|
48
|
-
arguments: ["-q"],
|
|
49
|
-
locateFile: (url) => {
|
|
50
|
-
// These are common with the web version.
|
|
51
|
-
if (url === "swipl-web.data") {
|
|
52
|
-
return path.join(path.dirname(swiplModuleLocation), "swipl-web.data");
|
|
53
|
-
} else if (url === "swipl-web.wasm") {
|
|
54
|
-
return path.join(path.dirname(swiplModuleLocation), "swipl-web.wasm");
|
|
55
|
-
}
|
|
56
|
-
return url;
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
// See https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650/1
|
|
41
|
+
const swipl = await SWIPL({ arguments: ["-q"] });
|
|
60
42
|
console.log(swipl.prolog.query("member(X, [a, b, c]).").once().X);
|
|
61
43
|
```
|
|
62
44
|
|
|
63
45
|
You can run this example with `node examples/run-on-node.js`.
|
|
64
46
|
|
|
47
|
+
## Running JavaScript from Prolog
|
|
48
|
+
|
|
49
|
+
This uses `eval`:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
swipl.prolog
|
|
53
|
+
.query("js_run_script(Script)", {
|
|
54
|
+
Script: `console.log('hello')`,
|
|
55
|
+
})
|
|
56
|
+
.once();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Using with Webpack
|
|
60
|
+
|
|
61
|
+
[Webpack](https://webpack.js.org/) is a JavaScript and resources
|
|
62
|
+
bundler for large-scale frontend projects.
|
|
63
|
+
|
|
64
|
+
There is an example Webpack project in `examples/webpack`. It uses
|
|
65
|
+
[Asset Modules](https://webpack.js.org/guides/asset-modules/) to "load"
|
|
66
|
+
necessary `.data` and `.wasm` files. The location of these files and then
|
|
67
|
+
fed to `locateFile` (see above).
|
|
68
|
+
|
|
69
|
+
The package `swipl-wasm` is linked into the example. In an actual project
|
|
70
|
+
you would declare `swipl-wasm` as a normal dependency.
|
|
71
|
+
|
|
72
|
+
To start the example:
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
cd examples/webpack
|
|
76
|
+
npm install
|
|
77
|
+
npm build
|
|
78
|
+
npm run server
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
and visit <http://127.0.0.1:8080>. You should see the message "Hello world from
|
|
82
|
+
Prolog".
|
|
83
|
+
|
|
65
84
|
## Build
|
|
66
85
|
|
|
67
86
|
The package can be built using npm or yarn. Please use yarn to
|
|
68
87
|
add new dependencies and update yarn.lock file. SWI-Prolog WebAssembly
|
|
69
88
|
version is currently built inside Docker with Emscripten.
|
|
70
89
|
|
|
90
|
+
## Versioning
|
|
91
|
+
|
|
92
|
+
The package uses its own versioning scheme using semver. It is
|
|
93
|
+
detached from the versioning of SWI-Prolog itself.
|
|
94
|
+
|
|
95
|
+
To get the underlying SWI-Prolog version:
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
const swipl = await SWIPL({ arguments: ["-q"] });
|
|
99
|
+
const version = swipl.prolog
|
|
100
|
+
.query("current_prolog_flag(version, Version)")
|
|
101
|
+
.once().Version;
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The version is returned as integer `10000 × Major + 100 × Minor + Patch`.
|
|
105
|
+
|
|
71
106
|
## TODO
|
|
72
107
|
|
|
73
|
-
- More examples (how to bundle with webpack)
|
|
74
108
|
- Integrate with SWI-Prolog CI
|
|
75
109
|
- TypeScript types for Prolog.js (and the Query interface)
|
|
76
110
|
- Use EcmaScript modules as output:
|
package/dist/common.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/// <reference types="emscripten" />
|
|
2
|
+
|
|
3
|
+
type CallOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Module in which to call Goal.
|
|
6
|
+
*/
|
|
7
|
+
module?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Call as yieldable.
|
|
10
|
+
*/
|
|
11
|
+
async?: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Query interface for opening new queries.
|
|
16
|
+
*/
|
|
17
|
+
type Query = {
|
|
18
|
+
/**
|
|
19
|
+
* Fetches the next solution.
|
|
20
|
+
*/
|
|
21
|
+
next(): unknown;
|
|
22
|
+
/**
|
|
23
|
+
* Runs the query. If there is a solution then returns it.
|
|
24
|
+
* Closes the query.
|
|
25
|
+
*/
|
|
26
|
+
once(): unknown;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Prolog-JavaScript interface of the Prolog instance.
|
|
31
|
+
*/
|
|
32
|
+
type Prolog = {
|
|
33
|
+
/**
|
|
34
|
+
* Call a Prolog goal. This function deals with munknown variations to
|
|
35
|
+
* call Prolog.
|
|
36
|
+
*/
|
|
37
|
+
call(goal: string, opts?: CallOptions): unknown;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Run a possibly long running goal and process its answers.
|
|
41
|
+
*
|
|
42
|
+
* @param goal Goal to run.
|
|
43
|
+
* @param input Goal input.
|
|
44
|
+
* @param callback Optional callback to process.
|
|
45
|
+
* @return A promise that is resolved on completion and rejected on
|
|
46
|
+
* a Prolog exception.
|
|
47
|
+
*/
|
|
48
|
+
forEach(
|
|
49
|
+
goal: string,
|
|
50
|
+
input?: unknown,
|
|
51
|
+
callback?: (prolog: Prolog, answer: unknown) => void
|
|
52
|
+
): Promise<unknown>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Calls the goal while binding input arguments.
|
|
56
|
+
*
|
|
57
|
+
* @param goal Goal to run.
|
|
58
|
+
* @param input Input variable bindings.
|
|
59
|
+
*/
|
|
60
|
+
query(goal: string, input?: Record<string, unknown>): Query;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* SWI-Prolog instance.
|
|
65
|
+
*/
|
|
66
|
+
type SWIPLModule = {
|
|
67
|
+
/**
|
|
68
|
+
* Emscripten emulated file system interface.
|
|
69
|
+
*/
|
|
70
|
+
FS: typeof FS;
|
|
71
|
+
/**
|
|
72
|
+
* Prolog interface.
|
|
73
|
+
*/
|
|
74
|
+
prolog: Prolog;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
declare namespace initSWIPL {
|
|
78
|
+
export { SWIPLModule, Prolog, Query };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Factory function that creates a SWI-Prolog instance.
|
|
83
|
+
*/
|
|
84
|
+
declare function initSWIPL(
|
|
85
|
+
options?: Partial<EmscriptenModule>
|
|
86
|
+
): Promise<SWIPLModule>;
|
|
87
|
+
|
|
88
|
+
export = initSWIPL;
|
|
Binary file
|