Skip to content

Commit db0e82f

Browse files
committed
Add a .data pointer on ArrayBuffers for convenience, see AssemblyScript#291
1 parent b7e7be2 commit db0e82f

13 files changed

+89
-31
lines changed

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

std/assembly/arraybuffer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export class ArrayBuffer {
99

1010
readonly byteLength: i32; // capped to [0, MAX_LENGTH]
1111

12+
// @unsafe
13+
get data(): usize { return changetype<usize>(this) + HEADER_SIZE; }
14+
1215
constructor(length: i32, unsafe: bool = false) {
1316
if (<u32>length > <u32>MAX_BLENGTH) throw new RangeError("Invalid array buffer length");
1417
var buffer = allocateUnsafe(length);

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ declare namespace table {
372372
declare class ArrayBuffer {
373373
/** The size, in bytes, of the array. */
374374
readonly byteLength: i32;
375+
/** Unsafe pointer to the start of the data in memory. */
376+
readonly data: usize;
375377
/** Constructs a new array buffer of the given length in bytes. */
376378
constructor(length: i32, unsafe?: bool);
377379
/** Returns a copy of this array buffer's bytes from begin, inclusive, up to end, exclusive. */

tests/compiler/std/arraybuffer.optimized.wat

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,13 @@
22762276
(get_local $2)
22772277
)
22782278
)
2279-
(func $start (; 10 ;) (; has Stack IR ;) (type $v)
2279+
(func $~lib/arraybuffer/ArrayBuffer#get:data (; 10 ;) (; has Stack IR ;) (type $ii) (param $0 i32) (result i32)
2280+
(i32.add
2281+
(get_local $0)
2282+
(i32.const 8)
2283+
)
2284+
)
2285+
(func $start (; 11 ;) (; has Stack IR ;) (type $v)
22802286
(set_global $~lib/allocator/arena/startOffset
22812287
(i32.const 160)
22822288
)
@@ -2329,6 +2335,25 @@
23292335
(unreachable)
23302336
)
23312337
)
2338+
(if
2339+
(i32.eq
2340+
(call $~lib/arraybuffer/ArrayBuffer#get:data
2341+
(get_global $std/arraybuffer/sliced)
2342+
)
2343+
(call $~lib/arraybuffer/ArrayBuffer#get:data
2344+
(get_global $std/arraybuffer/buffer)
2345+
)
2346+
)
2347+
(block
2348+
(call $~lib/env/abort
2349+
(i32.const 0)
2350+
(i32.const 120)
2351+
(i32.const 10)
2352+
(i32.const 0)
2353+
)
2354+
(unreachable)
2355+
)
2356+
)
23322357
(if
23332358
(i32.eq
23342359
(get_global $std/arraybuffer/sliced)
@@ -2338,7 +2363,7 @@
23382363
(call $~lib/env/abort
23392364
(i32.const 0)
23402365
(i32.const 120)
2341-
(i32.const 10)
2366+
(i32.const 11)
23422367
(i32.const 0)
23432368
)
23442369
(unreachable)
@@ -2364,7 +2389,7 @@
23642389
(call $~lib/env/abort
23652390
(i32.const 0)
23662391
(i32.const 120)
2367-
(i32.const 14)
2392+
(i32.const 15)
23682393
(i32.const 0)
23692394
)
23702395
(unreachable)
@@ -2390,7 +2415,7 @@
23902415
(call $~lib/env/abort
23912416
(i32.const 0)
23922417
(i32.const 120)
2393-
(i32.const 18)
2418+
(i32.const 19)
23942419
(i32.const 0)
23952420
)
23962421
(unreachable)
@@ -2414,7 +2439,7 @@
24142439
(call $~lib/env/abort
24152440
(i32.const 0)
24162441
(i32.const 120)
2417-
(i32.const 22)
2442+
(i32.const 23)
24182443
(i32.const 0)
24192444
)
24202445
(unreachable)
@@ -2438,7 +2463,7 @@
24382463
(call $~lib/env/abort
24392464
(i32.const 0)
24402465
(i32.const 120)
2441-
(i32.const 26)
2466+
(i32.const 27)
24422467
(i32.const 0)
24432468
)
24442469
(unreachable)
@@ -2462,7 +2487,7 @@
24622487
(call $~lib/env/abort
24632488
(i32.const 0)
24642489
(i32.const 120)
2465-
(i32.const 30)
2490+
(i32.const 31)
24662491
(i32.const 0)
24672492
)
24682493
(unreachable)
@@ -2486,7 +2511,7 @@
24862511
(call $~lib/env/abort
24872512
(i32.const 0)
24882513
(i32.const 120)
2489-
(i32.const 34)
2514+
(i32.const 35)
24902515
(i32.const 0)
24912516
)
24922517
(unreachable)
@@ -2509,7 +2534,7 @@
25092534
(call $~lib/env/abort
25102535
(i32.const 0)
25112536
(i32.const 120)
2512-
(i32.const 38)
2537+
(i32.const 39)
25132538
(i32.const 0)
25142539
)
25152540
(unreachable)
@@ -2523,14 +2548,14 @@
25232548
(call $~lib/env/abort
25242549
(i32.const 0)
25252550
(i32.const 120)
2526-
(i32.const 39)
2551+
(i32.const 40)
25272552
(i32.const 0)
25282553
)
25292554
(unreachable)
25302555
)
25312556
)
25322557
)
2533-
(func $null (; 11 ;) (; has Stack IR ;) (type $v)
2558+
(func $null (; 12 ;) (; has Stack IR ;) (type $v)
25342559
(nop)
25352560
)
25362561
)

tests/compiler/std/arraybuffer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ assert(buffer.byteLength == 8);
77
var sliced = buffer.slice();
88

99
assert(sliced.byteLength == 8);
10+
assert(sliced.data != buffer.data);
1011
assert(sliced !== buffer);
1112

1213
sliced = buffer.slice(1);

tests/compiler/std/arraybuffer.untouched.wat

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@
558558
(call $~lib/env/abort
559559
(i32.const 0)
560560
(i32.const 8)
561-
(i32.const 13)
561+
(i32.const 16)
562562
(i32.const 40)
563563
)
564564
(unreachable)
@@ -2871,7 +2871,13 @@
28712871
(get_local $2)
28722872
)
28732873
)
2874-
(func $start (; 10 ;) (type $v)
2874+
(func $~lib/arraybuffer/ArrayBuffer#get:data (; 10 ;) (type $ii) (param $0 i32) (result i32)
2875+
(i32.add
2876+
(get_local $0)
2877+
(get_global $~lib/internal/arraybuffer/HEADER_SIZE)
2878+
)
2879+
)
2880+
(func $start (; 11 ;) (type $v)
28752881
(set_global $~lib/allocator/arena/startOffset
28762882
(i32.and
28772883
(i32.add
@@ -2944,6 +2950,27 @@
29442950
(unreachable)
29452951
)
29462952
)
2953+
(if
2954+
(i32.eqz
2955+
(i32.ne
2956+
(call $~lib/arraybuffer/ArrayBuffer#get:data
2957+
(get_global $std/arraybuffer/sliced)
2958+
)
2959+
(call $~lib/arraybuffer/ArrayBuffer#get:data
2960+
(get_global $std/arraybuffer/buffer)
2961+
)
2962+
)
2963+
)
2964+
(block
2965+
(call $~lib/env/abort
2966+
(i32.const 0)
2967+
(i32.const 120)
2968+
(i32.const 10)
2969+
(i32.const 0)
2970+
)
2971+
(unreachable)
2972+
)
2973+
)
29472974
(if
29482975
(i32.eqz
29492976
(i32.ne
@@ -2955,7 +2982,7 @@
29552982
(call $~lib/env/abort
29562983
(i32.const 0)
29572984
(i32.const 120)
2958-
(i32.const 10)
2985+
(i32.const 11)
29592986
(i32.const 0)
29602987
)
29612988
(unreachable)
@@ -2986,7 +3013,7 @@
29863013
(call $~lib/env/abort
29873014
(i32.const 0)
29883015
(i32.const 120)
2989-
(i32.const 14)
3016+
(i32.const 15)
29903017
(i32.const 0)
29913018
)
29923019
(unreachable)
@@ -3017,7 +3044,7 @@
30173044
(call $~lib/env/abort
30183045
(i32.const 0)
30193046
(i32.const 120)
3020-
(i32.const 18)
3047+
(i32.const 19)
30213048
(i32.const 0)
30223049
)
30233050
(unreachable)
@@ -3043,7 +3070,7 @@
30433070
(call $~lib/env/abort
30443071
(i32.const 0)
30453072
(i32.const 120)
3046-
(i32.const 22)
3073+
(i32.const 23)
30473074
(i32.const 0)
30483075
)
30493076
(unreachable)
@@ -3069,7 +3096,7 @@
30693096
(call $~lib/env/abort
30703097
(i32.const 0)
30713098
(i32.const 120)
3072-
(i32.const 26)
3099+
(i32.const 27)
30733100
(i32.const 0)
30743101
)
30753102
(unreachable)
@@ -3095,7 +3122,7 @@
30953122
(call $~lib/env/abort
30963123
(i32.const 0)
30973124
(i32.const 120)
3098-
(i32.const 30)
3125+
(i32.const 31)
30993126
(i32.const 0)
31003127
)
31013128
(unreachable)
@@ -3121,7 +3148,7 @@
31213148
(call $~lib/env/abort
31223149
(i32.const 0)
31233150
(i32.const 120)
3124-
(i32.const 34)
3151+
(i32.const 35)
31253152
(i32.const 0)
31263153
)
31273154
(unreachable)
@@ -3152,7 +3179,7 @@
31523179
(call $~lib/env/abort
31533180
(i32.const 0)
31543181
(i32.const 120)
3155-
(i32.const 38)
3182+
(i32.const 39)
31563183
(i32.const 0)
31573184
)
31583185
(unreachable)
@@ -3169,13 +3196,13 @@
31693196
(call $~lib/env/abort
31703197
(i32.const 0)
31713198
(i32.const 120)
3172-
(i32.const 39)
3199+
(i32.const 40)
31733200
(i32.const 0)
31743201
)
31753202
(unreachable)
31763203
)
31773204
)
31783205
)
3179-
(func $null (; 11 ;) (type $v)
3206+
(func $null (; 12 ;) (type $v)
31803207
)
31813208
)

tests/compiler/std/map.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@
478478
(call $~lib/env/abort
479479
(i32.const 0)
480480
(i32.const 8)
481-
(i32.const 13)
481+
(i32.const 16)
482482
(i32.const 40)
483483
)
484484
(unreachable)

tests/compiler/std/map.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@
582582
(call $~lib/env/abort
583583
(i32.const 0)
584584
(i32.const 8)
585-
(i32.const 13)
585+
(i32.const 16)
586586
(i32.const 40)
587587
)
588588
(unreachable)

tests/compiler/std/set.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@
479479
(call $~lib/env/abort
480480
(i32.const 0)
481481
(i32.const 8)
482-
(i32.const 13)
482+
(i32.const 16)
483483
(i32.const 40)
484484
)
485485
(unreachable)

tests/compiler/std/set.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@
584584
(call $~lib/env/abort
585585
(i32.const 0)
586586
(i32.const 8)
587-
(i32.const 13)
587+
(i32.const 16)
588588
(i32.const 40)
589589
)
590590
(unreachable)

tests/compiler/std/symbol.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@
499499
(call $~lib/env/abort
500500
(i32.const 0)
501501
(i32.const 56)
502-
(i32.const 13)
502+
(i32.const 16)
503503
(i32.const 40)
504504
)
505505
(unreachable)

tests/compiler/std/symbol.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@
613613
(call $~lib/env/abort
614614
(i32.const 0)
615615
(i32.const 56)
616-
(i32.const 13)
616+
(i32.const 16)
617617
(i32.const 40)
618618
)
619619
(unreachable)

0 commit comments

Comments
 (0)