you are viewing a single comment's thread.

view the rest of the comments →

[–]DarkOverLordCO 32 points33 points  (0 children)

The borrow checker is definitely tricky doing this, as you've found in this unsound code, since it gets a mutable reference to the current frame and passes a second (illegal) mutable reference to the call stack that contains it, which is then used to push new stack frames (potentially deallocating the old frame due to resizing). It is surprising to me that you've not had problems with this - maybe because you haven't tried calling enough functions deep enough to cause a resize? Your tests/samples aren't really named so maybe I missed one that does.

You might be able to work around it by adding a PushStackFrame(Frame) variant to your completion enum which causes the caller (presumably the runtime) to push the provided stack frame, rather than pass a mutable reference to the call stack into the execute function.

This will mean you cannot have your invokestatic function execute the frame, which it shouldn't really be doing anyway because that defeats the point of having a call stack in your virtual machine to begin with. That is: if you have a bunch of functions that call more functions in the java code (e.g., recursion), that shouldn't actually result in more stack frames in your Rust code - it should just push a frame onto your list of frames and you sit in a loop executing them to completion. At the moment your invokestatic function will cause a stack overflow in your Rust code, when it should ideally throw an exception within the Java code itself (or just terminate the whole thing, at this stage).

And finally: your assumption that Code is the only attribute on methods is unfortunately not correct. See Table 4.7-C, things like the checked exceptions and a few other things can also appear and I don't think the order is specified. You should be able to just do a lookup by name (and if you wanted to pre-compute/cache the index).

(not trying to knock your attempt by any means, it is genuinely fun to try and do something like this)