mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 13:52:43 +00:00 
			
		
		
		
	 766f30f593
			
		
	
	
		766f30f593
		
	
	
	
	
		
			
			If we have a function as class extends value, we still cannot assume that it has a prototype property and that property has a function or null as its value - blindly calling to_object() on it may fail. Fixes #5075.
		
			
				
	
	
		
			49 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| test("extending function", () => {
 | |
|     class A extends function () {
 | |
|         this.foo = 10;
 | |
|     } {}
 | |
| 
 | |
|     expect(new A().foo).toBe(10);
 | |
| });
 | |
| 
 | |
| test("extending null", () => {
 | |
|     class A extends null {}
 | |
| 
 | |
|     expect(Object.getPrototypeOf(A.prototype)).toBeNull();
 | |
| 
 | |
|     expect(() => {
 | |
|         new A();
 | |
|     }).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
 | |
| });
 | |
| 
 | |
| test("extending String", () => {
 | |
|     class MyString extends String {}
 | |
| 
 | |
|     const ms = new MyString("abc");
 | |
|     expect(ms).toBeInstanceOf(MyString);
 | |
|     expect(ms).toBeInstanceOf(String);
 | |
|     expect(ms.charAt(1)).toBe("b");
 | |
| 
 | |
|     class MyString2 extends MyString {
 | |
|         charAt(i) {
 | |
|             return `#${super.charAt(i)}`;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     const ms2 = new MyString2("abc");
 | |
|     expect(ms2.charAt(1)).toBe("#b");
 | |
| });
 | |
| 
 | |
| test("class extends value is invalid", () => {
 | |
|     expect(() => {
 | |
|         class A extends 123 {}
 | |
|     }).toThrowWithMessage(TypeError, "Class extends value 123 is not a constructor or null");
 | |
| });
 | |
| 
 | |
| test("class extends value has invalid prototype", () => {
 | |
|     function f() {}
 | |
|     f.prototype = 123;
 | |
|     expect(() => {
 | |
|         class A extends f {}
 | |
|     }).toThrowWithMessage(TypeError, "Class extends value has an invalid prototype 123");
 | |
| });
 |