| 1 |
#ifndef RBIMPL_INTERN_LOAD_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_LOAD_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_f_require(). |
| 22 |
*/ |
| 23 |
#include "ruby/internal/attr/nonnull.h" |
| 24 |
#include "ruby/internal/dllexport.h" |
| 25 |
#include "ruby/internal/value.h" |
| 26 |
|
| 27 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 28 |
|
| 29 |
/* load.c */ |
| 30 |
|
| 31 |
/** |
| 32 |
* Loads and executes the Ruby program in the given file. |
| 33 |
* |
| 34 |
* If the path is an absolute path (e.g. starts with `'/'`), the file will be |
| 35 |
* loaded directly using the absolute path. If the path is an explicit |
| 36 |
* relative path (e.g. starts with `'./'` or `'../'`), the file will be loaded |
| 37 |
* using the relative path from the current directory. Otherwise, the file |
| 38 |
* will be searched for in the library directories listed in the `$LOAD_PATH`. |
| 39 |
* If the file is found in a directory, this function will attempt to load the |
| 40 |
* file relative to that directory. If the file is not found in any of the |
| 41 |
* directories in the `$LOAD_PATH`, the file will be loaded using the relative |
| 42 |
* path from the current directory. |
| 43 |
* |
| 44 |
* If the file doesn't exist when there is an attempt to load it, a LoadError |
| 45 |
* will be raised. |
| 46 |
* |
| 47 |
* If the `wrap` parameter is true, the loaded script will be executed under an |
| 48 |
* anonymous module, protecting the calling program's global namespace. In no |
| 49 |
* circumstance will any local variables in the loaded file be propagated to |
| 50 |
* the loading environment. |
| 51 |
* |
| 52 |
* @param[in] path Pathname of a file to load. |
| 53 |
* @param[in] wrap Either to load under an anonymous module. |
| 54 |
* @exception rb_eTypeError `path` is not a string. |
| 55 |
* @exception rb_eArgError `path` is broken as a pathname. |
| 56 |
* @exception rb_eEncCompatError `path` is incompatible with pathnames. |
| 57 |
* @exception rb_eLoadError `path` not found. |
| 58 |
* @exception rb_eException Any exceptions while loading the contents. |
| 59 |
* |
| 60 |
* @internal |
| 61 |
* |
| 62 |
* It seems this function is under the rule of bootsnap's regime? |
| 63 |
*/ |
| 64 |
void rb_load(VALUE path, int wrap); |
| 65 |
|
| 66 |
/** |
| 67 |
* Identical to rb_load(), except it avoids potential global escapes. Such |
| 68 |
* global escapes include exceptions, `throw`, `break`, for example. |
| 69 |
* |
| 70 |
* It first evaluates the given file as rb_load() does. If no global escape |
| 71 |
* occurred during the evaluation, it `*state` is set to zero on return. |
| 72 |
* Otherwise, it sets `*state` to nonzero. If state is `NULL`, it is not set |
| 73 |
* in both cases. |
| 74 |
* |
| 75 |
* @param[in] path Pathname of a file to load. |
| 76 |
* @param[in] wrap Either to load under an anonymous module. |
| 77 |
* @param[out] state State of execution. |
| 78 |
* @post `*state` is set to zero if succeeded. Nonzero otherwise. |
| 79 |
* @warning You have to clear the error info with `rb_set_errinfo(Qnil)` if |
| 80 |
* you decide to ignore the caught exception. |
| 81 |
* @see rb_load |
| 82 |
* @see rb_protect |
| 83 |
* |
| 84 |
* @internal |
| 85 |
* |
| 86 |
* Though not a part of our public API, `state` is in fact an |
| 87 |
* enum ruby_tag_type. You can see the potential "nonzero" values by looking |
| 88 |
* at vm_core.h. |
| 89 |
*/ |
| 90 |
void rb_load_protect(VALUE path, int wrap, int *state); |
| 91 |
|
| 92 |
RBIMPL_ATTR_NONNULL(()) |
| 93 |
/** |
| 94 |
* Queries if the given feature has already been loaded into the execution |
| 95 |
* context. The "feature" head are things like `"json"` or `"socket"`. |
| 96 |
* |
| 97 |
* @param[in] feature Name of a library you want to know about. |
| 98 |
* @retval 1 Yes there is. |
| 99 |
* @retval 0 Not yet. |
| 100 |
*/ |
| 101 |
int rb_provided(const char *feature); |
| 102 |
|
| 103 |
RBIMPL_ATTR_NONNULL((1)) |
| 104 |
/** |
| 105 |
* Identical to rb_provided(), except it additionally returns the "canonical" |
| 106 |
* name of the loaded feature. This can be handy when for instance you want to |
| 107 |
* know the actually loaded library is either `foo.rb` or `foo.so`. |
| 108 |
* |
| 109 |
* @param[in] feature Name of a library you want to know about. |
| 110 |
* @param[out] loading Return buffer. |
| 111 |
* @retval 1 Yes there is. |
| 112 |
* @retval 0 Not yet. |
| 113 |
*/ |
| 114 |
int rb_feature_provided(const char *feature, const char **loading); |
| 115 |
|
| 116 |
RBIMPL_ATTR_NONNULL(()) |
| 117 |
/** |
| 118 |
* Declares that the given feature is already provided by someone else. This |
| 119 |
* API can be handy when you have an extension called `foo.so` which, when |
| 120 |
* required, also provides functionality of `bar.so`. |
| 121 |
* |
| 122 |
* @param[in] feature Name of a library which had already been provided. |
| 123 |
* @post No further `require` would search `feature`. |
| 124 |
*/ |
| 125 |
void rb_provide(const char *feature); |
| 126 |
|
| 127 |
/** |
| 128 |
* Identical to rb_require_string(), except it ignores the first argument for |
| 129 |
* no reason. There seems to be no reason for 3rd party extension libraries to |
| 130 |
* use it. |
| 131 |
* |
| 132 |
* @param[in] self Ignored. Can be anything. |
| 133 |
* @param[in] feature Name of a feature, e.g. `"json"`. |
| 134 |
* @exception rb_eLoadError No such feature. |
| 135 |
* @exception rb_eRuntimeError `$"` is frozen; unable to push. |
| 136 |
* @retval RUBY_Qtrue The feature is loaded for the first time. |
| 137 |
* @retval RUBY_Qfalse The feature has already been loaded. |
| 138 |
* @post `$"` is updated. |
| 139 |
*/ |
| 140 |
VALUE rb_f_require(VALUE self, VALUE feature); |
| 141 |
|
| 142 |
/** |
| 143 |
* Finds and loads the given feature, if absent. |
| 144 |
* |
| 145 |
* If the feature is an absolute path (e.g. starts with `'/'`), the feature |
| 146 |
* will be loaded directly using the absolute path. If the feature is an |
| 147 |
* explicit relative path (e.g. starts with `'./'` or `'../'`), the feature |
| 148 |
* will be loaded using the relative path from the current directory. |
| 149 |
* Otherwise, the feature will be searched for in the library directories |
| 150 |
* listed in the `$LOAD_PATH`. |
| 151 |
* |
| 152 |
* If the feature has the extension `".rb"`, it is loaded as a source file; if |
| 153 |
* the extension is `".so"`, `".o"`, or `".dll"`, or the default shared library |
| 154 |
* extension on the current platform, Ruby loads the shared library as a Ruby |
| 155 |
* extension. Otherwise, Ruby tries adding `".rb"`, `".so"`, and so on to the |
| 156 |
* name until found. If the file named cannot be found, a LoadError will be |
| 157 |
* raised. |
| 158 |
* |
| 159 |
* For extension libraries the given feature may use any shared library |
| 160 |
* extension. For example, on Linux you can require `"socket.dll"` to actually |
| 161 |
* load `socket.so`. |
| 162 |
* |
| 163 |
* The absolute path of the loaded file is added to `$LOADED_FEATURES`. A file |
| 164 |
* will not be loaded again if its path already appears in there. |
| 165 |
* |
| 166 |
* Any constants or globals within the loaded source file will be available in |
| 167 |
* the calling program's global namespace. However, local variables will not |
| 168 |
* be propagated to the loading environment. |
| 169 |
* |
| 170 |
* @param[in] feature Name of a feature, e.g. `"json"`. |
| 171 |
* @exception rb_eLoadError No such feature. |
| 172 |
* @exception rb_eRuntimeError `$"` is frozen; unable to push. |
| 173 |
* @retval RUBY_Qtrue The feature is loaded for the first time. |
| 174 |
* @retval RUBY_Qfalse The feature has already been loaded. |
| 175 |
* @post `$"` is updated. |
| 176 |
*/ |
| 177 |
VALUE rb_require_string(VALUE feature); |
| 178 |
|
| 179 |
/** |
| 180 |
* Resolves and returns a symbol of a function in the native extension |
| 181 |
* specified by the feature and symbol names. Extensions will use this function |
| 182 |
* to access the symbols provided by other native extensions. |
| 183 |
* |
| 184 |
* @param[in] feature Name of a feature, e.g. `"json"`. |
| 185 |
* @param[in] symbol Name of a symbol defined by the feature. |
| 186 |
* @return The resolved symbol of a function, defined and externed by the |
| 187 |
* specified feature. It may be NULL if the feature is not loaded, |
| 188 |
* the feature is not extension, or the symbol is not found. |
| 189 |
*/ |
| 190 |
void *rb_ext_resolve_symbol(const char *feature, const char *symbol); |
| 191 |
|
| 192 |
/** |
| 193 |
* This macro is to provide backwards compatibility. It provides a way to |
| 194 |
* define function prototypes and resolving function symbols in a safe way. |
| 195 |
* |
| 196 |
* ```CXX |
| 197 |
* // prototypes |
| 198 |
* #ifdef HAVE_RB_EXT_RESOLVE_SYMBOL |
| 199 |
* VALUE *(*other_extension_func)(VALUE,VALUE); |
| 200 |
* #else |
| 201 |
* VALUE other_extension_func(VALUE); |
| 202 |
* #endif |
| 203 |
* |
| 204 |
* // in Init_xxx() |
| 205 |
* #ifdef HAVE_RB_EXT_RESOLVE_SYMBOL |
| 206 |
* other_extension_func = \ |
| 207 |
* (VALUE(*)(VALUE,VALUE))rb_ext_resolve_symbol(fname, sym_name); |
| 208 |
* if (other_extension_func == NULL) { |
| 209 |
* // raise your own error |
| 210 |
* } |
| 211 |
* #endif |
| 212 |
* ``` |
| 213 |
*/ |
| 214 |
#define HAVE_RB_EXT_RESOLVE_SYMBOL 1 |
| 215 |
|
| 216 |
/** |
| 217 |
* @name extension configuration |
| 218 |
* @{ |
| 219 |
*/ |
| 220 |
|
| 221 |
/** |
| 222 |
* Asserts that the extension library that calls this function is aware of |
| 223 |
* Ractor. Multiple Ractors run without protecting each other. This doesn't |
| 224 |
* interface well with C programs, unless designed with an in-depth |
| 225 |
* understanding of how Ractors work. Extension libraries are shut out from |
| 226 |
* Ractors by default. This API is to bypass that restriction. Once after it |
| 227 |
* was called, successive calls to rb_define_method() etc. become definitions |
| 228 |
* of methods that are aware of Ractors. The amendment would be in effect |
| 229 |
* until the end of rb_require_string() etc. |
| 230 |
* |
| 231 |
* @param[in] flag Either the library is aware of Ractors or not. |
| 232 |
* @post Methods would be callable form Ractors, if `flag` is true. |
| 233 |
*/ |
| 234 |
void rb_ext_ractor_safe(bool flag); |
| 235 |
|
| 236 |
/** @alias{rb_ext_ractor_safe} */ |
| 237 |
#define RB_EXT_RACTOR_SAFE(f) rb_ext_ractor_safe(f) |
| 238 |
|
| 239 |
/** |
| 240 |
* This macro is to provide backwards compatibility. It must be safe to do |
| 241 |
* something like: |
| 242 |
* |
| 243 |
* ```CXX |
| 244 |
* #ifdef HAVE_RB_EXT_RACTOR_SAFE |
| 245 |
* rb_ext_ractor_safe(true); |
| 246 |
* #endif |
| 247 |
* ``` |
| 248 |
*/ |
| 249 |
#define HAVE_RB_EXT_RACTOR_SAFE 1 |
| 250 |
|
| 251 |
/** @} */ |
| 252 |
|
| 253 |
RBIMPL_SYMBOL_EXPORT_END() |
| 254 |
|
| 255 |
#endif /* RBIMPL_INTERN_LOAD_H */ |