Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom dispatcher to Arraybuffer & Arraybuffer_prototype #4651

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"

/**
* This object has a custom dispatch function.
*/
#define BUILTIN_CUSTOM_DISPATCH

/**
* List of built-in routine identifiers.
*/
enum
{
ECMA_BUILTIN_ARRAYBUFFER_PROTOTYPE_ROUTINE_START = 0,
ECMA_BUILTIN_ARRAYBUFFER_PROTOTYPE_BYTELENGTH_GETTER,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which comments are missing?

ECMA_BUILTIN_ARRAYBUFFER_PROTOTYPE_OBJECT_SLICE,
};

#define BUILTIN_INC_HEADER_NAME "ecma-builtin-arraybuffer-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID arraybuffer_prototype
#include "ecma-builtin-internal-routines-template.inc.h"
Expand All @@ -56,25 +71,18 @@
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_arraybuffer_prototype_bytelength_getter (ecma_value_t this_arg) /**< this argument */
ecma_builtin_arraybuffer_prototype_bytelength_getter (ecma_value_t this_arg, /**< this argument */
ecma_object_t *object_p) /**< object value*/
{
if (ecma_is_value_object (this_arg))
JERRY_UNUSED (this_arg);
if (ecma_arraybuffer_is_detached (object_p))
{
ecma_object_t *object_p = ecma_get_object_from_value (this_arg);

if (ecma_object_class_is (object_p, ECMA_OBJECT_CLASS_ARRAY_BUFFER))
{
if (ecma_arraybuffer_is_detached (object_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
uint32_t len = ecma_arraybuffer_get_length (object_p);

return ecma_make_uint32_value (len);
}
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
uint32_t len = ecma_arraybuffer_get_length (object_p);

return ecma_make_uint32_value (len);

return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a ArrayBuffer object"));
} /* ecma_builtin_arraybuffer_prototype_bytelength_getter */

/**
Expand All @@ -90,6 +98,22 @@ static ecma_value_t
ecma_builtin_arraybuffer_prototype_object_slice (ecma_value_t this_arg, /**< this argument */
const ecma_value_t *argument_list_p, /**< arguments list */
uint32_t arguments_number) /**< number of arguments */
{
return ecma_builtin_arraybuffer_slice (this_arg, argument_list_p, arguments_number);
} /* ecma_builtin_arraybuffer_prototype_object_slice */

/**
* Dispatcher of the built-in's routines
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
ecma_value_t
ecma_builtin_arraybuffer_prototype_dispatch_routine (uint8_t builtin_routine_id, /**< built-in routine identifier */
ecma_value_t this_arg, /**< 'this' argument value */
const ecma_value_t arguments_list_p[], /**< list of arguments
* passed to routine */
uint32_t arguments_number) /**< length of arguments' list */
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checks from 108-119 lines (numbering based on the new line counts) seems to be similar in both methods. AFAIK we could extract those changes to this place, just before the switch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second, move the isArrayBuffer validation from the methods to the dispatcher.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems still mising.

if (!ecma_is_value_object (this_arg))
{
Expand All @@ -104,8 +128,22 @@ ecma_builtin_arraybuffer_prototype_object_slice (ecma_value_t this_arg, /**< thi
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an ArrayBuffer object"));
}

return ecma_builtin_arraybuffer_slice (this_arg, argument_list_p, arguments_number);
} /* ecma_builtin_arraybuffer_prototype_object_slice */
switch (builtin_routine_id)
{
case ECMA_BUILTIN_ARRAYBUFFER_PROTOTYPE_BYTELENGTH_GETTER:
{
return ecma_builtin_arraybuffer_prototype_bytelength_getter (this_arg, object_p);
}
case ECMA_BUILTIN_ARRAYBUFFER_PROTOTYPE_OBJECT_SLICE:
{
return ecma_builtin_arraybuffer_prototype_object_slice (this_arg, arguments_list_p, arguments_number);
}
default:
{
JERRY_UNREACHABLE ();
}
}
} /* ecma_builtin_arraybuffer_prototype_dispatch_routine */

/**
* @}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR, ECMA_BUILTIN_ID_ARRAYBUFFER, ECMA_PR

/* Readonly accessor properties */
ACCESSOR_READ_ONLY (LIT_MAGIC_STRING_BYTE_LENGTH_UL,
ecma_builtin_arraybuffer_prototype_bytelength_getter,
ECMA_BUILTIN_ARRAYBUFFER_PROTOTYPE_BYTELENGTH_GETTER,
ECMA_PROPERTY_FLAG_CONFIGURABLE)

/* ECMA-262 v6, 24.1.4.4 */
STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG, LIT_MAGIC_STRING_ARRAY_BUFFER_UL, ECMA_PROPERTY_FLAG_CONFIGURABLE)

/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_SLICE, ecma_builtin_arraybuffer_prototype_object_slice, NON_FIXED, 2)
ROUTINE (LIT_MAGIC_STRING_SLICE, ECMA_BUILTIN_ARRAYBUFFER_PROTOTYPE_OBJECT_SLICE, NON_FIXED, 2)

#endif /* JERRY_BUILTIN_TYPEDARRAY */

Expand Down
53 changes: 43 additions & 10 deletions jerry-core/ecma/builtin-objects/ecma-builtin-arraybuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"

/**
* This object has a custom dispatch function.
*/
#define BUILTIN_CUSTOM_DISPATCH

/**
* List of built-in routine identifiers.
*/
enum
{
ECMA_BUILTIN_ARRAYBUFFER_ROUTINE_START = 0,
ECMA_BUILTIN_ARRAYBUFFER_OBJECT_IS_VIEW,
ECMA_BUILTIN_ARRAYBUFFER_SPECIES_GET,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which comments are missing?

};

#define BUILTIN_INC_HEADER_NAME "ecma-builtin-arraybuffer.inc.h"
#define BUILTIN_UNDERSCORED_ID arraybuffer
#include "ecma-builtin-internal-routines-template.inc.h"
Expand All @@ -53,11 +68,8 @@
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_arraybuffer_object_is_view (ecma_value_t this_arg, /**< 'this' argument */
ecma_value_t arg) /**< argument 1 */
ecma_builtin_arraybuffer_object_is_view (ecma_value_t arg) /**< argument 1 */
{
JERRY_UNUSED (this_arg);

return ecma_make_boolean_value (ecma_is_typedarray (arg) || ecma_is_dataview (arg));
} /* ecma_builtin_arraybuffer_object_is_view */

Expand Down Expand Up @@ -94,16 +106,37 @@ ecma_builtin_arraybuffer_dispatch_construct (const ecma_value_t *arguments_list_
} /* ecma_builtin_arraybuffer_dispatch_construct */

/**
* 24.1.3.3 get ArrayBuffer [ @@species ] accessor
* Dispatcher of the built-in's routines
*
* @return ecma_value
* returned value must be freed with ecma_free_value
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
ecma_value_t
ecma_builtin_arraybuffer_species_get (ecma_value_t this_value) /**< This Value */
ecma_builtin_arraybuffer_dispatch_routine (uint8_t builtin_routine_id, /**< built-in wide routine identifier */
ecma_value_t this_arg, /**< 'this' argument value */
const ecma_value_t arguments_list_p[], /**< list of arguments
* passed to routine */
uint32_t arguments_number) /**< length of arguments' list */
{
return ecma_copy_value (this_value);
} /* ecma_builtin_arraybuffer_species_get */
JERRY_UNUSED (arguments_number);

switch (builtin_routine_id)
{
case ECMA_BUILTIN_ARRAYBUFFER_OBJECT_IS_VIEW:
{
ecma_value_t argument = arguments_number > 0 ? arguments_list_p[0] : ECMA_VALUE_UNDEFINED;
return ecma_builtin_arraybuffer_object_is_view (argument);
}
case ECMA_BUILTIN_ARRAYBUFFER_SPECIES_GET:
{
return ecma_copy_value (this_arg);
}
default:
{
JERRY_UNREACHABLE ();
}
}
} /* ecma_builtin_arraybuffer_dispatch_routine */

/**
* @}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ STRING_VALUE (LIT_MAGIC_STRING_NAME, LIT_MAGIC_STRING_ARRAY_BUFFER_UL, ECMA_PROP
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */

/* ES2015 24.1.3.1 */
ROUTINE (LIT_MAGIC_STRING_IS_VIEW_UL, ecma_builtin_arraybuffer_object_is_view, 1, 1)
ROUTINE (LIT_MAGIC_STRING_IS_VIEW_UL, ECMA_BUILTIN_ARRAYBUFFER_OBJECT_IS_VIEW, 1, 1)

/* ES2015 24.1.3.3 */
ACCESSOR_READ_ONLY (LIT_GLOBAL_SYMBOL_SPECIES, ecma_builtin_arraybuffer_species_get, ECMA_PROPERTY_FLAG_CONFIGURABLE)
ACCESSOR_READ_ONLY (LIT_GLOBAL_SYMBOL_SPECIES, ECMA_BUILTIN_ARRAYBUFFER_SPECIES_GET, ECMA_PROPERTY_FLAG_CONFIGURABLE)

#endif /* JERRY_BUILTIN_TYPEDARRAY */

Expand Down