Prechádzať zdrojové kódy

LibJS: Use u64 for the length parameter in Array::create()

This doesn't matter per se as the value is immediately validated to be
in the 0 to 2^32 - 1 range, but it avoids having to cast a number that
potentially doesn't fit into a size_t into one at the call site. More
often than not, array-like lengths are only validated to be <= 2^52 - 1,
i.e. MAX_SAFE_INTEGER.

This is fully backwards compatible with existing code as a size_t always
fits into an u64, but an u64 might not always fit into a size_t.
Linus Groh 3 rokov pred
rodič
commit
5927cdd9c5

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Array.cpp

@@ -17,7 +17,7 @@
 namespace JS {
 
 // 10.4.2.2 ArrayCreate ( length [ , proto ] ), https://tc39.es/ecma262/#sec-arraycreate
-ThrowCompletionOr<Array*> Array::create(GlobalObject& global_object, size_t length, Object* prototype)
+ThrowCompletionOr<Array*> Array::create(GlobalObject& global_object, u64 length, Object* prototype)
 {
     auto& vm = global_object.vm();
 

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Array.h

@@ -21,7 +21,7 @@ class Array : public Object {
     JS_OBJECT(Array, Object);
 
 public:
-    static ThrowCompletionOr<Array*> create(GlobalObject&, size_t length, Object* prototype = nullptr);
+    static ThrowCompletionOr<Array*> create(GlobalObject&, u64 length, Object* prototype = nullptr);
     static Array* create_from(GlobalObject&, Vector<Value> const&);
     // Non-standard but equivalent to CreateArrayFromList.
     template<typename T>