Skip to content

Commit

Permalink
feat: add internal-slot
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Aug 29, 2023
1 parent 0d394e0 commit 530d720
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 15 deletions.
1 change: 1 addition & 0 deletions DOWNLOAD_STATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
| `@nolyfill/has-proto` | [![npm](https://img.shields.io/npm/dm/@nolyfill/has-proto.svg?style=flat-square&logo=npm&logoColor=white&label=download&color=333)](https://www.npmjs.com/package/@nolyfill/has-proto) |
| `@nolyfill/has-symbols` | [![npm](https://img.shields.io/npm/dm/@nolyfill/has-symbols.svg?style=flat-square&logo=npm&logoColor=white&label=download&color=333)](https://www.npmjs.com/package/@nolyfill/has-symbols) |
| `@nolyfill/has-tostringtag` | [![npm](https://img.shields.io/npm/dm/@nolyfill/has-tostringtag.svg?style=flat-square&logo=npm&logoColor=white&label=download&color=333)](https://www.npmjs.com/package/@nolyfill/has-tostringtag) |
| `@nolyfill/internal-slot` | [![npm](https://img.shields.io/npm/dm/@nolyfill/internal-slot.svg?style=flat-square&logo=npm&logoColor=white&label=download&color=333)](https://www.npmjs.com/package/@nolyfill/internal-slot) |
| `@nolyfill/is-arguments` | [![npm](https://img.shields.io/npm/dm/@nolyfill/is-arguments.svg?style=flat-square&logo=npm&logoColor=white&label=download&color=333)](https://www.npmjs.com/package/@nolyfill/is-arguments) |
| `@nolyfill/is-array-buffer` | [![npm](https://img.shields.io/npm/dm/@nolyfill/is-array-buffer.svg?style=flat-square&logo=npm&logoColor=white&label=download&color=333)](https://www.npmjs.com/package/@nolyfill/is-array-buffer) |
| `@nolyfill/is-date-object` | [![npm](https://img.shields.io/npm/dm/@nolyfill/is-date-object.svg?style=flat-square&logo=npm&logoColor=white&label=download&color=333)](https://www.npmjs.com/package/@nolyfill/is-date-object) |
Expand Down
40 changes: 38 additions & 2 deletions create.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ module.exports = function isGeneratorFunction(fn) {
if (typeof fn !== 'function') return false;
if (isFnRegex.test(Function.prototype.toString.call(fn))) return true;
return Object.getPrototypeOf(fn) === GeneratorFunction;
}`],
};`],
['side-channel', `module.exports = () => {
let $wm, $m;
Expand Down Expand Up @@ -465,7 +465,43 @@ module.exports = function isGeneratorFunction(fn) {
}
};
return { get, set, has, assert };
}`]
};`],
['internal-slot', `const channel = new WeakMap();
const check = (O, slot) => {
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, slot) => {
check(O, slot);
const slots = channel.get(O);
return !!slots && Object.prototype.hasOwnProperty.call(slots, \`$\${slot}\`);
};
const get = (O, slot) => {
check(O, slot);
const slots = channel.get(O);
return slots && slots[\`$\${slot}\`];
};
const set = (O, slot, V) => {
check(O, slot);
let slots = channel.get(O);
if (!slots) {
slots = {};
channel.set(O, slots);
}
slots[\`$\${slot}\`] = V;
};
const assert = (O, slot) => {
check(O, slot);
channel.assert(O);
if (!has(O, slot)) {
throw new TypeError(\`\\\`\${slot}\\\` is not present on \\\`O\\\`\`);
}
};
module.exports = Object.freeze({ has, get, set, assert });`]
]);

const manualPackagesList = /** @type {const} */ ([
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"has-proto": "workspace:@nolyfill/has-proto@*",
"has-symbols": "workspace:@nolyfill/has-symbols@*",
"has-tostringtag": "workspace:@nolyfill/has-tostringtag@*",
"internal-slot": "workspace:@nolyfill/internal-slot@*",
"is-arguments": "workspace:@nolyfill/is-arguments@*",
"is-array-buffer": "workspace:@nolyfill/is-array-buffer@*",
"is-date-object": "workspace:@nolyfill/is-date-object@*",
Expand Down Expand Up @@ -146,6 +147,7 @@
"has-proto": "npm:@nolyfill/has-proto@latest",
"has-symbols": "npm:@nolyfill/has-symbols@latest",
"has-tostringtag": "npm:@nolyfill/has-tostringtag@latest",
"internal-slot": "npm:@nolyfill/internal-slot@latest",
"is-arguments": "npm:@nolyfill/is-arguments@latest",
"is-array-buffer": "npm:@nolyfill/is-array-buffer@latest",
"is-date-object": "npm:@nolyfill/is-date-object@latest",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/all-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const allPackages = [
"has-proto",
"has-symbols",
"has-tostringtag",
"internal-slot",
"is-arguments",
"is-array-buffer",
"is-date-object",
Expand Down
37 changes: 37 additions & 0 deletions packages/internal-slot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
const channel = new WeakMap();
const check = (O, slot) => {
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, slot) => {
check(O, slot);
const slots = channel.get(O);
return !!slots && Object.prototype.hasOwnProperty.call(slots, `$${slot}`);
};
const get = (O, slot) => {
check(O, slot);
const slots = channel.get(O);
return slots && slots[`$${slot}`];
};
const set = (O, slot, V) => {
check(O, slot);
let slots = channel.get(O);
if (!slots) {
slots = {};
channel.set(O, slots);
}
slots[`$${slot}`] = V;
};
const assert = (O, slot) => {
check(O, slot);
channel.assert(O);
if (!has(O, slot)) {
throw new TypeError(`\`${slot}\` is not present on \`O\``);
}
};
module.exports = Object.freeze({ has, get, set, assert });
19 changes: 19 additions & 0 deletions packages/internal-slot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@nolyfill/internal-slot",
"version": "1.0.19",
"repository": {
"type": "git",
"url": "https://github.com/SukkaW/nolyfill",
"directory": "packages/internal-slot"
},
"main": "./index.js",
"license": "MIT",
"files": [
"*.js"
],
"scripts": {},
"dependencies": {},
"engines": {
"node": ">=12.4.0"
}
}
2 changes: 1 addition & 1 deletion packages/is-generator-function/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ module.exports = function isGeneratorFunction(fn) {
if (typeof fn !== 'function') return false;
if (isFnRegex.test(Function.prototype.toString.call(fn))) return true;
return Object.getPrototypeOf(fn) === GeneratorFunction;
}
};
2 changes: 1 addition & 1 deletion packages/side-channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ module.exports = () => {
}
};
return { get, set, has, assert };
}
};
16 changes: 5 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 530d720

Please sign in to comment.