0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor: update runtime code for primordial check x in y (#13642)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Bartek Iwańczuk 2022-04-16 14:09:07 +02:00 committed by GitHub
parent 8b31fc23cd
commit 0bb96cde72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 37 deletions

View file

@ -107,6 +107,7 @@
ReflectGet, ReflectGet,
ReflectGetOwnPropertyDescriptor, ReflectGetOwnPropertyDescriptor,
ReflectGetPrototypeOf, ReflectGetPrototypeOf,
ReflectHas,
WeakMapPrototype, WeakMapPrototype,
WeakSetPrototype, WeakSetPrototype,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
@ -327,7 +328,10 @@
function inspectFunction(value, level, inspectOptions) { function inspectFunction(value, level, inspectOptions) {
const cyan = maybeColor(colors.cyan, inspectOptions); const cyan = maybeColor(colors.cyan, inspectOptions);
if (customInspect in value && typeof value[customInspect] === "function") { if (
ReflectHas(value, customInspect) &&
typeof value[customInspect] === "function"
) {
return String(value[customInspect](inspect)); return String(value[customInspect](inspect));
} }
// Might be Function/AsyncFunction/GeneratorFunction/AsyncGeneratorFunction // Might be Function/AsyncFunction/GeneratorFunction/AsyncGeneratorFunction
@ -1203,7 +1207,10 @@
inspectOptions, inspectOptions,
proxyDetails, proxyDetails,
) { ) {
if (customInspect in value && typeof value[customInspect] === "function") { if (
ReflectHas(value, customInspect) &&
typeof value[customInspect] === "function"
) {
return String(value[customInspect](inspect)); return String(value[customInspect](inspect));
} }
// This non-unique symbol is used to support op_crates, ie. // This non-unique symbol is used to support op_crates, ie.
@ -1212,7 +1219,7 @@
// Internal only, shouldn't be used by users. // Internal only, shouldn't be used by users.
const privateCustomInspect = SymbolFor("Deno.privateCustomInspect"); const privateCustomInspect = SymbolFor("Deno.privateCustomInspect");
if ( if (
privateCustomInspect in value && ReflectHas(value, privateCustomInspect) &&
typeof value[privateCustomInspect] === "function" typeof value[privateCustomInspect] === "function"
) { ) {
// TODO(nayeemrmn): `inspect` is passed as an argument because custom // TODO(nayeemrmn): `inspect` is passed as an argument because custom
@ -2048,8 +2055,8 @@
const valueObj = value || {}; const valueObj = value || {};
const keys = properties || ObjectKeys(valueObj); const keys = properties || ObjectKeys(valueObj);
for (const k of keys) { for (const k of keys) {
if (!primitive && k in valueObj) { if (!primitive && ReflectHas(valueObj, k)) {
if (!(k in objectValues)) { if (!(ReflectHas(objectValues, k))) {
objectValues[k] = ArrayPrototypeFill(new Array(numRows), ""); objectValues[k] = ArrayPrototypeFill(new Array(numRows), "");
} }
objectValues[k][idx] = stringifyValue(valueObj[k]); objectValues[k][idx] = stringifyValue(valueObj[k]);

View file

@ -32,6 +32,7 @@
ObjectGetOwnPropertyDescriptor, ObjectGetOwnPropertyDescriptor,
ObjectPrototypeIsPrototypeOf, ObjectPrototypeIsPrototypeOf,
ReflectDefineProperty, ReflectDefineProperty,
ReflectHas,
SafeArrayIterator, SafeArrayIterator,
StringPrototypeStartsWith, StringPrototypeStartsWith,
Symbol, Symbol,
@ -104,7 +105,7 @@
function hasRelatedTarget( function hasRelatedTarget(
event, event,
) { ) {
return "relatedTarget" in event; return ReflectHas(event, "relatedTarget");
} }
const isTrusted = ObjectGetOwnPropertyDescriptor({ const isTrusted = ObjectGetOwnPropertyDescriptor({
@ -450,7 +451,7 @@
function isNode( function isNode(
eventTarget, eventTarget,
) { ) {
return Boolean(eventTarget && "nodeType" in eventTarget); return Boolean(eventTarget && ReflectHas(eventTarget, "nodeType"));
} }
// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor // https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor
@ -485,7 +486,7 @@
function isSlotable( function isSlotable(
nodeImpl, nodeImpl,
) { ) {
return Boolean(isNode(nodeImpl) && "assignedSlot" in nodeImpl); return Boolean(isNode(nodeImpl) && ReflectHas(nodeImpl, "assignedSlot"));
} }
// DOM Logic functions // DOM Logic functions
@ -908,7 +909,7 @@
options = normalizeAddEventHandlerOptions(options); options = normalizeAddEventHandlerOptions(options);
const { listeners } = (this ?? globalThis)[eventTargetData]; const { listeners } = (this ?? globalThis)[eventTargetData];
if (!(type in listeners)) { if (!(ReflectHas(listeners, type))) {
listeners[type] = []; listeners[type] = [];
} }
@ -952,7 +953,7 @@
}); });
const { listeners } = (this ?? globalThis)[eventTargetData]; const { listeners } = (this ?? globalThis)[eventTargetData];
if (callback !== null && type in listeners) { if (callback !== null && ReflectHas(listeners, type)) {
listeners[type] = ArrayPrototypeFilter( listeners[type] = ArrayPrototypeFilter(
listeners[type], listeners[type],
(listener) => listener.callback !== callback, (listener) => listener.callback !== callback,
@ -989,7 +990,7 @@
const self = this ?? window; const self = this ?? window;
const { listeners } = self[eventTargetData]; const { listeners } = self[eventTargetData];
if (!(event.type in listeners)) { if (!ReflectHas(listeners, event.type)) {
setTarget(event, this); setTarget(event, this);
return true; return true;
} }

View file

@ -41,6 +41,7 @@
PromiseResolve, PromiseResolve,
queueMicrotask, queueMicrotask,
RangeError, RangeError,
ReflectHas,
SharedArrayBuffer, SharedArrayBuffer,
Symbol, Symbol,
SymbolAsyncIterator, SymbolAsyncIterator,
@ -190,7 +191,7 @@
* @returns {boolean} * @returns {boolean}
*/ */
function isDetachedBuffer(O) { function isDetachedBuffer(O) {
return isFakeDetached in O; return ReflectHas(O, isFakeDetached);
} }
/** /**
@ -392,7 +393,10 @@
* @returns {T} * @returns {T}
*/ */
function dequeueValue(container) { function dequeueValue(container) {
assert(_queue in container && _queueTotalSize in container); assert(
ReflectHas(container, _queue) &&
ReflectHas(container, _queueTotalSize),
);
assert(container[_queue].length); assert(container[_queue].length);
const valueWithSize = ArrayPrototypeShift(container[_queue]); const valueWithSize = ArrayPrototypeShift(container[_queue]);
container[_queueTotalSize] -= valueWithSize.size; container[_queueTotalSize] -= valueWithSize.size;
@ -410,7 +414,10 @@
* @returns {void} * @returns {void}
*/ */
function enqueueValueWithSize(container, value, size) { function enqueueValueWithSize(container, value, size) {
assert(_queue in container && _queueTotalSize in container); assert(
ReflectHas(container, _queue) &&
ReflectHas(container, _queueTotalSize),
);
if (isNonNegativeNumber(size) === false) { if (isNonNegativeNumber(size) === false) {
throw RangeError("chunk size isn't a positive number"); throw RangeError("chunk size isn't a positive number");
} }
@ -592,7 +599,7 @@
*/ */
function isReadableStream(value) { function isReadableStream(value) {
return !(typeof value !== "object" || value === null || return !(typeof value !== "object" || value === null ||
!(_controller in value)); !ReflectHas(value, _controller));
} }
/** /**
@ -612,7 +619,7 @@
*/ */
function isReadableStreamDefaultReader(value) { function isReadableStreamDefaultReader(value) {
return !(typeof value !== "object" || value === null || return !(typeof value !== "object" || value === null ||
!(_readRequests in value)); !ReflectHas(value, _readRequests));
} }
/** /**
@ -621,7 +628,7 @@
*/ */
function isReadableStreamBYOBReader(value) { function isReadableStreamBYOBReader(value) {
return !(typeof value !== "object" || value === null || return !(typeof value !== "object" || value === null ||
!(_readIntoRequests in value)); !ReflectHas(value, _readIntoRequests));
} }
/** /**
@ -639,7 +646,7 @@
*/ */
function isWritableStream(value) { function isWritableStream(value) {
return !(typeof value !== "object" || value === null || return !(typeof value !== "object" || value === null ||
!(_controller in value)); !ReflectHas(value, _controller));
} }
/** /**
@ -659,7 +666,10 @@
* @returns {T | _close} * @returns {T | _close}
*/ */
function peekQueueValue(container) { function peekQueueValue(container) {
assert(_queue in container && _queueTotalSize in container); assert(
ReflectHas(container, _queue) &&
ReflectHas(container, _queueTotalSize),
);
assert(container[_queue].length); assert(container[_queue].length);
const valueWithSize = container[_queue][0]; const valueWithSize = container[_queue][0];
return valueWithSize.value; return valueWithSize.value;
@ -4333,7 +4343,7 @@
highWaterMark, highWaterMark,
); );
} else { } else {
assert(!("type" in underlyingSourceDict)); assert(!(ReflectHas(underlyingSourceDict, "type")));
const sizeAlgorithm = extractSizeAlgorithm(strategy); const sizeAlgorithm = extractSizeAlgorithm(strategy);
const highWaterMark = extractHighWaterMark(strategy, 1); const highWaterMark = extractHighWaterMark(strategy, 1);
setUpReadableStreamDefaultControllerFromUnderlyingSource( setUpReadableStreamDefaultControllerFromUnderlyingSource(

View file

@ -10,6 +10,7 @@
ArrayPrototypeSlice, ArrayPrototypeSlice,
ObjectKeys, ObjectKeys,
ObjectPrototypeIsPrototypeOf, ObjectPrototypeIsPrototypeOf,
ReflectHas,
Symbol, Symbol,
SymbolFor, SymbolFor,
TypeError, TypeError,
@ -470,17 +471,17 @@
throw new TypeError("Options cannot be passed with endMark."); throw new TypeError("Options cannot be passed with endMark.");
} }
if ( if (
!("start" in startOrMeasureOptions) && !ReflectHas(startOrMeasureOptions, "start") &&
!("end" in startOrMeasureOptions) !ReflectHas(startOrMeasureOptions, "end")
) { ) {
throw new TypeError( throw new TypeError(
"A start or end mark must be supplied in options.", "A start or end mark must be supplied in options.",
); );
} }
if ( if (
"start" in startOrMeasureOptions && ReflectHas(startOrMeasureOptions, "start") &&
"duration" in startOrMeasureOptions && ReflectHas(startOrMeasureOptions, "duration") &&
"end" in startOrMeasureOptions ReflectHas(startOrMeasureOptions, "end")
) { ) {
throw new TypeError( throw new TypeError(
"Cannot specify start, end, and duration together in options.", "Cannot specify start, end, and duration together in options.",
@ -492,13 +493,13 @@
endTime = convertMarkToTimestamp(endMark); endTime = convertMarkToTimestamp(endMark);
} else if ( } else if (
typeof startOrMeasureOptions === "object" && typeof startOrMeasureOptions === "object" &&
"end" in startOrMeasureOptions ReflectHas(startOrMeasureOptions, "end")
) { ) {
endTime = convertMarkToTimestamp(startOrMeasureOptions.end); endTime = convertMarkToTimestamp(startOrMeasureOptions.end);
} else if ( } else if (
typeof startOrMeasureOptions === "object" && typeof startOrMeasureOptions === "object" &&
"start" in startOrMeasureOptions && ReflectHas(startOrMeasureOptions, "start") &&
"duration" in startOrMeasureOptions ReflectHas(startOrMeasureOptions, "duration")
) { ) {
const start = convertMarkToTimestamp(startOrMeasureOptions.start); const start = convertMarkToTimestamp(startOrMeasureOptions.start);
const duration = convertMarkToTimestamp(startOrMeasureOptions.duration); const duration = convertMarkToTimestamp(startOrMeasureOptions.duration);
@ -509,13 +510,13 @@
let startTime; let startTime;
if ( if (
typeof startOrMeasureOptions === "object" && typeof startOrMeasureOptions === "object" &&
"start" in startOrMeasureOptions ReflectHas(startOrMeasureOptions, "start")
) { ) {
startTime = convertMarkToTimestamp(startOrMeasureOptions.start); startTime = convertMarkToTimestamp(startOrMeasureOptions.start);
} else if ( } else if (
typeof startOrMeasureOptions === "object" && typeof startOrMeasureOptions === "object" &&
"end" in startOrMeasureOptions && ReflectHas(startOrMeasureOptions, "end") &&
"duration" in startOrMeasureOptions ReflectHas(startOrMeasureOptions, "duration")
) { ) {
const end = convertMarkToTimestamp(startOrMeasureOptions.end); const end = convertMarkToTimestamp(startOrMeasureOptions.end);
const duration = convertMarkToTimestamp(startOrMeasureOptions.duration); const duration = convertMarkToTimestamp(startOrMeasureOptions.duration);

View file

@ -57,6 +57,7 @@
ReflectApply, ReflectApply,
ReflectDefineProperty, ReflectDefineProperty,
ReflectGetOwnPropertyDescriptor, ReflectGetOwnPropertyDescriptor,
ReflectHas,
ReflectOwnKeys, ReflectOwnKeys,
RegExpPrototypeTest, RegExpPrototypeTest,
Set, Set,
@ -648,7 +649,7 @@
const defaultValues = {}; const defaultValues = {};
for (const member of allMembers) { for (const member of allMembers) {
if ("defaultValue" in member) { if (ReflectHas(member, "defaultValue")) {
const idlMemberValue = member.defaultValue; const idlMemberValue = member.defaultValue;
const imvType = typeof idlMemberValue; const imvType = typeof idlMemberValue;
// Copy by value types can be directly assigned, copy by reference types // Copy by value types can be directly assigned, copy by reference types
@ -1013,13 +1014,16 @@
for (const key in descriptors) { for (const key in descriptors) {
if (key === "constructor") continue; if (key === "constructor") continue;
const descriptor = descriptors[key]; const descriptor = descriptors[key];
if ("value" in descriptor && typeof descriptor.value === "function") { if (
ReflectHas(descriptor, "value") &&
typeof descriptor.value === "function"
) {
ObjectDefineProperty(prototype.prototype, key, { ObjectDefineProperty(prototype.prototype, key, {
enumerable: true, enumerable: true,
writable: true, writable: true,
configurable: true, configurable: true,
}); });
} else if ("get" in descriptor) { } else if (ReflectHas(descriptor, "get")) {
ObjectDefineProperty(prototype.prototype, key, { ObjectDefineProperty(prototype.prototype, key, {
enumerable: true, enumerable: true,
configurable: true, configurable: true,

View file

@ -13,6 +13,7 @@
ObjectFromEntries, ObjectFromEntries,
ObjectEntries, ObjectEntries,
ReflectGet, ReflectGet,
ReflectHas,
Proxy, Proxy,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
@ -113,7 +114,7 @@
}, },
get(target, key) { get(target, key) {
if (typeof key == "symbol") return target[key]; if (typeof key == "symbol") return target[key];
if (key in target) { if (ReflectHas(target, key)) {
return ReflectGet(...new SafeArrayIterator(arguments)); return ReflectGet(...new SafeArrayIterator(arguments));
} else { } else {
return target.getItem(key) ?? undefined; return target.getItem(key) ?? undefined;
@ -141,7 +142,7 @@
if (arguments.length === 1) { if (arguments.length === 1) {
return undefined; return undefined;
} }
if (key in target) { if (ReflectHas(target, key)) {
return undefined; return undefined;
} }
const value = target.getItem(key); const value = target.getItem(key);

View file

@ -21,6 +21,7 @@
FunctionPrototypeCall, FunctionPrototypeCall,
PromiseResolve, PromiseResolve,
PromiseReject, PromiseReject,
ReflectHas,
SymbolFor, SymbolFor,
TypeError, TypeError,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
@ -120,7 +121,10 @@
*/ */
function cache(desc, state) { function cache(desc, state) {
let { name: key } = desc; let { name: key } = desc;
if ((desc.name === "read" || desc.name === "write") && "path" in desc) { if (
(desc.name === "read" || desc.name === "write") &&
ReflectHas(desc, "path")
) {
key += `-${desc.path}`; key += `-${desc.path}`;
} else if (desc.name === "net" && desc.host) { } else if (desc.name === "net" && desc.host) {
key += `-${desc.host}`; key += `-${desc.host}`;