aboutsummaryrefslogtreecommitdiffhomepage
path: root/notes/pointer-less_targets.txt
blob: 0468b091358e08eb46af80905bfa2322326da395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

Pointer-less targets
====================

There are some bytecodes/VMs that do not support interior pointers, i.e.
a pointer that points to a field inside a struct or an array element inside
an array. JVM is one such example.

However, that can be solved by creating "proxy types" for having
mutable references to primitive types:

    // Interface
    public interface MutableIntReference {
        public void set(int value);
        public int get();
    }

    // For structs:
    class MyStruct {
        public int a;
    }

    public class Ref_a implements MutableIntReference {
        private final MyStruct target;

        public Ref_a(final MyStruct target) {
            this.target = target;
        }

        @Override
        public void set(final int value) {
            target.a = value;
        }
        @Override
        public void get() {
            return target.a;
        }
    }

    // For arrays:
    public class IntElementReference MutableIntReference {
        private final int[] target;
        private final int index;

        public IntElementReference(final int[] target, final int index) {
            this.target = target;
            this.index = index;
        }

        @Override
        public void set(final int value) {
            target[index] = value;
        }
        @Override
        public void get() {
            return target[index];
        }
    }