Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1214001

native functions can't access instance variables correctly

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.1
    • CPU:
      sparc
    • OS:
      solaris_2.4
    • Verification:
      Not verified

      Description

      (I hope I have this filed correctly. perhaps it's not a compiler bug; refile as appropriate)

      I've run into a situation where an instance tries to set the values of its class variables
      in a native function (using the structure passed to it for the class structure). It can set
      the values and read them back in correctly in the native function, but the java methods of
      the class don't get the same results.

      That is:
      If you have the following java function:

      import java.util.Linker;

      class TypeObj {
      /* int c[];*/
              long b0;
              int a0, a1, a2, a3;

              static {
                      Linker.loadLibrary("type");
              }
              public native void setvals();

              public void setvalsjava() {
                      a0 = 10;
                      a1 = 20;
                      a2 = 30;
                      a3 = 40;
              }

              public void printvals() {
                      System.out.println("a0, 1, 2, 3, = " + a0+", "+a1 + ", "+a2 + ", "+a3);
              }
      }

      class TypeTest {
              public static void main(String args[]) {
                      TypeObj t = new TypeObj();
                      t.setvalsjava();
                      t.printvals();
                      t.setvals();
                      t.printvals();
              }
      }

      where the native function is defined as so:

      #include <stdio.h>
      #include <StubPreamble.h>
      #include "TypeObj.h"
      void TypeObj_setvals(struct HTypeObj *this)
      {
       ClassTypeObj *tobj = unhand(this);

        printf("a0, a1, a2, a3 = %d, %d, %d, %d\\n",
                      tobj->a0, tobj->a1, tobj->a2, tobj->a3);
        tobj->a0 = 100;
        tobj->a1 = 200;
        tobj->a2 = 300;
        tobj->a3 = 400;
        printf("a0, a1, a2, a3 = %d, %d, %d, %d\\n",
                      tobj->a0, tobj->a1, tobj->a2, tobj->a3);
      }

      The output you will see is:

      a0, 1, 2, 3, = 10, 20, 30, 40
      a0, a1, a2, a3 = 10, 20, 30, 40
      a0, a1, a2, a3 = 100, 200, 300, 400
      a0, 1, 2, 3, = 100, 200, 300, 400

      If you uncomment the "int c[]" declaration, you'll now get the strange result:

      a0, 1, 2, 3, = 10, 20, 30, 40
      a0, a1, a2, a3 = 20, 30, 40, 40
      a0, a1, a2, a3 = 100, 200, 300, 400
      a0, 1, 2, 3, = 10, 100, 200, 300

      (Commenting out the "long b0" declaration will make the program behave correctly
      as well).

      I don't know whether it's having the long in there, or the combination of that and
      the int array, or just what is causing the problem. But it's a bit frustrating not being
      able to handle the java data in C. One way to get around the problem is to call
      an execute_java_dynamic_method to have java set the values, but this seems a
      bit obtuse...


        Attachments

          Activity

            People

            Assignee:
            fyellinsunw Frank Yellin (Inactive)
            Reporter:
            chaasesunw Chet Haase (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: