Created
March 15, 2016 08:27
-
-
Save joesavage/841021062d69aa6d6469 to your computer and use it in GitHub Desktop.
emscripten_suspend web worker test case
This file contains 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
self.addEventListener('message', (function(e) { | |
var data = e.data; | |
switch (data.cmd) { | |
case 'init': | |
(function() { | |
run(); | |
var desiredInput = "success"; | |
var insertChar = (function(i, self) { | |
Module.userInput.push(desiredInput.charCodeAt(i)); | |
Module.resume("char_ready"); | |
if (i < desiredInput.length) { | |
return setTimeout((function() { self(i + 1, self) }), Math.floor(Math.random() * 1500)); | |
} | |
}); | |
insertChar(0, insertChar); | |
})(); | |
break; | |
}; | |
})); |
This file contains 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
var Module = { | |
userInput: [], | |
preRun: [], | |
postRun: [], | |
stdin: (function() { return Module.userInput.shift() || null; }), | |
print: (function() { | |
return function(text) { | |
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' '); | |
postMessage({ 'cmd': 'print', 'text': text }); | |
}; | |
})(), | |
printErr: function(text) { | |
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' '); | |
postMessage({ 'cmd': 'printErr', 'text': text }); | |
}, | |
}; |
This file contains 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
<script type='text/javascript'> | |
if (window.Worker) { | |
var worker = new Worker('emscripten_suspend.js'); | |
worker.addEventListener('message', (function(e) { | |
var data = e.data; | |
switch (data.cmd) { | |
case 'print': | |
console.log(data.text); | |
break; | |
case 'printErr': | |
console.error(data.text); | |
break; | |
}; | |
})); | |
worker.postMessage('init'); | |
} | |
</script> |
This file contains 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
#include <stdio.h> | |
#include <emscripten.h> | |
#define INPUT_LENGTH 7 | |
int main(int argc, char **argv) { | |
char input[INPUT_LENGTH] = {}; | |
for (int n = 0; n < INPUT_LENGTH; ++n) { | |
emscripten_suspend("char_ready"); | |
input[n] = getchar(); | |
printf("%c\n", input[n]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment