Make stdlib classes uniform

This commit is contained in:
2026-10-24 16:04:01 +00:00
parent 95e5c1225e
commit f7c5b2c80f
+39 -39
View File
@@ -20,8 +20,7 @@ const __PENDING = 0;
const __FULFILLED = 1; const __FULFILLED = 1;
const __REJECTED = 2; const __REJECTED = 2;
class Promise { function Promise(executor) {
constructor(executor) {
const p = { __proto__: this.__proto__, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; const p = { __proto__: this.__proto__, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] };
if (executor !== undefined) { if (executor !== undefined) {
@@ -29,8 +28,43 @@ class Promise {
} }
return p; return p;
}
Promise.resolve = function (value) {
if (__is_promise(value)) return value;
const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] };
__resolve(p, value);
return p;
};
Promise.reject = function (reason) {
const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] };
__reject(p, reason);
return p;
};
Promise.all = function (promises) {
const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] };
const values = [];
let left = promises.length;
if (left === 0) __resolve(p, values);
for (let i = 0; i < promises.length; i++) {
Promise.resolve(promises[i]).then(
(value) => {
values[i] = value;
left--;
if (left === 0) __resolve(p, values);
},
(reason) => __reject(p, reason),
);
} }
return p;
};
Promise.prototype = {
then(onFulfilled, onRejected) { then(onFulfilled, onRejected) {
const derived = { __proto__: this.__proto__, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; const derived = { __proto__: this.__proto__, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] };
let value; let value;
@@ -68,11 +102,11 @@ class Promise {
} }
return derived; return derived;
} },
catch(onRejected) { catch(onRejected) {
return this.then(undefined, onRejected); return this.then(undefined, onRejected);
} },
finally(f) { finally(f) {
return this.then( return this.then(
@@ -85,42 +119,8 @@ class Promise {
return Promise.reject(reason); return Promise.reject(reason);
}, },
); );
}
static resolve(value) {
if (__is_promise(value)) return value;
const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] };
__resolve(p, value);
return p;
}
static reject(reason) {
const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] };
__reject(p, reason);
return p;
}
static all(promises) {
const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] };
const values = [];
let left = promises.length;
if (left === 0) __resolve(p, values);
for (let i = 0; i < promises.length; i++) {
Promise.resolve(promises[i]).then(
(value) => {
values[i] = value;
left--;
if (left === 0) __resolve(p, values);
}, },
(reason) => __reject(p, reason), };
);
}
return p;
}
}
function __is_promise(x) { function __is_promise(x) {
return typeof x === "object" && x !== null && x.reactions !== undefined; return typeof x === "object" && x !== null && x.reactions !== undefined;