Skip to content

Commit 9ef8b16

Browse files
committed
Also lint stdlib
1 parent ae05006 commit 9ef8b16

30 files changed

+504
-459
lines changed

Diff for: .travis.yml

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@ jobs:
55
- stage: lint
66
node_js: node
77
script: npm run lint
8-
env: DESC="Checks the sources with TSLint"
8+
env: Checks the sources with TSLint
99

1010
- stage: test
1111
node_js: lts/*
1212
script: npm run clean && node bin/asc -v && npm test
13-
env: DESC="Tests the sources on latest node.js LTS"
14-
- stage: test
15-
node_js: node
16-
script: npm run clean && node bin/asc -v && npm test
17-
env: DESC="Tests the sources on latest stable node.js"
13+
env: Tests the sources on latest node.js LTS
14+
- node_js: node
15+
env: Tests the sources on latest stable node.js
1816

1917
- stage: build
2018
node_js: lts/*
2119
script: npm run build && node bin/asc -v && npm test
22-
env: DESC="Builds and tests the bundle on latest node.js LTS"
23-
- stage: build
24-
node_js: node
25-
script: npm run build && node bin/asc -v && npm test
26-
env: DESC="Builds and tests the bundle on latest stable node.js"
20+
env: Builds and tests the bundle on latest node.js LTS
21+
- node_js: node
22+
env: Builds and tests the bundle on latest stable node.js

Diff for: dist/asc.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/assemblyscript.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@
4040
"scripts": {
4141
"build": "webpack",
4242
"clean": "node scripts/clean",
43-
"lint": "tslint -c tslint.json --project src --formatters-dir lib/tslint --format as",
44-
"test:config": "npm run test:config:portable --scripts-prepend-node-path && npm run test:config:src --scripts-prepend-node-path",
45-
"test:config:portable": "tsc --noEmit -p std/portable --diagnostics --listFiles",
46-
"test:config:src": "tsc --noEmit -p src --diagnostics --listFiles",
43+
"lint": "npm run lint:compiler && npm run lint:library",
44+
"lint:compiler": "tslint -c tslint.json --project src --formatters-dir lib/tslint --format as",
45+
"lint:library": "tslint -c tslint.json --project std/assembly --formatters-dir lib/tslint --format as",
46+
"test:config": "tsc --noEmit -p src --diagnostics --listFiles",
4747
"test:parser": "node tests/parser",
4848
"test:compiler": "node tests/compiler",
49-
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path"
49+
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path",
50+
"all": "npm run lint && npm run clean && npm test && npm run build && npm test"
5051
},
5152
"files": [
5253
"bin/",

Diff for: std/assembly.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ declare function sqrt<T = f32 | f64>(value: T): T;
155155
/** Rounds to the nearest integer towards zero of a 32-bit or 64-bit float. */
156156
declare function trunc<T = f32 | f64>(value: T): T;
157157
/** Loads a value of the specified type from memory. Equivalent to dereferncing a pointer in other languages. */
158-
declare function load<T>(ptr: usize, constantOffset?: usize): any;
158+
declare function load<T>(ptr: usize, constantOffset?: usize): T;
159159
/** Stores a value of the specified type to memory. Equivalent to dereferencing a pointer in other languages when assigning a value. */
160160
declare function store<T>(ptr: usize, value: any, constantOffset?: usize): void;
161161
/** Returns the current memory size in units of pages. One page is 64kb. */
@@ -292,10 +292,10 @@ declare class Set<T> {
292292
// Internal decorators
293293

294294
/** Annotates an element as a program global. */
295-
declare function global(target: Function): any;
295+
declare function global(target: Function, propertyKey: string, descriptor: any): void;
296296

297297
/** Annotates a method as an operator overload. */
298-
declare function operator(token: string): any;
298+
declare function operator(token: string): (target: any, propertyKey: string, descriptor: any) => void;
299299

300300
/** Annotates a class as being unmanaged with limited capabilities. */
301301
declare function unmanaged(target: Function): any;

Diff for: std/assembly/allocator/tlsf.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const SL_SIZE: usize = 1 << <usize>SL_BITS;
1717

1818
const SB_BITS: usize = <usize>(SL_BITS + AL_BITS);
1919
const SB_SIZE: usize = 1 << <usize>SB_BITS;
20-
const SB_MASK: usize = SB_SIZE - 1;
2120

2221
const FL_BITS: u32 = (sizeof<usize>() == sizeof<u32>()
2322
? 30 // ^= up to 1GB per block
@@ -267,10 +266,8 @@ class Root {
267266
// link previous and next free block
268267
var prev = block.prev;
269268
var next = block.next;
270-
if (prev)
271-
prev.next = next;
272-
if (next)
273-
next.prev = prev;
269+
if (prev) prev.next = next;
270+
if (next) next.prev = prev;
274271

275272
// update head if we are removing it
276273
if (block == this.getHead(fl, sl)) {
@@ -282,8 +279,7 @@ class Root {
282279
this.setSLMap(fl, slMap &= ~(1 << sl));
283280

284281
// clear first level map if second level is empty now
285-
if (!slMap)
286-
this.flMap &= ~(1 << fl);
282+
if (!slMap) this.flMap &= ~(1 << fl);
287283
}
288284
}
289285
}
@@ -320,10 +316,10 @@ class Root {
320316
slMap = assert(this.getSLMap(fl)); // can't be zero if fl points here
321317
head = this.getHead(fl, ffs<u32>(slMap));
322318
}
323-
} else
319+
} else {
324320
head = this.getHead(fl, ffs<u32>(slMap));
325-
326-
return head;
321+
}
322+
return head;
327323
}
328324

329325
/** Links a free left with its right block in memory. */
@@ -386,13 +382,15 @@ class Root {
386382
tailInfo = changetype<Block>(tailRef).info;
387383
}
388384

389-
} else
385+
} else {
390386
assert(start >= changetype<usize>(this) + Root.SIZE); // starts after root
387+
}
391388

392389
// check if size is large enough for a free block and the tail block
393390
var size = end - start;
394-
if (size < Block.INFO + Block.MIN_SIZE + Block.INFO)
391+
if (size < Block.INFO + Block.MIN_SIZE + Block.INFO) {
395392
return false;
393+
}
396394

397395
// left size is total minus its own and the zero-length tail's header
398396
var leftSize = size - 2 * Block.INFO;
@@ -443,8 +441,9 @@ export function allocate_memory(size: usize): usize {
443441
root.flMap = 0;
444442
for (var fl: usize = 0; fl < FL_BITS; ++fl) {
445443
root.setSLMap(fl, 0);
446-
for (var sl: u32 = 0; sl < SL_SIZE; ++sl)
444+
for (var sl: u32 = 0; sl < SL_SIZE; ++sl) {
447445
root.setHead(fl, sl, null);
446+
}
448447
}
449448
root.addMemory(rootOffset + Root.SIZE, current_memory() << 16);
450449
}
@@ -461,9 +460,11 @@ export function allocate_memory(size: usize): usize {
461460
var pagesBefore = current_memory();
462461
var pagesNeeded = ((size + 0xffff) & ~0xffff) >>> 16;
463462
var pagesWanted = max(pagesBefore, pagesNeeded); // double memory
464-
if (grow_memory(pagesWanted) < 0)
465-
if (grow_memory(pagesNeeded) < 0)
463+
if (grow_memory(pagesWanted) < 0) {
464+
if (grow_memory(pagesNeeded) < 0) {
466465
unreachable(); // out of memory
466+
}
467+
}
467468
var pagesAfter = current_memory();
468469
root.addMemory(<usize>pagesBefore << 16, <usize>pagesAfter << 16);
469470
block = assert(root.search(size)); // must be found now

Diff for: std/assembly/array.ts

+84-34
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ export class Array<T> {
1616
}
1717

1818
constructor(capacity: i32 = 0) {
19-
if (capacity < 0)
19+
if (capacity < 0) {
2020
throw new RangeError("Invalid array length");
21-
this.__memory = capacity ? allocate_memory(<usize>capacity * sizeof<T>()) : 0;
21+
}
22+
this.__memory = capacity
23+
? allocate_memory(<usize>capacity * sizeof<T>())
24+
: 0;
2225
this.__capacity = this.__length = capacity;
2326
}
2427

@@ -27,72 +30,92 @@ export class Array<T> {
2730
}
2831

2932
set length(length: i32) {
30-
if (length < 0)
33+
if (length < 0) {
3134
throw new RangeError("Invalid array length");
32-
if (length > this.__capacity)
35+
}
36+
if (length > this.__capacity) {
3337
this.__grow(max(length, this.__capacity << 1));
38+
}
3439
this.__length = length;
3540
}
3641

3742
@operator("[]")
3843
private __get(index: i32): T {
39-
if (<u32>index >= this.__capacity)
44+
if (<u32>index >= this.__capacity) {
4045
throw new Error("Index out of bounds"); // return changetype<T>(0) ?
46+
}
4147
return load<T>(this.__memory + <usize>index * sizeof<T>());
4248
}
4349

4450
@operator("[]=")
4551
private __set(index: i32, value: T): void {
46-
if (index < 0)
52+
if (index < 0) {
4753
throw new Error("Index out of bounds");
48-
if (index >= this.__capacity)
54+
}
55+
if (index >= this.__capacity) {
4956
this.__grow(max(index + 1, this.__capacity << 1));
57+
}
5058
store<T>(this.__memory + <usize>index * sizeof<T>(), value);
5159
}
5260

5361
indexOf(searchElement: T, fromIndex: i32 = 0): i32 {
54-
if (fromIndex < 0)
62+
if (fromIndex < 0) {
5563
fromIndex = this.__length + fromIndex;
64+
}
5665
while (<u32>fromIndex < this.__length) {
57-
if (load<T>(this.__memory + fromIndex * sizeof<T>()) == searchElement)
66+
if (load<T>(this.__memory + fromIndex * sizeof<T>()) == searchElement) {
5867
return fromIndex;
68+
}
5969
++fromIndex;
6070
}
6171
return -1;
6272
}
6373

6474
lastIndexOf(searchElement: T, fromIndex: i32 = 0): i32 {
65-
if (fromIndex < 0)
75+
if (fromIndex < 0) {
6676
fromIndex = this.__length + fromIndex;
67-
else if (fromIndex >= this.__length)
77+
} else if (fromIndex >= this.__length) {
6878
fromIndex = this.__length - 1;
79+
}
6980
while (fromIndex >= 0) {
70-
if (load<T>(this.__memory + fromIndex * sizeof<T>()) == searchElement)
81+
if (load<T>(this.__memory + fromIndex * sizeof<T>()) == searchElement) {
7182
return fromIndex;
83+
}
7284
--fromIndex;
7385
}
7486
return -1;
7587
}
7688

7789
push(element: T): i32 {
78-
if (this.__length == this.__capacity)
90+
if (this.__length == this.__capacity) {
7991
this.__grow(this.__capacity ? this.__capacity << 1 : 1);
92+
}
8093
store<T>(this.__memory + this.__length * sizeof<T>(), element);
8194
return ++this.__length;
8295
}
8396

8497
pop(): T {
85-
if (this.__length < 1)
98+
if (this.__length < 1) {
8699
throw new RangeError("Array is empty"); // return changetype<T>(0) ?
100+
}
87101
return load<T>(this.__memory + --this.__length * sizeof<T>());
88102
}
89103

90104
shift(): T {
91-
if (this.__length < 1)
105+
if (this.__length < 1) {
92106
throw new RangeError("Array is empty"); // return changetype<T>(0) ?
107+
}
93108
var element = load<T>(this.__memory);
94-
move_memory(this.__memory, this.__memory + sizeof<T>(), (this.__capacity - 1) * sizeof<T>());
95-
set_memory(this.__memory + (this.__capacity - 1) * sizeof<T>(), 0, sizeof<T>());
109+
move_memory(
110+
this.__memory,
111+
this.__memory + sizeof<T>(),
112+
(this.__capacity - 1) * sizeof<T>()
113+
);
114+
set_memory(
115+
this.__memory + (this.__capacity - 1) * sizeof<T>(),
116+
0,
117+
sizeof<T>()
118+
);
96119
--this.__length;
97120
return element;
98121
}
@@ -105,51 +128,76 @@ export class Array<T> {
105128
assert(newCapacity > this.__capacity);
106129
var newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
107130
if (this.__memory) {
108-
move_memory(newMemory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
131+
move_memory(
132+
newMemory + sizeof<T>(),
133+
this.__memory,
134+
oldCapacity * sizeof<T>()
135+
);
109136
free_memory(this.__memory);
110137
}
111138
this.__memory = newMemory;
112139
this.__capacity = newCapacity;
113-
} else
114-
move_memory(this.__memory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
140+
} else {
141+
move_memory(
142+
this.__memory + sizeof<T>(),
143+
this.__memory,
144+
oldCapacity * sizeof<T>()
145+
);
146+
}
115147
store<T>(this.__memory, element);
116148
return ++this.__length;
117149
}
118150

119151
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
120152
if (begin < 0) {
121153
begin = this.__length + begin;
122-
if (begin < 0)
154+
if (begin < 0) {
123155
begin = 0;
124-
} else if (begin > this.__length)
156+
}
157+
} else if (begin > this.__length) {
125158
begin = this.__length;
126-
if (end < 0)
159+
}
160+
if (end < 0) {
127161
end = this.__length + end;
128-
else if (end > this.__length)
162+
} else if (end > this.__length) {
129163
end = this.__length;
130-
if (end < begin)
164+
}
165+
if (end < begin) {
131166
end = begin;
167+
}
132168
var capacity = end - begin;
133169
assert(capacity >= 0);
134170
var sliced = new Array<T>(capacity);
135-
if (capacity)
136-
move_memory(sliced.__memory, this.__memory + <usize>begin * sizeof<T>(), <usize>capacity * sizeof<T>());
171+
if (capacity) {
172+
move_memory(
173+
sliced.__memory,
174+
this.__memory + <usize>begin * sizeof<T>(),
175+
<usize>capacity * sizeof<T>()
176+
);
177+
}
137178
return sliced;
138179
}
139180

140181
splice(start: i32, deleteCount: i32 = i32.MAX_VALUE): void {
141-
if (deleteCount < 1)
182+
if (deleteCount < 1) {
142183
return;
184+
}
143185
if (start < 0) {
144186
start = this.__length + start;
145-
if (start < 0)
187+
if (start < 0) {
146188
start = 0;
147-
else if (start >= this.__length)
189+
} else if (start >= this.__length) {
148190
return;
149-
} else if (start >= this.__length)
191+
}
192+
} else if (start >= this.__length) {
150193
return;
194+
}
151195
deleteCount = min(deleteCount, this.__length - start);
152-
move_memory(this.__memory + <usize>start * sizeof<T>(), this.__memory + <usize>(start + deleteCount) * sizeof<T>(), deleteCount * sizeof<T>());
196+
move_memory(
197+
this.__memory + <usize>start * sizeof<T>(),
198+
this.__memory + <usize>(start + deleteCount) * sizeof<T>(),
199+
deleteCount * sizeof<T>()
200+
);
153201
this.__length -= deleteCount;
154202
}
155203

@@ -170,15 +218,17 @@ export class CArray<T> {
170218

171219
@operator("[]")
172220
private __get(index: i32): T {
173-
if (index < 0)
221+
if (index < 0) {
174222
throw new RangeError("Index out of range");
223+
}
175224
return load<T>(changetype<usize>(this) + <usize>index * sizeof<T>());
176225
}
177226

178227
@operator("[]=")
179228
private __set(index: i32, value: T): void {
180-
if (index < 0)
229+
if (index < 0) {
181230
throw new RangeError("Index out of range");
231+
}
182232
store<T>(changetype<usize>(this) + <usize>index * sizeof<T>(), value);
183233
}
184234
}

0 commit comments

Comments
 (0)