|
| 1 | +function pointerTo(type, value) { |
| 2 | + var outerPtr = interop.alloc(interop.sizeof(interop.Pointer)); |
| 3 | + var outerRef = new interop.Reference(type, outerPtr); |
| 4 | + outerRef.value = value; |
| 5 | + return outerPtr; |
| 6 | +} |
| 7 | + |
| 8 | +function referenceFromPointerNumber(type, value) { |
| 9 | + const ptr = new interop.Pointer(value); |
| 10 | + const ptrToPtr = pointerTo(interop.Pointer, ptr); |
| 11 | + return new interop.Reference(type, ptrToPtr); |
| 12 | +} |
| 13 | + |
| 14 | +function valueFromPointerNumber(type, value) { |
| 15 | + return referenceFromPointerNumber(type, value).value; |
| 16 | +} |
| 17 | + |
1 | 18 | describe(module.id, function () {
|
2 | 19 | afterEach(function () {
|
3 | 20 | TNSClearOutput();
|
@@ -79,13 +96,6 @@ describe(module.id, function () {
|
79 | 96 | expect(interop.sizeof(interop.types.selector)).toBe(interop.sizeof(interop.Pointer));
|
80 | 97 | });
|
81 | 98 |
|
82 |
| - function pointerTo(type, value) { |
83 |
| - var outerPtr = interop.alloc(interop.sizeof(interop.Pointer)); |
84 |
| - var outerRef = new interop.Reference(type, outerPtr); |
85 |
| - outerRef.value = value; |
86 |
| - return outerPtr; |
87 |
| - } |
88 |
| - |
89 | 99 | it("ReferenceType", function () {
|
90 | 100 | var ptr = interop.alloc(2 * interop.sizeof(interop.types.id));
|
91 | 101 | var ref = new interop.Reference(new interop.types.ReferenceType(interop.types.int32), ptr);
|
@@ -198,4 +208,27 @@ describe(module.id, function () {
|
198 | 208 | var result = interop.types.double(doublePtr);
|
199 | 209 | expect(result === number).toBe(true);
|
200 | 210 | });
|
| 211 | + |
| 212 | + it("Initialize pointer from non-int throws", function () { |
| 213 | + expect(() => new interop.Pointer("")).toThrowError(/must be an integer/); |
| 214 | + }); |
| 215 | + |
| 216 | + it("Initialize reference from pointer", function () { |
| 217 | + // Create a native NSDictionary holding the pair "value" -> "key" |
| 218 | + const key = "key"; |
| 219 | + const value = "value"; |
| 220 | + const d1 = NSDictionary.dictionaryWithObjectForKey(value, key); |
| 221 | + |
| 222 | + expect(d1.valueForKey(key)).toBe(value, "Dictionary should return initial value string."); |
| 223 | + |
| 224 | + // Take the address of the NSDictionary |
| 225 | + const addr = interop.handleof(d1).toNumber(); |
| 226 | + // Create a new native wrapper using the address |
| 227 | + const d2 = valueFromPointerNumber(NSDictionary, addr); |
| 228 | + // Take the address of the new wrapper |
| 229 | + const addr2 = interop.handleof(d2).toNumber(); |
| 230 | + |
| 231 | + expect(addr2).toBe(addr, "The new object should have the same address."); |
| 232 | + expect(d2.valueForKey(key)).toEqual(d1.valueForKey(key), "Returned values should be equal"); |
| 233 | + }); |
201 | 234 | });
|
0 commit comments