Skip to content

Commit dd4be7b

Browse files
committed
Fix some map issues; Simplify internal ArrayBuffer API a bit
1 parent 48e96cb commit dd4be7b

14 files changed

+16846
-819
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.

dist/assemblyscript.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/assemblyscript.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.

src/ast.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ export enum NodeKind {
9999
COMMENT
100100
}
101101

102+
/** Checks if a node represents a constant value. */
103+
export function nodeIsConstantValue(kind: NodeKind): bool {
104+
switch (kind) {
105+
case NodeKind.LITERAL:
106+
case NodeKind.NULL:
107+
case NodeKind.TRUE:
108+
case NodeKind.FALSE: return true;
109+
}
110+
return false;
111+
}
112+
102113
/** Base class of all nodes. */
103114
export abstract class Node {
104115

src/compiler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ import {
126126
StringLiteralExpression,
127127
UnaryPostfixExpression,
128128
UnaryPrefixExpression,
129-
FieldDeclaration
129+
FieldDeclaration,
130+
131+
nodeIsConstantValue
130132
} from "./ast";
131133

132134
import {
@@ -5503,9 +5505,7 @@ export class Compiler extends DiagnosticEmitter {
55035505
let allOptionalsAreConstant = true;
55045506
for (let i = numArguments; i < maxArguments; ++i) {
55055507
let initializer = parameterNodes[i].initializer;
5506-
if (!(initializer && initializer.kind == NodeKind.LITERAL)) {
5507-
// TODO: other kinds might be constant as well
5508-
// NOTE: if the initializer is missing this is reported in ensureTrampoline below
5508+
if (!(initializer !== null && nodeIsConstantValue(initializer.kind))) {
55095509
allOptionalsAreConstant = false;
55105510
break;
55115511
}

src/program.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,34 +3505,33 @@ export class ClassPrototype extends Element {
35053505
fieldDeclaration.type,
35063506
instance.contextualTypeArguments
35073507
);
3508-
if (fieldType) {
3509-
let fieldInstance = new Field(
3510-
<FieldPrototype>member,
3511-
internalName + INSTANCE_DELIMITER + (<FieldPrototype>member).simpleName,
3512-
fieldType,
3513-
fieldDeclaration,
3514-
instance
3515-
);
3516-
switch (fieldType.byteSize) { // align
3517-
case 1: break;
3518-
case 2: {
3519-
if (memoryOffset & 1) ++memoryOffset;
3520-
break;
3521-
}
3522-
case 4: {
3523-
if (memoryOffset & 3) memoryOffset = (memoryOffset | 3) + 1;
3524-
break;
3525-
}
3526-
case 8: {
3527-
if (memoryOffset & 7) memoryOffset = (memoryOffset | 7) + 1;
3528-
break;
3529-
}
3530-
default: assert(false);
3508+
if (!fieldType) break;
3509+
let fieldInstance = new Field(
3510+
<FieldPrototype>member,
3511+
internalName + INSTANCE_DELIMITER + (<FieldPrototype>member).simpleName,
3512+
fieldType,
3513+
fieldDeclaration,
3514+
instance
3515+
);
3516+
switch (fieldType.byteSize) { // align
3517+
case 1: break;
3518+
case 2: {
3519+
if (memoryOffset & 1) ++memoryOffset;
3520+
break;
3521+
}
3522+
case 4: {
3523+
if (memoryOffset & 3) memoryOffset = (memoryOffset | 3) + 1;
3524+
break;
3525+
}
3526+
case 8: {
3527+
if (memoryOffset & 7) memoryOffset = (memoryOffset | 7) + 1;
3528+
break;
35313529
}
3532-
fieldInstance.memoryOffset = memoryOffset;
3533-
memoryOffset += fieldType.byteSize;
3534-
instance.members.set(member.simpleName, fieldInstance);
3530+
default: assert(false);
35353531
}
3532+
fieldInstance.memoryOffset = memoryOffset;
3533+
memoryOffset += fieldType.byteSize;
3534+
instance.members.set(member.simpleName, fieldInstance);
35363535
break;
35373536
}
35383537

std/assembly.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ declare class ArrayBuffer {
307307
/** The size, in bytes, of the array. */
308308
readonly byteLength: i32;
309309
/** Constructs a new array buffer of the given length in bytes. */
310-
constructor(length: i32);
310+
constructor(length: i32, unsafe?: bool);
311311
/** Returns a copy of this array buffer's bytes from begin, inclusive, up to end, exclusive. */
312312
slice(begin?: i32, end?: i32): ArrayBuffer;
313313
}

std/assembly/arraybuffer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ export class ArrayBuffer {
99

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

12-
constructor(length: i32) {
12+
constructor(length: i32, unsafe: bool = false) {
1313
if (<u32>length > <u32>MAX_BLENGTH) throw new RangeError("Invalid array buffer length");
1414
var buffer = allocUnsafe(length);
15-
set_memory(changetype<usize>(buffer) + HEADER_SIZE, 0, <usize>length);
15+
if (!unsafe) set_memory(changetype<usize>(buffer) + HEADER_SIZE, 0, <usize>length);
1616
return buffer;
1717
}
1818

@@ -27,4 +27,16 @@ export class ArrayBuffer {
2727
move_memory(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
2828
return buffer;
2929
}
30+
31+
// internals
32+
33+
static readonly HEADER_SIZE: usize = HEADER_SIZE;
34+
35+
@inline load<T>(index: i32): T {
36+
return load<T>(changetype<usize>(this) + index * sizeof<T>(), HEADER_SIZE);
37+
}
38+
39+
@inline store<T>(index: i32, value: T): void {
40+
store<T>(changetype<usize>(this) + index * sizeof<T>(), value, HEADER_SIZE);
41+
}
3042
}

tests/compiler/std/arraybuffer.optimized.wat

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
(module
2-
(type $iii (func (param i32 i32) (result i32)))
2+
(type $iiii (func (param i32 i32 i32) (result i32)))
33
(type $iiiiv (func (param i32 i32 i32 i32)))
44
(type $ii (func (param i32) (result i32)))
55
(type $iiiv (func (param i32 i32 i32)))
6-
(type $iiii (func (param i32 i32 i32) (result i32)))
76
(type $v (func))
87
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
98
(global $~lib/allocator/arena/startOffset (mut i32) (i32.const 0))
@@ -477,8 +476,8 @@
477476
)
478477
)
479478
)
480-
(func $~lib/arraybuffer/ArrayBuffer#constructor (; 5 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
481-
(local $2 i32)
479+
(func $~lib/arraybuffer/ArrayBuffer#constructor (; 5 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
480+
(local $3 i32)
482481
(if
483482
(i32.gt_u
484483
(get_local $1)
@@ -494,19 +493,28 @@
494493
(unreachable)
495494
)
496495
)
497-
(call $~lib/memory/set_memory
498-
(i32.add
499-
(tee_local $2
500-
(call $~lib/internal/arraybuffer/allocUnsafe
501-
(get_local $1)
502-
)
496+
(set_local $3
497+
(call $~lib/internal/arraybuffer/allocUnsafe
498+
(get_local $1)
499+
)
500+
)
501+
(if
502+
(i32.eqz
503+
(i32.and
504+
(get_local $2)
505+
(i32.const 1)
503506
)
504-
(i32.const 8)
505507
)
506-
(i32.const 0)
507-
(get_local $1)
508+
(call $~lib/memory/set_memory
509+
(i32.add
510+
(get_local $3)
511+
(i32.const 8)
512+
)
513+
(i32.const 0)
514+
(get_local $1)
515+
)
508516
)
509-
(get_local $2)
517+
(get_local $3)
510518
)
511519
(func $~lib/memory/copy_memory (; 6 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
512520
(local $3 i32)
@@ -2331,6 +2339,7 @@
23312339
(call $~lib/arraybuffer/ArrayBuffer#constructor
23322340
(i32.const 0)
23332341
(i32.const 8)
2342+
(i32.const 0)
23342343
)
23352344
)
23362345
(if

tests/compiler/std/arraybuffer.untouched.wat

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
(module
2-
(type $iii (func (param i32 i32) (result i32)))
2+
(type $iiii (func (param i32 i32 i32) (result i32)))
33
(type $iiiiv (func (param i32 i32 i32 i32)))
44
(type $ii (func (param i32) (result i32)))
55
(type $iiiv (func (param i32 i32 i32)))
6-
(type $iiii (func (param i32 i32 i32) (result i32)))
76
(type $v (func))
87
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
98
(global $~lib/internal/allocator/AL_BITS i32 (i32.const 3))
@@ -539,8 +538,8 @@
539538
)
540539
)
541540
)
542-
(func $~lib/arraybuffer/ArrayBuffer#constructor (; 5 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
543-
(local $2 i32)
541+
(func $~lib/arraybuffer/ArrayBuffer#constructor (; 5 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
542+
(local $3 i32)
544543
(if
545544
(i32.gt_u
546545
(get_local $1)
@@ -556,21 +555,29 @@
556555
(unreachable)
557556
)
558557
)
559-
(set_local $2
558+
(set_local $3
560559
(call $~lib/internal/arraybuffer/allocUnsafe
561560
(get_local $1)
562561
)
563562
)
564-
(call $~lib/memory/set_memory
565-
(i32.add
566-
(get_local $2)
567-
(i32.const 8)
563+
(if
564+
(i32.eqz
565+
(i32.and
566+
(get_local $2)
567+
(i32.const 1)
568+
)
569+
)
570+
(call $~lib/memory/set_memory
571+
(i32.add
572+
(get_local $3)
573+
(i32.const 8)
574+
)
575+
(i32.const 0)
576+
(get_local $1)
568577
)
569-
(i32.const 0)
570-
(get_local $1)
571578
)
572579
(return
573-
(get_local $2)
580+
(get_local $3)
574581
)
575582
)
576583
(func $~lib/memory/copy_memory (; 6 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
@@ -2863,6 +2870,7 @@
28632870
(call $~lib/arraybuffer/ArrayBuffer#constructor
28642871
(i32.const 0)
28652872
(i32.const 8)
2873+
(i32.const 0)
28662874
)
28672875
)
28682876
(if

0 commit comments

Comments
 (0)