Skip to content

Commit

Permalink
full qualify Address to avoid collision with wit record of same name (#…
Browse files Browse the repository at this point in the history
…960)

* full qualify Address to avoid collision with wit record of same name

* add test for fully qualified java address

---------

Co-authored-by: Alex Crichton <alex@alexcrichton.com>
  • Loading branch information
nick1udwig and alexcrichton authored Jun 3, 2024
1 parent 3ca7739 commit 2645c4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
52 changes: 26 additions & 26 deletions crates/teavm-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1569,15 +1569,15 @@ impl Bindgen for FunctionBindgen<'_, '_> {
// Note that we can only reliably use `Address.ofData` for elements with alignment <= 4 because as
// of this writing TeaVM does not guarantee 64 bit items are aligned on 8 byte boundaries.
if realloc.is_none() && size <= 4 {
results.push(format!("Address.ofData({op}).toInt()"));
results.push(format!("org.teavm.interop.Address.ofData({op}).toInt()"));
} else {
let address = self.locals.tmp("address");
let ty = ty.to_upper_camel_case();

uwrite!(
self.src,
"
Address {address} = Memory.malloc({size} * ({op}).length, {size});
org.teavm.interop.Address {address} = Memory.malloc({size} * ({op}).length, {size});
Memory.put{ty}s({address}, {op}, 0, ({op}).length);
"
);
Expand Down Expand Up @@ -1606,7 +1606,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.src,
"
{ty}[] {array} = new {ty}[{length}];
Memory.get{ty_upper}s(Address.fromInt({address}), {array}, 0, ({array}).length);
Memory.get{ty_upper}s(org.teavm.interop.Address.fromInt({address}), {array}, 0, ({array}).length);
"
);

Expand All @@ -1622,14 +1622,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
);

if realloc.is_none() {
results.push(format!("Address.ofData({bytes}).toInt()"));
results.push(format!("org.teavm.interop.Address.ofData({bytes}).toInt()"));
} else {
let address = self.locals.tmp("address");

uwrite!(
self.src,
"
Address {address} = Memory.malloc({bytes}.length, 1);
org.teavm.interop.Address {address} = Memory.malloc({bytes}.length, 1);
Memory.putBytes({address}, {bytes}, 0, {bytes}.length);
"
);
Expand All @@ -1648,7 +1648,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.src,
"
byte[] {bytes} = new byte[{length}];
Memory.getBytes(Address.fromInt({address}), {bytes}, 0, {length});
Memory.getBytes(org.teavm.interop.Address.fromInt({address}), {bytes}, 0, {length});
"
);

Expand Down Expand Up @@ -1724,7 +1724,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
{body}
{array}.add({result});
}}
Memory.free(Address.fromInt({address}), ({length}) * {size}, {align});
Memory.free(org.teavm.interop.Address.fromInt({address}), ({length}) * {size}, {align});
"
);

Expand Down Expand Up @@ -1829,7 +1829,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
{
uwriteln!(
self.src,
"Memory.free(Address.fromInt({address}), {size}, {align});"
"Memory.free(org.teavm.interop.Address.fromInt({address}), {size}, {align});"
);
}

Expand All @@ -1838,7 +1838,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.src,
"
for ({}Cleanup cleanup : cleanupList) {{
Memory.free(Address.fromInt(cleanup.address), cleanup.size, cleanup.align);
Memory.free(org.teavm.interop.Address.fromInt(cleanup.address), cleanup.size, cleanup.align);
}}
",
self.gen.gen.qualifier()
Expand All @@ -1862,85 +1862,85 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::I32Load { offset }
| Instruction::PointerLoad { offset }
| Instruction::LengthLoad { offset } => results.push(format!(
"Address.fromInt(({}) + {offset}).getInt()",
"org.teavm.interop.Address.fromInt(({}) + {offset}).getInt()",
operands[0]
)),

Instruction::I32Load8U { offset } => results.push(format!(
"(((int) Address.fromInt(({}) + {offset}).getByte()) & 0xFF)",
"(((int) org.teavm.interop.Address.fromInt(({}) + {offset}).getByte()) & 0xFF)",
operands[0]
)),

Instruction::I32Load8S { offset } => results.push(format!(
"((int) Address.fromInt(({}) + {offset}).getByte())",
"((int) org.teavm.interop.Address.fromInt(({}) + {offset}).getByte())",
operands[0]
)),

Instruction::I32Load16U { offset } => results.push(format!(
"(((int) Address.fromInt(({}) + {offset}).getShort()) & 0xFFFF)",
"(((int) org.teavm.interop.Address.fromInt(({}) + {offset}).getShort()) & 0xFFFF)",
operands[0]
)),

Instruction::I32Load16S { offset } => results.push(format!(
"((int) Address.fromInt(({}) + {offset}).getShort())",
"((int) org.teavm.interop.Address.fromInt(({}) + {offset}).getShort())",
operands[0]
)),

Instruction::I64Load { offset } => results.push(format!(
"Address.fromInt(({}) + {offset}).getLong()",
"org.teavm.interop.Address.fromInt(({}) + {offset}).getLong()",
operands[0]
)),

Instruction::F32Load { offset } => results.push(format!(
"Address.fromInt(({}) + {offset}).getFloat()",
"org.teavm.interop.Address.fromInt(({}) + {offset}).getFloat()",
operands[0]
)),

Instruction::F64Load { offset } => results.push(format!(
"Address.fromInt(({}) + {offset}).getDouble()",
"org.teavm.interop.Address.fromInt(({}) + {offset}).getDouble()",
operands[0]
)),

Instruction::I32Store { offset }
| Instruction::PointerStore { offset }
| Instruction::LengthStore { offset } => uwriteln!(
self.src,
"Address.fromInt(({}) + {offset}).putInt({});",
"org.teavm.interop.Address.fromInt(({}) + {offset}).putInt({});",
operands[1],
operands[0]
),

Instruction::I32Store8 { offset } => uwriteln!(
self.src,
"Address.fromInt(({}) + {offset}).putByte((byte) ({}));",
"org.teavm.interop.Address.fromInt(({}) + {offset}).putByte((byte) ({}));",
operands[1],
operands[0]
),

Instruction::I32Store16 { offset } => uwriteln!(
self.src,
"Address.fromInt(({}) + {offset}).putShort((short) ({}));",
"org.teavm.interop.Address.fromInt(({}) + {offset}).putShort((short) ({}));",
operands[1],
operands[0]
),

Instruction::I64Store { offset } => uwriteln!(
self.src,
"Address.fromInt(({}) + {offset}).putLong({});",
"org.teavm.interop.Address.fromInt(({}) + {offset}).putLong({});",
operands[1],
operands[0]
),

Instruction::F32Store { offset } => uwriteln!(
self.src,
"Address.fromInt(({}) + {offset}).putFloat({});",
"org.teavm.interop.Address.fromInt(({}) + {offset}).putFloat({});",
operands[1],
operands[0]
),

Instruction::F64Store { offset } => uwriteln!(
self.src,
"Address.fromInt(({}) + {offset}).putDouble({});",
"org.teavm.interop.Address.fromInt(({}) + {offset}).putDouble({});",
operands[1],
operands[0]
),
Expand All @@ -1950,14 +1950,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::GuestDeallocate { size, align } => {
uwriteln!(
self.src,
"Memory.free(Address.fromInt({}), {size}, {align});",
"Memory.free(org.teavm.interop.Address.fromInt({}), {size}, {align});",
operands[0]
)
}

Instruction::GuestDeallocateString => uwriteln!(
self.src,
"Memory.free(Address.fromInt({}), {}, 1);",
"Memory.free(org.teavm.interop.Address.fromInt({}), {}, 1);",
operands[0],
operands[1]
),
Expand Down Expand Up @@ -2023,7 +2023,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {

uwriteln!(
self.src,
"Memory.free(Address.fromInt({address}), ({length}) * {size}, {align});"
"Memory.free(org.teavm.interop.Address.fromInt({address}), ({length}) * {size}, {align});"
);
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/codegen/fully-qualified-java-address.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package foo:bar;

interface my-import {
record address {
field: u32,
attribute: bool,
}
}

world bindings {
import my-import;

export init: func(baz: string);
}

0 comments on commit 2645c4f

Please sign in to comment.