Skip to content

Commit 8cf38f5

Browse files
committed
Move classmethods on JsValue to be normal methods
1 parent 072c872 commit 8cf38f5

File tree

3 files changed

+33
-84
lines changed

3 files changed

+33
-84
lines changed

derive/src/pyclass.rs

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ enum ClassItem {
1111
item_ident: Ident,
1212
py_name: String,
1313
},
14-
ClassMethod {
15-
item_ident: Ident,
16-
py_name: String,
17-
},
1814
Property {
1915
item_ident: Ident,
2016
py_name: String,
@@ -53,8 +49,8 @@ impl ClassItem {
5349
let nesteds = meta_to_vec(meta).map_err(|meta| {
5450
err_span!(
5551
meta,
56-
"#[pymethod = \"...\"] cannot be a name/value, you probably meant \
57-
#[pymethod(name = \"...\")]",
52+
"#[pyproperty = \"...\"] cannot be a name/value, you probably meant \
53+
#[pyproperty(name = \"...\")]",
5854
)
5955
})?;
6056
let mut py_name = None;
@@ -84,47 +80,6 @@ impl ClassItem {
8480
py_name: py_name.unwrap_or_else(|| sig.ident.to_string()),
8581
});
8682
attr_idx = Some(i);
87-
} else if name == "pyclassmethod" {
88-
if item.is_some() {
89-
bail_span!(
90-
sig.ident,
91-
"You can only have one #[py*] attribute on an impl item"
92-
)
93-
}
94-
let nesteds = meta_to_vec(meta).map_err(|meta| {
95-
err_span!(
96-
meta,
97-
"#[pyclassmethod = \"...\"] cannot be a name/value, you probably meant \
98-
#[pyclassmethod(name = \"...\")]",
99-
)
100-
})?;
101-
let mut py_name = None;
102-
for meta in nesteds {
103-
let meta = match meta {
104-
NestedMeta::Meta(meta) => meta,
105-
NestedMeta::Literal(_) => continue,
106-
};
107-
match meta {
108-
Meta::NameValue(name_value) => {
109-
if name_value.ident == "name" {
110-
if let Lit::Str(s) = &name_value.lit {
111-
py_name = Some(s.value());
112-
} else {
113-
bail_span!(
114-
&sig.ident,
115-
"#[pyclassmethod(name = ...)] must be a string"
116-
);
117-
}
118-
}
119-
}
120-
_ => {}
121-
}
122-
}
123-
item = Some(ClassItem::ClassMethod {
124-
item_ident: sig.ident.clone(),
125-
py_name: py_name.unwrap_or_else(|| sig.ident.to_string()),
126-
});
127-
attr_idx = Some(i);
12883
} else if name == "pyproperty" {
12984
if item.is_some() {
13085
bail_span!(
@@ -255,20 +210,18 @@ pub fn impl_pyimpl(_attr: AttributeArgs, item: Item) -> Result<TokenStream2, Dia
255210
_ => {}
256211
}
257212
}
258-
let methods = items.iter().filter_map(|item| match item {
259-
ClassItem::Method {
213+
let methods = items.iter().filter_map(|item| {
214+
if let ClassItem::Method {
260215
item_ident,
261216
py_name,
262-
} => Some(quote! {
263-
class.set_str_attr(#py_name, ctx.new_rustfunc(Self::#item_ident));
264-
}),
265-
ClassItem::ClassMethod {
266-
item_ident,
267-
py_name,
268-
} => Some(quote! {
269-
class.set_str_attr(#py_name, ctx.new_classmethod(Self::#item_ident));
270-
}),
271-
_ => None,
217+
} = item
218+
{
219+
Some(quote! {
220+
class.set_str_attr(#py_name, ctx.new_rustfunc(Self::#item_ident));
221+
})
222+
} else {
223+
None
224+
}
272225
});
273226
let properties = properties
274227
.iter()

wasm/lib/src/browser.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
from _window import window
55

66

7+
jsstr = window.new_from_str
8+
9+
710
_alert = window.get_prop("alert")
811

912

1013
def alert(msg):
1114
if type(msg) != str:
1215
raise TypeError("msg must be a string")
13-
_alert.call(JsValue.fromstr(msg))
16+
_alert.call(jsstr(msg))
1417

1518

1619
_confirm = window.get_prop("confirm")
@@ -19,7 +22,7 @@ def alert(msg):
1922
def confirm(msg):
2023
if type(msg) != str:
2124
raise TypeError("msg must be a string")
22-
return _confirm.call(JsValue.fromstr(msg)).as_bool()
25+
return _confirm.call(jsstr(msg)).as_bool()
2326

2427

2528
_prompt = window.get_prop("prompt")
@@ -31,7 +34,4 @@ def prompt(msg, default_val=None):
3134
if default_val is not None and type(default_val) != str:
3235
raise TypeError("default_val must be a string")
3336

34-
return _prompt.call(
35-
JsValue.fromstr(arg) for arg in [msg, default_val] if arg
36-
).as_str()
37-
37+
return _prompt.call(*(jsstr(arg) for arg in [msg, default_val] if arg)).as_str()

wasm/lib/src/js_module.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,28 @@ impl PyJsValue {
7070
}
7171
}
7272

73-
#[pyclassmethod]
74-
fn null(cls: PyClassRef, vm: &VirtualMachine) -> PyResult<PyJsValueRef> {
75-
PyJsValue::new(JsValue::NULL).into_ref_with_type(vm, cls)
73+
#[pymethod]
74+
fn null(&self, _vm: &VirtualMachine) -> PyJsValue {
75+
PyJsValue::new(JsValue::NULL)
7676
}
7777

78-
#[pyclassmethod]
79-
fn undefined(cls: PyClassRef, vm: &VirtualMachine) -> PyResult<PyJsValueRef> {
80-
PyJsValue::new(JsValue::UNDEFINED).into_ref_with_type(vm, cls)
78+
#[pymethod]
79+
fn undefined(&self, _vm: &VirtualMachine) -> PyJsValue {
80+
PyJsValue::new(JsValue::UNDEFINED)
8181
}
8282

83-
#[pyclassmethod]
84-
fn fromstr(cls: PyClassRef, s: PyStringRef, vm: &VirtualMachine) -> PyResult<PyJsValueRef> {
85-
PyJsValue::new(s.as_str()).into_ref_with_type(vm, cls)
83+
#[pymethod]
84+
fn new_from_str(&self, s: PyStringRef, _vm: &VirtualMachine) -> PyJsValue {
85+
PyJsValue::new(s.as_str())
8686
}
8787

88-
#[pyclassmethod]
89-
fn fromfloat(cls: PyClassRef, n: PyFloatRef, vm: &VirtualMachine) -> PyResult<PyJsValueRef> {
90-
PyJsValue::new(n.to_f64()).into_ref_with_type(vm, cls)
88+
#[pymethod]
89+
fn new_from_float(&self, n: PyFloatRef, _vm: &VirtualMachine) -> PyJsValue {
90+
PyJsValue::new(n.to_f64())
9191
}
9292

93-
#[pyclassmethod]
94-
fn new_object(
95-
cls: PyClassRef,
96-
opts: NewObjectOptions,
97-
vm: &VirtualMachine,
98-
) -> PyResult<PyJsValueRef> {
93+
#[pymethod]
94+
fn new_object(&self, opts: NewObjectOptions, vm: &VirtualMachine) -> PyResult<PyJsValue> {
9995
let value = if let Some(proto) = opts.prototype {
10096
if let Some(proto) = proto.value.dyn_ref::<Object>() {
10197
Object::create(proto)
@@ -107,7 +103,7 @@ impl PyJsValue {
107103
} else {
108104
Object::new()
109105
};
110-
PyJsValue::new(value).into_ref_with_type(vm, cls)
106+
Ok(PyJsValue::new(value))
111107
}
112108

113109
#[pymethod]

0 commit comments

Comments
 (0)