-
Notifications
You must be signed in to change notification settings - Fork 0
Timothy gillespie/swipl wasm integration #111
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
| /src/util/diproche/diprocheProgram | ||
| /diproche_source | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,50 @@ | ||
| { | ||
| "name": "diproche-webinterface", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@types/lodash": "^4.14.136", | ||
| "@types/react-router-dom": "^4.3.4", | ||
| "@types/shortid": "0.0.29", | ||
| "bootstrap": "^4.3.1", | ||
| "jquery": "^3.4.1", | ||
| "lodash": "^4.17.15", | ||
| "node-sass": "^4.12.0", | ||
| "react": "^16.8.6", | ||
| "react-bootstrap": "^1.0.0-beta.10", | ||
| "react-dom": "^16.8.6", | ||
| "react-router-dom": "^5.0.1", | ||
| "react-scripts": "^3.0.1", | ||
| "shortid": "^2.2.14", | ||
| "tau-prolog": "^0.2.66", | ||
| "typescript": "3.4.2" | ||
| }, | ||
| "scripts": { | ||
| "start": "react-scripts start", | ||
| "lint": "tslint --project .", | ||
| "build": "react-scripts build", | ||
| "test": "react-scripts test", | ||
| "eject": "react-scripts eject" | ||
| }, | ||
| "eslintConfig": { | ||
| "extends": "react-app" | ||
| }, | ||
| "browserslist": [ | ||
| ">0.2%", | ||
| "not dead", | ||
| "not ie <= 11", | ||
| "not op_mini all" | ||
| ], | ||
| "devDependencies": { | ||
| "@types/jest": "24.0.11", | ||
| "@types/node": "11.13.2", | ||
| "@types/node-sass": "^4.11.0", | ||
| "@types/react": "16.8.13", | ||
| "@types/react-dom": "16.8.3", | ||
| "shx": "^0.3.2", | ||
| "tslint": "^5.18.0" | ||
| }, | ||
| "proxy": "http://localhost:5000" | ||
| "name": "diproche-webinterface", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@types/lodash": "^4.14.136", | ||
| "@types/react-router-dom": "^4.3.4", | ||
| "@types/shortid": "0.0.29", | ||
| "bootstrap": "^4.3.1", | ||
| "diproche": "git+https://github.com/TimothyGillespie/Diproche-Prolog.git", | ||
| "jquery": "^3.4.1", | ||
| "lodash": "^4.17.15", | ||
| "node-sass": "^4.12.0", | ||
| "react": "^16.8.6", | ||
| "react-bootstrap": "^1.0.0-beta.10", | ||
| "react-dom": "^16.8.6", | ||
| "react-router-dom": "^5.0.1", | ||
| "react-scripts": "^3.0.1", | ||
| "shortid": "^2.2.14", | ||
| "tau-prolog": "^0.2.66", | ||
| "typescript": "3.4.2" | ||
| }, | ||
| "scripts": { | ||
| "start": "npm run copy-diproche && react-scripts start", | ||
| "lint": "tslint --project .", | ||
| "build": "npm run copy-diproche && react-scripts build", | ||
| "test": "npm run copy-diproche && react-scripts test", | ||
| "eject": "react-scripts eject", | ||
| "copy-diproche": "shx cp -r ./node_modules/diproche/*.pl ./diproche_source" | ||
| }, | ||
| "eslintConfig": { | ||
| "extends": "react-app" | ||
| }, | ||
| "browserslist": [ | ||
| ">0.2%", | ||
| "not dead", | ||
| "not ie <= 11", | ||
| "not op_mini all" | ||
| ], | ||
| "devDependencies": { | ||
| "@types/jest": "24.0.11", | ||
| "@types/node": "11.13.2", | ||
| "@types/node-sass": "^4.11.0", | ||
| "@types/react": "16.8.13", | ||
| "@types/react-dom": "16.8.3", | ||
| "shx": "^0.3.2", | ||
| "tslint": "^5.18.0" | ||
| }, | ||
| "proxy": "http://localhost:5000" | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import "../../../swipl-wasm/swipl-web"; | ||
| import { loadingFinishedPromise, query, result } from "./fubar"; | ||
|
|
||
| describe("Fubar", () => { | ||
| test("Tests bob is a man", async () => { | ||
| await loadingFinishedPromise; | ||
| query("man(bob)."); | ||
| console.log("Inside the test"); | ||
| console.log(result); | ||
| }); | ||
|
|
||
| test("Tests frank is not a man", async () => { | ||
| await loadingFinishedPromise; | ||
| query("diproche_fo([[wir,zeigen,[a,and,b],<->,[b,and,a]],[=>],[angenommen,a,and,b],[dann,a],[ferner,gilt,b],[damit,folgt,b,and,a],[qed],[abs],[<=],[nehmen,wir,jetzt,an,dass,b,and,a],[dann,folgt,a],[ausserdem,folgt,b],[also,gilt,nun,a,and,b],[qed],[damit,gilt,[a,and,b],<->,[c,and,a]]], X)."); | ||
| console.log("Inside the test"); | ||
| console.log(result); | ||
| }); | ||
|
|
||
| test("Gets X", async () => { | ||
| await loadingFinishedPromise; | ||
| query("man(X)."); | ||
| console.log("Inside the test"); | ||
| console.log(result); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import { FS } from "../../../swipl-wasm/swipl-web"; | ||
|
|
||
| // tslint:disable-next-line:prefer-const | ||
| let bindings: any = null; | ||
| let stdin = ""; | ||
| let stdinPosition = 0; | ||
|
|
||
| const basePathDiproche: string = path.resolve(__dirname, "../../../diproche_source"); | ||
| const diprocheFiles: string[] = fs.readdirSync(basePathDiproche); | ||
| const diprocheMap: Map<string, string> = new Map(); | ||
| diprocheFiles.forEach((fileName: string) => { | ||
| const filePath: string = path.resolve(basePathDiproche, "./" + fileName); | ||
| diprocheMap.set(fileName.replace(".pl", ""), fs.readFileSync(filePath, "utf8")); | ||
| }); | ||
|
|
||
| // var to make this variable global | ||
| // tslint:disable-next-line:no-var-keyword | ||
| export var result: string[] = []; | ||
|
|
||
| // We use this to provide data into | ||
| // the SWI stdin. | ||
| const setStdin = (s: string) => { | ||
| stdin = s; | ||
| stdinPosition = 0; | ||
| }; | ||
| const readStdin = () => { | ||
| if (stdinPosition >= stdin.length) { | ||
| return null; | ||
| } else { | ||
| const code = stdin.charCodeAt(stdinPosition); | ||
| stdinPosition++; | ||
| return code; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Helper function to call a query. | ||
| */ | ||
| export function query(queryText: string) { | ||
| setStdin(queryText); | ||
| // This will execute one iteration of toplevel. | ||
| call(bindings, "break"); // see call.js | ||
| } | ||
|
|
||
| // Creates bindings to the SWI foreign API. | ||
| function createBindings(bindingModule: any) { | ||
| return { | ||
| PL_initialise: bindingModule.cwrap("PL_initialise", "number", [ | ||
| "number", | ||
| "number", | ||
| ]), | ||
| PL_new_term_ref: bindingModule.cwrap("PL_new_term_ref", "number", []), | ||
| PL_chars_to_term: bindingModule.cwrap("PL_chars_to_term", "number", [ | ||
| "string", | ||
| "number", | ||
| ]), | ||
| PL_call: bindingModule.cwrap("PL_call", "number", ["number", "number"]), | ||
| }; | ||
| } | ||
|
|
||
| // Helper function to parse a JavaScript | ||
| // string into a Prolog term and call is as a query. | ||
| function call(callBindings: any, callQuery: any) { | ||
| const ref = callBindings.PL_new_term_ref(); | ||
| if (!callBindings.PL_chars_to_term(callQuery, ref)) { | ||
| throw new Error("Query has a syntax error: " + callQuery); | ||
| } | ||
| return !!callBindings.PL_call(ref, 0); | ||
| } | ||
|
|
||
| // This will set up the arguments necessary for the PL_initialise | ||
| // function and will call it. | ||
| // See http://www.swi-prolog.org/pldoc/doc_for?object=c(%27PL_initialise%27) | ||
| function initialise(initBindings: any, initModule: any) { | ||
| const argvArray = [ | ||
| initModule.allocate( | ||
| initModule.intArrayFromString("swipl"), | ||
| "i8", | ||
| initModule.ALLOC_NORMAL, | ||
| ), | ||
| initModule.allocate( | ||
| initModule.intArrayFromString("-x"), | ||
| "i8", | ||
| initModule.ALLOC_NORMAL, | ||
| ), | ||
| initModule.allocate( | ||
| initModule.intArrayFromString("wasm-preload/swipl.prc"), | ||
| "i8", | ||
| initModule.ALLOC_NORMAL, | ||
| ), | ||
| initModule.allocate( | ||
| initModule.intArrayFromString("--nosignals"), | ||
| "i8", | ||
| initModule.ALLOC_NORMAL, | ||
| ), | ||
| ]; | ||
| const argvPtr = initModule._malloc(argvArray.length * 4); | ||
| for (let i = 0; i < argvArray.length; i++) { | ||
| initModule.setValue(argvPtr + i * 4, argvArray[i], "*"); | ||
| } | ||
| if (!initBindings.PL_initialise(4, argvPtr)) { | ||
| throw new Error("SWI-Prolog initialisation failed."); | ||
| } | ||
| // Set the path of the preloaded (from swipl-web.dat) standard library. | ||
| // This makes it possible to call use_module(library(lists)) and so on. | ||
| call( | ||
| initBindings, | ||
| "assert(user:file_search_path(library, 'wasm-preload/library')).", | ||
| ); | ||
| } | ||
|
|
||
| function pushToResultArray(value: string): void { | ||
| result.push(value); | ||
| } | ||
|
|
||
| const swiplWasm = fs.readFileSync("swipl-wasm/swipl-web.wasm"); | ||
| const swiplWasmData = fs.readFileSync("swipl-wasm/swipl-web.data").buffer; | ||
|
|
||
| // Stub Module object. Used by swipl-web.js to | ||
| // populate the actual Module object. | ||
| export const Module = { | ||
| noInitialRun: true, | ||
| locateFile: (url: string) => `swipl-wasm/${url}`, | ||
| print: pushToResultArray, | ||
| printErr: console.error, | ||
| wasmBinary: swiplWasm, | ||
| preRun: [() => FS.init(readStdin)], // sets up stdin | ||
| getPreloadedPackage: (fileName: string) => | ||
| fileName === "swipl-wasm/swipl-web.data" ? swiplWasmData : null, | ||
| onRuntimeInitialized: () => { | ||
| // Bind foreign functions to JavaScript. | ||
| bindings = createBindings(Module); | ||
| // Initialise SWI-Prolog. | ||
| initialise(bindings, Module); | ||
|
|
||
| diprocheMap.forEach((programCode: string, fileName: string) => { | ||
| FS.writeFile("/" + fileName + ".pl", programCode); | ||
| }); | ||
|
|
||
| query("consult('/diproche.pl')."); | ||
|
|
||
| loadingFinished(); | ||
| }, | ||
| }; | ||
|
|
||
| let loadingFinished!: () => void; | ||
|
|
||
| export const loadingFinishedPromise = new Promise<void>(resolve => { | ||
| loadingFinished = () => resolve(); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
varis not making the variable global.exportis.