rewrite logic

pull/15589/head
ComputerGuy 5 months ago
parent d28fe7e152
commit 8ebcd913ec

@ -16,6 +16,21 @@ import * as w from './warnings.js';
import { get_stack } from './dev/tracing.js'; import { get_stack } from './dev/tracing.js';
import { tracing_mode_flag } from '../flags/index.js'; import { tracing_mode_flag } from '../flags/index.js';
/**
* @param {unknown} value
* @returns {boolean}
*/
function should_proxy(value) {
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
return false;
}
const prototype = get_prototype_of(value);
if (prototype !== object_prototype && prototype !== array_prototype) {
return false;
}
return true;
}
/** /**
* @template T * @template T
* @param {T} value * @param {T} value
@ -23,13 +38,7 @@ import { tracing_mode_flag } from '../flags/index.js';
*/ */
export function proxy(value) { export function proxy(value) {
// if non-proxyable, or is already a proxy, return `value` // if non-proxyable, or is already a proxy, return `value`
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) { if (!should_proxy(value)) {
return value;
}
const prototype = get_prototype_of(value);
if (prototype !== object_prototype && prototype !== array_prototype) {
return value; return value;
} }
@ -289,10 +298,12 @@ export function proxy(value) {
* @returns {T | void} * @returns {T | void}
*/ */
export function return_proxy(value) { export function return_proxy(value) {
const res = proxy(value); if (
if (res !== value || (typeof value === 'object' && value !== null && STATE_SYMBOL in value)) { !should_proxy(value) &&
!(typeof value === 'object' && value !== null && STATE_SYMBOL in value)
) {
// if the argument passed was already a proxy, we don't warn // if the argument passed was already a proxy, we don't warn
return res; return proxy(value);
} }
w.state_return_not_proxyable(); w.state_return_not_proxyable();
} }

Loading…
Cancel
Save