diff --git a/Libraries/LibJS/CMakeLists.txt b/Libraries/LibJS/CMakeLists.txt index 61d9bb17af..55edf67311 100644 --- a/Libraries/LibJS/CMakeLists.txt +++ b/Libraries/LibJS/CMakeLists.txt @@ -10,6 +10,9 @@ set(SOURCES MarkupGenerator.cpp Parser.cpp Runtime/Array.cpp + Runtime/ArrayBuffer.cpp + Runtime/ArrayBufferConstructor.cpp + Runtime/ArrayBufferPrototype.cpp Runtime/ArrayConstructor.cpp Runtime/ArrayIterator.cpp Runtime/ArrayIteratorPrototype.cpp diff --git a/Libraries/LibJS/Forward.h b/Libraries/LibJS/Forward.h index bcde508370..34ec9d2952 100644 --- a/Libraries/LibJS/Forward.h +++ b/Libraries/LibJS/Forward.h @@ -45,17 +45,18 @@ void name([[maybe_unused]] JS::VM& vm, [[maybe_unused]] JS::GlobalObject& global_object, JS::Value value) // NOTE: Proxy is not included here as it doesn't have a prototype - m_proxy_constructor is initialized separately. -#define JS_ENUMERATE_NATIVE_OBJECTS_EXCLUDING_TEMPLATES \ - __JS_ENUMERATE(Array, array, ArrayPrototype, ArrayConstructor, void) \ - __JS_ENUMERATE(BigIntObject, bigint, BigIntPrototype, BigIntConstructor, void) \ - __JS_ENUMERATE(BooleanObject, boolean, BooleanPrototype, BooleanConstructor, void) \ - __JS_ENUMERATE(Date, date, DatePrototype, DateConstructor, void) \ - __JS_ENUMERATE(Error, error, ErrorPrototype, ErrorConstructor, void) \ - __JS_ENUMERATE(Function, function, FunctionPrototype, FunctionConstructor, void) \ - __JS_ENUMERATE(NumberObject, number, NumberPrototype, NumberConstructor, void) \ - __JS_ENUMERATE(Object, object, ObjectPrototype, ObjectConstructor, void) \ - __JS_ENUMERATE(RegExpObject, regexp, RegExpPrototype, RegExpConstructor, void) \ - __JS_ENUMERATE(StringObject, string, StringPrototype, StringConstructor, void) \ +#define JS_ENUMERATE_NATIVE_OBJECTS_EXCLUDING_TEMPLATES \ + __JS_ENUMERATE(Array, array, ArrayPrototype, ArrayConstructor, void) \ + __JS_ENUMERATE(ArrayBuffer, array_buffer, ArrayBufferPrototype, ArrayBufferConstructor, void) \ + __JS_ENUMERATE(BigIntObject, bigint, BigIntPrototype, BigIntConstructor, void) \ + __JS_ENUMERATE(BooleanObject, boolean, BooleanPrototype, BooleanConstructor, void) \ + __JS_ENUMERATE(Date, date, DatePrototype, DateConstructor, void) \ + __JS_ENUMERATE(Error, error, ErrorPrototype, ErrorConstructor, void) \ + __JS_ENUMERATE(Function, function, FunctionPrototype, FunctionConstructor, void) \ + __JS_ENUMERATE(NumberObject, number, NumberPrototype, NumberConstructor, void) \ + __JS_ENUMERATE(Object, object, ObjectPrototype, ObjectConstructor, void) \ + __JS_ENUMERATE(RegExpObject, regexp, RegExpPrototype, RegExpConstructor, void) \ + __JS_ENUMERATE(StringObject, string, StringPrototype, StringConstructor, void) \ __JS_ENUMERATE(SymbolObject, symbol, SymbolPrototype, SymbolConstructor, void) #define JS_ENUMERATE_NATIVE_OBJECTS \ diff --git a/Libraries/LibJS/Runtime/ArrayBuffer.cpp b/Libraries/LibJS/Runtime/ArrayBuffer.cpp new file mode 100644 index 0000000000..007e61c0fa --- /dev/null +++ b/Libraries/LibJS/Runtime/ArrayBuffer.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020, Linus Groh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +namespace JS { + +ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, size_t byte_size) +{ + return global_object.heap().allocate(global_object, byte_size, *global_object.array_buffer_prototype()); +} + +ArrayBuffer::ArrayBuffer(size_t byte_size, Object& prototype) + : Object(prototype) + , m_buffer(ByteBuffer::create_zeroed(byte_size)) +{ +} + +ArrayBuffer::~ArrayBuffer() +{ +} + +} diff --git a/Libraries/LibJS/Runtime/ArrayBuffer.h b/Libraries/LibJS/Runtime/ArrayBuffer.h new file mode 100644 index 0000000000..cd9c92dcd1 --- /dev/null +++ b/Libraries/LibJS/Runtime/ArrayBuffer.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020, Linus Groh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include +#include + +namespace JS { + +class ArrayBuffer : public Object { + JS_OBJECT(ArrayBuffer, Object); + +public: + static ArrayBuffer* create(GlobalObject&, size_t); + + ArrayBuffer(size_t, Object& prototype); + virtual ~ArrayBuffer() override; + + size_t byte_length() const { return m_buffer.size(); } + ByteBuffer& buffer() { return m_buffer; } + const ByteBuffer& buffer() const { return m_buffer; } + +private: + virtual bool is_array_buffer() const override { return true; } + + ByteBuffer m_buffer; +}; + +} diff --git a/Libraries/LibJS/Runtime/ArrayBufferConstructor.cpp b/Libraries/LibJS/Runtime/ArrayBufferConstructor.cpp new file mode 100644 index 0000000000..b869dcd5f3 --- /dev/null +++ b/Libraries/LibJS/Runtime/ArrayBufferConstructor.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2020, Linus Groh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +namespace JS { + +ArrayBufferConstructor::ArrayBufferConstructor(GlobalObject& global_object) + : NativeFunction(vm().names.ArrayBuffer, *global_object.function_prototype()) +{ +} + +void ArrayBufferConstructor::initialize(GlobalObject& global_object) +{ + auto& vm = this->vm(); + NativeFunction::initialize(global_object); + u8 attr = Attribute::Writable | Attribute::Configurable; + define_property(vm.names.prototype, global_object.array_buffer_prototype(), 0); + define_property(vm.names.length, Value(1), Attribute::Configurable); + define_native_function(vm.names.isView, is_view, 1, attr); +} + +ArrayBufferConstructor::~ArrayBufferConstructor() +{ +} + +Value ArrayBufferConstructor::call() +{ + auto& vm = this->vm(); + vm.throw_exception(global_object(), ErrorType::ConstructorWithoutNew, vm.names.ArrayBuffer); + return {}; +} + +Value ArrayBufferConstructor::construct(Function&) +{ + auto& vm = this->vm(); + auto byte_length = vm.argument(0).to_index(global_object()); + if (vm.exception()) { + // Re-throw more specific RangeError + vm.clear_exception(); + vm.throw_exception(global_object(), ErrorType::InvalidLength, "array buffer"); + return {}; + } + return ArrayBuffer::create(global_object(), byte_length); +} + +JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::is_view) +{ + auto arg = vm.argument(0); + if (!arg.is_object()) + return Value(false); + if (arg.as_object().is_typed_array()) + return Value(true); + // FIXME: Check for DataView as well + return Value(false); +} + +} diff --git a/Libraries/LibJS/Runtime/ArrayBufferConstructor.h b/Libraries/LibJS/Runtime/ArrayBufferConstructor.h new file mode 100644 index 0000000000..455fa7e7e2 --- /dev/null +++ b/Libraries/LibJS/Runtime/ArrayBufferConstructor.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2020, Linus Groh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +namespace JS { + +class ArrayBufferConstructor final : public NativeFunction { + JS_OBJECT(ArrayBufferConstructor, NativeFunction); + +public: + explicit ArrayBufferConstructor(GlobalObject&); + virtual void initialize(GlobalObject&) override; + virtual ~ArrayBufferConstructor() override; + + virtual Value call() override; + virtual Value construct(Function& new_target) override; + +private: + virtual bool has_constructor() const override { return true; } + + JS_DECLARE_NATIVE_FUNCTION(is_view); +}; + +} diff --git a/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp b/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp new file mode 100644 index 0000000000..320895893e --- /dev/null +++ b/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2020, Linus Groh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +namespace JS { + +ArrayBufferPrototype::ArrayBufferPrototype(GlobalObject& global_object) + : Object(*global_object.object_prototype()) +{ +} + +void ArrayBufferPrototype::initialize(GlobalObject& global_object) +{ + auto& vm = this->vm(); + Object::initialize(global_object); + u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.slice, slice, 2, attr); + // FIXME: This should be an accessor property + define_native_property(vm.names.byteLength, byte_length_getter, nullptr, Attribute::Configurable); + + define_property(vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "ArrayBuffer"), Attribute::Configurable); +} + +ArrayBufferPrototype::~ArrayBufferPrototype() +{ +} + +static ArrayBuffer* array_buffer_object_from(VM& vm, GlobalObject& global_object) +{ + // ArrayBuffer.prototype.* deliberately don't coerce |this| value to object. + auto this_value = vm.this_value(global_object); + if (!this_value.is_object()) + return nullptr; + auto& this_object = this_value.as_object(); + if (!this_object.is_array_buffer()) { + vm.throw_exception(global_object, ErrorType::NotAn, "ArrayBuffer"); + return nullptr; + } + return static_cast(&this_object); +} + +JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice) +{ + auto array_buffer_object = array_buffer_object_from(vm, global_object); + if (!array_buffer_object) + return {}; + TODO(); +} + +JS_DEFINE_NATIVE_GETTER(ArrayBufferPrototype::byte_length_getter) +{ + auto array_buffer_object = array_buffer_object_from(vm, global_object); + if (!array_buffer_object) + return {}; + // FIXME: Check for shared buffer + // FIXME: Check for detached buffer + return Value((double)array_buffer_object->byte_length()); +} + +} diff --git a/Libraries/LibJS/Runtime/ArrayBufferPrototype.h b/Libraries/LibJS/Runtime/ArrayBufferPrototype.h new file mode 100644 index 0000000000..96ed852830 --- /dev/null +++ b/Libraries/LibJS/Runtime/ArrayBufferPrototype.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020, Linus Groh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +namespace JS { + +class ArrayBufferPrototype final : public Object { + JS_OBJECT(ArrayBufferPrototype, Object); + +public: + explicit ArrayBufferPrototype(GlobalObject&); + virtual void initialize(GlobalObject&) override; + virtual ~ArrayBufferPrototype() override; + +private: + JS_DECLARE_NATIVE_FUNCTION(slice); + JS_DECLARE_NATIVE_GETTER(byte_length_getter); +}; + +} diff --git a/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Libraries/LibJS/Runtime/CommonPropertyNames.h index 63e3aa9691..0e22fee351 100644 --- a/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -67,6 +67,7 @@ namespace JS { P(asinh) \ P(atanh) \ P(bind) \ + P(byteLength) \ P(call) \ P(cbrt) \ P(ceil) \ @@ -144,6 +145,7 @@ namespace JS { P(isInteger) \ P(isNaN) \ P(isSafeInteger) \ + P(isView) \ P(join) \ P(keyFor) \ P(keys) \ diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index 921d1efc83..5f718b675a 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include #include #include @@ -127,6 +129,7 @@ void GlobalObject::initialize() define_property(vm.names.Reflect, heap().allocate(*this, *this), attr); add_constructor(vm.names.Array, m_array_constructor, m_array_prototype); + add_constructor(vm.names.ArrayBuffer, m_array_buffer_constructor, m_array_buffer_prototype); add_constructor(vm.names.BigInt, m_bigint_constructor, m_bigint_prototype); add_constructor(vm.names.Boolean, m_boolean_constructor, m_boolean_prototype); add_constructor(vm.names.Date, m_date_constructor, m_date_prototype); diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index 54705576f6..2e37b50dc4 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -130,6 +130,7 @@ public: virtual bool is_lexical_environment() const { return false; } virtual bool is_global_object() const { return false; } virtual bool is_typed_array() const { return false; } + virtual bool is_array_buffer() const { return false; } virtual const char* class_name() const override { return "Object"; } virtual void visit_edges(Cell::Visitor&) override; diff --git a/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.isView.js b/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.isView.js new file mode 100644 index 0000000000..c7056b88b2 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.isView.js @@ -0,0 +1,18 @@ +// Update when more typed arrays get added +const TYPED_ARRAYS = [Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array]; + +test("basic functionality", () => { + expect(ArrayBuffer.isView).toHaveLength(1); + + expect(ArrayBuffer.isView()).toBeFalse(); + expect(ArrayBuffer.isView(null)).toBeFalse(); + expect(ArrayBuffer.isView(undefined)).toBeFalse(); + expect(ArrayBuffer.isView([])).toBeFalse(); + expect(ArrayBuffer.isView({})).toBeFalse(); + expect(ArrayBuffer.isView(123)).toBeFalse(); + expect(ArrayBuffer.isView("foo")).toBeFalse(); + expect(ArrayBuffer.isView(new ArrayBuffer())).toBeFalse(); + TYPED_ARRAYS.forEach(T => { + expect(ArrayBuffer.isView(new T())).toBeTrue(); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.js b/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.js new file mode 100644 index 0000000000..8fb05a250c --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.js @@ -0,0 +1,13 @@ +test("basic functionality", () => { + expect(ArrayBuffer).toHaveLength(1); + expect(ArrayBuffer.name).toBe("ArrayBuffer"); + expect(ArrayBuffer.prototype.constructor).toBe(ArrayBuffer); + expect(new ArrayBuffer()).toBeInstanceOf(ArrayBuffer); + expect(typeof new ArrayBuffer()).toBe("object"); +}); + +test("ArrayBuffer constructor must be invoked with 'new'", () => { + expect(() => { + ArrayBuffer(); + }).toThrowWithMessage(TypeError, "ArrayBuffer constructor must be called with 'new'"); +}); diff --git a/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.prototype.byteLength.js b/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.prototype.byteLength.js new file mode 100644 index 0000000000..9880e597aa --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.prototype.byteLength.js @@ -0,0 +1,6 @@ +test("basic functionality", () => { + expect(new ArrayBuffer().byteLength).toBe(0); + expect(new ArrayBuffer(1).byteLength).toBe(1); + expect(new ArrayBuffer(64).byteLength).toBe(64); + expect(new ArrayBuffer(123).byteLength).toBe(123); +});