Skip to content

Commit f8cce25

Browse files
committed
Formatting; move the + '\n' hack to eval().
1 parent b428f2e commit f8cce25

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

wasm/app/.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"tabWidth": 4
4+
}

wasm/app/bootstrap.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// A dependency graph that contains any wasm must all be imported
22
// asynchronously. This `bootstrap.js` file does the single async import, so
33
// that no one else needs to worry about it again.
4-
import("./index.js")
5-
.catch(e => console.error("Error importing `index.js`:", e));
4+
import('./index.js').catch(e =>
5+
console.error('Error importing `index.js`:', e)
6+
);

wasm/app/index.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
4+
<meta charset="utf-8" />
55
<title>RustPython Demo</title>
66
<style type="text/css" media="screen">
77
textarea {
@@ -32,9 +32,17 @@
3232
</head>
3333
<body>
3434
<h1>RustPython Demo</h1>
35-
<p>RustPython is a Python interpreter writter in Rust. This demo is compiled from Rust to WebAssembly so it runs in the browser</p>
35+
<p>
36+
RustPython is a Python interpreter writter in Rust. This demo is compiled
37+
from Rust to WebAssembly so it runs in the browser
38+
</p>
3639
<p>Please input your python code below and click <kbd>Run</kbd>:</p>
37-
<textarea id="code">n1 = 0
40+
<p>
41+
Alternatively, open up your browser's devtools and play with
42+
<code>rp.eval_py('print("a")')</code>
43+
</p>
44+
<textarea id="code">
45+
n1 = 0
3846
n2 = 1
3947
count = 0
4048
until = 10
@@ -45,13 +53,19 @@ <h1>RustPython Demo</h1>
4553
print(n1)
4654
n1, n2 = n2, n1 + n2
4755
count += 1
48-
</textarea>
56+
57+
</textarea>
4958
<button id="run-btn">Run &#9655;</button>
5059
<div id="error"></div>
5160
<script src="./bootstrap.js"></script>
5261
<h3>Standard Output</h3>
5362
<textarea id="console">Loading...</textarea>
5463

55-
<a href="https://github.com/RustPython/RustPython"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png" alt="Fork me on GitHub"></a>
64+
<a href="https://github.com/RustPython/RustPython"
65+
><img
66+
style="position: absolute; top: 0; right: 0; border: 0;"
67+
src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png"
68+
alt="Fork me on GitHub"
69+
/></a>
5670
</body>
5771
</html>

wasm/app/index.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
import * as rp from "rustpython_wasm";
1+
import * as rp from 'rustpython_wasm';
22

3+
// so people can play around with it
34
window.rp = rp;
45

56
function runCodeFromTextarea(_) {
6-
const consoleElement = document.getElementById("console");
7-
const errorElement = document.getElementById("error");
8-
// Clean the console
9-
consoleElement.value = "";
7+
const consoleElement = document.getElementById('console');
8+
const errorElement = document.getElementById('error');
9+
// Clean the console
10+
consoleElement.value = '';
1011

11-
const code = document.getElementById("code").value;
12-
try {
13-
if (!code.endsWith("\n")) {
14-
// HACK: if the code doesn't end with newline it crashes.
15-
rp.run_code(code + "\n");
16-
return;
12+
const code = document.getElementById('code').value;
13+
try {
14+
rp.run_code(code);
15+
} catch (e) {
16+
errorElement.textContent = e;
17+
console.error(e);
1718
}
18-
19-
rp.run_code(code);
20-
} catch (e) {
21-
errorElement.textContent = e;
22-
console.error(e);
23-
}
2419
}
2520

2621
document
27-
.getElementById("run-btn")
28-
.addEventListener("click", runCodeFromTextarea);
22+
.getElementById('run-btn')
23+
.addEventListener('click', runCodeFromTextarea);
2924

3025
runCodeFromTextarea(); // Run once for demo

wasm/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,17 @@ fn py_to_js(vm: &mut VirtualMachine, py_obj: &PyObjectRef) -> JsValue {
6666
}
6767

6868
fn eval(vm: &mut VirtualMachine, source: &str) -> PyResult {
69-
let code_obj = compile::compile(vm, &source.to_string(), compile::Mode::Exec, None)?;
69+
// HACK: if the code doesn't end with newline it crashes.
70+
let mut source = source.to_string();
71+
if !source.ends_with('\n') {
72+
source.push('\n');
73+
}
74+
75+
let code_obj = compile::compile(vm, &source, compile::Mode::Exec, None)?;
7076

7177
let builtins = vm.get_builtin_scope();
7278
let vars = vm.context().new_scope(Some(builtins));
79+
7380
vm.run_code_obj(code_obj, vars)
7481
}
7582

@@ -96,8 +103,9 @@ pub fn run_code(source: &str) -> Result<JsValue, JsValue> {
96103
console::log_1(&source.to_string().into());
97104

98105
let mut vm = VirtualMachine::new();
106+
99107
// We are monkey-patching the builtin print to use console.log
100-
// TODO: moneky-patch sys.stdout instead, after print actually uses sys.stdout
108+
// TODO: monkey-patch sys.stdout instead, after print actually uses sys.stdout
101109
vm.ctx.set_attr(
102110
&vm.builtins,
103111
"print",

0 commit comments

Comments
 (0)