Skip to content

Commit 1c558fa

Browse files
authored
Merge pull request #1592 from alexcrichton/explicit-self
Include self-pointer in `Function` descriptions
2 parents 80c75df + afbd7d3 commit 1c558fa

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

crates/cli-support/src/js/js2rust.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ pub struct Js2Rust<'a, 'b: 'a> {
6868
/// The string value here is the class that this should be a constructor
6969
/// for.
7070
constructor: Option<String>,
71+
72+
/// whether or not we're generating a method
73+
method: bool,
7174
}
7275

7376
impl<'a, 'b> Js2Rust<'a, 'b> {
@@ -83,6 +86,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
8386
ret_ty: String::new(),
8487
ret_expr: String::new(),
8588
constructor: None,
89+
method: false,
8690
}
8791
}
8892

@@ -93,12 +97,19 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
9397
function: &Function,
9498
opt_arg_names: &Option<Vec<String>>,
9599
) -> Result<&mut Self, Error> {
100+
// Chop off the implicit i32 first argument if we're a method since it
101+
// was already handled by `method` below.
102+
let arguments = if self.method {
103+
&function.arguments[1..]
104+
} else {
105+
&function.arguments[..]
106+
};
96107
let arg_names = match opt_arg_names {
97108
Some(arg_names) => arg_names.iter().map(|s| Some(s.as_str())).collect(),
98-
None => vec![None; function.arguments.len()],
109+
None => vec![None; arguments.len()],
99110
};
100-
assert_eq!(arg_names.len(), function.arguments.len());
101-
for (arg, arg_name) in function.arguments.iter().zip(arg_names) {
111+
assert_eq!(arg_names.len(), arguments.len());
112+
for (arg, arg_name) in arguments.iter().zip(arg_names) {
102113
// Process the function argument and assert that the metadata about
103114
// the number of arguments on the Rust side required is correct.
104115
let before = self.rust_arguments.len();
@@ -124,6 +135,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
124135
/// Flag this shim as a method call into Rust, so the first Rust argument
125136
/// passed should be `this.ptr`.
126137
pub fn method(&mut self, consumed: bool) -> &mut Self {
138+
self.method = true;
127139
if self.cx.config.debug {
128140
self.prelude(
129141
"if (this.ptr === 0) {

crates/cli-support/src/webidl.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl<'a> Context<'a> {
580580
Some(class) => struct_function_export_name(class, export.function.name),
581581
None => export.function.name.to_string(),
582582
};
583-
let descriptor = match self.descriptors.remove(&wasm_name) {
583+
let mut descriptor = match self.descriptors.remove(&wasm_name) {
584584
None => return Ok(()),
585585
Some(d) => d.unwrap_function(),
586586
};
@@ -595,23 +595,32 @@ impl<'a> Context<'a> {
595595
match export.method_kind {
596596
decode::MethodKind::Constructor => AuxExportKind::Constructor(class),
597597
decode::MethodKind::Operation(op) => match op.kind {
598-
decode::OperationKind::Getter(f) => AuxExportKind::Getter {
599-
class,
600-
field: f.to_string(),
601-
},
602-
decode::OperationKind::Setter(f) => AuxExportKind::Setter {
603-
class,
604-
field: f.to_string(),
605-
},
598+
decode::OperationKind::Getter(f) => {
599+
descriptor.arguments.insert(0, Descriptor::I32);
600+
AuxExportKind::Getter {
601+
class,
602+
field: f.to_string(),
603+
}
604+
}
605+
decode::OperationKind::Setter(f) => {
606+
descriptor.arguments.insert(0, Descriptor::I32);
607+
AuxExportKind::Setter {
608+
class,
609+
field: f.to_string(),
610+
}
611+
}
606612
_ if op.is_static => AuxExportKind::StaticFunction {
607613
class,
608614
name: export.function.name.to_string(),
609615
},
610-
_ => AuxExportKind::Method {
611-
class,
612-
name: export.function.name.to_string(),
613-
consumed: export.consumed,
614-
},
616+
_ => {
617+
descriptor.arguments.insert(0, Descriptor::I32);
618+
AuxExportKind::Method {
619+
class,
620+
name: export.function.name.to_string(),
621+
consumed: export.consumed,
622+
}
623+
}
615624
},
616625
}
617626
}
@@ -931,7 +940,7 @@ impl<'a> Context<'a> {
931940
// Register a webidl transformation for the getter
932941
let (getter_id, _) = self.function_exports[&getter];
933942
let getter_descriptor = Function {
934-
arguments: Vec::new(),
943+
arguments: vec![Descriptor::I32],
935944
shim_idx: 0,
936945
ret: descriptor.clone(),
937946
};
@@ -956,7 +965,7 @@ impl<'a> Context<'a> {
956965

957966
let (setter_id, _) = self.function_exports[&setter];
958967
let setter_descriptor = Function {
959-
arguments: vec![descriptor],
968+
arguments: vec![Descriptor::I32, descriptor],
960969
shim_idx: 0,
961970
ret: Descriptor::Unit,
962971
};

0 commit comments

Comments
 (0)