Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jamesliu96
Last active September 27, 2020 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesliu96/b5f3da494eb6c356ec09fdf9bcdfc436 to your computer and use it in GitHub Desktop.
Save jamesliu96/b5f3da494eb6c356ec09fdf9bcdfc436 to your computer and use it in GitHub Desktop.
const Primitives = {
uint8: Uint8Array,
uint16: Uint16Array,
uint32: Uint32Array,
int8: Int8Array,
int16: Int16Array,
int32: Int32Array,
float32: Float32Array,
float64: Float64Array,
};
export default class Primitive {
static types = Primitives;
#primitive;
constructor(ctor, init = 0) {
this.#primitive = new ctor(1);
this.#primitive[0] = init;
}
get value() {
return this.#primitive[0];
}
set value(v) {
this.#primitive[0] = v;
}
}
const Primitives = {
uint8: Uint8Array,
uint16: Uint16Array,
uint32: Uint32Array,
int8: Int8Array,
int16: Int16Array,
int32: Int32Array,
float32: Float32Array,
float64: Float64Array,
};
type PrimitivesType = typeof Primitives[keyof typeof Primitives];
export default class Primitive {
public static types = Primitives;
#primitive: InstanceType<PrimitivesType>;
public constructor(ctor: PrimitivesType, init = 0) {
this.#primitive = new ctor(1);
this.#primitive[0] = init;
}
public get value() {
return this.#primitive[0];
}
public set value(v: number) {
this.#primitive[0] = v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment