j2s/lib/src/stdlib/_entry.ts

111 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-01-24 03:57:15 +00:00
import { now, object, print, setGlobalPrototypes, setIntrinsic, target } from "./primordials.ts";
2025-01-06 11:24:44 +00:00
import { Error, RangeError, SyntaxError, TypeError } from "./values/errors.ts";
import { Boolean } from "./values/boolean.ts";
import { Function } from "./values/function.ts";
import { Number } from "./values/number.ts";
import { Object } from "./values/object.ts";
import { String } from "./values/string.ts";
import { Symbol } from "./values/symbol.ts";
import { Array } from "./values/array.ts";
import { Map, WeakMap } from "./classes/map.ts";
import { RegExp } from "./values/regex.ts";
import { Date } from "./classes/date.ts";
import { Math as _Math } from "./namespaces/math.ts";
import { Set, WeakSet } from "./classes/set.ts";
import { JSON } from "./namespaces/json.ts";
import { console } from "./namespaces/console.ts";
2024-12-25 00:48:04 +00:00
import { encodeURI, encodeURIComponent } from "./url.ts";
2025-01-06 11:24:44 +00:00
import { Promise } from "./classes/promise.ts";
import { ArrayBuffer } from "./arrays/ArrayBuffer.ts";
import { Uint8Array } from "./arrays/Uint8Array.ts";
import { Int32Array } from "./arrays/Int32Array.ts";
import { TypedArray } from "./arrays/TypedArray.ts";
2024-12-11 09:53:02 +00:00
2024-12-13 00:29:41 +00:00
function fixup<T extends Function>(clazz: T) {
object.setPrototype(clazz, Function.prototype);
2024-12-27 17:17:07 +00:00
object.setPrototype(clazz.prototype as any, Object.prototype);
2024-12-13 00:29:41 +00:00
return clazz;
}
2024-12-28 11:16:54 +00:00
object.setPrototype(target, Object.prototype);
2024-12-13 00:29:41 +00:00
object.defineField(target, "undefined", { e: false, c: false, w: false, v: void 0 });
2024-12-11 09:53:02 +00:00
2024-12-13 00:29:41 +00:00
target.Symbol = fixup(Symbol);
target.Number = fixup(Number);
target.String = fixup(String);
target.Boolean = fixup(Boolean);
2024-12-11 09:53:02 +00:00
target.Object = Object;
2024-12-13 00:29:41 +00:00
target.Function = fixup(Function);
target.Array = fixup(Array);
2024-12-11 09:53:02 +00:00
2024-12-13 00:29:41 +00:00
target.Error = fixup(Error);
2024-12-11 09:53:02 +00:00
target.RangeError = RangeError;
target.SyntaxError = SyntaxError;
target.TypeError = TypeError;
2025-01-06 11:24:44 +00:00
fixup(TypedArray);
target.ArrayBuffer = fixup(ArrayBuffer);
target.Uint8Array = Uint8Array;
target.Int32Array = Int32Array;
2024-12-13 00:29:41 +00:00
target.Map = fixup(Map);
target.WeakMap = fixup(WeakMap);
target.Set = fixup(Set);
target.WeakSet = fixup(WeakSet);
target.RegExp = fixup(RegExp);
target.Date = fixup(Date);
2024-12-27 17:17:07 +00:00
target.Promise = fixup(Promise);
2024-12-13 00:29:41 +00:00
target.Math = object.setPrototype(_Math, Object.prototype);
2024-12-25 00:48:04 +00:00
target.JSON = object.setPrototype(JSON, Object.prototype);
2024-12-28 11:16:54 +00:00
target.console = object.setPrototype(console, Object.prototype);
2024-12-11 09:53:02 +00:00
target.parseInt = Number.parseInt;
target.parseFloat = Number.parseFloat;
target.NaN = Number.NaN;
target.Infinity = Number.POSITIVE_INFINITY;
2024-12-25 00:48:04 +00:00
target.encodeURI = encodeURI;
target.encodeURIComponent = encodeURIComponent;
2025-01-24 03:57:15 +00:00
target.print = print;
target.measure = (func: () => void) => {
const start = now();
try {
return func();
}
finally {
print(`Took ${now() - start}ms`);
}
};
2024-12-11 09:53:02 +00:00
setGlobalPrototypes({
string: String.prototype,
number: Number.prototype,
boolean: Boolean.prototype,
symbol: Symbol.prototype,
object: Object.prototype,
array: Array.prototype,
function: Function.prototype,
error: Error.prototype,
syntax: SyntaxError.prototype,
range: RangeError.prototype,
type: TypeError.prototype,
2025-01-06 11:24:44 +00:00
uint8: Uint8Array.prototype,
int32: Int32Array.prototype,
2024-12-11 09:53:02 +00:00
});
2025-01-22 01:57:32 +00:00
setIntrinsic("regex", RegExp);
setIntrinsic("keys", (obj: object, own: boolean, onlyEnumerable: boolean) => {
const members = object.getMembers(obj, own, onlyEnumerable);
let i = 0;
return () => {
if (i >= members.length) return { done: true };
else return { value: members[i++] };
};
});
setIntrinsic("defGetter", (obj: object, key: any, func: Function) => {
object.defineProperty(obj, key, { g: func, e: true, c: true });
});
setIntrinsic("defSetter", (obj: object, key: any, func: Function) => {
object.defineProperty(obj, key, { s: func, e: true, c: true });
});