From 715be35764df183f473344a794a0aed6eeeb0eed Mon Sep 17 00:00:00 2001 From: Kerem Kat Date: Tue, 31 Oct 2023 06:05:32 +0000 Subject: [PATCH] Fix 6281 (#6809) * throw an error when `Script` is called without new fix https://github.com/oven-sh/bun/issues/6281 * fix typo in `File` without constructor error fix https://github.com/oven-sh/bun/issues/6281 --- src/bun.js/bindings/JSDOMFile.cpp | 2 +- src/bun.js/bindings/NodeVMScript.cpp | 5 +++++ test/js/bun/globals.test.js | 8 ++++++++ test/js/node/vm/vm.test.ts | 8 ++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/bun.js/bindings/JSDOMFile.cpp b/src/bun.js/bindings/JSDOMFile.cpp index 4e0e4bf482..37536729d5 100644 --- a/src/bun.js/bindings/JSDOMFile.cpp +++ b/src/bun.js/bindings/JSDOMFile.cpp @@ -96,7 +96,7 @@ public: static EncodedJSValue call(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame) { auto scope = DECLARE_THROW_SCOPE(lexicalGlobalObject->vm()); - throwTypeError(lexicalGlobalObject, scope, "Class constructor File cannot be invoked without 'new"_s); + throwTypeError(lexicalGlobalObject, scope, "Class constructor File cannot be invoked without 'new'"_s); return {}; } }; diff --git a/src/bun.js/bindings/NodeVMScript.cpp b/src/bun.js/bindings/NodeVMScript.cpp index 17a50f7060..1969fabc25 100644 --- a/src/bun.js/bindings/NodeVMScript.cpp +++ b/src/bun.js/bindings/NodeVMScript.cpp @@ -111,6 +111,11 @@ constructScript(JSGlobalObject* globalObject, CallFrame* callFrame, JSValue newT if (UNLIKELY(zigGlobalObject->NodeVMScript() != newTarget)) { auto scope = DECLARE_THROW_SCOPE(vm); JSObject* targetObj = asObject(newTarget); + if (targetObj == nullptr) { + throwTypeError(globalObject, scope, "Class constructor Script cannot be invoked without 'new'"_s); + return {}; + } + auto* functionGlobalObject = reinterpret_cast(getFunctionRealm(globalObject, targetObj)); RETURN_IF_EXCEPTION(scope, {}); structure = InternalFunction::createSubclassStructure( diff --git a/test/js/bun/globals.test.js b/test/js/bun/globals.test.js index 3dd129e6ad..63927b2b19 100644 --- a/test/js/bun/globals.test.js +++ b/test/js/bun/globals.test.js @@ -87,6 +87,14 @@ describe("File", () => { } }); + it("constructor without new", () => { + const result = () => File(); + expect(result).toThrow({ + name: "TypeError", + message: "Class constructor File cannot be invoked without 'new'", + }); + }); + it("instanceof", () => { const file = new File(["foo"], "bar.txt", { type: "text/plain;charset=utf-8" }); expect(file instanceof File).toBe(true); diff --git a/test/js/node/vm/vm.test.ts b/test/js/node/vm/vm.test.ts index 99ec07ce2e..982d5eed90 100644 --- a/test/js/node/vm/vm.test.ts +++ b/test/js/node/vm/vm.test.ts @@ -39,6 +39,14 @@ describe("Script", () => { return script.runInThisContext(context); }); }); + test("can throw without new", () => { + // @ts-ignore + const result = () => Script(); + expect(result).toThrow({ + name: "TypeError", + message: "Class constructor Script cannot be invoked without 'new'", + }); + }); }); function testRunInContext(