swipl-wasm 1.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/.vscode/settings.json +7 -0
- package/LICENSE.txt +41 -0
- package/README.md +82 -0
- package/docker/Dockerfile +53 -0
- package/docker/build-all.sh +29 -0
- package/examples/browser.html +32 -0
- package/examples/run-on-node.js +23 -0
- package/package.json +28 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# SWI-Prolog copyright status
|
|
2
|
+
|
|
3
|
+
SWI-Prolog is covered by the Simplified BSD license:
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions
|
|
7
|
+
are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright
|
|
10
|
+
notice, this list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
13
|
+
notice, this list of conditions and the following disclaimer in
|
|
14
|
+
the documentation and/or other materials provided with the
|
|
15
|
+
distribution.
|
|
16
|
+
|
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
18
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
19
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
20
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
21
|
+
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
22
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
23
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
24
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
26
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
27
|
+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
28
|
+
POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
|
|
30
|
+
## Possible additional license requirements
|
|
31
|
+
|
|
32
|
+
Note that SWI-Prolog may be linked with libraries covered by more
|
|
33
|
+
restrictive license and therefore the above conditions may not suffice
|
|
34
|
+
for a particular version of SWI-Prolog or a program loaded into
|
|
35
|
+
SWI-Prolog. Run the following predicate to find which components with
|
|
36
|
+
additional requirements are loaded into a particular version.
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
?- license.
|
|
40
|
+
```
|
|
41
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# swipl-wasm
|
|
2
|
+
|
|
3
|
+
SWI-Prolog WebAssembly build as a NPM package. Please see this page
|
|
4
|
+
for ongoing progress and information:
|
|
5
|
+
<https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650>
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
In browser:
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<div id="solution"></div>
|
|
13
|
+
<script src="/dist/swipl/swipl-web.js"></script>
|
|
14
|
+
<script>
|
|
15
|
+
(async () => {
|
|
16
|
+
const swipl = await SWIPL({
|
|
17
|
+
noInitialRun: true,
|
|
18
|
+
arguments: ["-q"],
|
|
19
|
+
locateFile: (url) => {
|
|
20
|
+
if (url === "swipl-web.data") {
|
|
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;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
const query = "member(X, [a, b, c]).";
|
|
29
|
+
const solutionElement = document.getElementById("solution");
|
|
30
|
+
// See https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650/1
|
|
31
|
+
const firstSolution = swipl.prolog.query(query).once().X;
|
|
32
|
+
solutionElement.textContent = firstSolution;
|
|
33
|
+
})();
|
|
34
|
+
</script>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
You can run this example by executing `npm run test:serve-http` and
|
|
38
|
+
visiting <http://localhost:8080/examples/browser.html>.
|
|
39
|
+
|
|
40
|
+
In Nodejs:
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
// This helps to find the correct locations of the
|
|
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
|
|
60
|
+
console.log(swipl.prolog.query("member(X, [a, b, c]).").once().X);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
You can run this example with `node examples/run-on-node.js`.
|
|
64
|
+
|
|
65
|
+
## Build
|
|
66
|
+
|
|
67
|
+
The package can be built using npm or yarn. Please use yarn to
|
|
68
|
+
add new dependencies and update yarn.lock file. SWI-Prolog WebAssembly
|
|
69
|
+
version is currently built inside Docker with Emscripten.
|
|
70
|
+
|
|
71
|
+
## TODO
|
|
72
|
+
|
|
73
|
+
- More examples (how to bundle with webpack)
|
|
74
|
+
- Integrate with SWI-Prolog CI
|
|
75
|
+
- TypeScript types for Prolog.js (and the Query interface)
|
|
76
|
+
- Use EcmaScript modules as output:
|
|
77
|
+
<https://github.com/emscripten-core/emscripten/issues/11792>
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
Same as SWI-Prolog license, BSD simplified:
|
|
82
|
+
<https://github.com/SWI-Prolog/swipl-devel/blob/master/LICENSE>
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Builds SWI-Prolog WebAssembly version.
|
|
2
|
+
# https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650
|
|
3
|
+
|
|
4
|
+
FROM debian:bullseye
|
|
5
|
+
|
|
6
|
+
# Installs build dependencies.
|
|
7
|
+
|
|
8
|
+
ENV DEBIAN_FRONTEND noninteractive
|
|
9
|
+
|
|
10
|
+
RUN apt-get update && apt-get install -y \
|
|
11
|
+
bzip2 \
|
|
12
|
+
cmake \
|
|
13
|
+
git \
|
|
14
|
+
ninja-build \
|
|
15
|
+
python3 \
|
|
16
|
+
wget \
|
|
17
|
+
xz-utils
|
|
18
|
+
|
|
19
|
+
RUN git config --global pull.ff only
|
|
20
|
+
|
|
21
|
+
# Installs Emscripten.
|
|
22
|
+
# See https://emscripten.org/docs/getting_started/downloads.html
|
|
23
|
+
|
|
24
|
+
ENV EMSDK_VERSION 3.1.20
|
|
25
|
+
|
|
26
|
+
RUN git clone https://github.com/emscripten-core/emsdk.git /emsdk
|
|
27
|
+
WORKDIR /emsdk
|
|
28
|
+
RUN git pull
|
|
29
|
+
RUN ./emsdk install $EMSDK_VERSION
|
|
30
|
+
RUN ./emsdk activate $EMSDK_VERSION
|
|
31
|
+
|
|
32
|
+
# Downloads ZLIB.
|
|
33
|
+
|
|
34
|
+
RUN wget --no-verbose https://zlib.net/zlib-1.2.12.tar.gz -O /zlib-1.2.12.tar.gz
|
|
35
|
+
RUN tar -xf /zlib-1.2.12.tar.gz --directory /
|
|
36
|
+
|
|
37
|
+
# Clones SWI-Prolog.
|
|
38
|
+
|
|
39
|
+
RUN git clone https://github.com/SWI-Prolog/swipl-devel.git /swipl-devel
|
|
40
|
+
WORKDIR /swipl-devel
|
|
41
|
+
|
|
42
|
+
# Uncomment the following to build a specific commit.
|
|
43
|
+
# ENV SWIPL_COMMIT 5c7d1f8352b27d45cb8066eac5af95cf925a05b7
|
|
44
|
+
# RUN git checkout $SWIPL_COMMIT -b local-wasm
|
|
45
|
+
|
|
46
|
+
RUN git fetch
|
|
47
|
+
RUN git submodule update --init
|
|
48
|
+
|
|
49
|
+
# Compiles everything.
|
|
50
|
+
|
|
51
|
+
COPY build-all.sh /build-all.sh
|
|
52
|
+
RUN chmod +x /build-all.sh
|
|
53
|
+
RUN /build-all.sh
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
set -o errexit
|
|
4
|
+
set -o xtrace
|
|
5
|
+
|
|
6
|
+
source /emsdk/emsdk_env.sh
|
|
7
|
+
|
|
8
|
+
# ZLIB
|
|
9
|
+
|
|
10
|
+
cd /zlib-1.2.12
|
|
11
|
+
emconfigure ./configure
|
|
12
|
+
EMCC_CFLAGS=-Wno-deprecated-non-prototype emmake make
|
|
13
|
+
|
|
14
|
+
# SWI-Prolog
|
|
15
|
+
|
|
16
|
+
mkdir /swipl-devel/swipl.wasm
|
|
17
|
+
cd /swipl-devel/swipl.wasm
|
|
18
|
+
cmake -DCMAKE_TOOLCHAIN_FILE=/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake \
|
|
19
|
+
-DCMAKE_BUILD_TYPE=Release \
|
|
20
|
+
-DZLIB_LIBRARY=/zlib-1.2.12/libz.a \
|
|
21
|
+
-DZLIB_INCLUDE_DIR=/zlib-1.2.12 \
|
|
22
|
+
-DINSTALL_DOCUMENTATION=OFF \
|
|
23
|
+
-DMULTI_THREADED=OFF \
|
|
24
|
+
-DUSE_SIGNALS=OFF \
|
|
25
|
+
-DUSE_GMP=OFF \
|
|
26
|
+
-DBUILD_SWIPL_LD=OFF \
|
|
27
|
+
-DSWIPL_PACKAGES=OFF \
|
|
28
|
+
-G Ninja ..
|
|
29
|
+
ninja
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF8">
|
|
5
|
+
<title>SWI-Prolog WebAssembly browser test</title>
|
|
6
|
+
</head>
|
|
7
|
+
<body>
|
|
8
|
+
<div id="solution"></div>
|
|
9
|
+
<script src="/dist/swipl/swipl-web.js"></script>
|
|
10
|
+
<script>
|
|
11
|
+
(async () => {
|
|
12
|
+
const swipl = await SWIPL({
|
|
13
|
+
noInitialRun: true,
|
|
14
|
+
arguments: ['-q'],
|
|
15
|
+
locateFile: (url) => {
|
|
16
|
+
if (url === 'swipl-web.data') {
|
|
17
|
+
return '/dist/swipl/swipl-web.data';
|
|
18
|
+
} else if (url === 'swipl-web.wasm') {
|
|
19
|
+
return '/dist/swipl/swipl-web.wasm';
|
|
20
|
+
}
|
|
21
|
+
return url;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const query = "member(X, [a, b, c]).";
|
|
25
|
+
const solutionElement = document.getElementById('solution');
|
|
26
|
+
// See https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650/1
|
|
27
|
+
const firstSolution = swipl.prolog.query(query).once().X;
|
|
28
|
+
solutionElement.textContent = firstSolution;
|
|
29
|
+
})();
|
|
30
|
+
</script>
|
|
31
|
+
</body>
|
|
32
|
+
</html>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const SWIPL = require('swipl-wasm/swipl');
|
|
3
|
+
|
|
4
|
+
(async () => {
|
|
5
|
+
// This helps to find the correct locations of the
|
|
6
|
+
// .data and .wasm files.
|
|
7
|
+
const swiplModuleLocation = require.resolve('swipl-wasm/swipl');
|
|
8
|
+
const swipl = await SWIPL({
|
|
9
|
+
noInitialRun: true,
|
|
10
|
+
arguments: ['-q'],
|
|
11
|
+
locateFile: (url) => {
|
|
12
|
+
// These are common with the web version.
|
|
13
|
+
if (url === 'swipl-web.data') {
|
|
14
|
+
return path.join(path.dirname(swiplModuleLocation), 'swipl-web.data');
|
|
15
|
+
} else if (url === 'swipl-web.wasm') {
|
|
16
|
+
return path.join(path.dirname(swiplModuleLocation), 'swipl-web.wasm');
|
|
17
|
+
}
|
|
18
|
+
return url;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
// See https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650/1
|
|
22
|
+
console.log(swipl.prolog.query("member(X, [a, b, c]).").once().X);
|
|
23
|
+
})();
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "swipl-wasm",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"exports": {
|
|
5
|
+
"./swipl": "./dist/swipl/swipl.js",
|
|
6
|
+
"./swipl-web": "./dist/swipl/swipl-web.js"
|
|
7
|
+
},
|
|
8
|
+
"author": "Raivo Laanemets",
|
|
9
|
+
"license": "BSD-2-Clause",
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"npm-run-all": "^4.1.5",
|
|
13
|
+
"http-server": "^14.1.1"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build:wasm-docker:build": "docker build -t swipl-wasm-image docker",
|
|
17
|
+
"build:wasm-docker:create": "docker create --name swipl-wasm swipl-wasm-image",
|
|
18
|
+
"build:wasm-docker:remove": "docker rm swipl-wasm",
|
|
19
|
+
"build:wasm-docker:extract:data": "docker cp swipl-wasm:/swipl-devel/swipl.wasm/src/swipl-web.data dist/swipl/swipl-web.data",
|
|
20
|
+
"build:wasm-docker:extract:wasm": "docker cp swipl-wasm:/swipl-devel/swipl.wasm/src/swipl-web.wasm dist/swipl/swipl-web.wasm",
|
|
21
|
+
"build:wasm-docker:extract:web": "docker cp swipl-wasm:/swipl-devel/swipl.wasm/src/swipl-web.js dist/swipl/swipl-web.js",
|
|
22
|
+
"build:wasm-docker:extract:node": "docker cp swipl-wasm:/swipl-devel/swipl.wasm/src/swipl-web.js dist/swipl/swipl.js",
|
|
23
|
+
"build:wasm-docker:extract": "run-s build:wasm-docker:extract:*",
|
|
24
|
+
"build:wasm-docker": "run-s build:wasm-docker:build build:wasm-docker:create build:wasm-docker:extract build:wasm-docker:remove",
|
|
25
|
+
"build": "run-s build:wasm-docker",
|
|
26
|
+
"test:serve-http": "http-server . -c-1"
|
|
27
|
+
}
|
|
28
|
+
}
|