Skip to content

Commit

Permalink
Merge pull request jruby#7933 from headius/better_line_numbers_for_dy…
Browse files Browse the repository at this point in the history
…namicscopes

Improve line numbers for generated dynscopes
  • Loading branch information
headius authored Sep 10, 2023
2 parents 02531b3 + 9a4ec8d commit 052d6c6
Showing 1 changed file with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ private static Class generateInternal(final ClassDefiningClassLoader cdcl, final
final String baseName = p(DynamicScope.class);

JiteClass jiteClass = new JiteClass(clsPath, baseName, new String[0]) {{
setSourceFile(clsPath + ".java");

// parent class constructor
defineMethod("<init>", ACC_PUBLIC, sig(void.class, StaticScope.class, DynamicScope.class), new CodeBlock() {{
aload(0);
Expand Down Expand Up @@ -238,38 +240,56 @@ private static Class generateInternal(final ClassDefiningClassLoader cdcl, final
iload(3);
pushInt(1);
isub();

line(6);
invokevirtual(baseName, "setValueVoid", sig(void.class, IRubyObject.class, int.class, int.class));
voidreturn();
}});

// optional overrides
defineMethod("getValueDepthZero", ACC_PUBLIC, sig(IRubyObject.class, int.class), new CodeBlock() {{
line(6);
line(7);

if (size > 0) genGetSwitch(clsPath, newFields, this, 1);

line(1);
line(8);

invokestatic(clsPath, "sizeError", sig(RuntimeException.class));
athrow();
}});

defineMethod("setValueDepthZeroVoid", ACC_PUBLIC, sig(void.class, IRubyObject.class, int.class), new CodeBlock() {{
line(6);
line(9);

if (size > 0) genPutSwitch(clsPath, newFields, this, 2);

line(1);
line(10);

invokestatic(clsPath, "sizeError", sig(RuntimeException.class));
athrow();
}});

// utilities

defineMethod("sizeError", ACC_PRIVATE | ACC_STATIC, sig(RuntimeException.class), new CodeBlock() {{
line(11);
newobj(p(RuntimeException.class));
dup();
ldc(clsName + " only supports scopes with " + size + " variables");

line(12);
invokespecial(p(RuntimeException.class), "<init>", sig(void.class, String.class));
areturn();
}});

// specialized getters and setters

for (int i = 0; i < SPECIALIZED_GETS.size(); i++) {
final int offset = i;

defineMethod(SPECIALIZED_GETS.get(offset), ACC_PUBLIC, sig(IRubyObject.class), new CodeBlock() {{
line(6);
// line numbers for specialized getters are 100 + offset
line(100 + offset);

if (size <= offset) {
invokestatic(clsPath, "sizeError", sig(RuntimeException.class));
Expand All @@ -286,7 +306,8 @@ private static Class generateInternal(final ClassDefiningClassLoader cdcl, final
final int offset = i;

defineMethod(SPECIALIZED_GETS_OR_NIL.get(offset), ACC_PUBLIC, sig(IRubyObject.class, IRubyObject.class), new CodeBlock() {{
line(6);
// line numbers for specialized get-or-nils are 200 + offset
line(200 + offset);

if (size <= offset) {
invokestatic(clsPath, "sizeError", sig(RuntimeException.class));
Expand Down Expand Up @@ -317,7 +338,8 @@ private static Class generateInternal(final ClassDefiningClassLoader cdcl, final
final int offset = i;

defineMethod(SPECIALIZED_SETS.get(offset), ACC_PUBLIC, sig(void.class, IRubyObject.class), new CodeBlock() {{
line(6);
// line numbers for specialized setters are 300 + offset
line(300 + offset);

if (size <= offset) {
invokestatic(clsPath, "sizeError", sig(RuntimeException.class));
Expand All @@ -332,18 +354,10 @@ private static Class generateInternal(final ClassDefiningClassLoader cdcl, final
}

// fields

for (String prop : newFields) {
defineField(prop, ACC_PUBLIC, ci(IRubyObject.class), null);
}

// utilities
defineMethod("sizeError", ACC_PRIVATE | ACC_STATIC, sig(RuntimeException.class), new CodeBlock() {{
newobj(p(RuntimeException.class));
dup();
ldc(clsName + " only supports scopes with " + size + " variables");
invokespecial(p(RuntimeException.class), "<init>", sig(void.class, String.class));
areturn();
}});
}};

return defineClass(cdcl, jiteClass);
Expand Down

0 comments on commit 052d6c6

Please sign in to comment.