diff --git a/wasm/demo/package.json b/wasm/demo/package.json index 2b6dd27fa72..b05fe5c8a84 100644 --- a/wasm/demo/package.json +++ b/wasm/demo/package.json @@ -9,14 +9,13 @@ "xterm": "^3.8.0" }, "devDependencies": { - "@wasm-tool/wasm-pack-plugin": "0.2.0", - "copy-webpack-plugin": "^4.5.2", - "css-loader": "^2.0.1", + "@wasm-tool/wasm-pack-plugin": "^1.1.0", + "clean-webpack-plugin": "^3.0.0", + "css-loader": "^3.4.1", "html-webpack-plugin": "^3.2.0", - "mini-css-extract-plugin": "^0.5.0", - "raw-loader": "^1.0.0", + "mini-css-extract-plugin": "^0.9.0", + "raw-loader": "^4.0.0", "serve": "^11.0.2", - "start-server-and-test": "^1.7.11", "webpack": "^4.16.3", "webpack-cli": "^3.1.0", "webpack-dev-server": "^3.1.5" @@ -25,8 +24,7 @@ "dev": "webpack-dev-server -d", "build": "webpack", "dist": "webpack --mode production", - "test": "cd ../tests; pipenv run pytest", - "ci": "start-server-and-test 'npm run build && serve dist/ -n -p 8080' :8080 test" + "test": "webpack && cd ../tests && pipenv run pytest" }, "repository": { "type": "git", diff --git a/wasm/demo/snippets/asyncbrowser.py b/wasm/demo/snippets/asyncbrowser.py new file mode 100644 index 00000000000..5c4d1fcb6e5 --- /dev/null +++ b/wasm/demo/snippets/asyncbrowser.py @@ -0,0 +1,68 @@ +import browser +import functools + + +# just setting up the framework, skip to the bottom to see the real code + +ready = object() +go = object() + + +def run(coro, *, payload=None, error=False): + send = coro.throw if error else coro.send + try: + cmd = send(payload) + except StopIteration: + return + if cmd is ready: + coro.send( + ( + lambda *args: run(coro, payload=args), + lambda *args: run(coro, payload=args, error=True), + ) + ) + elif cmd is go: + pass + else: + raise RuntimeError(f"expected cmd to be ready or go, got {cmd}") + + +class JSFuture: + def __init__(self, prom): + self._prom = prom + + def __await__(self): + done, error = yield ready + self._prom.then(done, error) + res, = yield go + return res + + +def wrap_prom_func(func): + @functools.wraps(func) + async def wrapper(*args, **kwargs): + return await JSFuture(func(*args, **kwargs)) + + return wrapper + + +fetch = wrap_prom_func(browser.fetch) + +################### +# Real code start # +################### + + +async def main(delay): + url = f"https://httpbin.org/delay/{delay}" + print(f"fetching {url}...") + res = await fetch( + url, response_format="json", headers={"X-Header-Thing": "rustpython is neat!"} + ) + print(f"got res from {res['url']}:") + print(res, end="\n\n") + + +for delay in range(3): + run(main(delay)) +print() diff --git a/wasm/demo/src/index.ejs b/wasm/demo/src/index.ejs index a2a02d214b0..314ed7f31f6 100644 --- a/wasm/demo/src/index.ejs +++ b/wasm/demo/src/index.ejs @@ -9,9 +9,9 @@
RustPython is a Python interpreter written in Rust. This demo is
compiled from Rust to WebAssembly so it runs in the browser.
- Please input your Python code below and click Run
+ Input your Python code below and click Run
(or Ctrl+Enter), or you can open up your
- browser's devtools and play with rp.pyEval('print("a")')
+ browser's devtools and play with rp.pyEval('1 + 1')