Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when accessing the object without initialized in native. #314

Closed
Kray-G opened this issue Aug 12, 2021 · 1 comment
Closed

Crash when accessing the object without initialized in native. #314

Kray-G opened this issue Aug 12, 2021 · 1 comment
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Kray-G
Copy link
Owner

Kray-G commented Aug 12, 2021

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
@Kray-G Kray-G self-assigned this Aug 12, 2021
@Kray-G Kray-G added the bug Something isn't working label Aug 12, 2021
@Kray-G Kray-G added this to the v1.1.0 milestone Aug 12, 2021
@Kray-G
Copy link
Owner Author

Kray-G commented Aug 12, 2021

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]

Kray-G added a commit that referenced this issue Aug 12, 2021
@Kray-G Kray-G closed this as completed in a8db752 Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant