-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSIL.Unsafe.js
More file actions
2290 lines (1895 loc) · 68.9 KB
/
Copy pathJSIL.Unsafe.js
File metadata and controls
2290 lines (1895 loc) · 68.9 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
if (typeof (JSIL) === "undefined")
throw new Error("JSIL.Core is required");
if (!$jsilcore)
throw new Error("JSIL.Core is required");
JSIL.$StructSizeOverrides = {
"System.Int64": 8,
"System.UInt64": 8
};
JSIL.DeclareNamespace("JSIL.Runtime");
JSIL.DeclareNamespace("JSIL.PackedArray");
JSIL.ImplementExternals("System.IntPtr", function ($) {
var tIntPtr = $.IntPtr;
$.RawMethod(false, "$fromPointer", function (pointer) {
this.pointer = pointer;
this.value = null;
});
$.RawMethod(false, "$fromInt32", function (int32) {
this.pointer = null;
this.value = int32 | 0;
});
$.RawMethod(false, "$fromInt64", function (int64) {
this.pointer = null;
this.value = int64.ToNumber() | 0;
});
$.RawMethod(true, ".cctor", function () {
System.IntPtr.Zero = new System.IntPtr();
});
$.Method({Static:false, Public:false }, ".ctor",
(new JSIL.MethodSignature(null, [], [])),
function _ctor () {
this.pointer = null;
this.value = 0;
}
);
$.Method({Static:false, Public:true }, ".ctor",
(new JSIL.MethodSignature(null, [$.Int32], [])),
function _ctor (value) {
this.pointer = null;
this.value = value | 0;
}
);
$.Method({Static:false, Public:true }, ".ctor",
(new JSIL.MethodSignature(null, [$.Int64], [])),
function _ctor (value) {
this.pointer = null;
this.value = value.ToNumber() | 0;
}
);
$.RawMethod(false, "__CopyMembers__",
function IntPtr_CopyMembers (source, target) {
target.value = source.value;
if (!source.pointer)
target.pointer = null;
else
target.pointer = source.pointer;
}
);
function isNullPointer(p) {
return (!p.pointer && p.value === 0) ||
(p.pointer && p.pointer.offsetInBytes === 0);
};
$.Method({Static:true , Public:true }, "op_Equality",
(new JSIL.MethodSignature($.Boolean, [tIntPtr, tIntPtr], [])),
function op_Equality (lhs, rhs) {
// Null pointers always equal, regardless of where they came from
if (isNullPointer(lhs) && isNullPointer(rhs)) {
return true;
}
if (lhs.pointer) {
if (!rhs.pointer) // Non-null emscripten pointers can't equal C# ones
return false;
return rhs.pointer.equals(lhs.pointer);
} else {
return lhs.value === rhs.value;
}
}
);
$.Method({Static:true , Public:true }, "op_Inequality",
(new JSIL.MethodSignature($.Boolean, [tIntPtr, tIntPtr], [])),
function op_Inequality (lhs, rhs) {
if (isNullPointer(lhs) && isNullPointer(rhs)) {
return false;
}
if (lhs.pointer) {
if (!rhs.pointer)
return true;
return !rhs.pointer.equals(lhs.pointer);
} else {
return lhs.value !== rhs.value;
}
}
);
$.Method({Static:true , Public:true }, "op_Addition",
(new JSIL.MethodSignature(tIntPtr, [tIntPtr, $.Int32], [])),
function op_Addition (lhs, rhs) {
if (lhs.pointer) {
var newPointer = lhs.pointer.add(rhs, false);
return JSIL.CreateInstanceOfType(
System.IntPtr.__Type__,
"$fromPointer",
[newPointer]
);
} else {
return JSIL.CreateInstanceOfType(
System.IntPtr.__Type__,
"$fromInt32",
[lhs.value + rhs]
);
}
}
);
$.Method({Static:false, Public:true }, "ToInt32",
(new JSIL.MethodSignature($.Int32, [], [])),
function ToInt32 () {
if (this.pointer) {
return this.pointer.offsetInBytes;
} else {
return this.value;
}
}
);
$.Method({Static:false, Public:true }, "ToInt64",
(new JSIL.MethodSignature($.Int64, [], [])),
function ToInt64 () {
if (this.pointer) {
return $jsilcore.System.Int64.FromInt32(
this.pointer.offsetInBytes
);
} else {
return $jsilcore.System.Int64.FromInt32(
this.value
);
}
}
);
$.Method({Static:true , Public:true }, "op_Explicit",
new JSIL.MethodSignature($jsilcore.TypeRef("System.IntPtr"), [$.Int32]),
function op_Explicit (value) {
return JSIL.CreateInstanceOfType(
System.IntPtr.__Type__,
"$fromInt32",
[value]
);
}
);
$.Method({Static:true , Public:true }, "op_Explicit",
new JSIL.MethodSignature($jsilcore.TypeRef("System.IntPtr"), [$.Int64]),
function op_Explicit (value) {
return JSIL.CreateInstanceOfType(
System.IntPtr.__Type__,
"$fromInt64",
[value]
);
}
);
$.Method({Static:true , Public:true }, "op_Explicit",
new JSIL.MethodSignature($jsilcore.TypeRef("System.IntPtr"), [$jsilcore.TypeRef("JSIL.Pointer", [$jsilcore.TypeRef("System.Void")])]),
function op_Explicit (value) {
return JSIL.CreateInstanceOfType(
System.IntPtr.__Type__,
"$fromPointer",
[value]
);
}
);
$.Method({Static:true , Public:true }, "op_Explicit",
new JSIL.MethodSignature($jsilcore.TypeRef("JSIL.Pointer", [$jsilcore.TypeRef("System.Void")]), [$jsilcore.TypeRef("System.IntPtr")]),
function op_Explicit (value) {
if (value.pointer) {
// FIXME: Cast it?
return value.pointer;
} else {
throw new Error('Not implemented');
}
}
);
$.Method({Static:true , Public:true }, "op_Explicit",
new JSIL.MethodSignature($.Int32, [$jsilcore.TypeRef("System.IntPtr")]),
function op_Explicit (value) {
if (value.pointer) {
return value.pointer.offsetInBytes;
} else {
return value.value;
}
}
);
$.Method({Static:true , Public:true }, "op_Explicit",
new JSIL.MethodSignature($.Int64, [$jsilcore.TypeRef("System.IntPtr")]),
function op_Explicit (value) {
if (value.pointer) {
return $jsilcore.System.Int64.FromInt32(value.pointer.offsetInBytes);
} else {
return $jsilcore.System.Int64.FromInt32(value.value);
}
}
);
});
JSIL.ImplementExternals("System.UIntPtr", function ($) {
$.Method({Static:false, Public:true }, ".ctor",
(new JSIL.MethodSignature(null, [$.UInt32], [])),
function _ctor (value) {
this.value = value >>> 0;
}
);
$.Method({Static:false, Public:true }, ".ctor",
(new JSIL.MethodSignature(null, [$.UInt64], [])),
function _ctor (value) {
this.value = value.ToNumber() >>> 0;
}
);
$.Method({Static:false, Public:true }, "ToUInt32",
(new JSIL.MethodSignature($.UInt32, [], [])),
function ToUInt32 () {
return this.value;
}
);
$.Method({Static:false, Public:true }, "ToUInt64",
(new JSIL.MethodSignature($.UInt64, [], [])),
function ToUInt64 () {
return $jsilcore.System.UInt64.FromUInt32(this.value);
}
);
});
JSIL.MakeStruct("System.ValueType", "System.IntPtr", true, [], function ($) {
$.Field({Static:false, Public:false }, "value", $.Int32);
$.Field({Static:true, Public:true }, "Zero", $.Type);
});
JSIL.MakeStruct("System.ValueType", "System.UIntPtr", true, [], function ($) {
$.Field({Static:false, Public:false }, "value", $.UInt32);
$.Field({Static:true, Public:true }, "Zero", $.Type);
});
JSIL.MakeStruct("System.ValueType", "System.Void", true, [], function ($) {
});
JSIL.DeclareNamespace("System.Runtime.InteropServices");
JSIL.ImplementExternals("System.Runtime.InteropServices.Marshal", function ($) {
$.Method({Static:true , Public:true }, "StructureToPtr",
(new JSIL.MethodSignature(null, [
$.Object, $.IntPtr,
$.Boolean
], [])),
function StructureToPtr (structure, ptr, fDeleteOld) {
throw new Error('Not implemented');
}
);
$.Method({Static:true , Public:true }, "SizeOf",
(new JSIL.MethodSignature($.Int32, [$.Object], [])),
function SizeOf (structure) {
var type = JSIL.GetType(structure);
return JSIL.GetNativeSizeOf(type, true);
}
)
$.Method({Static:true , Public:true }, "SizeOf",
(new JSIL.MethodSignature($.Int32, [$jsilcore.TypeRef("System.Type")], [])),
function SizeOf (type) {
return JSIL.GetNativeSizeOf(type, true);
}
);
$.Method({Static:true , Public:true }, "OffsetOf",
(new JSIL.MethodSignature($.IntPtr, [$jsilcore.TypeRef("System.Type"), $.String], [])),
function OffsetOf (type, fieldName) {
var fields = JSIL.GetFieldList(type);
for (var i = 0, l = fields.length; i < l; i++) {
var field = fields[i];
if (field.name === fieldName)
return new System.IntPtr(field.offsetBytes);
}
throw new System.Exception("No field named '" + fieldName + "' declared in type");
}
);
$.Method({Static:true , Public:true }, "Copy",
new JSIL.MethodSignature(null, [
$.IntPtr, $jsilcore.TypeRef("System.Array", [$.Byte]),
$.Int32, $.Int32
]),
function Copy (source, destination, startIndex, length) {
if (!source.pointer)
JSIL.RuntimeError("Source argument must be a pointer into a pinned buffer, not a raw value");
var pSource = source.pointer.cast($jsilcore.System.Byte);
for (var i = 0, l = length | 0, s = startIndex | 0; i < l; i++) {
var d = (s + i) | 0;
destination[d] = pSource.getElement(i);
}
}
);
$.Method({Static:true , Public:true }, "Copy",
new JSIL.MethodSignature(null, [
$jsilcore.TypeRef("System.Array", [$.Byte]), $.Int32, $.IntPtr, $.Int32
]),
function Copy (source, startIndex, destination, length) {
if (!destination.pointer)
JSIL.RuntimeError("Destination argument must be a pointer into a pinned buffer, not a raw value");
var pDest = destination.pointer.cast($jsilcore.System.Byte);
for (var i = 0, l = length | 0, s = startIndex | 0; i < l; i++) {
pDest.setElement(
i,
source[(s + i) | 0]
);
}
}
);
var mallocImpl = function (sizeBytes) {
var module = JSIL.PInvoke.GetDefaultModule();
var address = module._malloc(sizeBytes | 0);
var result = JSIL.PInvoke.CreateIntPtrForModule(module, address);
return result;
};
$.Method({Static:true , Public:true }, "AllocHGlobal",
new JSIL.MethodSignature($.IntPtr, [$.IntPtr]),
function AllocHGlobal (cb) {
return mallocImpl(cb.ToInt32());
}
);
$.Method({Static:true , Public:true }, "AllocHGlobal",
new JSIL.MethodSignature($.IntPtr, [$.Int32]),
function AllocHGlobal (cb) {
return mallocImpl(cb);
}
);
$.Method({Static:true , Public:true }, "FreeHGlobal",
JSIL.MethodSignature.Action($.IntPtr),
function FreeHGlobal (hglobal) {
var module = JSIL.PInvoke.PickModuleForPointer(hglobal, true);
module._free(hglobal.ToInt32());
}
);
});
JSIL.MakeType({
BaseType: $jsilcore.TypeRef("System.ValueType"),
Name: "System.Runtime.InteropServices.GCHandle",
IsPublic: true,
IsReferenceType: false,
MaximumConstructorArguments: 2,
}, function ($interfaceBuilder) {
}
);
JSIL.ImplementExternals("System.Runtime.InteropServices.GCHandle", function ($) {
$.RawMethod(false, "$internalCtor", function (obj) {
this._target = obj;
if (obj && obj.__ThisType__ && obj.__ThisType__.__IsDelegate__) {
this._pointer = obj.$pin();
} else {
this._pointer = JSIL.PinAndGetPointer(obj, 0);
}
});
$.RawMethod(false, "__CopyMembers__",
function GCHandle_CopyMembers (source, target) {
target._pointer = source._pointer;
target._target = source._target;
}
);
$.Method({Static:false, Public:true }, "AddrOfPinnedObject",
new JSIL.MethodSignature($.IntPtr, [], []),
function AddrOfPinnedObject () {
return JSIL.CreateInstanceOfType(
System.IntPtr.__Type__,
"$fromPointer",
[this._pointer]
);
}
);
$.Method({Static:true , Public:true }, "Alloc",
new JSIL.MethodSignature($jsilcore.TypeRef("System.Runtime.InteropServices.GCHandle"), [$.Object], []),
function Alloc (value) {
return JSIL.CreateInstanceOfType(
System.Runtime.InteropServices.GCHandle.__Type__,
"$internalCtor",
[value]
);
}
);
$.Method({Static:true , Public:true }, "Alloc",
new JSIL.MethodSignature($jsilcore.TypeRef("System.Runtime.InteropServices.GCHandle"), [$.Object, $jsilcore.TypeRef("System.Runtime.InteropServices.GCHandleType")], []),
function Alloc (value, type) {
// FIXME: type
return JSIL.CreateInstanceOfType(
System.Runtime.InteropServices.GCHandle.__Type__,
"$internalCtor",
[value]
);
}
);
$.Method({Static:false, Public:true }, "Free",
JSIL.MethodSignature.Void,
function Free () {
if (this._target && this._target.__ThisType__.__IsDelegate__) {
this._target.$unpin();
} else {
// FIXME: Unpin
}
this._pointer = null;
this._target = null;
}
);
});
JSIL.MakeEnum(
{
FullName: "System.Runtime.InteropServices.GCHandleType",
BaseType: $jsilcore.TypeRef("System.Int32"),
IsPublic: true,
IsFlags: false,
},
{
Weak: 0,
WeakTrackResurrection: 1,
Normal: 2,
Pinned: 3,
}
);
JSIL.ImplementExternals("System.Buffer", function ($interfaceBuilder) {
var $ = $interfaceBuilder;
$.Method({Static:true , Public:true }, "BlockCopy",
new JSIL.MethodSignature(null, [
$jsilcore.TypeRef("System.Array"), $.Int32,
$jsilcore.TypeRef("System.Array"), $.Int32,
$.Int32
], []),
function BlockCopy (src, srcOffset, dst, dstOffset, count) {
var srcBuffer = JSIL.GetArrayBuffer(src);
srcOffset += JSIL.GetArrayByteOffset(src);
var dstBuffer = JSIL.GetArrayBuffer(dst);
dstOffset += JSIL.GetArrayByteOffset(dst);
var srcView = new Uint8Array(srcBuffer, srcOffset, count);
var dstView = new Uint8Array(dstBuffer, dstOffset, count);
dstView.set(srcView);
}
);
$.Method({Static:true , Public:true }, "ByteLength",
new JSIL.MethodSignature($.Int32, [$jsilcore.TypeRef("System.Array")], []),
function ByteLength (array) {
return JSIL.GetArrayByteLength(array);
}
);
$.Method({Static:true , Public:true }, "GetByte",
new JSIL.MethodSignature($.Byte, [$jsilcore.TypeRef("System.Array"), $.Int32], []),
function GetByte (array, index) {
var buffer = JSIL.GetArrayBuffer(array);
index += JSIL.GetArrayByteOffset(array);
var view = new Uint8Array(buffer, index, 1);
return view[0];
}
);
});
JSIL.MakeStaticClass("System.Runtime.InteropServices.Marshal", true, [], function ($) {
});
JSIL.MakeClass("System.Object", "JSIL.MemoryRange", true, [], function ($) {
$.RawMethod(false, ".ctor",
function MemoryRange_ctor (buffer, offset, length) {
this.buffer = buffer;
this.offset = offset | 0;
if (typeof (length) === "number") {
this.length = length | 0;
} else {
this.length = buffer.byteLength;
}
if (this.offset < 0)
JSIL.RuntimeError("MemoryRange offset must be >= 0");
else if (this.length < 0)
JSIL.RuntimeError("MemoryRange length must be >= 0");
if (typeof (Map) !== "undefined") {
this.viewCache = new Map();
this.viewCacheIsMap = true;
} else {
this.viewCache = JSIL.CreateDictionaryObject(null);
this.viewCacheIsMap = false;
}
this.viewCacheByElementType = JSIL.CreateDictionaryObject(null);
}
);
$.RawMethod(false, "getCachedView",
function MemoryRange_getCachedView (key) {
if (this.viewCacheIsMap) {
return this.viewCache.get(key);
} else {
var ctorKey = key.name || String(key.constructor);
return this.viewCache[ctorKey];
}
}
);
$.RawMethod(false, "setCachedView",
function MemoryRange_setCachedView (key, value) {
if (this.viewCacheIsMap) {
this.viewCache.set(key, value);
} else {
var ctorKey = key.name || String(key.constructor);
this.viewCache[ctorKey] = value;
}
}
);
$.RawMethod(false, "storeExistingView",
function MemoryRange_storeExistingView (view) {
var arrayCtor = Object.getPrototypeOf(view);
var cachedView = this.getCachedView(arrayCtor);
if (cachedView) {
// FIXME: I don't think this is actually a problem?
// I'm not really sure why it happens, though...
/*
if (cachedView !== view)
JSIL.RuntimeError("A different view is already stored for this element type");
*/
} else {
this.setCachedView(arrayCtor, view);
}
}
);
$.RawMethod(false, "getView",
function MemoryRange_getView (elementTypeObject, byteFallback) {
var arrayCtor = JSIL.GetTypedArrayConstructorForElementType(elementTypeObject, byteFallback);
if (!arrayCtor)
return null;
var result = this.getCachedView(arrayCtor);
if (!result) {
var elementCount = (this.length / arrayCtor.BYTES_PER_ELEMENT) | 0;
result = new arrayCtor(this.buffer, this.offset, elementCount);
this.setCachedView(arrayCtor, result);
}
return result;
}
);
});
JSIL.MakeStruct("System.ValueType", "JSIL.Pointer", true, ["T"], function ($) {
var shiftTable = [];
for (var i = 0; i < 256; i++) {
shiftTable[i] = (Math.log(i) / Math.LN2) | 0;
}
$.SetValue("__IsPointer__", true);
function Pointer_ctor (elementType, memoryRange, view, offsetInBytes) {
if (arguments.length !== 4)
JSIL.RuntimeError("Pointer ctor expects (elementType, memoryRange, view, offsetInBytes)");
this.memoryRange = memoryRange;
this.view = view;
this.offsetInBytes = offsetInBytes | 0;
if (this.view) {
this.shift = shiftTable[this.view.BYTES_PER_ELEMENT] | 0;
} else {
this.shift = 0;
}
this.offsetInElements = offsetInBytes >> this.shift;
this.elementType = elementType;
};
$.RawMethod(false, ".ctor", Pointer_ctor);
$.RawMethod(false, "__CopyMembers__",
function Pointer_CopyMembers (source, target) {
target.memoryRange = source.memoryRange;
target.view = source.view;
target.offsetInBytes = source.offsetInBytes;
target.offsetInElements = source.offsetInElements;
target.shift = source.shift;
target.elementType = source.elementType;
}
);
$.RawMethod(false, "get",
function Pointer_Get () {
return this.view[this.offsetInElements];
}
);
$.RawMethod(false, "set",
function Pointer_Set (value) {
this.view[this.offsetInElements] = value;
}
);
$.RawMethod(false, "getElement",
function Pointer_GetElement (offsetInElements) {
return this.view[(this.offsetInElements + offsetInElements) | 0];
}
);
$.RawMethod(false, "setElement",
function Pointer_SetElement (offsetInElements, value) {
this.view[(this.offsetInElements + offsetInElements) | 0] = value;
}
);
$.RawMethod(false, "getOffset",
function Pointer_GetOffset (offsetInBytes) {
var index = ((this.offsetInBytes + offsetInBytes) | 0) >> this.shift;
return this.view[index];
}
);
$.RawMethod(false, "setOffset",
function Pointer_SetOffset (offsetInBytes, value) {
var index = ((this.offsetInBytes + offsetInBytes) | 0) >> this.shift;
this.view[index] = value;
}
);
$.RawMethod(false, "cast",
function Pointer_Cast (elementType) {
var typeObject =
elementType.__Type__ || elementType;
var view = this.memoryRange.getView(typeObject, true);
return JSIL.NewPointer(typeObject, this.memoryRange, view, this.offsetInBytes);
}
);
$.RawMethod(false, "asView",
function Pointer_asView (elementType, sizeInBytes) {
var underlyingBufferSize = (this.memoryRange.buffer.byteLength - this.offsetInBytes) | 0;
// FIXME: Maybe make this null instead? Int-only is probably better.
sizeInBytes = sizeInBytes | 0;
if (sizeInBytes === -1)
sizeInBytes = underlyingBufferSize;
var arrayCtor = JSIL.GetTypedArrayConstructorForElementType(elementType.__Type__, true);
var offsetInElements = (this.offsetBytes / arrayCtor.BYTES_PER_ELEMENT) | 0;
var sizeInElements = ((sizeInBytes | 0) / arrayCtor.BYTES_PER_ELEMENT) | 0;
if ((this.offsetInBytes % arrayCtor.BYTES_PER_ELEMENT) !== 0)
JSIL.RuntimeError("Pointer must be element-aligned");
if ((sizeInBytes % arrayCtor.BYTES_PER_ELEMENT) !== 0)
JSIL.RuntimeError("Size must be an integral multiple of element size");
// Where possible, return a cached view to avoid creating garbage.
// FIXME: Maybe don't even apply the size constraint here?
if ((offsetInElements === 0) && (sizeInBytes === underlyingBufferSize))
return this.memoryRange.getView(elementType.__Type__, true);
var view = new arrayCtor(this.memoryRange.buffer, offsetInElements, sizeInElements);
return view;
}
);
$.RawMethod(false, "add",
function Pointer_Add (offsetInBytes, modifyInPlace) {
if (modifyInPlace === true) {
this.offsetInBytes = (this.offsetInBytes + offsetInBytes) | 0;
this.offsetInElements = this.offsetInBytes >> this.shift;
} else {
var ctor = this.__ThisType__.__PublicInterface__;
return new (ctor)(
this.elementType, this.memoryRange, this.view, (this.offsetInBytes + offsetInBytes) | 0
);
}
}
);
$.RawMethod(false, "addElements",
function Pointer_AddElements (offsetInElements, modifyInPlace) {
if (modifyInPlace === true) {
this.offsetInElements = (this.offsetInElements + offsetInElements) | 0;
this.offsetInBytes = (this.offsetInBytes + (offsetInElements << this.shift)) | 0;
} else {
var ctor = this.__ThisType__.__PublicInterface__;
return new (ctor)(
this.elementType, this.memoryRange, this.view, (this.offsetInBytes + (offsetInElements << this.shift)) | 0
);
}
}
);
$.RawMethod(false, "deltaBytes",
function Pointer_DeltaBytes (otherPointer) {
if (otherPointer.memoryRange.buffer !== this.memoryRange.buffer)
JSIL.RuntimeError("Cannot subtract two pointers from different pinned buffers");
return (this.offsetInBytes - otherPointer.offsetInBytes) | 0;
}
);
function getBuffer (ptr) {
if (ptr.memoryRange !== null)
return ptr.memoryRange.buffer;
else
return null;
};
$.RawMethod(false, "equals",
function Pointer_Equals (rhs) {
if (rhs === null)
return false;
else if (rhs === this)
return true;
else
return (getBuffer(this) === getBuffer(rhs)) &&
(this.offsetInBytes === rhs.offsetInBytes);
}
);
$.RawMethod(false, "lessThan",
function Pointer_LessThan (rhs) {
if (rhs === null)
return false;
else
return (getBuffer(this) === getBuffer(rhs)) &&
(this.offsetInBytes < rhs.offsetInBytes);
}
);
$.RawMethod(false, "greaterThan",
function Pointer_GreaterThan (rhs) {
if (rhs === null)
return false;
else
return (getBuffer(this) === getBuffer(rhs)) &&
(this.offsetInBytes > rhs.offsetInBytes);
}
);
$.Method({ Static: false, Public: true }, "Object.Equals",
new JSIL.MethodSignature($.Boolean, [$.Object]),
function Pointer_Equals (rhs) {
return this.equals(rhs);
}
);
$.RawMethod(false, "toString",
function Pointer_ToString () {
return "<ptr " + this.view + " + " + this.offsetInBytes + ">";
}
);
});
JSIL.MakeStruct("JSIL.Pointer", "JSIL.VoidPointer", true, [], function ($) {
});
JSIL.MakeStruct("JSIL.Pointer", "JSIL.NullPointer", true, [], function ($) {
$.SetValue("__IsNull__", true);
function NullPointer_ctor (elementType) {
if (arguments.length !== 1)
JSIL.RuntimeError("NullPointer ctor expects (elementType)");
this.memoryRange = null;
this.view = null;
this.offsetInBytes = 0;
this.shift = 0;
this.offsetInElements = 0;
this.elementType = elementType;
};
$.RawMethod(false, ".ctor", NullPointer_ctor);
$.RawMethod(false, "toString",
function NullPointer_ToString () {
return "<null ptr>";
}
);
});
JSIL.MakeStruct("JSIL.Pointer", "JSIL.WordPointer", true, [], function ($) {
$.RawMethod(false, "get",
function WordPointer_Get () {
return this.view[this.offsetInElements];
}
);
$.RawMethod(false, "set",
function WordPointer_Set (value) {
this.view[this.offsetInElements] = value;
}
);
$.RawMethod(false, "getElement",
function WordPointer_GetElement (offsetInElements) {
return this.view[(this.offsetInElements + offsetInElements) | 0];
}
);
$.RawMethod(false, "setElement",
function WordPointer_SetElement (offsetInElements, value) {
this.view[(this.offsetInElements + offsetInElements) | 0] = value;
}
);
$.RawMethod(false, "getOffset",
function WordPointer_GetOffset (offsetInBytes) {
return this.view[((this.offsetInBytes + offsetInBytes) | 0) >> 1];
}
);
$.RawMethod(false, "setOffset",
function WordPointer_SetOffset (offsetInBytes, value) {
this.view[((this.offsetInBytes + offsetInBytes) | 0) >> 1] = value;
}
);
});
(function () {
function DoubleWordPointer_Get () {
return this.view[this.offsetInElements];
};
function DoubleWordPointer_Set (value) {
this.view[this.offsetInElements] = value;
};
function DoubleWordPointer_GetElement (offsetInElements) {
return this.view[(this.offsetInElements + offsetInElements) | 0];
};
function DoubleWordPointer_SetElement (offsetInElements, value) {
this.view[(this.offsetInElements + offsetInElements) | 0] = value;
};
function DoubleWordPointer_GetOffset (offsetInBytes) {
return this.view[((this.offsetInBytes + offsetInBytes) | 0) >> 2];
};
function DoubleWordPointer_SetOffset (offsetInBytes, value) {
this.view[((this.offsetInBytes + offsetInBytes) | 0) >> 2] = value;
};
JSIL.MakeStruct("JSIL.Pointer", "JSIL.DoubleWordPointer", true, [], function ($) {
$.RawMethod(false, "get", DoubleWordPointer_Get);
$.RawMethod(false, "set", DoubleWordPointer_Set);
$.RawMethod(false, "getElement", DoubleWordPointer_GetElement);
$.RawMethod(false, "setElement", DoubleWordPointer_SetElement);
$.RawMethod(false, "getOffset", DoubleWordPointer_GetOffset);
$.RawMethod(false, "setOffset", DoubleWordPointer_SetOffset);
});
})();
JSIL.MakeStruct("JSIL.Pointer", "JSIL.QuadWordPointer", true, [], function ($) {
$.RawMethod(false, "get",
function QuadWordPointer_Get () {
return this.view[this.offsetInElements];
}
);
$.RawMethod(false, "set",
function QuadWordPointer_Set (value) {
this.view[this.offsetInElements] = value;
}
);
$.RawMethod(false, "getElement",
function QuadWordPointer_GetElement (offsetInElements) {
return this.view[(this.offsetInElements + offsetInElements) | 0];
}
);
$.RawMethod(false, "setElement",
function QuadWordPointer_SetElement (offsetInElements, value) {
this.view[(this.offsetInElements + offsetInElements) | 0] = value;
}
);
$.RawMethod(false, "getOffset",
function QuadWordPointer_GetOffset (offsetInBytes) {
return this.view[((this.offsetInBytes + offsetInBytes) | 0) >> 3];
}
);
$.RawMethod(false, "setOffset",
function QuadWordPointer_SetOffset (offsetInBytes, value) {
this.view[((this.offsetInBytes + offsetInBytes) | 0) >> 3] = value;
}
);
});
JSIL.MakeStruct("JSIL.Pointer", "JSIL.BytePointer", true, [], function ($) {
$.RawMethod(false, "get",
function BytePointer_Get () {
return this.view[this.offsetInBytes];
}
);
$.RawMethod(false, "set",
function BytePointer_Set (value) {
this.view[this.offsetInBytes] = value;
}
);
$.RawMethod(false, "getElement",
function BytePointer_GetElement (offsetInElements) {
return this.view[(this.offsetInBytes + offsetInElements) | 0];
}
);
$.RawMethod(false, "setElement",
function BytePointer_SetElement (offsetInElements, value) {
this.view[(this.offsetInBytes + offsetInElements) | 0] = value;
}
);
$.RawMethod(false, "getOffset",
function BytePointer_GetOffset (offsetInBytes) {
return this.view[(this.offsetInBytes + offsetInBytes) | 0];
}
);
$.RawMethod(false, "setOffset",
function BytePointer_SetOffset (offsetInBytes, value) {
this.view[(this.offsetInBytes + offsetInBytes) | 0] = value;
}
);
});
JSIL.MakeStruct("JSIL.Pointer", "JSIL.StructPointer", true, [], function ($) {
function ElementTypeRecord (elementType) {
this.type = elementType;
this.typeId = elementType.__TypeId__;
this.nativeSize = elementType.__NativeSize__;
this.unmarshalConstructor = JSIL.$GetStructUnmarshalConstructor(elementType);
// this.unmarshaller = JSIL.$GetStructUnmarshaller(structType);