6 ms·
Nice project, congrats! One thing struck me as a bit odd: > In particular, it does not support: generics What kind of support is there for generics in the JV
by haspok 3y ago
Nice project, congrats!
One thing struck me as a bit odd:
> In particular, it does not support: generics
What kind of support is there for generics in the JVM? Maybe I'm too naive to assume that due to type erasure on bytecode level everything is just an Object, ie. a reference type? Or do you mean the class definition parser - but then, you don't really have any checks in place to see if the class file is valid (other than the basic syntax)?
- xxs 3y agopretty much this - generics have (rare) implications to the reflection (but it's unsupported as well) but overall they are replaced with the nearest class/interface when compiled. OTOH lack of string interning is super strange [it's trivial to implement], and w/o it JVM is not a thing. String being equal by reference is important, and part of JLS. Lack of thread makes the entire endeavor a toy project.
- senorrib 3y ago“I want to stress that this is a toy JVM, built for learning purposes and not a serious implementation.”
- xxs 3y agoThe rest of the stuff, incl. I/O is actually on the trivial side - threads do require planning. This is what I meant by being a 'toy' project, threads (and JMM) would be impossible to bolt in later on.
- ncallaway 3y agoThe reason you're being downvoted is you keep dismissing this as a "toy" project, and pointing out that it would be hard to make a real project. But, as the previous commenter attempted to point out to you this project is a *self-described* toy project. On the very page that is linked, the author of the JVM specifically says: "I want to stress that this is a toy JVM, built for learning purposes and not a serious implementation." Thus, absolutely no one disagrees that its a toy JVM. They just want you to stop being dismissive of someone's toy project by repeatedly pointing out its a toy project and not a "not a thing"
- __jem 3y agoRight, just because something is a "toy" doesn't mean it's not still impressive. If someone implemented a "toy" database that could parse and execute SQL queries, distribute data across nodes, etc., you would probably not want to use that in production, but it's still a very impressive project for a single person to pull off, even if it's riddled with bugs. Getting a very complex system to "just barely functional" is still a huge achievement and very cool!
- xxs 3y agoBeing impressing or otherwise is very subjective. SQL (ACID) over multiple non-cache-coherent nodes is extremely difficult to pull with regards to consistency, though.
- xxs 3y agoI've pointed the only part that makes it a toy project is the lack of Threading support, the rest is not hard to add. So the items in list of things missing after 'toy' thing should have totally different weights (with Threads being the added to the last).
- lolinder 3y agoYou're still missing the point—it was always intended to be a toy project, and the author has explicitly declared that they are completely done with it and won't be doing any more work. What does it matter how they sort the list of missing items? It's not a todo list in need of prioritization, it's just an "FYI, these are some of the things I never got to".
- 3cats-in-a-coat 3y agoJava strings are compared by reference, if they do not match, they're compared by value. There's no guarantee every single string has a single instance. That would hurt performance.
- maverwa 3y agoI think op meant "String literals". For those the spec seems to require interning: > Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern. And later: > Literal strings within different classes in different packages likewise represent references to the same String object. Source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5 https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html... But that does - as far as I can see - say nothing for non-literal strings. [edit]: formatting
- 3cats-in-a-coat 3y agoThanks, makes sense yes. Still if the JVM look up in all cases defers to value after ref mismatch, it should work identically, no? Even if interning is mandatory as per spec, I'm not sure how it'd change the outcome of evaluation.
- maverwa 3y agoyeah, I'd assume as much. If it indeed falls back to a by-value comparison it would be slower, but should work.
- xxs 3y agonope - it'd be plain wrong. Literals must be equal by reference, comparing them by value would just break JLS, as they would be equal to any other composed string by reference as well.
- 3y ago
- whizzter 3y agoNot entirely correct, last I checked string interning was ONLY guaranteed for those strings defined in source and read in during class loading, strings created via the String constructor (f.ex. via StringBuilder) CAN duplicate those strings that you hardcoded in your sources, to get the "canonical" string in those cases you have to invoke String.intern() if memory serves me correct. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#intern https://docs.oracle.com/en/java/javase/11/docs/api/java.base...() Also interning strings to optimize equality checks to be able to use pointer comparison is dangerous for external inputs since iirc at some point interned strings could permanently be stored (unless implemented by a WeakSet) and attackers could fill up your heap (or cause other GC issues since the entire interning functionality is a cache) by filling up your interning lists with crap.
- xxs 3y ago>String being equal by reference is important, and part of JLS. I never said String must be equal by reference when their content is. However string literals must be equal by reference. I thought Mentioning the JLS would make it obvious, esp. having 'intern' in the context
- ywei3410 3y agoThere's also the new JVM option which eludes me at the moment which sweeps the strings which are promoted to the older generation and interns them. Not certain about whether `String.intern` is permanently stored; I rather suspect that it sweeps the existing strings since iirc the java string has a hash associated with it anyway.
- znpy 3y ago> Lack of thread makes the entire endeavor a toy project. yeah, as stated by the author in the line that says "I want to stress that this is a toy JVM, built for learning purposes and not a serious implementation."
- xxs 3y agoYeah, that's the only part that makes it a toy project - the rest can be added w/o too much of an effort. This is pretty much what makes it a toy project.
- newmana 3y agoThey might be talking about the checkcast operation: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.checkcast https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.ht... This is generated when you do something like: final Main value = list.get(0); http://henrikeichenhardt.blogspot.com/2013/05/how-are-java-generics-implemented-under.html http://henrikeichenhardt.blogspot.com/2013/05/how-are-java-g...
- xxs 3y agoThe cast is added by javac, so it just needs to verify the object on the stack to be compatible w/ the provided class. That part is very simple.
- andreabergia 3y agoThanks! About the generics - some people have pointed out the same on reddit, and yeah, you are correct. The only thing that should be done is to read the Signature attribute that encodes the generic information about classes, methods, and fields (https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.9 https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.ht...) As a matter of fact, I just did a test and the following code works! :-) public class Generic { public static void main(String[] args) { List<String> strings = new ArrayList<String>(10); strings.add("hey"); strings.add("hackernews"); for (String s : strings) { tempPrint(s); } } private static native void tempPrint(String value); }