2020-04-10 08:11:40 +03:00
|
|
|
#ifndef INTERNAL_ARRAY_H /*-*-C-*-vi:se ft=c:*/
|
|
|
|
#define INTERNAL_ARRAY_H
|
|
|
|
/**
|
2020-04-08 07:28:13 +03:00
|
|
|
* @author Ruby developers <ruby-core@ruby-lang.org>
|
2019-11-29 09:18:34 +03:00
|
|
|
* @copyright This file is a part of the programming language Ruby.
|
|
|
|
* Permission is hereby granted, to either redistribute and/or
|
|
|
|
* modify this file, provided that the conditions mentioned in the
|
|
|
|
* file COPYING are met. Consult the file for details.
|
2020-04-08 07:28:13 +03:00
|
|
|
* @brief Internal header for Array.
|
2019-11-29 09:18:34 +03:00
|
|
|
*/
|
2020-05-08 12:31:09 +03:00
|
|
|
#include "ruby/internal/config.h"
|
2019-12-03 09:32:02 +03:00
|
|
|
#include <stddef.h> /* for size_t */
|
|
|
|
#include "internal/static_assert.h" /* for STATIC_ASSERT */
|
2020-05-08 12:31:09 +03:00
|
|
|
#include "ruby/internal/stdbool.h" /* for bool */
|
2019-12-03 09:32:02 +03:00
|
|
|
#include "ruby/ruby.h" /* for RARRAY_LEN */
|
2019-11-29 09:18:34 +03:00
|
|
|
|
|
|
|
#ifndef ARRAY_DEBUG
|
2019-12-03 09:32:02 +03:00
|
|
|
# define ARRAY_DEBUG (0+RUBY_DEBUG)
|
2019-11-29 09:18:34 +03:00
|
|
|
#endif
|
|
|
|
|
2022-07-20 22:34:42 +03:00
|
|
|
#define RARRAY_SHARED_FLAG ELTS_SHARED
|
2022-07-20 22:30:19 +03:00
|
|
|
#define RARRAY_SHARED_ROOT_FLAG FL_USER12
|
|
|
|
#define RARRAY_PTR_IN_USE_FLAG FL_USER14
|
2019-11-29 09:18:34 +03:00
|
|
|
|
2019-12-03 09:32:02 +03:00
|
|
|
/* array.c */
|
Emit special instruction for array literal + .(hash|min|max)
This commit introduces a new instruction `opt_newarray_send` which is
used when there is an array literal followed by either the `hash`,
`min`, or `max` method.
```
[a, b, c].hash
```
Will emit an `opt_newarray_send` instruction. This instruction falls
back to a method call if the "interested" method has been monkey
patched.
Here are some examples of the instructions generated:
```
$ ./miniruby --dump=insns -e '[@a, @b].max'
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
0000 getinstancevariable :@a, <is:0> ( 1)[Li]
0003 getinstancevariable :@b, <is:1>
0006 opt_newarray_send 2, :max
0009 leave
$ ./miniruby --dump=insns -e '[@a, @b].min'
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
0000 getinstancevariable :@a, <is:0> ( 1)[Li]
0003 getinstancevariable :@b, <is:1>
0006 opt_newarray_send 2, :min
0009 leave
$ ./miniruby --dump=insns -e '[@a, @b].hash'
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,13)> (catch: FALSE)
0000 getinstancevariable :@a, <is:0> ( 1)[Li]
0003 getinstancevariable :@b, <is:1>
0006 opt_newarray_send 2, :hash
0009 leave
```
[Feature #18897] [ruby-core:109147]
Co-authored-by: John Hawthorn <jhawthorn@github.com>
2022-06-07 03:27:56 +03:00
|
|
|
VALUE rb_ary_hash_values(long len, const VALUE *elements);
|
2019-11-29 09:18:34 +03:00
|
|
|
VALUE rb_ary_last(int, const VALUE *, VALUE);
|
|
|
|
void rb_ary_set_len(VALUE, long);
|
|
|
|
void rb_ary_delete_same(VALUE, VALUE);
|
2022-07-25 17:40:45 +03:00
|
|
|
VALUE rb_ary_hidden_new_fill(long capa);
|
2019-11-29 09:18:34 +03:00
|
|
|
VALUE rb_ary_at(VALUE, VALUE);
|
|
|
|
size_t rb_ary_memsize(VALUE);
|
|
|
|
VALUE rb_to_array_type(VALUE obj);
|
2021-02-12 05:47:04 +03:00
|
|
|
VALUE rb_to_array(VALUE obj);
|
2020-12-01 05:14:36 +03:00
|
|
|
void rb_ary_cancel_sharing(VALUE ary);
|
2022-06-09 17:59:08 +03:00
|
|
|
size_t rb_ary_size_as_embedded(VALUE ary);
|
|
|
|
void rb_ary_make_embedded(VALUE ary);
|
|
|
|
bool rb_ary_embeddable_p(VALUE ary);
|
2022-09-30 12:23:19 +03:00
|
|
|
VALUE rb_ary_diff(VALUE ary1, VALUE ary2);
|
2020-12-01 05:14:36 +03:00
|
|
|
|
2019-12-03 09:32:02 +03:00
|
|
|
static inline VALUE rb_ary_entry_internal(VALUE ary, long offset);
|
|
|
|
static inline bool ARY_PTR_USING_P(VALUE ary);
|
|
|
|
static inline void RARY_TRANSIENT_SET(VALUE ary);
|
|
|
|
static inline void RARY_TRANSIENT_UNSET(VALUE ary);
|
|
|
|
|
2019-11-29 09:18:34 +03:00
|
|
|
VALUE rb_ary_tmp_new_from_values(VALUE, long, const VALUE *);
|
2019-12-03 09:32:02 +03:00
|
|
|
VALUE rb_check_to_array(VALUE ary);
|
2019-11-29 09:18:34 +03:00
|
|
|
VALUE rb_ary_behead(VALUE, long);
|
2019-12-03 09:32:02 +03:00
|
|
|
VALUE rb_ary_aref1(VALUE ary, VALUE i);
|
2020-12-06 09:41:21 +03:00
|
|
|
|
|
|
|
struct rb_execution_context_struct;
|
|
|
|
VALUE rb_ec_ary_new_from_values(struct rb_execution_context_struct *ec, long n, const VALUE *elts);
|
2019-11-29 09:18:34 +03:00
|
|
|
|
2021-10-18 18:30:18 +03:00
|
|
|
// YJIT needs this function to never allocate and never raise
|
2019-11-29 09:18:34 +03:00
|
|
|
static inline VALUE
|
|
|
|
rb_ary_entry_internal(VALUE ary, long offset)
|
|
|
|
{
|
|
|
|
long len = RARRAY_LEN(ary);
|
|
|
|
const VALUE *ptr = RARRAY_CONST_PTR_TRANSIENT(ary);
|
|
|
|
if (len == 0) return Qnil;
|
|
|
|
if (offset < 0) {
|
|
|
|
offset += len;
|
|
|
|
if (offset < 0) return Qnil;
|
|
|
|
}
|
|
|
|
else if (len <= offset) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return ptr[offset];
|
|
|
|
}
|
|
|
|
|
2019-12-03 09:32:02 +03:00
|
|
|
static inline bool
|
|
|
|
ARY_PTR_USING_P(VALUE ary)
|
|
|
|
{
|
|
|
|
return FL_TEST_RAW(ary, RARRAY_PTR_IN_USE_FLAG);
|
|
|
|
}
|
|
|
|
|
2022-07-20 22:30:19 +03:00
|
|
|
RBIMPL_ATTR_MAYBE_UNUSED()
|
|
|
|
static inline int
|
|
|
|
ary_should_not_be_shared_and_embedded(VALUE ary)
|
|
|
|
{
|
2022-07-20 22:34:42 +03:00
|
|
|
return !FL_ALL_RAW(ary, RARRAY_SHARED_FLAG|RARRAY_EMBED_FLAG);
|
2022-07-20 22:30:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
ARY_SHARED_P(VALUE ary)
|
|
|
|
{
|
|
|
|
assert(RB_TYPE_P(ary, T_ARRAY));
|
|
|
|
assert(ary_should_not_be_shared_and_embedded(ary));
|
2022-07-20 22:34:42 +03:00
|
|
|
return FL_TEST_RAW(ary, RARRAY_SHARED_FLAG);
|
2022-07-20 22:30:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
ARY_EMBED_P(VALUE ary)
|
|
|
|
{
|
|
|
|
assert(RB_TYPE_P(ary, T_ARRAY));
|
|
|
|
assert(ary_should_not_be_shared_and_embedded(ary));
|
|
|
|
return FL_TEST_RAW(ary, RARRAY_EMBED_FLAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
|
|
|
ARY_SHARED_ROOT(VALUE ary)
|
|
|
|
{
|
|
|
|
assert(ARY_SHARED_P(ary));
|
|
|
|
return RARRAY(ary)->as.heap.aux.shared_root;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
ARY_SHARED_ROOT_P(VALUE ary)
|
|
|
|
{
|
|
|
|
assert(RB_TYPE_P(ary, T_ARRAY));
|
|
|
|
return FL_TEST_RAW(ary, RARRAY_SHARED_ROOT_FLAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline long
|
|
|
|
ARY_SHARED_ROOT_REFCNT(VALUE ary)
|
|
|
|
{
|
|
|
|
assert(ARY_SHARED_ROOT_P(ary));
|
|
|
|
return RARRAY(ary)->as.heap.aux.capa;
|
|
|
|
}
|
|
|
|
|
2019-12-03 09:32:02 +03:00
|
|
|
static inline void
|
|
|
|
RARY_TRANSIENT_SET(VALUE ary)
|
|
|
|
{
|
|
|
|
#if USE_TRANSIENT_HEAP
|
|
|
|
FL_SET_RAW(ary, RARRAY_TRANSIENT_FLAG);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
RARY_TRANSIENT_UNSET(VALUE ary)
|
|
|
|
{
|
|
|
|
#if USE_TRANSIENT_HEAP
|
|
|
|
FL_UNSET_RAW(ary, RARRAY_TRANSIENT_FLAG);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-12-04 11:16:30 +03:00
|
|
|
#undef rb_ary_new_from_args
|
2020-05-04 09:52:56 +03:00
|
|
|
#if RBIMPL_HAS_WARNING("-Wgnu-zero-variadic-macro-arguments")
|
2020-04-08 07:28:13 +03:00
|
|
|
# /* Skip it; clang -pedantic doesn't like the following */
|
|
|
|
#elif defined(__GNUC__) && defined(HAVE_VA_ARGS_MACRO)
|
2019-12-03 09:32:02 +03:00
|
|
|
#define rb_ary_new_from_args(n, ...) \
|
|
|
|
__extension__ ({ \
|
|
|
|
const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
|
|
|
|
if (__builtin_constant_p(n)) { \
|
|
|
|
STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
|
|
|
|
} \
|
|
|
|
rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
|
|
|
|
})
|
|
|
|
#endif
|
2019-11-29 09:18:34 +03:00
|
|
|
|
2020-08-14 08:45:23 +03:00
|
|
|
#undef RARRAY_AREF
|
|
|
|
RBIMPL_ATTR_PURE_UNLESS_DEBUG()
|
|
|
|
RBIMPL_ATTR_ARTIFICIAL()
|
|
|
|
static inline VALUE
|
|
|
|
RARRAY_AREF(VALUE ary, long i)
|
|
|
|
{
|
|
|
|
RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
|
|
|
|
|
|
|
|
return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
|
|
|
|
}
|
|
|
|
|
2019-11-29 09:18:34 +03:00
|
|
|
#endif /* INTERNAL_ARRAY_H */
|