Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support auto-unboxing #153

Open
baroquebobcat opened this Issue · 6 comments

4 participants

@baroquebobcat

was Support casting Integer to int

It'd be nice if you could do

  list =[1,2,3]
  list.each do |x|
    puts int(x) * 2
  end

currently you'd have to

  list =[1,2,3]
  list.each do |x|
    puts Integer(x).intValue * 2
  end

One place you could do it is here https://github.com/mirah/mirah/blob/master/lib/mirah/jvm/compiler/jvm_bytecode.rb#L400

but that might not be the right layer of abstraction.

@thomaslee
Owner

Feels like it should be possible to do this as a macro, but looks like the compiler/type system gets a bit confused... (because of the multiplication operator seeing the LHS macro as a Type(java.lang.Object) or something)

@thomaslee thomaslee closed this
@thomaslee thomaslee reopened this
@thomaslee
Owner

Apologies, trigger happy with the "Comment & Close" button.

@baroquebobcat

You could certainly make it into a macro instead of a cast--but essentially what you'd be doing by putting it in code generation would be that same thing as if you were writing a macro--but at code generation time instead of modifying the AST. I think it makes sense to start there, since casting to primitives seems like a fairly platform dependent thing.

@ribrdb
Owner

I believe this should work with the new code generator.

@baroquebobcat
@altamic

At the moment the new backend fails with a cryptic internal error, while the old one reports a meaningful: not a primitive type: #<Type java.lang.Integer>

Details here:

$ cat test.mirah
list = [1,2,3]
list.each do |x|
  puts int(x) * 2
end
$ java -jar javalib/mirahc.jar test.mirah
* [org.mirah.jvm.compiler.ClassCompiler] Compiling class Test
test.mirah:3:
ERROR: Internal error: null
  puts int(x) * 2
       ^^^^^^
<mirah.lang.ast.Script>: void
  <mirah.lang.ast.NodeList>: null
    <mirah.lang.ast.ClassDefinition:<mirah.lang.ast.Unquote>>: Test
      <mirah.lang.ast.Unquote>
        Test
      <mirah.lang.ast.NodeList>: <MethodType: name=<init> [] : void>
        <mirah.lang.ast.ConstructorDefinition:<SimpleString:initialize>>: <MethodType: name=<init> [] : void>
          <SimpleString:initialize>
          <mirah.lang.ast.Arguments>: void
            <mirah.lang.ast.RequiredArgumentList>
            <mirah.lang.ast.OptionalArgumentList>
            <mirah.lang.ast.RequiredArgumentList>
          <mirah.lang.ast.NodeList>: java.lang.Object
            <mirah.lang.ast.Super>: java.lang.Object
              <mirah.lang.ast.NodeList>
            <mirah.lang.ast.NodeList>: void
          <mirah.lang.ast.AnnotationList>
        <mirah.lang.ast.StaticMethodDefinition:<SimpleString:main>>: <MethodType: name=main [java.lang.String[]] : void>
          <SimpleString:main>
          <mirah.lang.ast.Arguments>: void
            <mirah.lang.ast.RequiredArgumentList>
              <mirah.lang.ast.RequiredArgument:<SimpleString:ARGV>>: java.lang.String[]
                <SimpleString:ARGV>
                <mirah.lang.ast.TypeRefImpl>
            <mirah.lang.ast.OptionalArgumentList>
            <mirah.lang.ast.RequiredArgumentList>
          <SimpleString:void>
          <mirah.lang.ast.NodeList>: null
            <mirah.lang.ast.LocalAssignment:<SimpleString:list>>: java.util.List<java.lang.Integer>
              <SimpleString:list>
              <mirah.lang.ast.Array>: java.util.List<java.lang.Integer>
                <mirah.lang.ast.NodeList>
                  <Fixnum:1>: int
                  <Fixnum:2>: int
                  <Fixnum:3>: int
            <mirah.lang.ast.Loop>: null
              <mirah.lang.ast.NodeList>: java.util.Iterator<java.lang.Integer>
                <mirah.lang.ast.NodeList>: java.util.Iterator<java.lang.Integer>
                  <mirah.lang.ast.LocalAssignment:<mirah.lang.ast.Unquote>>: java.util.Iterator<java.lang.Integer>
                    <mirah.lang.ast.Unquote>
                      gensym0
                    <mirah.lang.ast.Call:<SimpleString:iterator>>: java.util.Iterator<java.lang.Integer>
                      <mirah.lang.ast.LocalAccess:<SimpleString:list>>: java.util.List<java.lang.Integer>
                        <SimpleString:list>
                      <SimpleString:iterator>
                      <mirah.lang.ast.NodeList>
              <mirah.lang.ast.Call:<SimpleString:hasNext>>: boolean
                <mirah.lang.ast.LocalAccess:<SimpleString:gensym0>>: java.util.Iterator<java.lang.Integer>
                  <SimpleString:gensym0>
                <SimpleString:hasNext>
                <mirah.lang.ast.NodeList>
              <mirah.lang.ast.NodeList>: java.lang.Integer
                <mirah.lang.ast.NodeList>: java.lang.Integer
                  <mirah.lang.ast.LocalAssignment:<mirah.lang.ast.Unquote>>: java.lang.Integer
                    <mirah.lang.ast.Unquote>
                      x
                    <mirah.lang.ast.Call:<SimpleString:next>>: java.lang.Integer
                      <mirah.lang.ast.LocalAccess:<SimpleString:gensym0>>: java.util.Iterator<java.lang.Integer>
                        <SimpleString:gensym0>
                      <SimpleString:next>
                      <mirah.lang.ast.NodeList>
              <mirah.lang.ast.NodeList>: java.io.PrintStream
                <mirah.lang.ast.Call:<SimpleString:println>>: java.io.PrintStream
                  <mirah.lang.ast.Call:<SimpleString:out>>: java.io.PrintStream
                    <mirah.lang.ast.Constant:<SimpleString:System>>: java.lang.System
                      <SimpleString:System>
                    <SimpleString:out>
                    <mirah.lang.ast.NodeList>
                  <SimpleString:println>
                  <mirah.lang.ast.NodeList>
                    <mirah.lang.ast.Call:<SimpleString:*>>: int
                      <mirah.lang.ast.Cast>: int
                        <mirah.lang.ast.TypeRefImpl>
                        <mirah.lang.ast.LocalAccess:<SimpleString:x>>: java.lang.Integer
                          <SimpleString:x>
                      <SimpleString:*>
                      <mirah.lang.ast.NodeList>
                        <Fixnum:2>: int
              <mirah.lang.ast.NodeList>: void
          <mirah.lang.ast.AnnotationList>
      <mirah.lang.ast.TypeNameList>
      <mirah.lang.ast.AnnotationList>
        <mirah.lang.ast.Annotation>
          <SimpleString:org.mirah.jvm.compiler.Cleaned>
          <mirah.lang.ast.HashEntryList>
Exception in thread "main" java.lang.Exception: org.mirah.jvm.compiler.ReportedException: java.lang.NullPointerException
    at org.mirah.jvm.compiler.ScriptCompiler.visitScript(script_compiler.mirah:38)
    at mirah.lang.ast.Script.accept(structure.mirah:392)
    at org.mirah.jvm.compiler.Backend.visit(backend.mirah:37)
    at org.mirah.tool.Mirahc.compile(mirahc.mirah:167)
    at org.mirah.tool.Mirahc.main(mirahc.mirah:314)
Caused by: org.mirah.jvm.compiler.ReportedException: java.lang.NullPointerException
    at org.mirah.jvm.compiler.BaseCompiler.reportICE(base_compiler.mirah:104)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:178)
    at org.mirah.jvm.compiler.CallCompiler.compile(call_compiler.mirah:85)
    at org.mirah.jvm.compiler.CallCompiler.visitMath(call_compiler.mirah:168)
    at org.mirah.jvm.mirrors.Member.accept(member.mirah:65)
    at org.mirah.jvm.compiler.CallCompiler.compile(call_compiler.mirah:62)
    at org.mirah.jvm.compiler.MethodCompiler.visitCall(method_compiler.mirah:321)
    at mirah.lang.ast.Call.accept(call.mirah:392)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    at org.mirah.jvm.compiler.CallCompiler.compile(call_compiler.mirah:85)
    at org.mirah.jvm.compiler.CallCompiler.convertArgs(call_compiler.mirah:97)
    at org.mirah.jvm.compiler.CallCompiler.visitMethodCall(call_compiler.mirah:226)
    at org.mirah.jvm.mirrors.Member.accept(member.mirah:69)
    at org.mirah.jvm.compiler.CallCompiler.compile(call_compiler.mirah:62)
    at org.mirah.jvm.compiler.MethodCompiler.visitCall(method_compiler.mirah:321)
    at mirah.lang.ast.Call.accept(call.mirah:392)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    at org.mirah.jvm.compiler.BaseCompiler.visitNodeList(base_compiler.mirah:186)
    at mirah.lang.ast.NodeList.accept(lists.mirah:343)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    at org.mirah.jvm.compiler.MethodCompiler.visitLoop(method_compiler.mirah:542)
    at mirah.lang.ast.Loop.accept(flow.mirah:392)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    at org.mirah.jvm.compiler.BaseCompiler.visitNodeList(base_compiler.mirah:186)
    at org.mirah.jvm.compiler.MethodCompiler.compileBody(method_compiler.mirah:332)
    at org.mirah.jvm.compiler.MethodCompiler.compile(method_compiler.mirah:72)
    at org.mirah.jvm.compiler.ClassCompiler.visitMethodDefinition(class_compiler.mirah:72)
    at org.mirah.jvm.compiler.ClassCompiler.visitStaticMethodDefinition(class_compiler.mirah:76)
    at mirah.lang.ast.StaticMethodDefinition.accept(method.mirah:412)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    at org.mirah.jvm.compiler.BaseCompiler.visitNodeList(base_compiler.mirah:186)
    at mirah.lang.ast.NodeList.accept(lists.mirah:343)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    at org.mirah.jvm.compiler.ClassCompiler.compile(class_compiler.mirah:50)
    at org.mirah.jvm.compiler.ScriptCompiler.visitClassDefinition(script_compiler.mirah:44)
    at mirah.lang.ast.ClassDefinition.accept(klass.mirah:392)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    at org.mirah.jvm.compiler.BaseCompiler.visitNodeList(base_compiler.mirah:186)
    at mirah.lang.ast.NodeList.accept(lists.mirah:343)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    at org.mirah.jvm.compiler.ScriptCompiler.visitScript(script_compiler.mirah:35)
    ... 4 more
Caused by: java.lang.NullPointerException
    at java.lang.String.<init>(String.java:210)
    at org.jruby.org.objectweb.asm.Type.getInternalName(Unknown Source)
    at org.jruby.org.objectweb.asm.commons.GeneratorAdapter.typeInsn(Unknown Source)
    at org.jruby.org.objectweb.asm.commons.GeneratorAdapter.checkCast(Unknown Source)
    at org.mirah.jvm.compiler.MethodCompiler.visitCast(method_compiler.mirah:380)
    at mirah.lang.ast.Cast.accept(call.mirah:392)
    at org.mirah.jvm.compiler.BaseCompiler.visit(base_compiler.mirah:174)
    ... 43 more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.