aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Kerem Kat <keremkat@gmail.com> 2023-10-31 06:05:32 +0000
committerGravatar GitHub <noreply@github.com> 2023-10-30 23:05:32 -0700
commit715be35764df183f473344a794a0aed6eeeb0eed (patch)
treefc5f1cd60b9c5ba634062a5f96ad67e59d05f8f5
parente259056bd8966f38057f5f9962246503be7b4cb2 (diff)
downloadbun-715be35764df183f473344a794a0aed6eeeb0eed.tar.gz
bun-715be35764df183f473344a794a0aed6eeeb0eed.tar.zst
bun-715be35764df183f473344a794a0aed6eeeb0eed.zip
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
-rw-r--r--src/bun.js/bindings/JSDOMFile.cpp2
-rw-r--r--src/bun.js/bindings/NodeVMScript.cpp5
-rw-r--r--test/js/bun/globals.test.js8
-rw-r--r--test/js/node/vm/vm.test.ts8
4 files changed, 22 insertions, 1 deletions
diff --git a/src/bun.js/bindings/JSDOMFile.cpp b/src/bun.js/bindings/JSDOMFile.cpp
index 4e0e4bf48..37536729d 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 17a50f706..1969fabc2 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<Zig::GlobalObject*>(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 3dd129e6a..63927b2b1 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 99ec07ce2..982d5eed9 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(