-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvariable.h
337 lines (310 loc) · 11.8 KB
/
variable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifndef RBIMPL_VARIABLE_H /*-*-C++-*-vi:se ft=cpp:*/
#define RBIMPL_VARIABLE_H
/**
* @file
* @author Ruby developers <ruby-core@ruby-lang.org>
* @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.
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
* implementation details. Don't take them as canon. They could
* rapidly appear then vanish. The name (path) of this header file
* is also an implementation detail. Do not expect it to persist
* at the place it is now. Developers are free to move it anywhere
* anytime at will.
* @note To ruby-core: remember that this header can be possibly
* recursively included from extension libraries written in C++.
* Do not expect for instance `__VA_ARGS__` is always available.
* We assume C99 for ruby itself but we don't assume languages of
* extension libraries. They could be written in C++98.
* @brief Declares rb_define_variable().
*/
#include "ruby/internal/dllexport.h"
#include "ruby/internal/value.h"
#include "ruby/internal/attr/nonnull.h"
#include "ruby/internal/attr/noreturn.h"
RBIMPL_SYMBOL_EXPORT_BEGIN()
/**
* Type that represents a global variable getter function.
*
* @param[in] id The variable name.
* @param[in,out] data Where the value is stored.
* @return The value that shall be visible from Ruby.
*/
typedef VALUE rb_gvar_getter_t(ID id, VALUE *data);
/**
* Type that represents a global variable setter function.
*
* @param[in] val The value to set.
* @param[in] id The variable name.
* @param[in,out] data Where the value is to be stored.
*/
typedef void rb_gvar_setter_t(VALUE val, ID id, VALUE *data);
/**
* Type that represents a global variable marker function.
*
* @param[in] var Where the value is to be stored.
*/
typedef void rb_gvar_marker_t(VALUE *var);
/**
* @deprecated
*
* This function has no actual usage (than in ruby itself). Please ignore. It
* was a bad idea to expose this function to 3rd parties, but we can no longer
* delete it.
*/
rb_gvar_getter_t rb_gvar_undef_getter;
/**
* @deprecated
*
* This function has no actual usage (than in ruby itself). Please ignore. It
* was a bad idea to expose this function to 3rd parties, but we can no longer
* delete it.
*/
rb_gvar_setter_t rb_gvar_undef_setter;
/**
* @deprecated
*
* This function has no actual usage (than in ruby itself). Please ignore. It
* was a bad idea to expose this function to 3rd parties, but we can no longer
* delete it.
*/
rb_gvar_marker_t rb_gvar_undef_marker;
/**
* This is the getter function that backs global variables defined from a ruby
* script. Extension libraries can use this if its global variable needs no
* custom logic.
*/
rb_gvar_getter_t rb_gvar_val_getter;
/**
* This is the setter function that backs global variables defined from a ruby
* script. Extension libraries can use this if its global variable needs no
* custom logic.
*/
rb_gvar_setter_t rb_gvar_val_setter;
/**
* This is the setter function that backs global variables defined from a ruby
* script. Extension libraries can use this if its global variable needs no
* custom logic.
*/
rb_gvar_marker_t rb_gvar_val_marker;
/**
* @deprecated
*
* This function has no actual usage (than in ruby itself). Please ignore. It
* was a bad idea to expose this function to 3rd parties, but we can no longer
* delete it.
*/
rb_gvar_getter_t rb_gvar_var_getter;
/**
* @deprecated
*
* This function has no actual usage (than in ruby itself). Please ignore. It
* was a bad idea to expose this function to 3rd parties, but we can no longer
* delete it.
*/
rb_gvar_setter_t rb_gvar_var_setter;
/**
* @deprecated
*
* This function has no actual usage (than in ruby itself). Please ignore. It
* was a bad idea to expose this function to 3rd parties, but we can no longer
* delete it.
*/
rb_gvar_marker_t rb_gvar_var_marker;
RBIMPL_ATTR_NORETURN()
/**
* This function just raises ::rb_eNameError. Handy when you want to prohibit
* a global variable from being squashed by someone.
*/
rb_gvar_setter_t rb_gvar_readonly_setter;
RBIMPL_ATTR_NONNULL(())
/**
* "Shares" a global variable between Ruby and C. Normally a Ruby-level global
* variable is stored somewhere deep inside of the interpreter's execution
* context, but this way you can explicitly specify its storage.
*
* ```CXX
* static VALUE foo;
*
* extern "C" void
* init_Foo(void)
* {
* foo = rb_eval_string("...");
* rb_define_variable("$foo", &foo);
* }
* ```
*
* In the above example a Ruby global variable named `$foo` is stored in a C
* global variable named `foo`.
*
* @param[in] name Variable (Ruby side).
* @param[in] var Variable (C side).
* @post Ruby level global variable named `name` is defined if absent,
* and its storage is set to `var`.
*/
void rb_define_variable(const char *name, VALUE *var);
RBIMPL_ATTR_NONNULL((1))
/**
* Defines a global variable that is purely function-backended. By using this
* API a programmer can define a global variable that dynamically changes from
* time to time.
*
* @param[in] name Variable name, in C's string.
* @param[in] getter A getter function.
* @param[in] setter A setter function.
* @post Ruby level global variable named `name` is defined if absent.
*
* @internal
*
* @shyouhei doesn't know if this is an Easter egg or an official feature, but
* you can pass 0 to the third argument (setter). That effectively nullifies
* any efforts to write to the defining global variable.
*/
void rb_define_virtual_variable(const char *name, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter);
RBIMPL_ATTR_NONNULL((1))
/**
* Identical to rb_define_virtual_variable(), but can also specify a storage.
* A programmer can use the storage for e.g. memoisation, storing intermediate
* computation result, etc.
*
* Also you can pass 0 to this function, unlike other variants:
*
* - When getter is 0 ::rb_gvar_var_getter is used instead.
* - When setter is 0 ::rb_gvar_var_setter is used instead.
* - When data is 0, you must specify a non-zero setter function. Otherwise
* ::rb_gvar_var_setter tries to write to `*NULL`, and just causes SEGV.
*
* @param[in] name Variable name, in C's string.
* @param[in] var Variable storage.
* @param[in] getter A getter function.
* @param[in] setter A setter function.
* @post Ruby level global variable named `name` is defined if absent.
*/
void rb_define_hooked_variable(const char *name, VALUE *var, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter);
RBIMPL_ATTR_NONNULL(())
/**
* Identical to rb_define_variable(), except it does not allow Ruby programs to
* assign values to such global variable. C codes can still set values at
* will. This could be handy for you when implementing an `errno`-like
* experience, where a method updates a read-only global variable as a side-
* effect.
*
* @param[in] name Variable (Ruby side).
* @param[in] var Variable (C side).
* @post Ruby level global variable named `name` is defined if absent,
* and its storage is set to `var`.
*/
void rb_define_readonly_variable(const char *name, const VALUE *var);
RBIMPL_ATTR_NONNULL(())
/**
* Defines a Ruby level constant under a namespace.
*
* @param[out] klass Namespace for the constant to reside.
* @param[in] name Name of the constant.
* @param[in] val Value of the constant.
* @exception rb_eTypeError `klass` is not a kind of ::rb_cModule.
* @exception rb_eFrozenError `klass` is frozen.
* @post Ruby level constant `klass::name` is defined to be `val`.
* @note This API does not stop you from defining a constant that is
* unable to reach from ruby (like for instance passing
* non-capital letter to `name`).
* @note This API does not stop you from overwriting a constant that
* already exist.
*
* @internal
*
* Above description is in fact inaccurate. This API interfaces with Ractors.
*/
void rb_define_const(VALUE klass, const char *name, VALUE val);
RBIMPL_ATTR_NONNULL(())
/**
* Identical to rb_define_const(), except it defines that of "global",
* i.e. toplevel constant.
*
* @param[in] name Name of the constant.
* @param[in] val Value of the constant.
* @exception rb_eFrozenError ::rb_cObject is frozen.
* @post Ruby level constant \::name is defined to be `val`.
* @note This API does not stop you from defining a constant that is
* unable to reach from ruby (like for instance passing
* non-capital letter to `name`).
* @note This API does not stop you from overwriting a constant that
* already exist.
*/
void rb_define_global_const(const char *name, VALUE val);
RBIMPL_ATTR_NONNULL(())
/**
* Asserts that the given constant is deprecated. Attempt to refer such
* constant will produce a warning.
*
* @param[in] mod Namespace of the target constant.
* @param[in] name Name of the constant.
* @exception rb_eNameError No such constant.
* @exception rb_eFrozenError `mod` is frozen.
* @post `name` under `mod` is deprecated.
*/
void rb_deprecate_constant(VALUE mod, const char *name);
RBIMPL_ATTR_NONNULL(())
/**
* Assigns to a global variable.
*
* @param[in] name Target global variable.
* @param[in] val Value to assign.
* @return Passed value.
* @post Ruby level global variable named `name` is defined if absent,
* whose value is set to `val`.
*
* @internal
*
* Above description is in fact inaccurate. This API interfaces with
* `set_trace_func`.
*/
VALUE rb_gv_set(const char *name, VALUE val);
RBIMPL_ATTR_NONNULL(())
/**
* Obtains a global variable.
*
* @param[in] name Global variable to query.
* @retval RUBY_Qnil The global variable does not exist.
* @retval otherwise The value assigned to the global variable.
*
* @internal
*
* Unlike rb_gv_set(), there is no way to trace this function.
*/
VALUE rb_gv_get(const char *name);
RBIMPL_ATTR_NONNULL(())
/**
* Obtains an instance variable.
*
* @param[in] obj Target object.
* @param[in] name Target instance variable to query.
* @exception rb_eEncodingError `name` is corrupt (contains Hanzi etc.).
* @retval RUBY_nil No such instance variable.
* @retval otherwise The value assigned to the instance variable.
*/
VALUE rb_iv_get(VALUE obj, const char *name);
RBIMPL_ATTR_NONNULL(())
/**
* Assigns to an instance variable.
*
* @param[out] obj Target object.
* @param[in] name Target instance variable.
* @param[in] val Value to assign.
* @exception rb_eFrozenError Can't modify `obj`.
* @exception rb_eArgError `obj` has too many instance variables.
* @return Passed value.
* @post An instance variable named `name` is defined if absent on
* `obj`, whose value is set to `val`.
*
* @internal
*
* This function does not stop you form creating an ASCII-incompatible instance
* variable, but there is no way to get one because rb_iv_get raises exceptions
* for such things. This design seems broken... But no idea why.
*/
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val);
RBIMPL_SYMBOL_EXPORT_END()
#endif /* RBIMPL_VARIABLE_H */