Comments (22)
Right now we're hacking apart python.js by doing this:
function BMb(c, e) {
/* This is raw_input */
var val = prompt("raw_input","");
var arr = [];
for(var i = 0; i < val.length; i++){
arr.push(val.charCodeAt(i));
}
return FQ(u(arr, 0, t));
}
var RMb = Runtime.a(BMb, "_builtin_raw_input")
from emscripten.
I just committed most of the necessary code for this. I should be able to finish that and update the demo in the next few days.
from emscripten.
Thanks heaps :)
from emscripten.
Can this by any chance be asynchronous?
(i.e. block execution until the input is received by means of a callback)
Thanks.
from emscripten.
I can't see how that would work, the python internal expects c-style execution. With a prompt execution halts until the prompt has received input.
Perhaps as an alternative, to make it flexible, Emscripten could allow one to define ones own STDIN function? So it would do something like:
/* Emscripten default: */
emscripten = {
stdin: function(){return prompt("?")}
}
/* Your code: */
emscripten.stdin = function(){return myInput.value}
from emscripten.
I understand this, but using the browser prompt is sub-optimal.
Maybe could be done in the user-space using the signal module?
from emscripten.
It will be possible to easily change the stdin implementation, to replace window.prompt, so you can have it run a callback. However it will necessarily be synchronous because that is how CPython works, I don't see a way around that.
amasad, not sure what you mean by using the signal module here?
from emscripten.
I'm not really a Python programmer. But from what I know is that most VMs implement pause and resume methods, so I was looking for a way to possibly suspend/pause execution in Python VM until the user input is received and then to invoke resume() to continue execution.
The only thing I came around in my search is Python's signal.pause()
from emscripten.
Calling window.prompt will pause execution, because it is synchronous. So it will behave exactly like raw_input in normal Python.
Likewise if you replace the stdin function, it will pause until your callback finishes.
from emscripten.
Ok, pushed an update to the demo, please tell me if it works ok.
http://syntensity.com/static/python.html
The example code includes raw_input so just pressing execute should test it.
Will push an additional commit soon to allow modifying Module.stdin to customize the interactive input, I liked that idea (thanks!). Still waiting on automatic tests.
from emscripten.
I understand that window.prompt is blocking. But what if the input is to be received using a console like this one: http://amasad.github.com/jq-console/
It would be asynchronous, and the code would may look something like this:
emscripten.stdin(function (input_callback) {
jqconsole.Input(input_callback);
});
jqconsole.Input
will execute the passed callback argument when the input is received passing the inputted string.
Btw the demo is working great!
from emscripten.
I see, so you would want to open a terminal like that, and wait for it to asynchronously return what the user enters? I don't know of a way to do that in JavaScript - basically need to wait synchronously for an asynchronous event. (With generators it might be done, perhaps, but they aren't widespread yet.(
In other news, final part of this issue is in f83a7e7. Marking as closed (just for the main issue, we can of course continue to discuss other stuff here).
from emscripten.
I think using the Module.stdin style of hook it would be possible to use a terminal system like yours if combined with something like Web Workers. Or with some method of polling.
from emscripten.
Unfortunately Web Workers do not support blocking (pausing, suspending) or even busy waiting (that locks the whole worker). I don't think it's possible to implement blocking on the Emscripten-level, but it might be doable in CPython specifically, if the CPython VM has a way to suspend and resume.
from emscripten.
I was trying to explore using signal.pause. The signal module did successfully load, however ran into the following error when tried calling the pause method:
JS crash: |ReferenceError: _pause is not defined|. Please let us know about this problem!
from emscripten.
I think it would be possible to combine a blocking while loop with a background worker to get the effect you want. In fact, it should be completely doable without a web worker. If a CPU can do it, JavaScript can. It just won't necessarily be very nice :P
from emscripten.
taybenlor, this would also mean blocking the browser's event loop which means blocking keydown/press events meaning we can't get the user input :S
from emscripten.
Unfortunately JS uses a simple, blocking single-threaded event loop. A busy loop will hang your browser, regardless of whatever else may happen on the page (try it!). The same happens in a web worker. There's no way to toggle a mutex and leave the loop since nothing is allowed to run in the same JS context (thread) while you're inside a busy loop, and you can't access the worker context from another context; the busy loop will prevent the onmessage handler from running.
from emscripten.
Mmmm, you're right. I would still like to look into the Web Worker spec. It seems like it should be possible.
from emscripten.
Also, alternative direction to head with this is to make emscripten work based on deferred execution. So that you can essentially "pause" it, then resume it later.
from emscripten.
That was my reaction as well. However, the spec has no mention of blocking or pausing a worker - you can only create, terminate and send messages. Something as simple as a processPendingEvents() API would have solved all of this.
from emscripten.
Perhaps @amasad should be using something more like http://bellard.org/jslinux/
from emscripten.
Related Issues (20)
- Chromium CI tests are failing flakily, probably due to a race between installing and using port headers
- emsdk/upstream/emscripten/cache/sysroot/include/compat/emmintrin.h is not lib/clang/19/include/**/emmintrin.h HOT 7
- ReferenceError: _setThrew is not defined when linking as C++ HOT 18
- Bug in page: Emscripten Documentation | Small Typo HOT 1
- [Question] How to write unit tests for multithreaded WASM? HOT 3
- undefined symbol: emscripten_compute_dom_pk_code HOT 2
- Cannot compile after switching to 3.1.63 HOT 3
- [BUG]: Incorrect Parameter Order in Promise Constructor Causes Program Errors
- wasm-opt: unknown option '--no-stack-ir' HOT 13
- Unexpected error in system/lib/libcxx/include/__format/format_arg_store.h HOT 2
- Is `--fatal-warnings` too strict for `STRICT` mode? HOT 4
- Met `function signature mismatch` warning and got `RuntimeError: unreachable` in execution HOT 1
- doWritev has a race condition HOT 5
- Unexpected error in system/lib/libcxx/include/__utility/no_destroy.h HOT 1
- Opening a JS based file with WASMFS hits unimplemented code HOT 1
- Linking against `noderawfs.js` causes `ioctl_tiocgwinsz` to fail HOT 1
- Argument order passed to the em_dlopen_callback does not match the declaration.
- FEATURE REQUEST: browser's jpg, png, webp decoders should be usable from emscripten HOT 4
- It seems C standard library headers are being included before C++ standard library headers.
- Can emscripten_fiber_t only be accessed within one thread?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from emscripten.