Skip to content

Commit 9a7bb31

Browse files
authored
Merge branch 'master' into wasm-better-panic
2 parents 878c131 + 098675d commit 9a7bb31

File tree

17 files changed

+323
-43
lines changed

17 files changed

+323
-43
lines changed

tests/snippets/floats.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@
8383
assert 1.0.__rtruediv__(2) == 2.0
8484
assert 2.0.__mul__(1) == 2.0
8585
assert 2.0.__rsub__(1) == -1.0
86+
87+
assert (1.7).real == 1.7
88+
assert (1.3).is_integer() == False
89+
assert (1.0).is_integer() == True

vm/src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ macro_rules! no_kwargs {
111111
};
112112
}
113113

114+
#[macro_export]
114115
macro_rules! py_module {
115116
( $ctx:expr, $module_name:expr, { $($name:expr => $value:expr),* $(,)* }) => {
116117
{

vm/src/obj/objfloat.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,19 @@ fn float_rmul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
418418
float_mul(vm, args)
419419
}
420420

421+
fn float_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
422+
arg_check!(vm, args, required = [(i, Some(vm.ctx.float_type()))]);
423+
let v = get_value(i);
424+
Ok(vm.ctx.new_float(v))
425+
}
426+
427+
fn float_is_integer(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
428+
arg_check!(vm, args, required = [(i, Some(vm.ctx.float_type()))]);
429+
let v = get_value(i);
430+
let result = v == v.round();
431+
Ok(vm.ctx.new_bool(result))
432+
}
433+
421434
pub fn init(context: &PyContext) {
422435
let float_type = &context.float_type;
423436

@@ -465,4 +478,10 @@ pub fn init(context: &PyContext) {
465478
);
466479
context.set_attr(&float_type, "__mul__", context.new_rustfunc(float_mul));
467480
context.set_attr(&float_type, "__rmul__", context.new_rustfunc(float_rmul));
481+
context.set_attr(&float_type, "real", context.new_property(float_real));
482+
context.set_attr(
483+
&float_type,
484+
"is_integer",
485+
context.new_rustfunc(float_is_integer),
486+
);
468487
}

vm/src/pyobject.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,29 @@ impl PyFuncArgs {
920920
}
921921
None
922922
}
923+
924+
pub fn get_optional_kwarg_with_type(
925+
&self,
926+
key: &str,
927+
ty: PyObjectRef,
928+
vm: &mut VirtualMachine,
929+
) -> Result<Option<PyObjectRef>, PyObjectRef> {
930+
match self.get_optional_kwarg(key) {
931+
Some(kwarg) => {
932+
if objtype::isinstance(&kwarg, &ty) {
933+
Ok(Some(kwarg))
934+
} else {
935+
let expected_ty_name = vm.to_pystr(&ty)?;
936+
let actual_ty_name = vm.to_pystr(&kwarg.typ())?;
937+
Err(vm.new_type_error(format!(
938+
"argument of type {} is required for named parameter `{}` (got: {})",
939+
expected_ty_name, key, actual_ty_name
940+
)))
941+
}
942+
}
943+
None => Ok(None),
944+
}
945+
}
923946
}
924947

925948
/// Rather than determining the type of a python object, this enum is more

wasm/demo/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
},
99
"devDependencies": {
1010
"@wasm-tool/wasm-pack-plugin": "0.2.0",
11-
"webpack": "^4.16.3",
12-
"webpack-cli": "^3.1.0",
13-
"webpack-dev-server": "^3.1.5",
1411
"copy-webpack-plugin": "^4.5.2",
15-
"mini-css-extract-plugin": "^0.5.0",
12+
"css-loader": "^2.0.1",
1613
"html-webpack-plugin": "^3.2.0",
17-
"css-loader": "^2.0.1"
14+
"mini-css-extract-plugin": "^0.5.0",
15+
"raw-loader": "^1.0.0",
16+
"webpack": "^4.16.3",
17+
"webpack-cli": "^3.1.0",
18+
"webpack-dev-server": "^3.1.5"
1819
},
1920
"scripts": {
2021
"dev": "webpack-dev-server -d",

wasm/demo/snippets/fetch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from browser import fetch
2+
3+
def fetch_handler(res):
4+
print(f"headers: {res['headers']}")
5+
6+
fetch(
7+
"https://httpbin.org/get",
8+
fetch_handler,
9+
lambda err: print(f"error: {err}"),
10+
response_format="json",
11+
headers={"X-Header-Thing": "rustpython is neat!"},
12+
)

wasm/demo/snippets/fibonacci.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
n1 = 0
2+
n2 = 1
3+
count = 0
4+
until = 10
5+
6+
print(f"These are the first {until} numbers in the Fibonacci sequence:")
7+
8+
while count < until:
9+
print(n1)
10+
n1, n2 = n2, n1 + n2
11+
count += 1

wasm/demo/snippets/fizzbuzz.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
for i in range(1, 100):
2+
print(f"{i} ", end="")
3+
if not i % 3:
4+
print("fizz", end="")
5+
if not i % 5:
6+
print("buzz", end="")
7+
print()

wasm/demo/snippets/mandelbrot.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# NOTE: will take a while, up to around a minute, to run.
2+
# Expect this page to freeze.
3+
4+
w = 50.0
5+
h = 50.0
6+
7+
y = 0.0
8+
while y < h:
9+
x = 0.0
10+
while x < w:
11+
Zr, Zi, Tr, Ti = 0.0, 0.0, 0.0, 0.0
12+
Cr = 2 * x / w - 1.5
13+
Ci = 2 * y / h - 1.0
14+
15+
i = 0
16+
while i < 50 and Tr + Ti <= 4:
17+
Zi = 2 * Zr * Zi + Ci
18+
Zr = Tr - Ti + Cr
19+
Tr = Zr * Zr
20+
Ti = Zi * Zi
21+
i = i + 1
22+
23+
if Tr + Ti <= 4:
24+
print('*', end='')
25+
else:
26+
print('·', end='')
27+
28+
x = x + 1
29+
30+
print()
31+
y = y + 1

wasm/demo/src/index.html renamed to wasm/demo/src/index.ejs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ <h1>RustPython Demo</h1>
99
<p>
1010
RustPython is a Python interpreter written in Rust. This demo is
1111
compiled from Rust to WebAssembly so it runs in the browser.<br>
12-
Please input your Python code below and click <kbd>Run</kbd>, or you
13-
can open up your browser's devtools and play with
14-
<code>rp.pyEval('print("a")')</code>
12+
Please input your Python code below and click <kbd>Run</kbd>
13+
(or <kbd>Ctrl+Enter</kbd>), or you can open up your
14+
browser's devtools and play with <code>rp.pyEval('print("a")')</code>
1515
</p>
16-
<textarea id="code">
17-
n1 = 0
18-
n2 = 1
19-
count = 0
20-
until = 10
21-
22-
print("These are the first {} numbers in the Fibonacci sequence:".format(until))
23-
24-
while count < until:
25-
print(n1)
26-
n1, n2 = n2, n1 + n2
27-
count += 1
16+
<div id="code-wrapper">
17+
<textarea id="code">
18+
<%= defaultSnippet %>
2819
</textarea>
20+
<select id="snippets">
21+
<% for (const name of snippets) { %>
22+
<option
23+
<% if (name == defaultSnippetName) %> selected
24+
><%= name %></option>
25+
<% } %>
26+
</select>
27+
</div>
2928
<button id="run-btn">Run &#9655;</button>
3029
<div id="error"></div>
3130
<h3>Standard Output</h3>

0 commit comments

Comments
 (0)