diff options
author | Linus Groh <mail@linusgroh.de> | 2020-05-08 16:28:35 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-08 20:06:49 +0200 |
commit | ca22476d9d641fc5a3def0e7de2237e8eb79015a (patch) | |
tree | f0aefa2fd6be38920f3ae912ba82a88d041b03d5 /Libraries | |
parent | 01fd6ce04582e9714d592a3bcb9825792cca81dc (diff) | |
download | serenity-ca22476d9d641fc5a3def0e7de2237e8eb79015a.zip |
LibJS: Add Array.isArray()
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayConstructor.cpp | 12 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayConstructor.h | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.isArray.js | 28 |
3 files changed, 42 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Libraries/LibJS/Runtime/ArrayConstructor.cpp index cab2a3f755..003171de39 100644 --- a/Libraries/LibJS/Runtime/ArrayConstructor.cpp +++ b/Libraries/LibJS/Runtime/ArrayConstructor.cpp @@ -41,6 +41,9 @@ ArrayConstructor::ArrayConstructor() { put("prototype", interpreter().global_object().array_prototype(), 0); put("length", Value(1), Attribute::Configurable); + + u8 attr = Attribute::Writable | Attribute::Configurable; + put_native_function("isArray", is_array, 1, attr); } ArrayConstructor::~ArrayConstructor() @@ -74,4 +77,13 @@ Value ArrayConstructor::construct(Interpreter& interpreter) return call(interpreter); } +Value ArrayConstructor::is_array(Interpreter& interpreter) +{ + auto value = interpreter.argument(0); + if (!value.is_array()) + return Value(false); + // Exclude TypedArray and similar + return Value(StringView(value.as_object().class_name()) == "Array"); +} + } diff --git a/Libraries/LibJS/Runtime/ArrayConstructor.h b/Libraries/LibJS/Runtime/ArrayConstructor.h index 07763cfdaf..a038d080e4 100644 --- a/Libraries/LibJS/Runtime/ArrayConstructor.h +++ b/Libraries/LibJS/Runtime/ArrayConstructor.h @@ -41,6 +41,8 @@ public: private: virtual bool has_constructor() const override { return true; } virtual const char* class_name() const override { return "ArrayConstructor"; } + + static Value is_array(Interpreter&); }; } diff --git a/Libraries/LibJS/Tests/Array.isArray.js b/Libraries/LibJS/Tests/Array.isArray.js new file mode 100644 index 0000000000..714f6e08ea --- /dev/null +++ b/Libraries/LibJS/Tests/Array.isArray.js @@ -0,0 +1,28 @@ +load("test-common.js"); + +try { + assert(Array.isArray.length === 1); + + assert(Array.isArray() === false); + assert(Array.isArray("1") === false); + assert(Array.isArray("foo") === false); + assert(Array.isArray(1) === false); + assert(Array.isArray(1, 2, 3) === false); + assert(Array.isArray(undefined) === false); + assert(Array.isArray(null) === false); + assert(Array.isArray(Infinity) === false); + assert(Array.isArray({}) === false); + + assert(Array.isArray([]) === true); + assert(Array.isArray([1]) === true); + assert(Array.isArray([1, 2, 3]) === true); + assert(Array.isArray(new Array()) === true); + assert(Array.isArray(new Array(10)) === true); + assert(Array.isArray(new Array("a", "b", "c")) === true); + // FIXME: Array.prototype is supposed to be an array! + // assert(Array.isArray(Array.prototype) === true); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |