Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
ca194a1b51 Fix regression in plugin onLoad validation causing missing error throws
The cleanup-onloadresult branch introduced a regression where invalid onLoad
objects no longer throw errors when they should. Specifically:

1. Empty objects ({}) without contents property weren't throwing errors
2. Objects with invalid contents types (e.g., numbers) weren't being validated properly

The issue was in handleOnLoadResultNotPromise where the validation logic was
restructured but the error handling for missing or invalid contents was broken.

Fixed by:
- Properly checking for missing contents (undefined/null) before processing
- Adding comprehensive validation for invalid contents types
- Ensuring all code paths return appropriate error results

Fixes failing test: "invalid onLoad objects throw"

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-06 22:59:15 +00:00

View File

@@ -299,38 +299,42 @@ OnLoadResult handleOnLoadResultNotPromise(MarkedArgumentBuffer& arguments, Zig::
if (scope.exception()) [[unlikely]] {
RELEASE_AND_RETURN(scope, toErrorResult(scope.exception()));
}
if (contentsValue) {
if (contentsValue.isString()) {
if (JSC::JSString* contentsJSString = contentsValue.toStringOrNull(globalObject)) {
arguments.append(contentsValue);
return {
.value = { .sourceText = { .string = Zig::toZigString(contentsJSString, globalObject), .value = contentsValue, .loader = loader } },
.type = OnLoadResultTypeCode,
.wasMock = false,
};
}
} else if (JSC::JSArrayBufferView* view = JSC::jsDynamicCast<JSC::JSArrayBufferView*>(contentsValue)) {
arguments.append(contentsValue);
return {
.value = { .sourceText = { .string = ZigString { reinterpret_cast<const unsigned char*>(view->vector()), view->byteLength() }, .value = contentsValue, .loader = loader } },
.type = OnLoadResultTypeCode,
.wasMock = false,
};
}
}
if (contentsValue.isEmpty()) [[unlikely]] {
if (!contentsValue || contentsValue.isUndefinedOrNull()) [[unlikely]] {
throwException(globalObject, scope, createError(globalObject, "Expected \"contents\" to be a string or an ArrayBufferView"_s));
const auto result = toErrorResult(scope.exception());
scope.clearException();
return result;
}
return {
.value = { .sourceText = { .string = ZigStringEmpty, .value = contentsValue, .loader = loader } },
.type = OnLoadResultTypeCode,
.wasMock = false,
};
if (contentsValue.isString()) {
if (JSC::JSString* contentsJSString = contentsValue.toStringOrNull(globalObject)) {
arguments.append(contentsValue);
return {
.value = { .sourceText = { .string = Zig::toZigString(contentsJSString, globalObject), .value = contentsValue, .loader = loader } },
.type = OnLoadResultTypeCode,
.wasMock = false,
};
} else {
// String conversion failed
throwException(globalObject, scope, createError(globalObject, "Expected \"contents\" to be a string or an ArrayBufferView"_s));
const auto result = toErrorResult(scope.exception());
scope.clearException();
return result;
}
} else if (JSC::JSArrayBufferView* view = JSC::jsDynamicCast<JSC::JSArrayBufferView*>(contentsValue)) {
arguments.append(contentsValue);
return {
.value = { .sourceText = { .string = ZigString { reinterpret_cast<const unsigned char*>(view->vector()), view->byteLength() }, .value = contentsValue, .loader = loader } },
.type = OnLoadResultTypeCode,
.wasMock = false,
};
} else {
// contentsValue exists but is not a string or ArrayBufferView
throwException(globalObject, scope, createError(globalObject, "Expected \"contents\" to be a string or an ArrayBufferView"_s));
const auto result = toErrorResult(scope.exception());
scope.clearException();
return result;
}
}
static OnLoadResult handleOnLoadResult(MarkedArgumentBuffer& arguments, Zig::GlobalObject* globalObject, JSC::JSValue objectValue, BunString* specifier, bool wasModuleMock = false)