Skip to content

Commit 59dafc8

Browse files
committed
Some thoughts on an initial stdlib to get things going
1 parent 325ecf5 commit 59dafc8

25 files changed

+197
-281
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Side effects:
2626
- Good benchmark when comparing both versions
2727
- Benefits standard library design ideas
2828

29+
How does it work?
30+
-----------------
31+
32+
AssemblyScript NEXT compiles a subset (or variant) of TypeScript to Binaryen IR. The resulting module can then be optimized, emitted in text or binary format, or even be converted to asm.js as a polyfill.
33+
2934
Getting started
3035
---------------
3136

@@ -38,7 +43,7 @@ $> npm install
3843
$> node bin\asc yourModule.ts
3944
```
4045

41-
Building an UMD bundle to `dist/assemblyscript.js` (does not bundle [binaryen.js](https://github.com/AssemblyScript/binaryen.js):
46+
Building an UMD bundle to `dist/assemblyscript.js` (does not bundle [binaryen.js](https://github.com/AssemblyScript/binaryen.js)):
4247

4348
```
4449
$> npm run build

assembly.d.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,10 @@ declare function assert(isTrue: bool): void;
8787

8888
// internal decorators
8989

90-
declare function global(name?: string): any;
91-
declare function struct(): any
90+
declare function global(): any;
9291
declare function inline(): any;
93-
declare function allocates(): any;
94-
declare function operator(token: string, fn: any): any;
9592

9693
// standard library
9794

98-
/// <reference path="./std/array.d.ts" />
99-
/// <reference path="./std/map.d.ts" />
100-
/// <reference path="./std/math.d.ts" />
101-
/// <reference path="./std/memory.d.ts" />
102-
/// <reference path="./std/set.d.ts" />
103-
/// <reference path="./std/string.d.ts" />
95+
/// <reference path="./std/carray.d.ts" />
96+
/// <reference path="./std/cstring.d.ts" />

src/compiler.ts

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,59 +1624,54 @@ export class Compiler extends DiagnosticEmitter {
16241624
}
16251625

16261626
compileIdentifierExpression(expression: IdentifierExpression, contextualType: Type): ExpressionRef {
1627+
switch (expression.kind) {
16271628

1628-
// null
1629-
if (expression.kind == NodeKind.NULL) {
1630-
if (contextualType.classType) // keep contextualType
1631-
return this.options.target == Target.WASM64 ? this.module.createI64(0, 0) : this.module.createI32(0);
1632-
if (this.options.target == Target.WASM64) {
1633-
this.currentType = Type.u64;
1634-
return this.module.createI64(0, 0);
1635-
} else {
1636-
this.currentType = Type.u32;
1629+
case NodeKind.NULL:
1630+
if (this.options.target == Target.WASM64) {
1631+
if (!contextualType.classType) {
1632+
assert(contextualType.kind == TypeKind.USIZE);
1633+
this.currentType = Type.usize64;
1634+
}
1635+
return this.module.createI64(0, 0);
1636+
}
1637+
if (!contextualType.classType) {
1638+
assert(contextualType.kind == TypeKind.USIZE);
1639+
this.currentType = Type.usize32;
1640+
}
16371641
return this.module.createI32(0);
1638-
}
16391642

1640-
// true
1641-
} else if (expression.kind == NodeKind.TRUE) {
1642-
this.currentType = Type.bool;
1643-
return this.module.createI32(1);
1644-
1645-
// false
1646-
} else if (expression.kind == NodeKind.FALSE) {
1647-
this.currentType = Type.bool;
1648-
return this.module.createI32(0);
1649-
1650-
// this
1651-
} else if (expression.kind == NodeKind.THIS) {
1652-
if (this.currentFunction.instanceMethodOf) {
1653-
this.currentType = this.currentFunction.instanceMethodOf.type;
1654-
return this.module.createGetLocal(0, this.options.target == Target.WASM64 ? NativeType.I64 : NativeType.I32);
1655-
}
1656-
this.error(DiagnosticCode._this_cannot_be_referenced_in_current_location, expression.range);
1657-
this.currentType = this.options.target == Target.WASM64 ? Type.u64 : Type.u32;
1658-
return this.module.createUnreachable();
1659-
}
1643+
case NodeKind.TRUE:
1644+
this.currentType = Type.bool;
1645+
return this.module.createI32(1);
16601646

1661-
if (expression.kind == NodeKind.IDENTIFIER) {
1647+
case NodeKind.FALSE:
1648+
this.currentType = Type.bool;
1649+
return this.module.createI32(0);
16621650

1663-
// NaN
1664-
if ((<IdentifierExpression>expression).name == "NaN")
1665-
if (this.currentType.kind == TypeKind.F32)
1666-
return this.module.createF32(NaN);
1667-
else {
1651+
case NodeKind.THIS:
1652+
if (this.currentFunction.instanceMethodOf) {
1653+
this.currentType = this.currentFunction.instanceMethodOf.type;
1654+
return this.module.createGetLocal(0, this.options.target == Target.WASM64 ? NativeType.I64 : NativeType.I32);
1655+
}
1656+
this.error(DiagnosticCode._this_cannot_be_referenced_in_current_location, expression.range);
1657+
this.currentType = this.options.target == Target.WASM64 ? Type.u64 : Type.u32;
1658+
return this.module.createUnreachable();
1659+
1660+
case NodeKind.IDENTIFIER:
1661+
// TODO: some sort of resolveIdentifier maybe
1662+
if ((<IdentifierExpression>expression).name == "NaN") {
1663+
if (this.currentType == Type.f32)
1664+
return this.module.createF32(NaN);
16681665
this.currentType = Type.f64;
16691666
return this.module.createF64(NaN);
16701667
}
1671-
1672-
// Infinity
1673-
if ((<IdentifierExpression>expression).name == "Infinity")
1674-
if (this.currentType.kind == TypeKind.F32)
1675-
return this.module.createF32(Infinity);
1676-
else {
1668+
if ((<IdentifierExpression>expression).name == "Infinity") {
1669+
if (this.currentType == Type.f32)
1670+
return this.module.createF32(Infinity);
16771671
this.currentType = Type.f64;
16781672
return this.module.createF64(Infinity);
16791673
}
1674+
break;
16801675
}
16811676

16821677
const element: Element | null = this.program.resolveElement(expression, this.currentFunction); // reports

src/glue/js.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ declare type bool = boolean;
1414
// Raw memory access (here: Binaryen memory)
1515
declare function store<T = u8>(ptr: usize, val: T): void;
1616
declare function load<T = u8>(ptr: usize): T;
17+
declare function assert(isTrue: bool): void;
1718

1819
// Other things that might or might not be useful
1920
declare function select<T>(ifTrue: T, ifFalse: T, condition: bool): T;

std/array.d.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

std/carray.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference path="../assembly.d.ts" />
2+
3+
declare class CArray<T> {
4+
[key: number]: T;
5+
constructor(capacity: usize);
6+
}

std/cstring.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference path="../assembly.d.ts" />
2+
3+
declare class CString extends CArray<u8> {
4+
constructor(text: string);
5+
}

std/error.d.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

std/impl/array.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

std/impl/carray.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference path="../../assembly.d.ts" />
2+
3+
/** A C-compatible Array class. */
4+
@global()
5+
class CArray<T> {
6+
7+
/** Constructs a new C-Array of the specified capacity. */
8+
constructor(capacity: usize) {
9+
return unsafe_cast<usize,this>(Memory.allocate(capacity * sizeof<T>()));
10+
}
11+
12+
/** Gets the element at the specified index using bracket notation. */
13+
@inline()
14+
"[]"(index: usize): T {
15+
return load<T>(unsafe_cast<this,usize>(this) + index * sizeof<T>());
16+
}
17+
18+
/** Sets the element at the specified index using bracket notation. */
19+
@inline()
20+
"[]="(index: usize, value: T): T {
21+
store<T>(unsafe_cast<this,usize>(this) + index * sizeof<T>(), value);
22+
return value;
23+
}
24+
25+
/** Disposes this instance and the memory associated with it. */
26+
dispose(): void {
27+
Memory.dispose(unsafe_cast<this,usize>(this));
28+
}
29+
}

0 commit comments

Comments
 (0)