-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: don't materialize a callable leaf's built-in members as child endpoints #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+111
β0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/vitests/suites/unified-wrapper/leaf-function-prototype-read.test.vitest.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** | ||
| * @Project: @cldmv/slothlet | ||
| * @Filename: /tests/vitests/suites/unified-wrapper/leaf-function-prototype-read.test.vitest.mjs | ||
| * @Date: 2026-08-24T00:00:00-08:00 (1756022400) | ||
| * @Author: Nate Corcoran <CLDMV> | ||
| * @Email: <Shinrai@users.noreply.github.com> | ||
| * ----- | ||
| * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com) | ||
| * @Last modified time: 2026-08-24T00:00:00-08:00 (1756022400) | ||
| * ----- | ||
| * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved. | ||
| */ | ||
|
|
||
| /** | ||
| * @fileoverview Reading a Function.prototype member off a callable leaf must not mutate its record (#304). | ||
| * | ||
| * @description | ||
| * `leaf.apply(thisArg, args)` is a common forwarding idiom. The wrapper's get trap used to treat | ||
| * every function-valued property β including the leaf's own inherited `apply`/`call`/`bind`/`constructor` | ||
| * and its non-enumerable `prototype` β as a child endpoint, wrapping it and registering a phantom child. | ||
| * A mere READ therefore flipped the leaf from `kind: "function"` to `kind: "namespace"` and grew a phantom | ||
| * `leaf.apply` record. Built-in function members must return the function's own property untouched; only a | ||
| * user-added enumerable own property materializes as a child. Verified in eager and lazy modes. | ||
| * @module tests/vitests/suites/unified-wrapper/leaf-function-prototype-read | ||
| */ | ||
|
|
||
| import { describe, it, expect, beforeAll, afterAll, afterEach } from "vitest"; | ||
| import { mkdir, writeFile, rm } from "node:fs/promises"; | ||
| import { join, resolve } from "node:path"; | ||
| import slothlet from "@cldmv/slothlet"; | ||
|
|
||
| const ROOT = resolve("tmp", `slothlet-leaffnproto-${Date.now()}-${Math.random().toString(36).slice(2)}`); | ||
|
|
||
| beforeAll(async () => { | ||
| await mkdir(ROOT, { recursive: true }); | ||
| await writeFile(join(ROOT, "tool.mjs"), `export function ping() { return "pong"; }\n`); | ||
| // A callable that ALSO carries a real, user-added enumerable own child (`version`): the built-in | ||
| // members must still return native, but the genuine child must still materialize. | ||
| await writeFile(join(ROOT, "greet.mjs"), `export function greet() { return "hi"; }\ngreet.version = function version() { return "v1"; };\n`); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await rm(ROOT, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| const MODES = ["eager", "lazy"]; | ||
| // The leaf function's OWN built-in surface, not api children: inherited callable members and the | ||
| // non-enumerable `prototype` slot. Reading any of them must not mutate the record. | ||
| const CALLABLE_MEMBERS = ["apply", "call", "bind", "constructor"]; | ||
| const isPhantom = (p) => /\.(apply|call|bind|constructor|prototype)$/.test(p.path); | ||
|
|
||
| describe.each(MODES)("Function.prototype reads on a callable leaf leave the record intact β %s", (mode) => { | ||
| let api; | ||
|
|
||
| afterEach(async () => { | ||
| if (api?.shutdown) await api.shutdown(); | ||
| api = null; | ||
| }); | ||
|
|
||
| it("does not turn a function leaf into a namespace or grow phantom children", async () => { | ||
| api = await slothlet({ base: ROOT, silent: true, mode }); | ||
|
|
||
| const before = await api.slothlet.api.leaves(".", { details: true, includePrivate: true }); | ||
| expect(before.find((p) => p.path === "tool.ping")?.kind).toBe("function"); | ||
| expect(before.filter(isPhantom)).toHaveLength(0); | ||
|
|
||
| // Read every built-in member β this must not mutate the record. | ||
| for (const member of CALLABLE_MEMBERS) { | ||
| expect(typeof api.tool.ping[member]).toBe("function"); | ||
| } | ||
| expect(typeof api.tool.ping.prototype).toBe("object"); | ||
| expect(api.tool.ping()).toBe("pong"); | ||
|
|
||
| const after = await api.slothlet.api.leaves(".", { details: true, includePrivate: true }); | ||
| // The leaf stays a function, and no phantom `tool.ping.apply` / `.call` / `.bind` / β¦ appeared. | ||
| expect(after.find((p) => p.path === "tool.ping")?.kind).toBe("function"); | ||
| expect(after.filter(isPhantom)).toHaveLength(0); | ||
| expect(after.some((p) => p.path.startsWith("tool.ping."))).toBe(false); | ||
| }); | ||
|
|
||
| it("leaf.apply / leaf.call still invoke the leaf through slothlet", async () => { | ||
| api = await slothlet({ base: ROOT, silent: true, mode }); | ||
| expect(api.tool.ping.apply(null, [])).toBe("pong"); | ||
| expect(api.tool.ping.call(null)).toBe("pong"); | ||
| const bound = api.tool.ping.bind(null); | ||
| expect(bound()).toBe("pong"); | ||
| }); | ||
|
|
||
| it("a genuine user-added enumerable own child on a callable still materializes", async () => { | ||
| api = await slothlet({ base: ROOT, silent: true, mode }); | ||
| // `greet.version` is a real enumerable own property β it IS a child endpoint and must resolve, | ||
| // while `greet.apply` (built-in) returns the function's own member and is not a child. | ||
| expect(api.greet()).toBe("hi"); | ||
| expect(api.greet.version()).toBe("v1"); | ||
| expect(typeof api.greet.apply).toBe("function"); | ||
|
|
||
| const details = await api.slothlet.api.leaves(".", { details: true, includePrivate: true }); | ||
| expect(details.find((p) => p.path === "greet.version")?.kind).toBe("function"); | ||
| expect(details.some((p) => isPhantom(p) && p.path.startsWith("greet."))).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.