Skip to content

fix types of code attribute and stack_map writer

Julius Michaelis requested to merge formalferret/noak:master into master

I was trying to add a StackMapTable, but couldn't get the necessary writer. I also wasn't able to use StackMapTableWriter::full. After the changes in this MR, the following code at least compiles:

                                code
                                    .max_stack(42)?
                                    .max_locals(31)?
                                    .instructions(|code| {
                                        let l1 = code.new_label()?;
                                        code.lconst0()?
                                            .gotow(l1.1)?
                                            .label(l1.0)?
                                            .pop()?
                                            .getstatic(cpool::FieldRef::by(
                                                "java/lang/System",
                                                ("out", "Ljava/io/PrintStream;"),
                                            ))?
                                            .ldc(cpool::String::by("Hello, World!"))?
                                            .invokevirtual(cpool::MethodRef::by(
                                                "java/io/PrintStream",
                                                ("println", "(Ljava/lang/String;)V"),
                                            ))?
                                            .return_()?;
                                        labels.push(l1.1);
                                        Ok(())
                                    })?
                                    .exceptions(|_| Ok(()))?
                                    .attributes(|a| {
                                        a.begin(|a| {
                                            a.stack_map_table(|smt| {
                                                smt.full(labels[0], |full| {
                                                    Ok(full.locals(|locals| {
                                                        locals.begin(|local| local.long())?;
                                                        locals.begin(|local| local.top())?;
                                                        Ok(())
                                                    })?)
                                                })
                                            })
                                        })?;
                                        Ok(())
                                    })?

but I haven't really understood StackMapTables or this library yet, so, this might be complete nonsense. It does not get me a usable class, in fact:

Error: Unable to initialize main class Example
Caused by: java.lang.ClassFormatError: StackMapTable format error: wrong attribute size

There doesn't happen to be a example of using this library feature somewhere? (I wish the stack map could just be auto-calculated…)

(On a semi-related note: I also noticed that I can add a code section in the class attributes, which OpenJDK java apparently ignores as a custom attribute. Should I file an issue?)

Edited by Julius Michaelis

Merge request reports