Skip to content

Commit

Permalink
chore: migrate single file codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 11, 2023
1 parent 9bd6346 commit 14ed157
Show file tree
Hide file tree
Showing 35 changed files with 483 additions and 344 deletions.
396 changes: 52 additions & 344 deletions create.ts

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions packages/data/single-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "nolyfill-single-file",
"version": "0.0.0",
"private": true,
"license": "MIT",
"scripts": {
"lint": "eslint ."
},
"dependencies": {
"@nolyfill/shared": "workspace:*",
"@nolyfill/is-array-buffer": "workspace:*"
}
}
11 changes: 11 additions & 0 deletions packages/data/single-file/src/array-buffer-byte-length.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { uncurryThis } from '@nolyfill/shared';
// @ts-expect-error -- no types
import isArrayBuffer from '@nolyfill/is-array-buffer';

const bL = uncurryThis(Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength')!.get!);
const is = (ab: unknown) => {
if (!isArrayBuffer(ab)) return NaN;
return bL(ab);
};

export default is;
7 changes: 7 additions & 0 deletions packages/data/single-file/src/asynciterator.prototype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-expect-error -- AsyncIterator is not a real global value, just a mock
const asyncIterProto = typeof AsyncIterator === 'function' ? AsyncIterator.prototype : {};
if (!(Symbol.iterator in asyncIterProto)) {
asyncIterProto[Symbol.iterator] = function () { return this; };
}

export default asyncIterProto;
6 changes: 6 additions & 0 deletions packages/data/single-file/src/available-typed-arrays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default [
'BigInt64Array', 'BigUint64Array',
'Float32Array', 'Float64Array',
'Int16Array', 'Int32Array', 'Int8Array',
'Uint16Array', 'Uint32Array', 'Uint8Array', 'Uint8ClampedArray'
];
5 changes: 5 additions & 0 deletions packages/data/single-file/src/deep-equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { dequal } from "dequal"

const deepEqual = (a: any, b: any) => dequal(a, b);
export default deepEqual;

1 change: 1 addition & 0 deletions packages/data/single-file/src/define-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { defineProperties as default } from '@nolyfill/shared';
16 changes: 16 additions & 0 deletions packages/data/single-file/src/es-set-tostringtag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface SetToStringTagOption {
force?: boolean
}

const set = (object: any, value: any, options: SetToStringTagOption = {}) => {
if (options.force || !Object.prototype.hasOwnProperty.call(object, Symbol.toStringTag)) {
Object.defineProperty(object, Symbol.toStringTag, {
configurable: true,
enumerable: false,
value,
writable: false
});
}
};

export default set;
2 changes: 2 additions & 0 deletions packages/data/single-file/src/get-symbol-description.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { uncurryThis } from '@nolyfill/shared';
export default uncurryThis(Object.getOwnPropertyDescriptor(Symbol.prototype, 'description')!.get!);
1 change: 1 addition & 0 deletions packages/data/single-file/src/gopd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default Object.getOwnPropertyDescriptors;
1 change: 1 addition & 0 deletions packages/data/single-file/src/harmony-reflect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default Reflect;
3 changes: 3 additions & 0 deletions packages/data/single-file/src/has-property-descriptors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const hasPropertyDescriptors = () => true;
hasPropertyDescriptors.hasArrayLengthDefineBug = () => false;
export default hasPropertyDescriptors;
2 changes: 2 additions & 0 deletions packages/data/single-file/src/has-proto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const hasProto = () => true;
export default hasProto;
38 changes: 38 additions & 0 deletions packages/data/single-file/src/internal-slot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const channel = new WeakMap();
const check = (O: WeakKey, slot: string) => {
if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
throw new TypeError('`O` is not an object');
}
if (typeof slot !== 'string') {
throw new TypeError('`slot` must be a string');
}
};
const has = (O: WeakKey, slot: string) => {
check(O, slot);
const slots = channel.get(O);
return !!slots && Object.prototype.hasOwnProperty.call(slots, `$${slot}`);
};
const get = (O: WeakKey, slot: string) => {
check(O, slot);
const slots = channel.get(O);
return slots && slots[`$${slot}`];
};
const set = (O: WeakKey, slot: string, V: any) => {
check(O, slot);
let slots = channel.get(O);
if (!slots) {
slots = {};
channel.set(O, slots);
}
slots[`$${slot}`] = V;
};
const assert = (O: WeakKey, slot: string) => {
check(O, slot);
if (!channel.has(O)) {
throw new TypeError('Side channel does not contain the given key');
}
if (!has(O, slot)) {
throw new TypeError(`"${slot}" is not present on "O"`);
}
};
module.exports = Object.freeze({ has, get, set, assert });
17 changes: 17 additions & 0 deletions packages/data/single-file/src/is-arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const isStandardArguments = (value: any) => ((value && typeof value === 'object' && Symbol.toStringTag in value)
? false
: Object.prototype.toString.call(value) === '[object Arguments]');

const isLegacyArguments = (value: any) => (isStandardArguments(value)
? true
: (
value !== null
&& typeof value === 'object'
&& typeof value.length === 'number'
&& value.length >= 0
&& Object.prototype.toString.call(value) !== '[object Array]'
&& Object.prototype.toString.call(value.callee) === '[object Function]')
);
// isStandardArguments.isLegacyArguments = isLegacyArguments; // for tests
// eslint-disable-next-line prefer-rest-params -- detect arguments object
module.exports = (function () { return isStandardArguments(arguments); }()) ? isStandardArguments : isLegacyArguments
16 changes: 16 additions & 0 deletions packages/data/single-file/src/is-array-buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { uncurryThis } from '@nolyfill/shared';

const bL = uncurryThis(Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength')!.get!);
const is = (obj: unknown): obj is ArrayBuffer => {
if (!obj || typeof obj !== 'object') {
return false;
}
try {
bL(obj);
return true;
} catch (_) {
return false;
}
};

export default is;
11 changes: 11 additions & 0 deletions packages/data/single-file/src/is-date-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const is = (value: unknown): value is Date => {
if (typeof value !== 'object' || value === null) return false;
try {
Date.prototype.getDay.call(value);
return true;
} catch (e) {
return false;
}
};

export default is;
10 changes: 10 additions & 0 deletions packages/data/single-file/src/is-generator-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const isFnRegex = /^\s*(?:function)?\*/;
// Node.js has full native support for generators since Node.js 6.4.0, so we don't need eval
const GeneratorFunction = Object.getPrototypeOf(function* () {});
function isGeneratorFunction(fn: unknown): fn is Function {
if (typeof fn !== 'function') return false;
if (isFnRegex.test(Function.prototype.toString.call(fn))) return true;
return Object.getPrototypeOf(fn) === GeneratorFunction;
};

export default isGeneratorFunction;
6 changes: 6 additions & 0 deletions packages/data/single-file/src/is-regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const is = (value: unknown): value is RegExp => {
if (!value || (typeof value !== 'object' && typeof value !== 'function')) return false;
return Object.prototype.toString.call(value) === '[object RegExp]';
};

export default is;
16 changes: 16 additions & 0 deletions packages/data/single-file/src/is-shared-array-buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { uncurryThis } from '@nolyfill/shared';

const bL = uncurryThis(Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength')!.get!);
const is = (obj: unknown): obj is SharedArrayBuffer => {
if (!obj || typeof obj !== 'object') {
return false;
}
try {
bL(obj);
return true;
} catch (_) {
return false;
}
};

export default is;
10 changes: 10 additions & 0 deletions packages/data/single-file/src/is-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const is = (value: any): value is string => {
if (typeof value === 'string') return true;
if (typeof value !== 'object') return false;
try {
String.prototype.valueOf.call(value);
return true;
} catch (e) { return false; }
};

export default is;
12 changes: 12 additions & 0 deletions packages/data/single-file/src/is-symbol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const is = (value: unknown): value is symbol => {
if (typeof value === 'symbol') return true;
if (Object.prototype.toString.call(value) !== '[object Symbol]') return false;
try {
if (typeof (value as any).valueOf() !== 'symbol') return false;
return Symbol.prototype.toString.call(value).startsWith('Symbol(');
} catch (e) {
return false;
}
};

export default is;
10 changes: 10 additions & 0 deletions packages/data/single-file/src/is-weakref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const is = (value: unknown) => {
if (typeof WeakRef === 'undefined') return false;
if (!value || typeof value !== 'object') return false;
try {
WeakRef.prototype.deref.call(value);
return true;
} catch (e) {
return false;
}
}
1 change: 1 addition & 0 deletions packages/data/single-file/src/iterator.prototype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
18 changes: 18 additions & 0 deletions packages/data/single-file/src/safe-array-concat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const empty: any[] = [];
// @ts-expect-error -- JS is awesome
empty[Symbol.isConcatSpreadable] = true;

const concat = (...args: any[]) => {
for (let i = 0, l = args.length; i < l; i += 1) {
const arg = args[i];
if (arg && typeof arg === 'object' && typeof arg[Symbol.isConcatSpreadable] === 'boolean') {
const arr = Array.isArray(arg) ? Array.prototype.slice.call(arg) : [arg];
// @ts-expect-error -- JS is awesome
arr[Symbol.isConcatSpreadable] = true;
args[i] = arr;
}
}
return Array.prototype.concat.apply(empty, args);
};

export default concat;
12 changes: 12 additions & 0 deletions packages/data/single-file/src/safe-regex-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const safeRegexTest = (r: RegExp) => {
if (
!r
|| (typeof r !== 'object' && typeof r !== 'function')
|| Object.prototype.toString.call(r) !== '[object RegExp]'
) {
throw new TypeError('\`regex\` must be a RegExp');
}
return (s: string) => RegExp.prototype.exec.call(r, s) !== null;
};

export default safeRegexTest;
39 changes: 39 additions & 0 deletions packages/data/single-file/src/side-channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const create = () => {
let $wm: WeakMap<any, any>, $m: Map<any, any>;

const get = (key: any) => {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if ($wm) return $wm.get(key);
} else if ($m) {
return $m.get(key);
}
return undefined;
};
const set = (key: any, value: any) => {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if (!$wm) $wm = new WeakMap();
$wm.set(key, value);
} else {
if (!$m) $m = new Map();
$m.set(key, value);
}
};
const has = (key: any) => {
if (key && (typeof key === 'object' || typeof key === 'function')) {
if ($wm) {
return $wm.has(key);
}
} else if ($m) {
return $m.has(key);
}
return false;
};
const assert = (key: any) => {
if (!has(key)) {
throw new TypeError('Side channel does not contain the given key');
}
};
return { get, set, has, assert };
};

export default create;
3 changes: 3 additions & 0 deletions packages/data/single-file/src/typed-array-buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { uncurryThis } from '@nolyfill/shared';

export default uncurryThis(Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), 'buffer')!.get!);
12 changes: 12 additions & 0 deletions packages/data/single-file/src/typed-array-byte-length.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { uncurryThis, TypedArrayPrototype } from '@nolyfill/shared';

const typedArrayByteLength = uncurryThis(Object.getOwnPropertyDescriptor(TypedArrayPrototype, 'byteLength')!.get!);
const g = (value: unknown) => {
try {
return typedArrayByteLength(value);
} catch (e) {
return false;
}
};

export default g;
12 changes: 12 additions & 0 deletions packages/data/single-file/src/typed-array-byte-offset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { uncurryThis, TypedArrayPrototype } from '@nolyfill/shared';

const typedArrayByteOffSet = uncurryThis(Object.getOwnPropertyDescriptor(TypedArrayPrototype, 'byteOffset')!.get!);
const g = (value: unknown) => {
try {
return typedArrayByteOffSet(value);
} catch (e) {
return false;
}
};

export default g;
12 changes: 12 additions & 0 deletions packages/data/single-file/src/typed-array-length.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { uncurryThis, TypedArrayPrototype } from '@nolyfill/shared';

const typedArrayLength = uncurryThis(Object.getOwnPropertyDescriptor(TypedArrayPrototype, 'length')!.get!);
const g = (value: unknown) => {
try {
return typedArrayLength(value);
} catch (e) {
return false;
}
};

export default g;
25 changes: 25 additions & 0 deletions packages/data/single-file/src/unbox-primitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default function unboxPrimitive(value: unknown) {
if (value == null || (typeof value !== 'object' && typeof value !== 'function')) {
throw new TypeError(value === null ? 'value is an unboxed primitive' : 'value is a non-boxed-primitive object');
}
if (typeof value === 'string' || Object.prototype.toString.call(value) === '[object String]') {
return String.prototype.toString.call(value);
}
if (typeof value === 'number' || Object.prototype.toString.call(value) === '[object Number]') {
return Number.prototype.valueOf.call(value);
}
if (typeof value === 'boolean' || Object.prototype.toString.call(value) === '[object Boolean]') {
return Boolean.prototype.valueOf.call(value);
}
if (typeof value === 'symbol' || (
Object.prototype.toString.call(value) === '[object Symbol]'
&& typeof value.valueOf() === 'symbol'
&& Symbol.prototype.toString.call(value).startsWith('Symbol(')
)) {
return Symbol.prototype.valueOf.call(value);
}
try {
return BigInt.prototype.valueOf.call(value);
} catch (_) {}
throw new RangeError('unknown boxed primitive');
};
Loading

0 comments on commit 14ed157

Please sign in to comment.