Menu

#24 [3.0-beta2] throw java.lang.ArrayIndexOutOfBoundsException

v3.0-beta1
closed-fixed
nobody
None
5
2015-03-14
2014-03-12
zheng sun
No

code:
ScriptEngine engine = scriptEngineManager.getEngineByName("luaj");
engine.eval("--呵呵\nlocal x,y = 38,47\nlocal bossMetaId = 5390209");

out:
Exception in thread "main" javax.script.ScriptException: eval threw javax.script.ScriptException: load script: java.lang.ArrayIndexOutOfBoundsException: 32
at org.luaj.vm2.script.LuaScriptEngine.compile(LuaScriptEngine.java:94)
at org.luaj.vm2.script.LuaScriptEngine.eval(LuaScriptEngine.java:112)
at org.luaj.vm2.script.LuaScriptEngine.eval(LuaScriptEngine.java:106)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at net.szh.TScript.main(TScript.java:16)

Discussion

  • Duzc2

    Duzc2 - 2014-06-05

    I found that , luaj can't handle Chinese char.
    when I put strings with Chinese into script , it throws that:
    Exception in thread "main" org.luaj.vm2.LuaError: load test1.lua: java.lang.ArrayIndexOutOfBoundsException: 32
    at org.luaj.vm2.LuaValue.error(Unknown Source)
    at org.luaj.vm2.Globals.load(Unknown Source)
    at org.luaj.vm2.Globals.load(Unknown Source)
    at net.duzc2.test.luaj.lualib.LuaLibLoad.main(LuaLibLoad.java:54)

    and even,
    if I run the only command in script :
    print("你好")
    it will print wrong:
    `}

    I guess there must be some bugs in org.luaj.vm2.Globals.UTF8Stream class .

     
  • Duzc2

    Duzc2 - 2014-06-05

    org.luaj.vm2.LuaString.encodeToUtf8(char[], int, byte[], int)

    /**
     * Encode the given Java string as UTF-8 bytes, writing the result to bytes
     * starting at offset. 
     * <p>
     * The string should be measured first with lengthAsUtf8
     * to make sure the given byte array is large enough.
     * @param chars Array of unicode characters to be encoded as UTF-8
     * @param nchars Number of characters in the array to convert.
     * @param bytes byte array to hold the result
     * @param off offset into the byte array to start writing
     * @return number of bytes converted.
     * @see #lengthAsUtf8(char[])
     * @see #decodeAsUtf8(byte[], int, int)
     * @see #isValidUtf8()
     */
    public static int encodeToUtf8(char[] chars, int nchars, byte[] bytes, int off) {
        char c;
        int j = off;
        for ( int i=0; i<nchars; i++ ) {
            if ( (c = chars[i]) < 0x80 ) {
                bytes[j++] = (byte) c;
            } else if ( c < 0x800 ) {
                bytes[j++] = (byte) (0xC0 | ((c>>6)  & 0x1f));
                bytes[j++] = (byte) (0x80 | ( c      & 0x3f));              
            } else {
                bytes[j++] = (byte) (0xE0 | ((c>>12) & 0x0f));
                bytes[j++] = (byte) (0x80 | ((c>>6)  & 0x3f));
                bytes[j++] = (byte) (0x80 | ( c      & 0x3f));              
            }
        }
        return j - off;
    }
    

    I guess something wrong in this method...

     
  • Duzc2

    Duzc2 - 2014-06-05

    I got it!
    when I write
    LuaValue chunk1 = globals.load(
    new InputStreamReader(LuaLibLoad.class
    .getResourceAsStream("test1.lua")), "test1.lua");
    it throws that exception

    and I write it as :
    LuaValue chunk1 = globals.load(LuaLibLoad.class
    .getResourceAsStream("test1.lua"), "test1.lua","t",globals);

    works!

     
  • Duzc2

    Duzc2 - 2014-06-05

    I think there is must be a bug in org.luaj.vm2.Globals.UTF8Stream.

    I have a different way to encode utf8:

    public static byte[] encodeString(String string) {
        if (string == null || string.length() == 0) {
            return new byte[] { 0, 0 };
        }
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(
                    string.length() * 3);
            DataOutputStream dos = new DataOutputStream(baos);
            byte[] bytes = string.getBytes("UTF-8");
            dos.writeShort(bytes.length);
            dos.write(bytes);
            return baos.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
    
    public static String decodeString(InputStream in) throws IOException {
        DataInputStream dis = new DataInputStream(in);
        short length = dis.readShort();
        if (length == 0) {
            return "";
        }
        byte[] bytes = new byte[length];
        dis.readFully(bytes);
        return new String(bytes, "UTF-8");
    }
    

    maybe it's slow , but right ,I use it in my system in China.

     
  • zheng sun

    zheng sun - 2014-07-02

    Thank you for your reply

     
  • James Roseborough

    I believe this was a problem in the beta release but has been fixed for version 3.0.

     
  • James Roseborough

    • status: open --> closed-fixed
     

Log in to post a comment.