node:timers fixes (#16855)

This commit is contained in:
190n
2025-02-26 16:45:02 -08:00
committed by GitHub
parent baca1f4634
commit b3edef5989
54 changed files with 2669 additions and 355 deletions

View File

@@ -9,6 +9,22 @@ interface PropertyAttribute {
privateSymbol?: string;
}
/**
* Specifies what happens when a method is called with `this` set to a value that is not an instance
* of the class.
*/
export enum InvalidThisBehavior {
/**
* Default. Throws a `TypeError`.
*/
Throw,
/**
* Do not call the native implementation; return `undefined`. Some Node.js methods are supposed to
* work like this.
*/
NoOp,
}
export type Field =
| ({
getter: string;
@@ -35,6 +51,7 @@ export type Field =
*/
length?: number;
passThis?: boolean;
invalidThisBehavior?: InvalidThisBehavior;
DOMJIT?: {
returns: string;
args?: [string, string] | [string, string, string] | [string] | [];

View File

@@ -1,6 +1,6 @@
// @ts-nocheck
import path from "path";
import type { ClassDefinition, Field } from "./class-definitions";
import { InvalidThisBehavior, type ClassDefinition, type Field } from "./class-definitions";
import { camelCase, pascalCase, writeIfNotChanged } from "./helpers";
import jsclasses from "./../bun.js/bindings/js_classes";
@@ -1163,6 +1163,7 @@ JSC_DEFINE_CUSTOM_SETTER(${symbolName(typeName, name)}SetterWrap, (JSGlobalObjec
if ("fn" in proto[name]) {
const fn = proto[name].fn;
const invalidThisBehavior = proto[name].invalidThisBehavior ?? InvalidThisBehavior.Throw;
rows.push(`
JSC_DEFINE_HOST_FUNCTION(${symbolName(typeName, name)}Callback, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame))
{
@@ -1172,8 +1173,13 @@ JSC_DEFINE_HOST_FUNCTION(${symbolName(typeName, name)}Callback, (JSGlobalObject
${className(typeName)}* thisObject = jsDynamicCast<${className(typeName)}*>(callFrame->thisValue());
if (UNLIKELY(!thisObject)) {
scope.throwException(lexicalGlobalObject, Bun::createInvalidThisError(lexicalGlobalObject, callFrame->thisValue(), "${typeName}"_s));
return {};
${
invalidThisBehavior == InvalidThisBehavior.Throw
? `
scope.throwException(lexicalGlobalObject, Bun::createInvalidThisError(lexicalGlobalObject, callFrame->thisValue(), "${typeName}"_s));
return {};`
: `return JSValue::encode(JSC::jsUndefined());`
}
}
JSC::EnsureStillAliveScope thisArg = JSC::EnsureStillAliveScope(thisObject);