Kray-G/kinx

Crash when accessing the object without initialized in native.

Kray-G opened this issue · 1 comments

Crash with the code below.

var mem = [];
for (var i = 0; i < 10; ++i) {
    mem[i] = i;
}

native func0(n:int):obj {
    var r:int[];
    for (var i = 0; i < n; ++i) {
        r[i] = mem[i];
    }
    return r;
}

var res = func0(10);
System.println(res);

Besides, an initializer cannot be accepted in compiler.

native f() {
    var a:int[] = [];
}
// Error: Not supported operation in native function near the <(unknown)>:2

Workaround 1:
You can use an initialized object via the argument.

var mem = [];
for (var i = 0; i < 10; ++i) {
    mem[i] = i;
}

native func0(n:int, r:int[]):obj {
    for (var i = 0; i < n; ++i) {
        r[i] = mem[i];
    }
    return r;
}

var res = func0(10, []);
System.println(res);  // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Workaround 2:
You can use the object from a lexical scope.

const EMPTY = [];
var mem = [];
for (var i = 0; i < 10; ++i) {
    mem[i] = i;
}

native func0(n:int):obj {
    var r:int[] = EMPTY;
    for (var i = 0; i < n; ++i) {
        r[i] = mem[i];
    }
    return r;
}

var res = func0(10);
System.println(res);  // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]