diff options
author | 2023-08-21 21:11:27 -0700 | |
---|---|---|
committer | 2023-08-21 21:11:27 -0700 | |
commit | bca1bcf29c3bf925299b89dc75523c65549ca1fb (patch) | |
tree | 05cebf98510c9d9599504b611be954e8fa0d25a7 /src/bun.js | |
parent | 9027484ae1a9eaf9769d79828db48de66450a3fc (diff) | |
download | bun-bca1bcf29c3bf925299b89dc75523c65549ca1fb.tar.gz bun-bca1bcf29c3bf925299b89dc75523c65549ca1fb.tar.zst bun-bca1bcf29c3bf925299b89dc75523c65549ca1fb.zip |
import errors have `code` set to `ERR_MODULE_NOT_FOUND` and `require` errors have `code` set to `MODULE_NOT_FOUND` (#4244)
* ResolveMessage
* Fix it
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js')
-rw-r--r-- | src/bun.js/ResolveMessage.zig | 22 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGeneratedClasses.cpp | 38 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGeneratedClasses.h | 1 | ||||
-rw-r--r-- | src/bun.js/bindings/generated_classes.zig | 26 | ||||
-rw-r--r-- | src/bun.js/javascript.zig | 2 | ||||
-rw-r--r-- | src/bun.js/resolve_message.classes.ts | 7 |
6 files changed, 91 insertions, 5 deletions
diff --git a/src/bun.js/ResolveMessage.zig b/src/bun.js/ResolveMessage.zig index 457a8ec4f..cf2c5f496 100644 --- a/src/bun.js/ResolveMessage.zig +++ b/src/bun.js/ResolveMessage.zig @@ -26,6 +26,28 @@ pub const ResolveMessage = struct { return null; } + pub fn getCode(this: *ResolveMessage, globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + switch (this.msg.metadata) { + .resolve => |resolve| { + const label: []const u8 = brk: { + if (resolve.import_kind.isCommonJS()) { + break :brk "MODULE_NOT_FOUND"; + } + + break :brk switch (resolve.import_kind) { + .stmt, .dynamic => "ERR_MODULE_NOT_FOUND", + else => "RESOLVE_ERROR", + }; + }; + + var atom = bun.String.createAtom(label); + defer atom.deref(); + return atom.toJS(globalObject); + }, + else => return .undefined, + } + } + pub fn fmt(allocator: std.mem.Allocator, specifier: string, referrer: string, err: anyerror) !string { switch (err) { error.ModuleNotFound => { diff --git a/src/bun.js/bindings/ZigGeneratedClasses.cpp b/src/bun.js/bindings/ZigGeneratedClasses.cpp index 225fb5385..7106357b2 100644 --- a/src/bun.js/bindings/ZigGeneratedClasses.cpp +++ b/src/bun.js/bindings/ZigGeneratedClasses.cpp @@ -17471,6 +17471,9 @@ extern "C" void ResolveMessageClass__finalize(void*); extern "C" EncodedJSValue ResolveMessagePrototype__toPrimitive(void* ptr, JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame); JSC_DECLARE_HOST_FUNCTION(ResolveMessagePrototype__toPrimitiveCallback); +extern "C" JSC::EncodedJSValue ResolveMessagePrototype__getCode(void* ptr, JSC::JSGlobalObject* lexicalGlobalObject); +JSC_DECLARE_CUSTOM_GETTER(ResolveMessagePrototype__codeGetterWrap); + extern "C" JSC::EncodedJSValue ResolveMessagePrototype__getImportKind(void* ptr, JSC::JSGlobalObject* lexicalGlobalObject); JSC_DECLARE_CUSTOM_GETTER(ResolveMessagePrototype__importKindGetterWrap); @@ -17498,6 +17501,7 @@ JSC_DECLARE_HOST_FUNCTION(ResolveMessagePrototype__toStringCallback); STATIC_ASSERT_ISO_SUBSPACE_SHARABLE(JSResolveMessagePrototype, JSResolveMessagePrototype::Base); static const HashTableValue JSResolveMessagePrototypeTableValues[] = { + { "code"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, ResolveMessagePrototype__codeGetterWrap, 0 } }, { "importKind"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, ResolveMessagePrototype__importKindGetterWrap, 0 } }, { "level"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, ResolveMessagePrototype__levelGetterWrap, 0 } }, { "message"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, ResolveMessagePrototype__messageGetterWrap, 0 } }, @@ -17550,6 +17554,37 @@ JSC_DEFINE_HOST_FUNCTION(ResolveMessagePrototype__toPrimitiveCallback, (JSGlobal return ResolveMessagePrototype__toPrimitive(thisObject->wrapped(), lexicalGlobalObject, callFrame); } +JSC_DEFINE_CUSTOM_GETTER(ResolveMessagePrototype__codeGetterWrap, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + auto& vm = lexicalGlobalObject->vm(); + Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(lexicalGlobalObject); + auto throwScope = DECLARE_THROW_SCOPE(vm); + JSResolveMessage* thisObject = jsCast<JSResolveMessage*>(JSValue::decode(thisValue)); + JSC::EnsureStillAliveScope thisArg = JSC::EnsureStillAliveScope(thisObject); + + if (JSValue cachedValue = thisObject->m_code.get()) + return JSValue::encode(cachedValue); + + JSC::JSValue result = JSC::JSValue::decode( + ResolveMessagePrototype__getCode(thisObject->wrapped(), globalObject)); + RETURN_IF_EXCEPTION(throwScope, {}); + thisObject->m_code.set(vm, thisObject, result); + RELEASE_AND_RETURN(throwScope, JSValue::encode(result)); +} + +extern "C" void ResolveMessagePrototype__codeSetCachedValue(JSC::EncodedJSValue thisValue, JSC::JSGlobalObject* globalObject, JSC::EncodedJSValue value) +{ + auto& vm = globalObject->vm(); + auto* thisObject = jsCast<JSResolveMessage*>(JSValue::decode(thisValue)); + thisObject->m_code.set(vm, thisObject, JSValue::decode(value)); +} + +extern "C" EncodedJSValue ResolveMessagePrototype__codeGetCachedValue(JSC::EncodedJSValue thisValue) +{ + auto* thisObject = jsCast<JSResolveMessage*>(JSValue::decode(thisValue)); + return JSValue::encode(thisObject->m_code.get()); +} + JSC_DEFINE_CUSTOM_GETTER(ResolveMessagePrototype__importKindGetterWrap, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) { auto& vm = lexicalGlobalObject->vm(); @@ -17797,7 +17832,6 @@ void JSResolveMessagePrototype::finishCreation(JSC::VM& vm, JSC::JSGlobalObject* Base::finishCreation(vm); reifyStaticProperties(vm, JSResolveMessage::info(), JSResolveMessagePrototypeTableValues, *this); this->putDirect(vm, vm.propertyNames->toPrimitiveSymbol, JSFunction::create(vm, globalObject, 1, String("toPrimitive"_s), ResolveMessagePrototype__toPrimitiveCallback, ImplementationVisibility::Public), PropertyAttribute::Function | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | 0); - this->putDirect(vm, Identifier::fromString(vm, "code"_s), jsString(vm, String("ERR_MODULE_NOT_FOUND"_s)), PropertyAttribute::ReadOnly | 0); this->putDirect(vm, vm.propertyNames->name, jsString(vm, String("ResolveMessage"_s)), PropertyAttribute::ReadOnly | 0); JSC_TO_STRING_TAG_WITHOUT_TRANSITION(); } @@ -17953,6 +17987,7 @@ void JSResolveMessage::visitChildrenImpl(JSCell* cell, Visitor& visitor) ASSERT_GC_OBJECT_INHERITS(thisObject, info()); Base::visitChildren(thisObject, visitor); + visitor.append(thisObject->m_code); visitor.append(thisObject->m_importKind); visitor.append(thisObject->m_level); visitor.append(thisObject->m_message); @@ -17969,6 +18004,7 @@ void JSResolveMessage::visitAdditionalChildren(Visitor& visitor) JSResolveMessage* thisObject = this; ASSERT_GC_OBJECT_INHERITS(thisObject, info()); + visitor.append(thisObject->m_code); visitor.append(thisObject->m_importKind); visitor.append(thisObject->m_level); visitor.append(thisObject->m_message); diff --git a/src/bun.js/bindings/ZigGeneratedClasses.h b/src/bun.js/bindings/ZigGeneratedClasses.h index 6ed7eb5de..b8b694068 100644 --- a/src/bun.js/bindings/ZigGeneratedClasses.h +++ b/src/bun.js/bindings/ZigGeneratedClasses.h @@ -1854,6 +1854,7 @@ public: template<typename Visitor> void visitAdditionalChildren(Visitor&); DECLARE_VISIT_OUTPUT_CONSTRAINTS; + mutable JSC::WriteBarrier<JSC::Unknown> m_code; mutable JSC::WriteBarrier<JSC::Unknown> m_importKind; mutable JSC::WriteBarrier<JSC::Unknown> m_level; mutable JSC::WriteBarrier<JSC::Unknown> m_message; diff --git a/src/bun.js/bindings/generated_classes.zig b/src/bun.js/bindings/generated_classes.zig index bf1942fd6..50e1a620b 100644 --- a/src/bun.js/bindings/generated_classes.zig +++ b/src/bun.js/bindings/generated_classes.zig @@ -4357,6 +4357,28 @@ pub const JSResolveMessage = struct { return ResolveMessage__fromJS(value); } + extern fn ResolveMessagePrototype__codeSetCachedValue(JSC.JSValue, *JSC.JSGlobalObject, JSC.JSValue) void; + + extern fn ResolveMessagePrototype__codeGetCachedValue(JSC.JSValue) JSC.JSValue; + + /// `ResolveMessage.code` setter + /// This value will be visited by the garbage collector. + pub fn codeSetCached(thisValue: JSC.JSValue, globalObject: *JSC.JSGlobalObject, value: JSC.JSValue) void { + JSC.markBinding(@src()); + ResolveMessagePrototype__codeSetCachedValue(thisValue, globalObject, value); + } + + /// `ResolveMessage.code` getter + /// This value will be visited by the garbage collector. + pub fn codeGetCached(thisValue: JSC.JSValue) ?JSC.JSValue { + JSC.markBinding(@src()); + const result = ResolveMessagePrototype__codeGetCachedValue(thisValue); + if (result == .zero) + return null; + + return result; + } + extern fn ResolveMessagePrototype__importKindSetCachedValue(JSC.JSValue, *JSC.JSGlobalObject, JSC.JSValue) void; extern fn ResolveMessagePrototype__importKindGetCachedValue(JSC.JSValue) JSC.JSValue; @@ -4538,6 +4560,9 @@ pub const JSResolveMessage = struct { if (@TypeOf(ResolveMessage.toPrimitive) != CallbackType) @compileLog("Expected ResolveMessage.toPrimitive to be a callback but received " ++ @typeName(@TypeOf(ResolveMessage.toPrimitive))); + if (@TypeOf(ResolveMessage.getCode) != GetterType) + @compileLog("Expected ResolveMessage.getCode to be a getter"); + if (@TypeOf(ResolveMessage.getImportKind) != GetterType) @compileLog("Expected ResolveMessage.getImportKind to be a getter"); @@ -4563,6 +4588,7 @@ pub const JSResolveMessage = struct { if (!JSC.is_bindgen) { @export(ResolveMessage.constructor, .{ .name = "ResolveMessageClass__construct" }); @export(ResolveMessage.finalize, .{ .name = "ResolveMessageClass__finalize" }); + @export(ResolveMessage.getCode, .{ .name = "ResolveMessagePrototype__getCode" }); @export(ResolveMessage.getImportKind, .{ .name = "ResolveMessagePrototype__getImportKind" }); @export(ResolveMessage.getLevel, .{ .name = "ResolveMessagePrototype__getLevel" }); @export(ResolveMessage.getMessage, .{ .name = "ResolveMessagePrototype__getMessage" }); diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 07d3f64cf..a016129e2 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -1652,7 +1652,7 @@ pub const VirtualMachine = struct { ), .metadata = .{ // import_kind is wrong probably - .resolve = .{ .specifier = logger.BabyString.in(printed, specifier_utf8.slice()), .import_kind = .stmt }, + .resolve = .{ .specifier = logger.BabyString.in(printed, specifier_utf8.slice()), .import_kind = if (is_esm) .stmt else .require }, }, }; }; diff --git a/src/bun.js/resolve_message.classes.ts b/src/bun.js/resolve_message.classes.ts index 734e766ba..3e3c6d083 100644 --- a/src/bun.js/resolve_message.classes.ts +++ b/src/bun.js/resolve_message.classes.ts @@ -13,6 +13,10 @@ export default [ getter: "getMessage", cache: true, }, + code: { + getter: "getCode", + cache: true, + }, name: { value: "ResolveMessage", }, @@ -32,9 +36,6 @@ export default [ getter: "getImportKind", cache: true, }, - code: { - value: "ERR_MODULE_NOT_FOUND", - }, position: { getter: "getPosition", cache: true, |