Skip to content

Commit d338047

Browse files
committed
Change JsValue to JSObject
1 parent e83e3cc commit d338047

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

wasm/lib/src/convert.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustpython_vm::pyobject::{PyObjectRef, PyResult, PyValue};
88
use rustpython_vm::VirtualMachine;
99

1010
use crate::browser_module;
11-
use crate::objjsvalue::{PyJsFunction, PyJsValue};
11+
use crate::objjsobject::{PyJsFunction, PyJsObject};
1212
use crate::vm_class::{stored_vm_from_wasm, WASMVirtualMachine};
1313

1414
pub fn py_err_to_js_err(vm: &VirtualMachine, py_err: &PyObjectRef) -> JsValue {
@@ -130,8 +130,8 @@ pub fn py_to_js(vm: &VirtualMachine, py_obj: PyObjectRef) -> JsValue {
130130
let view = Uint8Array::view(&bytes);
131131
view.slice(0, bytes.len() as u32).into()
132132
}
133-
} else if let Some(jsval) = py_obj.payload::<PyJsValue>() {
134-
jsval.value().clone()
133+
} else if let Some(jsval) = py_obj.payload::<PyJsObject>() {
134+
jsval.object().clone().into()
135135
} else if let Some(jsfunc) = py_obj.payload::<PyJsFunction>() {
136136
jsfunc.to_function().into()
137137
} else {
@@ -197,7 +197,9 @@ pub fn js_to_py_with_this(
197197
u8_array.copy_to(&mut vec);
198198
vm.ctx.new_bytes(vec)
199199
} else {
200-
PyJsValue::new(js_val).into_ref(vm).into_object()
200+
PyJsObject::new(js_val.unchecked_into())
201+
.into_ref(vm)
202+
.into_object()
201203
// let dict = vm.ctx.new_dict();
202204
// for pair in object_entries(&Object::from(js_val)) {
203205
// let (key, val) = pair.expect("iteration over object to not fail");

wasm/lib/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod browser_module;
22
pub mod convert;
3-
pub mod objjsvalue;
3+
pub mod objjsobject;
44
pub mod vm_class;
55
pub mod wasm_builtins;
66

wasm/lib/src/objjsvalue.rs renamed to wasm/lib/src/objjsobject.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use js_sys::{Function, JsString, Reflect};
1+
use js_sys::{Function, JsString, Object, Reflect};
22
use wasm_bindgen::JsValue;
33

44
use rustpython_vm::function::{Args, OptionalArg};
@@ -8,68 +8,68 @@ use rustpython_vm::VirtualMachine;
88

99
use crate::convert;
1010

11-
fn get_prop(value: JsValue, name: &str, vm: &VirtualMachine) -> Option<PyObjectRef> {
11+
fn get_prop(object: Object, name: &str, vm: &VirtualMachine) -> Option<PyObjectRef> {
1212
let name: &JsString = &name.into();
13-
if Reflect::has(&value, name).expect("Reflect.has failed") {
13+
if Reflect::has(&object, name).expect("Reflect.has failed") {
1414
Some(convert::js_to_py_with_this(
1515
vm,
16-
Reflect::get(&value, name).expect("Reflect.get failed"),
17-
Some(value),
16+
Reflect::get(&object, name).expect("Reflect.get failed"),
17+
Some(object.into()),
1818
))
1919
} else {
2020
None
2121
}
2222
}
2323

24-
fn set_prop(value: &JsValue, name: &str, val: PyObjectRef, vm: &VirtualMachine) {
24+
fn set_prop(value: &Object, name: &str, val: PyObjectRef, vm: &VirtualMachine) {
2525
Reflect::set(value, &name.into(), &convert::py_to_js(vm, val)).expect("Reflect failed");
2626
}
2727

28-
#[pyclass(name = "JsValue")]
28+
#[pyclass(name = "JSObject")]
2929
#[derive(Debug)]
30-
pub struct PyJsValue {
31-
value: JsValue,
30+
pub struct PyJsObject {
31+
object: Object,
3232
}
33-
pub type PyJsValueRef = PyRef<PyJsValue>;
33+
pub type PyJsObjectRef = PyRef<PyJsObject>;
3434

35-
impl PyValue for PyJsValue {}
35+
impl PyValue for PyJsObject {}
3636

3737
#[pyimpl]
38-
impl PyJsValue {
39-
pub fn new(value: JsValue) -> PyJsValue {
40-
PyJsValue { value }
38+
impl PyJsObject {
39+
pub fn new(object: Object) -> PyJsObject {
40+
PyJsObject { object }
4141
}
4242

43-
pub fn value(&self) -> &JsValue {
44-
&self.value
43+
pub fn object(&self) -> &Object {
44+
&self.object
4545
}
4646

4747
#[pyproperty(name = "_props")]
4848
fn props(&self, _vm: &VirtualMachine) -> PyJsProps {
4949
PyJsProps {
50-
value: self.value().clone(),
50+
object: self.object().clone(),
5151
}
5252
}
5353

5454
#[pymethod(name = "__getattr__")]
5555
fn getattr(&self, attr_name: PyStringRef, vm: &VirtualMachine) -> PyResult {
56-
get_prop(self.value().clone(), attr_name.as_str(), vm).ok_or_else(|| {
56+
get_prop(self.object().clone(), attr_name.as_str(), vm).ok_or_else(|| {
5757
vm.new_attribute_error(format!("JS value has no property {:?}", attr_name.as_str()))
5858
})
5959
}
6060

6161
#[pymethod(name = "__setattr__")]
6262
fn setattr(&self, attr_name: PyStringRef, val: PyObjectRef, vm: &VirtualMachine) {
63-
set_prop(self.value(), attr_name.as_str(), val, vm);
63+
set_prop(self.object(), attr_name.as_str(), val, vm);
6464
}
6565

6666
#[pymethod(name = "__repr__")]
6767
fn repr(&self, _vm: &VirtualMachine) -> String {
68-
format!("{:?}", self.value())
68+
format!("{:?}", self.object())
6969
}
7070
}
7171

72-
#[pyclass(name = "JsFunction")]
72+
#[pyclass(name = "JSFunction")]
7373
#[derive(Debug)]
7474
pub struct PyJsFunction {
7575
func: Function,
@@ -113,10 +113,10 @@ impl PyJsFunction {
113113
}
114114
}
115115

116-
#[pyclass(name = "JsProps")]
116+
#[pyclass(name = "JSProps")]
117117
#[derive(Debug)]
118118
struct PyJsProps {
119-
value: JsValue,
119+
object: Object,
120120
}
121121

122122
impl PyValue for PyJsProps {}
@@ -130,25 +130,25 @@ impl PyJsProps {
130130
default: OptionalArg,
131131
vm: &VirtualMachine,
132132
) -> PyObjectRef {
133-
get_prop(self.value.clone(), item_name.as_str(), vm)
133+
get_prop(self.object.clone(), item_name.as_str(), vm)
134134
.or(default.into_option())
135135
.unwrap_or_else(|| vm.get_none())
136136
}
137137

138138
#[pymethod(name = "__getitem__")]
139139
fn getitem(&self, item_name: PyStringRef, vm: &VirtualMachine) -> PyResult {
140-
get_prop(self.value.clone(), item_name.as_str(), vm)
140+
get_prop(self.object.clone(), item_name.as_str(), vm)
141141
.ok_or_else(|| vm.new_key_error(format!("{:?}", item_name.as_str())))
142142
}
143143

144144
#[pymethod(name = "__setitem__")]
145145
fn setitem(&self, item_name: PyStringRef, val: PyObjectRef, vm: &VirtualMachine) {
146-
set_prop(&self.value, item_name.as_str(), val, vm);
146+
set_prop(&self.object, item_name.as_str(), val, vm);
147147
}
148148
}
149149

150150
pub fn init(ctx: &PyContext) {
151-
ctx.add_class::<PyJsValue>();
151+
ctx.add_class::<PyJsObject>();
152152
ctx.add_class::<PyJsFunction>();
153153
ctx.add_class::<PyJsProps>();
154154
}

wasm/lib/src/vm_class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustpython_vm::VirtualMachine;
1313

1414
use crate::browser_module::setup_browser_module;
1515
use crate::convert;
16-
use crate::objjsvalue;
16+
use crate::objjsobject;
1717
use crate::wasm_builtins;
1818

1919
pub(crate) struct StoredVirtualMachine {
@@ -32,7 +32,7 @@ impl StoredVirtualMachine {
3232
setup_browser_module(&vm);
3333
}
3434
vm.wasm_id = Some(id);
35-
objjsvalue::init(&vm.ctx);
35+
objjsobject::init(&vm.ctx);
3636
StoredVirtualMachine {
3737
vm,
3838
scope: RefCell::new(scope),

0 commit comments

Comments
 (0)