Skip to content

Commit

Permalink
use webworker
Browse files Browse the repository at this point in the history
  • Loading branch information
abentkamp committed Nov 3, 2023
1 parent 9e95795 commit 32e5348
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 105 deletions.
1 change: 0 additions & 1 deletion client/public/bin

This file was deleted.

1 change: 1 addition & 0 deletions client/public/lean.js
1 change: 1 addition & 0 deletions client/public/lean.wasm
103 changes: 103 additions & 0 deletions client/public/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

const IO = {
_resolveGetLine: null,
_resolveRead: null,
_readPointer: null,
_nbytes: 0,
bufferStdIn : "",
putStrListeners: [],
listenPutStr(callback) {
this.putStrListeners.push(callback)
},
putStr(str) {
console.log('PUTSTR' + str)
str = str.split('\n')[2]
this.putStrListeners.forEach((listener) => {
listener(str)
})
},
async getLine() {
return new Promise((resolve, reject) => {
this._resolveGetLine = resolve
this.flushStdIn();
});
},
async read(ptr, nbytes) {
this._nbytes = nbytes
this._readPointer = ptr
return new Promise((resolve, reject) => {
this._resolveRead = resolve
this.flushStdIn();
});
},
flushStdIn() {
if(this._resolveGetLine) {
var lineBreak = this.bufferStdIn.indexOf("\n")
if (lineBreak != -1) {
this._resolveGetLine(stringToNewUTF8(this.bufferStdIn.substring(0,lineBreak+1)))
this.bufferStdIn = this.bufferStdIn.substring(lineBreak+1)
this._resolveGetLine = null
}
}
if(this._resolveRead) {
// console.log(`read: ${this.bufferStdIn}`)
stringToUTF8(this.bufferStdIn, this._readPointer, this._nbytes);
this._resolveRead()
this.bufferStdIn = ""
this._resolveRead = null
}
},
putLine(data) {
console.log("Client: ",data)
const str = data + '\r\n'
const byteLength = lengthBytesUTF8(str)
this.bufferStdIn += `Content-Length: ${byteLength}\r\n\r\n` + str
this.flushStdIn();
}
}


var input = ""
var i = 0;

function submit (ev) {
ev.preventDefault()
return false;
}

var stdoutBuffer = ""
var stderrBuffer = ""


var Module = {
"arguments": ["--worker"],
"preRun": function() {
function stdin() {
if (i < input.length) {
i++;
return input.charCodeAt(i);// Return ASCII code of character, or null if no input
} else {
return null
}
}

function stdout(asciiCode) {
stdoutBuffer += String.fromCharCode(asciiCode)
}

function stderr(asciiCode) {
stderrBuffer += String.fromCharCode(asciiCode)
}

FS.init(stdin, stdout, stderr);
}};

importScripts("lean.js")

onmessage = (ev) => {
IO.putLine(ev.data)
}

IO.listenPutStr(message => {
postMessage(message)
})
5 changes: 3 additions & 2 deletions client/src/editor/leanclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ export class LeanClient implements Disposable {
})
websocket.addEventListener('open', () => {
const socket = toSocket(websocket)
const reader = new WasmReader()
const writer = new WasmWriter()
const worker = new Worker("worker.js")
const reader = new WasmReader(worker)
const writer = new WasmWriter(worker)
resolve({
reader,
writer
Expand Down
12 changes: 6 additions & 6 deletions client/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class WasmWriter implements MessageWriter {
protected errorCount = 0;
errorEmitter
closeEmitter
constructor() {
constructor(private worker: Worker) {
this.errorEmitter = new Emitter()
this.closeEmitter = new Emitter()
}
Expand Down Expand Up @@ -48,7 +48,7 @@ asError(error) {
async write(msg: Message): Promise<void> {
try {
const content = JSON.stringify(msg);
IO.putLine(content)
this.worker.postMessage(content)
} catch (e) {
this.errorCount++;
this.fireError(e, msg, this.errorCount);
Expand All @@ -62,10 +62,10 @@ export class WasmReader implements MessageReader {
protected callback: DataCallback | undefined;
protected readonly events: { message?: any, error?: any }[] = [];

constructor() {
IO.listenPutStr(message => {
this.readMessage(message)
})
constructor(private worker: Worker) {
this.worker.onmessage = (ev) => {
this.readMessage(ev.data)
}
// this.socket.onMessage(message =>
// this.readMessage(message)
// );
Expand Down
96 changes: 0 additions & 96 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,102 +22,6 @@
+49 211 81-12173<br />
</p>
</noscript>
<script>

const IO = {
_resolveGetLine: null,
_resolveRead: null,
_readPointer: null,
_nbytes: 0,
bufferStdIn : "",
putStrListeners: [],
listenPutStr(callback) {
this.putStrListeners.push(callback)
},
putStr(str) {
console.log('PUTSTR' + str)
str = str.split('\n')[2]
this.putStrListeners.forEach((listener) => {
listener(str)
})
},
async getLine() {
return new Promise((resolve, reject) => {
this._resolveGetLine = resolve
this.flushStdIn();
});
},
async read(ptr, nbytes) {
this._nbytes = nbytes
this._readPointer = ptr
return new Promise((resolve, reject) => {
this._resolveRead = resolve
this.flushStdIn();
});
},
flushStdIn() {
if(this._resolveGetLine) {
var lineBreak = this.bufferStdIn.indexOf("\n")
if (lineBreak != -1) {
this._resolveGetLine(stringToNewUTF8(this.bufferStdIn.substring(0,lineBreak+1)))
this.bufferStdIn = this.bufferStdIn.substring(lineBreak+1)
this._resolveGetLine = null
}
}
if(this._resolveRead) {
// console.log(`read: ${this.bufferStdIn}`)
stringToUTF8(this.bufferStdIn, this._readPointer, this._nbytes);
this._resolveRead()
this.bufferStdIn = ""
this._resolveRead = null
}
},
putLine(data) {
console.log("Client: ",data)
const str = data + '\r\n'
const byteLength = lengthBytesUTF8(str)
this.bufferStdIn += `Content-Length: ${byteLength}\r\n\r\n` + str
this.flushStdIn();
}
}


var input = ""
var i = 0;

function submit (ev) {
ev.preventDefault()
return false;
}

var stdoutBuffer = ""
var stderrBuffer = ""


var Module = {
"arguments": ["--worker"],
"preRun": function() {
function stdin() {
if (i < input.length) {
i++;
return input.charCodeAt(i);// Return ASCII code of character, or null if no input
} else {
return null
}
}

function stdout(asciiCode) {
stdoutBuffer += String.fromCharCode(asciiCode)
}

function stderr(asciiCode) {
stderrBuffer += String.fromCharCode(asciiCode)
}

FS.init(stdin, stdout, stderr);
}};
</script>
<script src="bin/lean.js"></script>
<script type="module" src="/client/src/index.tsx"></script>
</body>

Expand Down

0 comments on commit 32e5348

Please sign in to comment.