Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4020ee7
Add a bare-bones VirtualMachine class to the WASM library
coolreader18 Jan 30, 2019
13b2f83
Fix vms.get()
coolreader18 Jan 30, 2019
f064bca
Add ids() function to VMStore and destroy() method to VirtualMachine
coolreader18 Jan 30, 2019
d0a4f9d
Make destroy() method public
coolreader18 Jan 30, 2019
b32b732
Run cargo fmt
coolreader18 Jan 30, 2019
136a476
Call assert_valid() in destroy()
coolreader18 Jan 31, 2019
f47864f
Return Ok(()) from destroy()
coolreader18 Jan 31, 2019
3bec226
Rename VMStore
coolreader18 Feb 5, 2019
f7c91c7
Add add_to_scope() method for WASM VM
coolreader18 Feb 5, 2019
de3762c
Add .run(); fix .addToScope(); add panic-catcher for debugging
coolreader18 Feb 5, 2019
a24cd83
Merge branch 'master' into wasm-vm-class
coolreader18 Feb 5, 2019
9b6516e
Allow closures to be passed from Python to JS if using a WASM VM
coolreader18 Feb 6, 2019
6851767
Fix RefCell borrowing errors
coolreader18 Feb 17, 2019
f55a8ea
Merge branch 'master' into wasm-vm-class
coolreader18 Feb 17, 2019
c83ff47
Fix error with new compile() and set_attr/item()
coolreader18 Feb 17, 2019
3e22c4f
Add print to default vm scope
coolreader18 Feb 17, 2019
a86069d
Store the WASM id in the VirtualMachine, add a (broken) fetch builtin
coolreader18 Feb 17, 2019
f00c3c8
Fix fetch builtin
coolreader18 Feb 17, 2019
00bc9e9
Convert fetch to use wasm_bindgen_futures
coolreader18 Feb 17, 2019
d0b4751
Remove the VM pointer from the map when it's dropped
coolreader18 Feb 17, 2019
7405c84
Add some more options to fetch()
coolreader18 Feb 17, 2019
b043f21
Fix js_py_typeerror
coolreader18 Feb 17, 2019
416f088
Convert `ArrayBuffer`s and `TypedArray`s to Python bytearrays
coolreader18 Feb 17, 2019
bead3f6
Fix conversion from ArrayBuffer, allow array_buffer in fetch
coolreader18 Feb 17, 2019
5244707
Add eval() method, rename run() to exec()
coolreader18 Feb 17, 2019
74e7131
Clean up some code
coolreader18 Feb 18, 2019
101ee77
Improve error messages
coolreader18 Feb 18, 2019
0d3d090
Convert pyEval to use a WASM VM, allowing closures and stuff
coolreader18 Feb 18, 2019
634571f
Remove WASM fetch builtin
coolreader18 Feb 18, 2019
8e5073e
Convert `bytes` and `bytearray`s to `Uint8Array`s
coolreader18 Feb 19, 2019
8c222af
Include details about thread_local! for WASM
coolreader18 Feb 21, 2019
9e17690
Use crate:: imports
coolreader18 Feb 22, 2019
65857e7
Merge
coolreader18 Feb 23, 2019
955d0b3
Merge branch 'master' into wasm-vm-class
coolreader18 Feb 23, 2019
7fa0a0c
Fix open in new make_module
coolreader18 Feb 23, 2019
09e2a7a
Add js_name for .set_stdout()
coolreader18 Feb 23, 2019
e0f222c
Don't hold on to a PyObjectRef from a python -> js closure
coolreader18 Feb 23, 2019
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
Prev Previous commit
Next Next commit
Remove WASM fetch builtin
  • Loading branch information
coolreader18 committed Feb 18, 2019
commit 634571fd2ecac71a7e9fc32807d9acd13b8ab183
10 changes: 4 additions & 6 deletions wasm/lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
mod convert;
mod vm_class;
mod wasm_builtins;
pub mod convert;
pub mod vm_class;
pub mod wasm_builtins;

extern crate futures;
extern crate js_sys;
#[macro_use]
extern crate rustpython_vm;
extern crate futures;
extern crate wasm_bindgen;
extern crate wasm_bindgen_futures;
extern crate web_sys;

use js_sys::{Object, Reflect, TypeError};
Expand Down
4 changes: 2 additions & 2 deletions wasm/lib/src/vm_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl VMStore {
}

#[derive(Clone)]
pub(crate) struct AccessibleVM {
pub struct AccessibleVM {
weak: Weak<RefCell<StoredVirtualMachine>>,
id: String,
}
Expand Down Expand Up @@ -148,7 +148,7 @@ impl From<&WASMVirtualMachine> for AccessibleVM {
}
}

pub(crate) struct AccessibleVMPtr<'a> {
pub struct AccessibleVMPtr<'a> {
id: String,
top_level: bool,
inner: &'a mut VirtualMachine,
Expand Down
116 changes: 2 additions & 114 deletions wasm/lib/src/wasm_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
extern crate futures;
extern crate js_sys;
extern crate wasm_bindgen;
extern crate wasm_bindgen_futures;
extern crate web_sys;

use crate::{convert, vm_class::AccessibleVM};
use futures::{future, Future};
use js_sys::{Array, Promise};
use crate::convert;
use js_sys::Array;
use rustpython_vm::obj::{objstr, objtype};
use rustpython_vm::pyobject::{IdProtocol, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use rustpython_vm::VirtualMachine;
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::{future_to_promise, JsFuture};
use web_sys::{console, HtmlTextAreaElement};

fn window() -> web_sys::Window {
Expand Down Expand Up @@ -107,116 +104,7 @@ pub fn builtin_print_console(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyRes
Ok(vm.get_none())
}

enum FetchResponseFormat {
Json,
Text,
ArrayBuffer,
}

impl FetchResponseFormat {
fn from_str(vm: &mut VirtualMachine, s: &str) -> Result<Self, PyObjectRef> {
match s {
"json" => Ok(FetchResponseFormat::Json),
"text" => Ok(FetchResponseFormat::Text),
"array_buffer" => Ok(FetchResponseFormat::ArrayBuffer),
_ => Err(vm.new_type_error("Unkown fetch response_format".into())),
}
}
fn get_response(&self, response: &web_sys::Response) -> Result<Promise, JsValue> {
match self {
FetchResponseFormat::Json => response.json(),
FetchResponseFormat::Text => response.text(),
FetchResponseFormat::ArrayBuffer => response.array_buffer(),
}
}
}

fn builtin_fetch(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(url, Some(vm.ctx.str_type())),
(handler, Some(vm.ctx.function_type()))
],
// TODO: use named parameters for these
optional = [
(reject_handler, Some(vm.ctx.function_type())),
(response_format, Some(vm.ctx.str_type())),
(method, Some(vm.ctx.str_type())),
(headers, Some(vm.ctx.dict_type()))
]
);

let response_format = match response_format {
Some(s) => FetchResponseFormat::from_str(vm, &objstr::get_value(s))?,
None => FetchResponseFormat::Text,
};

let mut opts = web_sys::RequestInit::new();

match method {
Some(s) => opts.method(&objstr::get_value(s)),
None => opts.method("GET"),
};

let request = web_sys::Request::new_with_str_and_init(&objstr::get_value(url), &opts)
.map_err(|err| convert::js_py_typeerror(vm, err))?;

if let Some(headers) = headers {
use rustpython_vm::obj::objdict;
let h = request.headers();
for (key, value) in objdict::get_key_value_pairs(headers) {
let key = objstr::get_value(&vm.to_str(&key)?);
let value = objstr::get_value(&vm.to_str(&value)?);
h.set(&key, &value)
.map_err(|err| convert::js_py_typeerror(vm, err))?;
}
}

let window = window();
let request_prom = window.fetch_with_request(&request);

let handler = handler.clone();
let reject_handler = reject_handler.cloned();

let acc_vm = AccessibleVM::from_vm(vm);

let future = JsFuture::from(request_prom)
.and_then(move |val| {
let response = val
.dyn_into::<web_sys::Response>()
.expect("val to be of type Response");
response_format.get_response(&response)
})
.and_then(|prom| JsFuture::from(prom))
.then(move |val| {
let vm = &mut acc_vm
.upgrade()
.expect("that the VM *not* be destroyed while promise is being resolved");
match val {
Ok(val) => {
let val = convert::js_to_py(vm, val);
let args = PyFuncArgs::new(vec![val], vec![]);
let _ = vm.invoke(handler, args);
}
Err(val) => {
if let Some(reject_handler) = reject_handler {
let val = convert::js_to_py(vm, val);
let args = PyFuncArgs::new(vec![val], vec![]);
let _ = vm.invoke(reject_handler, args);
}
}
}
future::ok(JsValue::UNDEFINED)
});
future_to_promise(future);

Ok(vm.get_none())
}

pub fn setup_wasm_builtins(vm: &mut VirtualMachine, scope: &PyObjectRef) {
let ctx = vm.context();
ctx.set_attr(scope, "print", ctx.new_rustfunc(builtin_print_console));
ctx.set_attr(scope, "fetch", ctx.new_rustfunc(builtin_fetch));
}