mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 14:31:28 +00:00
Program elements and resolve infrastructure; Stdlib ideas; Restructuring
This commit is contained in:
17
std/array.d.ts
vendored
Normal file
17
std/array.d.ts
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
declare class Array<T> {
|
||||
length: i32;
|
||||
readonly capacity: i32;
|
||||
readonly data: usize;
|
||||
constructor(capacity: i32);
|
||||
}
|
||||
|
||||
declare class Int8Array extends Array<i8> {}
|
||||
declare class Int16Array extends Array<i16> {}
|
||||
declare class Int32Array extends Array<i32> {}
|
||||
declare class Uint8Array extends Array<u8> {}
|
||||
declare class Uint16Array extends Array<u16> {}
|
||||
declare class Uint32Array extends Array<u32> {}
|
||||
declare class Float32Array extends Array<f32> {}
|
||||
declare class Float64Array extends Array<f64> {}
|
17
std/array.ts
Normal file
17
std/array.ts
Normal file
@ -0,0 +1,17 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
@global()
|
||||
class Array<T> {
|
||||
|
||||
length: i32;
|
||||
readonly capacity: i32;
|
||||
readonly data: usize;
|
||||
|
||||
constructor(capacity: i32) {
|
||||
if (capacity < 0)
|
||||
throw new RangeError("capacity out of bounds");
|
||||
this.length = capacity;
|
||||
this.capacity = capacity;
|
||||
this.data = Memory.allocate(sizeof<T>() * capacity);
|
||||
}
|
||||
}
|
10
std/error.d.ts
vendored
Normal file
10
std/error.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
declare class Error {
|
||||
message: string;
|
||||
constructor(message: string);
|
||||
}
|
||||
|
||||
declare class RangeError extends Error {}
|
||||
declare class ReferenceError extends Error {}
|
||||
declare class TypeError extends Error {}
|
20
std/error.ts
Normal file
20
std/error.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
@global()
|
||||
class Error {
|
||||
|
||||
message: string;
|
||||
|
||||
constructor(message: string) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
@global()
|
||||
class RangeError extends Error {}
|
||||
|
||||
@global()
|
||||
class ReferenceError extends Error {}
|
||||
|
||||
@global()
|
||||
class TypeError extends Error {}
|
1
std/map.d.ts
vendored
Normal file
1
std/map.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
22
std/map.ts
Normal file
22
std/map.ts
Normal file
@ -0,0 +1,22 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
@global()
|
||||
class Map<K,V> {
|
||||
private keys: K[];
|
||||
private values: V[];
|
||||
|
||||
constructor() {
|
||||
this.keys = [];
|
||||
this.values = [];
|
||||
}
|
||||
|
||||
has(key: K): bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
set(key: K, value: V): void {
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
}
|
||||
}
|
4
std/math.d.ts
vendored
Normal file
4
std/math.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
declare class Math {
|
||||
}
|
5
std/math.ts
Normal file
5
std/math.ts
Normal file
@ -0,0 +1,5 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
@global()
|
||||
class Math {
|
||||
}
|
8
std/memory.d.ts
vendored
Normal file
8
std/memory.d.ts
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
declare class Memory {
|
||||
static allocate(size: usize): usize;
|
||||
static free(ptr: usize): void;
|
||||
static copy(src: usize, dst: usize, count: usize): void;
|
||||
static compare(src: usize, dst: usize, count: usize): i32;
|
||||
}
|
27
std/memory.ts
Normal file
27
std/memory.ts
Normal file
@ -0,0 +1,27 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
@global()
|
||||
class Memory {
|
||||
|
||||
static allocate(size: usize): usize {
|
||||
const ptr: usize = load<usize>(sizeof<usize>());
|
||||
store<usize>(sizeof<usize>(), ptr + size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static free(ptr: usize): void {
|
||||
}
|
||||
|
||||
static copy(src: usize, dst: usize, count: usize): void {
|
||||
for (let i: usize = 0; i < count; ++i)
|
||||
store<u8>(dst + i, load<u8>(src + i));
|
||||
}
|
||||
|
||||
static compare(src: usize, dst: usize, count: usize): i32 {
|
||||
for (let i: usize = 0; i < count; ++i) {
|
||||
const d: i32 = (load<u8>(src + i) as i32) - (load<u8>(dst + i) as i32);
|
||||
if (d) return d;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
4
std/set.d.ts
vendored
Normal file
4
std/set.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
declare class Set<T> {
|
||||
}
|
5
std/set.ts
Normal file
5
std/set.ts
Normal file
@ -0,0 +1,5 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
@global()
|
||||
class Set<T> {
|
||||
}
|
11
std/string.d.ts
vendored
Normal file
11
std/string.d.ts
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
declare class String {
|
||||
readonly length: i32;
|
||||
constructor(length: i32);
|
||||
static fromCharCode(c1: i32, c2?: i32);
|
||||
static equals(a: string, b: string): bool;
|
||||
static concat(a: string, b: string): string;
|
||||
charCodeAt(index: i32): u16;
|
||||
concat(other: string): string;
|
||||
}
|
58
std/string.ts
Normal file
58
std/string.ts
Normal file
@ -0,0 +1,58 @@
|
||||
/// <reference path="../assembly.d.ts" />
|
||||
|
||||
@global()
|
||||
@allocates()
|
||||
@operator("==", String.equals)
|
||||
@operator("!=", String.notEquals)
|
||||
@operator("+", String.concat)
|
||||
class String {
|
||||
|
||||
readonly length: i32;
|
||||
|
||||
constructor(length: i32) {
|
||||
if (length < 0)
|
||||
throw new RangeError("invalid length");
|
||||
const data: usize = Memory.allocate(4 + length);
|
||||
store<i32>(data, length);
|
||||
return classof<String>(data);
|
||||
}
|
||||
|
||||
static fromCharCode(c1: i32 /* sic */, c2: i32 = -1): String {
|
||||
throw new Error("not implemented");
|
||||
}
|
||||
|
||||
static equals(a: String, b: String): bool {
|
||||
const aLength: i32 = a.length;
|
||||
return aLength == b.length && !Memory.compare(pointerof(a) + 4, pointerof(b) + 4, aLength << 1);
|
||||
}
|
||||
|
||||
static notEquals(a: String, b: String): bool {
|
||||
const aLength: i32 = a.length;
|
||||
return aLength != b.length || Memory.compare(pointerof(a) + 4, pointerof(b) + 4, aLength << 1);
|
||||
}
|
||||
|
||||
static concat(a: String, b: String): String {
|
||||
const aLength: i32 = a.length;
|
||||
const bLength: i32 = b.length;
|
||||
const combinedLength: i32 = aLength + bLength;
|
||||
if (combinedLength < 0)
|
||||
throw new RangeError("invalid length");
|
||||
const aByteLength: i32 = aLength << 1;
|
||||
const bByteLength: i32 = bLength << 1;
|
||||
const data: usize = Memory.allocate(4 + combinedLength);
|
||||
store<i32>(data, combinedLength);
|
||||
Memory.copy(pointerof(a) + 4, data + 4, aByteLength);
|
||||
Memory.copy(pointerof(b) + 4, data + 4 + aByteLength, bByteLength);
|
||||
return classof<String>(data);
|
||||
}
|
||||
|
||||
charCodeAt(index: i32): u16 {
|
||||
if (index < 0 || index > this.length)
|
||||
throw new RangeError("index out of bounds");
|
||||
return load<u32>(pointerof(this) + 4 + index << 1);
|
||||
}
|
||||
|
||||
concat(other: String): String {
|
||||
return String.concat(this, other);
|
||||
}
|
||||
}
|
15
std/tsconfig.json
Normal file
15
std/tsconfig.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"noLib": true,
|
||||
"experimentalDecorators": true
|
||||
},
|
||||
"files": [
|
||||
"array.ts",
|
||||
"error.ts",
|
||||
"map.ts",
|
||||
"math.ts",
|
||||
"memory.ts",
|
||||
"set.ts",
|
||||
"string.ts"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user