| 1 |
#ifndef RBIMPL_INTERN_PROC_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_PROC_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs related to ::rb_cProc. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/dllexport.h" |
| 24 |
#include "ruby/internal/iterator.h" |
| 25 |
#include "ruby/internal/value.h" |
| 26 |
|
| 27 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 28 |
|
| 29 |
/* proc.c */ |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructs a Proc object from implicitly passed components. When a ruby |
| 33 |
* method is called with a block, that block is not explicitly passed around |
| 34 |
* using C level function parameters. This function gathers all the necessary |
| 35 |
* info to turn them into a Ruby level instance of ::rb_cProc. |
| 36 |
* |
| 37 |
* @exception rb_eArgError There is no passed block. |
| 38 |
* @return An instance of ::rb_cProc. |
| 39 |
*/ |
| 40 |
VALUE rb_block_proc(void); |
| 41 |
|
| 42 |
/** |
| 43 |
* Identical to rb_proc_new(), except it returns a lambda. |
| 44 |
* |
| 45 |
* @exception rb_eArgError There is no passed block. |
| 46 |
* @return An instance of ::rb_cProc. |
| 47 |
*/ |
| 48 |
VALUE rb_block_lambda(void); |
| 49 |
|
| 50 |
/** |
| 51 |
* This is an rb_iterate() + rb_block_proc() combo. |
| 52 |
* |
| 53 |
* ```CXX |
| 54 |
* VALUE |
| 55 |
* my_own_iterator(RB_BLOCK_CALL_FUNC_ARGLIST(y, c)) |
| 56 |
* { |
| 57 |
* const auto plus = rb_intern("+"); |
| 58 |
* return rb_funcall(c, plus, 1, y); |
| 59 |
* } |
| 60 |
* |
| 61 |
* VALUE |
| 62 |
* my_own_method(VALUE self) |
| 63 |
* { |
| 64 |
* return rb_proc_new(my_own_iterator, self); |
| 65 |
* } |
| 66 |
* ``` |
| 67 |
* |
| 68 |
* @param[in] func A backend function of a proc. |
| 69 |
* @param[in] callback_arg Passed to `func`'s callback_arg. |
| 70 |
* @return A C-backended proc object. |
| 71 |
* |
| 72 |
*/ |
| 73 |
VALUE rb_proc_new(rb_block_call_func_t func, VALUE callback_arg); |
| 74 |
|
| 75 |
/** |
| 76 |
* Queries if the given object is a proc. |
| 77 |
* |
| 78 |
* @note This is about the object's data structure, not its class etc. |
| 79 |
* @param[in] recv Object in question. |
| 80 |
* @retval RUBY_Qtrue It is a proc. |
| 81 |
* @retval RUBY_Qfalse Otherwise. |
| 82 |
*/ |
| 83 |
VALUE rb_obj_is_proc(VALUE recv); |
| 84 |
|
| 85 |
/** |
| 86 |
* Evaluates the passed proc with the passed arguments. |
| 87 |
* |
| 88 |
* @param[in] recv The proc to call. |
| 89 |
* @param[in] args An instance of ::RArray which is the arguments. |
| 90 |
* @exception rb_eException Any exceptions happen inside. |
| 91 |
* @return What the proc evaluates to. |
| 92 |
*/ |
| 93 |
VALUE rb_proc_call(VALUE recv, VALUE args); |
| 94 |
|
| 95 |
/** |
| 96 |
* Identical to rb_proc_call(), except you can specify how to handle the last |
| 97 |
* element of the given array. |
| 98 |
* |
| 99 |
* @param[in] recv The proc to call. |
| 100 |
* @param[in] args An instance of ::RArray which is the arguments. |
| 101 |
* @param[in] kw_splat Handling of keyword parameters: |
| 102 |
* - RB_NO_KEYWORDS `args`' last is not a keyword argument. |
| 103 |
* - RB_PASS_KEYWORDS `args`' last is a keyword argument. |
| 104 |
* - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block. |
| 105 |
* @exception rb_eException Any exceptions happen inside. |
| 106 |
* @return What the proc evaluates to. |
| 107 |
*/ |
| 108 |
VALUE rb_proc_call_kw(VALUE recv, VALUE args, int kw_splat); |
| 109 |
|
| 110 |
/** |
| 111 |
* Identical to rb_proc_call(), except you can additionally pass another proc |
| 112 |
* object, as a block. Nowadays procs can take blocks: |
| 113 |
* |
| 114 |
* ```ruby |
| 115 |
* l = -> (positional, optional=nil, *rest, kwarg:, **kwrest, &block) { |
| 116 |
* # ... how can we pass this `&block`? ^^^^^^ |
| 117 |
* } |
| 118 |
* ``` |
| 119 |
* |
| 120 |
* And this function is to pass one to such procs. |
| 121 |
* |
| 122 |
* @param[in] recv The proc to call. |
| 123 |
* @param[in] argc Number of arguments. |
| 124 |
* @param[in] argv Arbitrary number of proc arguments. |
| 125 |
* @param[in] proc Proc as a passed block. |
| 126 |
* @exception rb_eException Any exceptions happen inside. |
| 127 |
* @return What the proc evaluates to. |
| 128 |
*/ |
| 129 |
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc); |
| 130 |
|
| 131 |
/** |
| 132 |
* Identical to rb_proc_call_with_block(), except you can specify how to handle |
| 133 |
* the last element of the given array. It can also be seen as a routine |
| 134 |
* identical to rb_proc_call_kw(), except you can additionally pass another |
| 135 |
* proc object as a block. |
| 136 |
* |
| 137 |
* @param[in] recv The proc to call. |
| 138 |
* @param[in] argc Number of arguments. |
| 139 |
* @param[in] argv Arbitrary number of proc arguments. |
| 140 |
* @param[in] proc Proc as a passed block. |
| 141 |
* @param[in] kw_splat Handling of keyword parameters: |
| 142 |
* - RB_NO_KEYWORDS `args`' last is not a keyword argument. |
| 143 |
* - RB_PASS_KEYWORDS `args`' last is a keyword argument. |
| 144 |
* - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block. |
| 145 |
* @exception rb_eException Any exceptions happen inside. |
| 146 |
* @return What the proc evaluates to. |
| 147 |
*/ |
| 148 |
VALUE rb_proc_call_with_block_kw(VALUE recv, int argc, const VALUE *argv, VALUE proc, int kw_splat); |
| 149 |
|
| 150 |
/** |
| 151 |
* Queries the number of mandatory arguments of the given Proc. If its block |
| 152 |
* is declared to take no arguments, returns `0`. If the block is known to |
| 153 |
* take exactly `n` arguments, returns `n`. If the block has optional |
| 154 |
* arguments, returns `-n-1`, where `n` is the number of mandatory arguments, |
| 155 |
* with the exception for blocks that are not lambdas and have only a finite |
| 156 |
* number of optional arguments; in this latter case, returns `n`. Keyword |
| 157 |
* arguments will be considered as a single additional argument, that argument |
| 158 |
* being mandatory if any keyword argument is mandatory. |
| 159 |
* |
| 160 |
* @param[in] recv Target Proc object. |
| 161 |
* @retval 0 It takes no arguments. |
| 162 |
* @retval >0 It takes exactly this number of arguments. |
| 163 |
* @retval <0 It takes optional arguments. |
| 164 |
*/ |
| 165 |
int rb_proc_arity(VALUE recv); |
| 166 |
|
| 167 |
/** |
| 168 |
* Queries if the given object is a lambda. Instances of ::rb_cProc are either |
| 169 |
* lambda or proc. They differ in several points. This function can |
| 170 |
* distinguish them without actually evaluating their contents. |
| 171 |
* |
| 172 |
* @param[in] recv Target proc object. |
| 173 |
* @retval RUBY_Qtrue It is a lambda. |
| 174 |
* @retval RUBY_Qfalse Otherwise. |
| 175 |
*/ |
| 176 |
VALUE rb_proc_lambda_p(VALUE recv); |
| 177 |
|
| 178 |
/** |
| 179 |
* Snapshots the current execution context and turn it into an instance of |
| 180 |
* ::rb_cBinding. |
| 181 |
* |
| 182 |
* @return An instance of ::rb_cBinding. |
| 183 |
*/ |
| 184 |
VALUE rb_binding_new(void); |
| 185 |
|
| 186 |
/** |
| 187 |
* Creates a method object. A method object is a proc-like object that you can |
| 188 |
* "call". Note that a method object snapshots the method at the time the |
| 189 |
* object is created: |
| 190 |
* |
| 191 |
* ```ruby |
| 192 |
* class Foo |
| 193 |
* def foo |
| 194 |
* return 1 |
| 195 |
* end |
| 196 |
* end |
| 197 |
* |
| 198 |
* obj = Foo.new.method(:foo) |
| 199 |
* |
| 200 |
* class Foo |
| 201 |
* def foo |
| 202 |
* return 2 |
| 203 |
* end |
| 204 |
* end |
| 205 |
* |
| 206 |
* obj.call # => 1, not 2. |
| 207 |
* ``` |
| 208 |
* |
| 209 |
* @param[in] recv Receiver of the method. |
| 210 |
* @param[in] mid Method name, in either String or Symbol. |
| 211 |
* @exception rb_eNoMethodError No such method. |
| 212 |
* @return An instance of ::rb_cMethod. |
| 213 |
*/ |
| 214 |
VALUE rb_obj_method(VALUE recv, VALUE mid); |
| 215 |
|
| 216 |
/** |
| 217 |
* Queries if the given object is a method. |
| 218 |
* |
| 219 |
* @note This is about the object's data structure, not its class etc. |
| 220 |
* @param[in] recv Object in question. |
| 221 |
* @retval RUBY_Qtrue It is a method. |
| 222 |
* @retval RUBY_Qfalse Otherwise. |
| 223 |
*/ |
| 224 |
VALUE rb_obj_is_method(VALUE recv); |
| 225 |
|
| 226 |
/** |
| 227 |
* Evaluates the passed method with the passed arguments. |
| 228 |
* |
| 229 |
* @param[in] argc Number of objects of `argv`. |
| 230 |
* @param[in] argv Arbitrary number of method arguments. |
| 231 |
* @param[in] recv The method object to call. |
| 232 |
* @exception rb_eTypeError `recv` is not a method. |
| 233 |
* @exception rb_eException Any exceptions happen inside. |
| 234 |
* @return What the method returns. |
| 235 |
*/ |
| 236 |
VALUE rb_method_call(int argc, const VALUE *argv, VALUE recv); |
| 237 |
|
| 238 |
/** |
| 239 |
* Identical to rb_method_call(), except you can specify how to handle the last |
| 240 |
* element of the given array. |
| 241 |
* |
| 242 |
* @param[in] argc Number of objects of `argv`. |
| 243 |
* @param[in] argv Arbitrary number of method arguments. |
| 244 |
* @param[in] recv The method object to call. |
| 245 |
* @param[in] kw_splat Handling of keyword parameters: |
| 246 |
* - RB_NO_KEYWORDS `args`' last is not a keyword argument. |
| 247 |
* - RB_PASS_KEYWORDS `args`' last is a keyword argument. |
| 248 |
* - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block. |
| 249 |
* @exception rb_eTypeError `recv` is not a method. |
| 250 |
* @exception rb_eException Any exceptions happen inside. |
| 251 |
* @return What the method returns. |
| 252 |
*/ |
| 253 |
VALUE rb_method_call_kw(int argc, const VALUE *argv, VALUE recv, int kw_splat); |
| 254 |
|
| 255 |
/** |
| 256 |
* Identical to rb_proc_call(), except you can additionally pass a proc as a |
| 257 |
* block. |
| 258 |
* |
| 259 |
* @param[in] argc Number of objects of `argv`. |
| 260 |
* @param[in] argv Arbitrary number of method arguments. |
| 261 |
* @param[in] recv The method object to call. |
| 262 |
* @param[in] proc Proc as a passed block. |
| 263 |
* @exception rb_eTypeError `recv` is not a method. |
| 264 |
* @exception rb_eException Any exceptions happen inside. |
| 265 |
* @return What the method returns. |
| 266 |
*/ |
| 267 |
VALUE rb_method_call_with_block(int argc, const VALUE *argv, VALUE recv, VALUE proc); |
| 268 |
|
| 269 |
/** |
| 270 |
* Identical to rb_method_call_with_block(), except you can specify how to |
| 271 |
* handle the last element of the given array. It can also be seen as a |
| 272 |
* routine identical to rb_method_call_kw(), except you can additionally pass |
| 273 |
* another proc object as a block. |
| 274 |
* |
| 275 |
* @param[in] argc Number of objects of `argv`. |
| 276 |
* @param[in] argv Arbitrary number of method arguments. |
| 277 |
* @param[in] recv The method object to call. |
| 278 |
* @param[in] proc Proc as a passed block. |
| 279 |
* @param[in] kw_splat Handling of keyword parameters: |
| 280 |
* - RB_NO_KEYWORDS `args`' last is not a keyword argument. |
| 281 |
* - RB_PASS_KEYWORDS `args`' last is a keyword argument. |
| 282 |
* - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block. |
| 283 |
* @exception rb_eTypeError `recv` is not a method. |
| 284 |
* @exception rb_eException Any exceptions happen inside. |
| 285 |
* @return What the method returns. |
| 286 |
*/ |
| 287 |
VALUE rb_method_call_with_block_kw(int argc, const VALUE *argv, VALUE recv, VALUE proc, int kw_splat); |
| 288 |
|
| 289 |
/** |
| 290 |
* Queries the number of mandatory arguments of the method defined in the given |
| 291 |
* module. If it is declared to take no arguments, returns `0`. If it takes |
| 292 |
* exactly `n` arguments, returns `n`. If it has optional arguments, returns |
| 293 |
* `-n-1`, where `n` is the number of mandatory arguments. Keyword arguments |
| 294 |
* will be considered as a single additional argument, that argument being |
| 295 |
* mandatory if any keyword argument is mandatory. |
| 296 |
* |
| 297 |
* @param[in] mod Namespace to search a method for. |
| 298 |
* @param[in] mid Method id. |
| 299 |
* @retval 0 It takes no arguments. |
| 300 |
* @retval >0 It takes exactly this number of arguments. |
| 301 |
* @retval <0 It takes optional arguments. |
| 302 |
*/ |
| 303 |
int rb_mod_method_arity(VALUE mod, ID mid); |
| 304 |
|
| 305 |
/** |
| 306 |
* Identical to rb_mod_method_arity(), except it searches for singleton methods |
| 307 |
* rather than instance methods. |
| 308 |
* |
| 309 |
* @param[in] obj Object to search for a singleton method. |
| 310 |
* @param[in] mid Method id. |
| 311 |
* @retval 0 It takes no arguments. |
| 312 |
* @retval >0 It takes exactly this number of arguments. |
| 313 |
* @retval <0 It takes optional arguments. |
| 314 |
*/ |
| 315 |
int rb_obj_method_arity(VALUE obj, ID mid); |
| 316 |
|
| 317 |
/* eval.c */ |
| 318 |
|
| 319 |
RBIMPL_ATTR_NONNULL((1)) |
| 320 |
/** |
| 321 |
* Protects a function call from potential global escapes from the function. |
| 322 |
* Such global escapes include exceptions, `throw`, `break`, for example. |
| 323 |
* |
| 324 |
* It first calls the function func with `args` as the argument. If no global |
| 325 |
* escape occurred during the function, it returns the result and `*state` is |
| 326 |
* zero. Otherwise, it returns ::RUBY_Qnil and sets `*state` to nonzero. If |
| 327 |
* `state` is `NULL`, it is not set in both cases. |
| 328 |
* |
| 329 |
* @param[in] func A function that potentially escapes globally. |
| 330 |
* @param[in] args Passed as-is to `func`. |
| 331 |
* @param[out] state State of execution. |
| 332 |
* @return What `func` returns, or an undefined value when it did not |
| 333 |
* return. |
| 334 |
* @post `*state` is set to zero if succeeded. Nonzero otherwise. |
| 335 |
* @warning You have to clear the error info with `rb_set_errinfo(Qnil)` if |
| 336 |
* you decide to ignore the caught exception. |
| 337 |
* @see rb_eval_string_protect() |
| 338 |
* @see rb_load_protect() |
| 339 |
* |
| 340 |
* @internal |
| 341 |
* |
| 342 |
* The "undefined value" described above is in fact ::RUBY_Qnil for now. But |
| 343 |
* @shyouhei doesn't think that we would never change that. |
| 344 |
* |
| 345 |
* Though not a part of our public API, `state` is in fact an |
| 346 |
* enum ruby_tag_type. You can see the potential "nonzero" values by looking |
| 347 |
* at vm_core.h. |
| 348 |
*/ |
| 349 |
VALUE rb_protect(VALUE (*func)(VALUE args), VALUE args, int *state); |
| 350 |
|
| 351 |
RBIMPL_SYMBOL_EXPORT_END() |
| 352 |
|
| 353 |
#endif /* RBIMPL_INTERN_PROC_H */ |