From e6d12f835c16483fa862a4e71d5b25a29675dcf4 Mon Sep 17 00:00:00 2001 From: lupd <93457935+lupd@users.noreply.github.com> Date: Wed, 6 Apr 2022 14:56:46 +0000 Subject: [PATCH] Change `ArrayBuffer` `byteLength` to accessor property (#2010) This Pull Request fixes `byteLength` for `ArrayBuffer`. It should be an accessor property rather than a method, per the spec. It changes the following: - Removes `byteLength` method for `ArrayBuffer` built-in. - Add `byteLength` accessor property for `ArrayBuffer`. - Change `byte_length` function name to `get_byte_length`, to match other function names used for accessor properties. --- boa_engine/src/builtins/array_buffer/mod.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/boa_engine/src/builtins/array_buffer/mod.rs b/boa_engine/src/builtins/array_buffer/mod.rs index d05e7b8240..3f0524017d 100644 --- a/boa_engine/src/builtins/array_buffer/mod.rs +++ b/boa_engine/src/builtins/array_buffer/mod.rs @@ -37,11 +37,17 @@ impl BuiltIn for ArrayBuffer { fn init(context: &mut Context) -> Option { let _timer = Profiler::global().start_event(Self::NAME, "init"); + let flag_attributes = Attribute::CONFIGURABLE | Attribute::NON_ENUMERABLE; + let get_species = FunctionBuilder::native(context, Self::get_species) .name("get [Symbol.species]") .constructor(false) .build(); + let get_byte_length = FunctionBuilder::native(context, Self::get_byte_length) + .name("get byteLength") + .build(); + ConstructorBuilder::with_standard_constructor( context, Self::constructor, @@ -49,6 +55,7 @@ impl BuiltIn for ArrayBuffer { ) .name(Self::NAME) .length(Self::LENGTH) + .accessor("byteLength", Some(get_byte_length), None, flag_attributes) .static_accessor( WellKnownSymbols::species(), Some(get_species), @@ -56,7 +63,6 @@ impl BuiltIn for ArrayBuffer { Attribute::CONFIGURABLE, ) .static_method(Self::is_view, "isView", 1) - .method(Self::byte_length, "byteLength", 0) .method(Self::slice, "slice", 2) .property( WellKnownSymbols::to_string_tag(), @@ -133,7 +139,11 @@ impl ArrayBuffer { /// - [ECMAScript reference][spec] /// /// [spec]: https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength - fn byte_length(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult { + fn get_byte_length( + this: &JsValue, + _args: &[JsValue], + context: &mut Context, + ) -> JsResult { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). let obj = if let Some(obj) = this.as_object() {