Core library reprogramming #5
13
lib/core.ts
13
lib/core.ts
@ -45,15 +45,22 @@ globalThis.log = internals.constructor.log;
|
|||||||
var i = 0.0;
|
var i = 0.0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
run('values/object');
|
var Object = env.global.Object = internals.object;
|
||||||
|
var Function = env.global.Function = internals.function;
|
||||||
|
var Promise = env.global.Promise = internals.promise;
|
||||||
|
|
||||||
|
env.setProto('object', Object.prototype);
|
||||||
|
env.setProto('function', Function.prototype);
|
||||||
|
|
||||||
|
(Object.prototype as any).__proto__ = null;
|
||||||
|
|
||||||
run('values/symbol');
|
run('values/symbol');
|
||||||
run('values/function');
|
|
||||||
run('values/errors');
|
run('values/errors');
|
||||||
run('values/string');
|
run('values/string');
|
||||||
run('values/number');
|
run('values/number');
|
||||||
run('values/boolean');
|
run('values/boolean');
|
||||||
run('values/array');
|
run('values/array');
|
||||||
run('promise');
|
|
||||||
run('map');
|
run('map');
|
||||||
run('set');
|
run('set');
|
||||||
run('regex');
|
run('regex');
|
||||||
|
208
lib/promise.ts
208
lib/promise.ts
@ -1,208 +0,0 @@
|
|||||||
define("promise", () => {
|
|
||||||
var Promise = env.global.Promise = internals.promise
|
|
||||||
return;
|
|
||||||
|
|
||||||
// const syms = {
|
|
||||||
// callbacks: internals.symbol('Promise.callbacks'),
|
|
||||||
// state: internals.symbol('Promise.state'),
|
|
||||||
// value: internals.symbol('Promise.value'),
|
|
||||||
// handled: internals.symbol('Promise.handled'),
|
|
||||||
// } as {
|
|
||||||
// readonly callbacks: unique symbol,
|
|
||||||
// readonly state: unique symbol,
|
|
||||||
// readonly value: unique symbol,
|
|
||||||
// readonly handled: unique symbol,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type Callback<T> = [ PromiseFulfillFunc<T>, PromiseRejectFunc ];
|
|
||||||
// enum State {
|
|
||||||
// Pending,
|
|
||||||
// Fulfilled,
|
|
||||||
// Rejected,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// function isAwaitable(val: unknown): val is Thenable<any> {
|
|
||||||
// return (
|
|
||||||
// typeof val === 'object' &&
|
|
||||||
// val !== null &&
|
|
||||||
// 'then' in val &&
|
|
||||||
// typeof val.then === 'function'
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// function resolve(promise: Promise<any>, v: any, state: State) {
|
|
||||||
// if (promise[syms.state] === State.Pending) {
|
|
||||||
// if (typeof v === 'object' && v !== null && 'then' in v && typeof v.then === 'function') {
|
|
||||||
// v.then(
|
|
||||||
// (res: any) => resolve(promise, res, state),
|
|
||||||
// (res: any) => resolve(promise, res, State.Rejected)
|
|
||||||
// );
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// promise[syms.value] = v;
|
|
||||||
// promise[syms.state] = state;
|
|
||||||
|
|
||||||
// for (let i = 0; i < promise[syms.callbacks]!.length; i++) {
|
|
||||||
// promise[syms.handled] = true;
|
|
||||||
// promise[syms.callbacks]![i][state - 1](v);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// promise[syms.callbacks] = undefined;
|
|
||||||
|
|
||||||
// internals.pushMessage(true, internals.setEnv(() => {
|
|
||||||
// if (!promise[syms.handled] && state === State.Rejected) {
|
|
||||||
// log('Uncaught (in promise) ' + promise[syms.value]);
|
|
||||||
// }
|
|
||||||
// }, env), undefined, []);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// class _Promise<T> {
|
|
||||||
// public static isAwaitable(val: unknown): val is Thenable<any> {
|
|
||||||
// return isAwaitable(val);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public static resolve<T>(val: T): Promise<Awaited<T>> {
|
|
||||||
// return new Promise(res => res(val as any));
|
|
||||||
// }
|
|
||||||
// public static reject<T>(val: T): Promise<Awaited<T>> {
|
|
||||||
// return new Promise((_, rej) => rej(val as any));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public static race<T>(vals: T[]): Promise<Awaited<T>> {
|
|
||||||
// if (typeof vals.length !== 'number') throw new env.global.TypeError('vals must be an array. Note that Promise.race is not variadic.');
|
|
||||||
// return new Promise((res, rej) => {
|
|
||||||
// for (let i = 0; i < vals.length; i++) {
|
|
||||||
// const val = vals[i];
|
|
||||||
// if (this.isAwaitable(val)) val.then(res, rej);
|
|
||||||
// else res(val as any);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// public static any<T>(vals: T[]): Promise<Awaited<T>> {
|
|
||||||
// if (typeof vals.length !== 'number') throw new env.global.TypeError('vals must be an array. Note that Promise.any is not variadic.');
|
|
||||||
// return new Promise((res, rej) => {
|
|
||||||
// let n = 0;
|
|
||||||
|
|
||||||
// for (let i = 0; i < vals.length; i++) {
|
|
||||||
// const val = vals[i];
|
|
||||||
// if (this.isAwaitable(val)) val.then(res, (err) => {
|
|
||||||
// n++;
|
|
||||||
// if (n === vals.length) throw Error('No promise resolved.');
|
|
||||||
// });
|
|
||||||
// else res(val as any);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (vals.length === 0) throw Error('No promise resolved.');
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// public static all(vals: any[]): Promise<any[]> {
|
|
||||||
// if (typeof vals.length !== 'number') throw new env.global.TypeError('vals must be an array. Note that Promise.all is not variadic.');
|
|
||||||
// return new Promise((res, rej) => {
|
|
||||||
// const result: any[] = [];
|
|
||||||
// let n = 0;
|
|
||||||
|
|
||||||
// for (let i = 0; i < vals.length; i++) {
|
|
||||||
// const val = vals[i];
|
|
||||||
// if (this.isAwaitable(val)) val.then(
|
|
||||||
// val => {
|
|
||||||
// n++;
|
|
||||||
// result[i] = val;
|
|
||||||
// if (n === vals.length) res(result);
|
|
||||||
// },
|
|
||||||
// rej
|
|
||||||
// );
|
|
||||||
// else {
|
|
||||||
// n++;
|
|
||||||
// result[i] = val;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (vals.length === n) res(result);
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// public static allSettled(vals: any[]): Promise<any[]> {
|
|
||||||
// if (typeof vals.length !== 'number') throw new env.global.TypeError('vals must be an array. Note that Promise.allSettled is not variadic.');
|
|
||||||
// return new Promise((res, rej) => {
|
|
||||||
// const result: any[] = [];
|
|
||||||
// let n = 0;
|
|
||||||
|
|
||||||
// for (let i = 0; i < vals.length; i++) {
|
|
||||||
// const value = vals[i];
|
|
||||||
// if (this.isAwaitable(value)) value.then(
|
|
||||||
// value => {
|
|
||||||
// n++;
|
|
||||||
// result[i] = { status: 'fulfilled', value };
|
|
||||||
// if (n === vals.length) res(result);
|
|
||||||
// },
|
|
||||||
// reason => {
|
|
||||||
// n++;
|
|
||||||
// result[i] = { status: 'rejected', reason };
|
|
||||||
// if (n === vals.length) res(result);
|
|
||||||
// },
|
|
||||||
// );
|
|
||||||
// else {
|
|
||||||
// n++;
|
|
||||||
// result[i] = { status: 'fulfilled', value };
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (vals.length === n) res(result);
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// [syms.callbacks]?: Callback<T>[] = [];
|
|
||||||
// [syms.handled] = false;
|
|
||||||
// [syms.state] = State.Pending;
|
|
||||||
// [syms.value]?: T | unknown;
|
|
||||||
|
|
||||||
// public then(onFulfil?: PromiseFulfillFunc<T>, onReject?: PromiseRejectFunc) {
|
|
||||||
// return new Promise((resolve, reject) => {
|
|
||||||
// onFulfil ??= v => v;
|
|
||||||
// onReject ??= v => v;
|
|
||||||
|
|
||||||
// const callback = (func: (val: any) => any) => (v: any) => {
|
|
||||||
// try {
|
|
||||||
// resolve(func(v));
|
|
||||||
// }
|
|
||||||
// catch (e) { reject(e); }
|
|
||||||
// }
|
|
||||||
// switch (this[syms.state]) {
|
|
||||||
// case State.Pending:
|
|
||||||
// this[syms.callbacks]![this[syms.callbacks]!.length] = [callback(onFulfil), callback(onReject)];
|
|
||||||
// break;
|
|
||||||
// case State.Fulfilled:
|
|
||||||
// this[syms.handled] = true;
|
|
||||||
// callback(onFulfil)(this[syms.value]);
|
|
||||||
// break;
|
|
||||||
// case State.Rejected:
|
|
||||||
// this[syms.handled] = true;
|
|
||||||
// callback(onReject)(this[syms.value]);
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// public catch(func: PromiseRejectFunc) {
|
|
||||||
// return this.then(undefined, func);
|
|
||||||
// }
|
|
||||||
// public finally(func: () => void) {
|
|
||||||
// return this.then(
|
|
||||||
// v => {
|
|
||||||
// func();
|
|
||||||
// return v;
|
|
||||||
// },
|
|
||||||
// v => {
|
|
||||||
// func();
|
|
||||||
// throw v;
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public constructor(func: PromiseFunc<T>) {
|
|
||||||
// internals.pushMessage(true, func, undefined, [
|
|
||||||
// ((v) => resolve(this, v, State.Fulfilled)) as PromiseFulfillFunc<T>,
|
|
||||||
// ((err) => resolve(this, err, State.Rejected)) as PromiseRejectFunc
|
|
||||||
// ]);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// env.global.Promise = Promise as any;
|
|
||||||
});
|
|
@ -3,15 +3,12 @@
|
|||||||
"lib.d.ts",
|
"lib.d.ts",
|
||||||
"modules.ts",
|
"modules.ts",
|
||||||
"utils.ts",
|
"utils.ts",
|
||||||
"values/object.ts",
|
|
||||||
"values/symbol.ts",
|
"values/symbol.ts",
|
||||||
"values/function.ts",
|
|
||||||
"values/errors.ts",
|
"values/errors.ts",
|
||||||
"values/string.ts",
|
"values/string.ts",
|
||||||
"values/number.ts",
|
"values/number.ts",
|
||||||
"values/boolean.ts",
|
"values/boolean.ts",
|
||||||
"values/array.ts",
|
"values/array.ts",
|
||||||
"promise.ts",
|
|
||||||
"map.ts",
|
"map.ts",
|
||||||
"set.ts",
|
"set.ts",
|
||||||
"regex.ts",
|
"regex.ts",
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
define("values/function", () => {
|
|
||||||
var Function = env.global.Function = internals.function;
|
|
||||||
env.setProto('function', Function.prototype);
|
|
||||||
setConstr(Function.prototype, Function);
|
|
||||||
});
|
|
@ -1,5 +0,0 @@
|
|||||||
define("values/object", () => {
|
|
||||||
var Object = env.global.Object = internals.object;
|
|
||||||
(Object.prototype as any).__proto__ = null;
|
|
||||||
env.setProto('object', Object.prototype);
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user