Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add requestAnimationFrame to browser module
  • Loading branch information
coolreader18 committed Feb 25, 2019
commit 5dd2de0b1ee5f611603243be025efc483d130c1b
20 changes: 13 additions & 7 deletions wasm/demo/snippets/mandelbrot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# NOTE: will take a while, up to around a minute, to run.
# Expect this page to freeze.
from browser import request_animation_frame

w = 50.0
h = 50.0

y = 0.0
while y < h:
_y = {'y': 0.0}

def mandel(_time_elapsed=None):
y = _y['y']
if y >= h:
return
x = 0.0
while x < w:
Zr, Zi, Tr, Ti = 0.0, 0.0, 0.0, 0.0
Expand All @@ -18,14 +21,17 @@
Zr = Tr - Ti + Cr
Tr = Zr * Zr
Ti = Zi * Zi
i = i + 1
i += 1

if Tr + Ti <= 4:
print('*', end='')
else:
print('·', end='')

x = x + 1
x += 1

print()
y = y + 1
_y['y'] += 1
request_animation_frame(mandel)

mandel()
61 changes: 59 additions & 2 deletions wasm/lib/src/browser_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{convert, vm_class::AccessibleVM, wasm_builtins::window};
use futures::{future, Future};
use js_sys::Promise;
use rustpython_vm::obj::objstr;
use rustpython_vm::obj::{objint, objstr};
use rustpython_vm::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use rustpython_vm::VirtualMachine;
use wasm_bindgen::{prelude::*, JsCast};
Expand Down Expand Up @@ -125,11 +125,68 @@ fn browser_fetch(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.get_none())
}

fn browser_request_animation_frame(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(func, Some(vm.ctx.function_type()))]);

use std::{cell::RefCell, rc::Rc};

// this basic setup for request_animation_frame taken from:
// https://rustwasm.github.io/wasm-bindgen/examples/request-animation-frame.html

let f = Rc::new(RefCell::new(None));
let g = f.clone();

let func = func.clone();

let acc_vm = AccessibleVM::from_vm(vm);

*g.borrow_mut() = Some(Closure::wrap(Box::new(move |time: f64| {
let vm = &mut acc_vm
.upgrade()
.expect("that the vm is valid from inside of request_animation_frame");
let func = func.clone();
let args = PyFuncArgs {
args: vec![vm.ctx.new_float(time)],
kwargs: vec![],
};
let _ = vm.invoke(func, args);

let closure = f.borrow_mut().take();
drop(closure);
}) as Box<Fn(f64)>));

let id = window()
.request_animation_frame(&js_sys::Function::from(
g.borrow().as_ref().unwrap().as_ref().clone(),
))
.map_err(|err| convert::js_py_typeerror(vm, err))?;

Ok(vm.ctx.new_int(id))
}

fn browser_cancel_animation_frame(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(id, Some(vm.ctx.int_type()))]);

// fine because
let id = objint::get_value(id)
.to_string()
.parse()
.expect("bigint.to_string() to be parsable as i32");

window()
.cancel_animation_frame(id)
.map_err(|err| convert::js_py_typeerror(vm, err))?;

Ok(vm.get_none())
}

const BROWSER_NAME: &str = "browser";

pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
py_module!(ctx, BROWSER_NAME, {
"fetch" => ctx.new_rustfunc(browser_fetch)
"fetch" => ctx.new_rustfunc(browser_fetch),
"request_animation_frame" => ctx.new_rustfunc(browser_request_animation_frame),
"cancel_animation_frame" => ctx.new_rustfunc(browser_cancel_animation_frame),
})
}

Expand Down