@@ -6,24 +6,24 @@ defmodule Code do
6
6
end
7
7
8
8
@ moduledoc """
9
- The Code module is responsible for managing code compilation,
10
- code evaluation and code loading.
9
+ Utilities for managing code compilation, code evaluation and code loading.
11
10
12
- It complements [Erlang's code module](http://www.erlang.org/doc/man/code.html)
11
+ This module complements [Erlang's code module](http://www.erlang.org/doc/man/code.html)
13
12
to add behavior which is specific to Elixir.
14
13
15
14
"""
16
15
17
16
@ doc """
18
- Returns all loaded files.
17
+ List all loaded files.
19
18
"""
20
19
def loaded_files do
21
20
:elixir_code_server . call :loaded
22
21
end
23
22
24
23
@ doc """
25
- Removes the given files from the loaded files list.
26
- The modules defined in the file are not removed,
24
+ Remove files from the loaded files list.
25
+
26
+ The modules defined in the file are not removed;
27
27
calling this function only removes them from the list,
28
28
allowing them to be required again.
29
29
"""
@@ -32,33 +32,39 @@ defmodule Code do
32
32
end
33
33
34
34
@ doc """
35
- Appends a path to the Erlang VM code path.
35
+ Append a path to the Erlang VM code path.
36
+
36
37
The path is expanded with `Path.expand/1` before being appended.
37
38
"""
38
39
def append_path ( path ) do
39
40
:code . add_pathz ( Path . expand to_char_list ( path ) )
40
41
end
41
42
42
43
@ doc """
43
- Prepends a path to the Erlang VM code path.
44
+ Prepend a path to the Erlang VM code path.
45
+
44
46
The path is expanded with `Path.expand/1` before being prepended.
45
47
"""
46
48
def prepend_path ( path ) do
47
49
:code . add_patha ( Path . expand to_char_list ( path ) )
48
50
end
49
51
50
52
@ doc """
51
- Deletes a path from the Erlang VM code path.
53
+ Delete a path from the Erlang VM code path.
54
+
52
55
The path is expanded with `Path.expand/1` before being deleted.
53
56
"""
54
57
def delete_path ( path ) do
55
58
:code . del_path ( Path . expand to_char_list ( path ) )
56
59
end
57
60
58
61
@ doc """
59
- Evaluates the contents given by `string`. The second argument is
60
- a keyword list of variable bindings, followed by a keyword list of
61
- environment options. Those options can be:
62
+ Evaluate the contents given by `string`.
63
+
64
+ The `binding` argument is a keyword list of variable bindings.
65
+ The `opts` argument is a keyword list of environment options.
66
+
67
+ Those options can be:
62
68
63
69
* `:file` - the file to be considered in the evaluation
64
70
* `:line` - the line on which the script starts
@@ -88,8 +94,7 @@ defmodule Code do
88
94
89
95
`binding` is a keyword list with the value of all variable bindings
90
96
after evaluating `string`. The binding key is usually an atom, but it
91
- may be a tuple for variables defined in another contexts that are not
92
- the main one.
97
+ may be a tuple for variables defined in a different context.
93
98
94
99
## Examples
95
100
@@ -127,7 +132,7 @@ defmodule Code do
127
132
end
128
133
129
134
@ doc """
130
- Evaluates the quoted contents.
135
+ Evaluate the quoted contents.
131
136
132
137
See `eval_string/3` for a description of arguments and return values.
133
138
@@ -200,7 +205,9 @@ defmodule Code do
200
205
end
201
206
202
207
@ doc """
203
- Converts the given string to its quoted form. Returns `{ :ok, quoted_form }`
208
+ Convert the given string to its quoted form.
209
+
210
+ Returns `{ :ok, quoted_form }`
204
211
if it succeeds, `{ :error, { line, error, token } }` otherwise.
205
212
206
213
## Options
@@ -213,10 +220,10 @@ defmodule Code do
213
220
* `:existing_atoms_only` - When `true`, raises an error
214
221
when non-existing atoms are found by the tokenizer.
215
222
216
- ## Macro.to_string/1
223
+ ## Macro.to_string/2
217
224
218
225
The opposite of converting a string to its quoted form is
219
- `Macro.to_string/1 `, which converts a quoted form to a string/binary
226
+ `Macro.to_string/2 `, which converts a quoted form to a string/binary
220
227
representation.
221
228
"""
222
229
def string_to_quoted ( string , opts // [ ] ) when is_list ( opts ) do
@@ -231,7 +238,9 @@ defmodule Code do
231
238
end
232
239
233
240
@ doc """
234
- Converts the given string to its quoted form. It returns the ast if it succeeds,
241
+ Convert the given string to its quoted form.
242
+
243
+ It returns the ast if it succeeds,
235
244
raises an exception otherwise. The exception is a `TokenMissingError`
236
245
in case a token is missing (usually because the expression is incomplete),
237
246
`SyntaxError` otherwise.
@@ -250,7 +259,9 @@ defmodule Code do
250
259
defp unpack_quote ( line , forms ) , do: { :__block__ , [ line: line ] , forms }
251
260
252
261
@ doc """
253
- Loads the given `file`. Accepts `relative_to` as an argument to tell where
262
+ Load the given file.
263
+
264
+ Accepts `relative_to` as an argument to tell where
254
265
the file is located. If the file was already required/loaded, loads it again.
255
266
It returns a list of tuples `{ ModuleName, <<byte_code>> }`, one tuple for each
256
267
module defined in the file.
@@ -270,9 +281,11 @@ defmodule Code do
270
281
end
271
282
272
283
@ doc """
273
- Requires the given `file`. Accepts `relative_to` as an argument to tell where
284
+ Require the given `file`.
285
+
286
+ Accepts `relative_to` as an argument to tell where
274
287
the file is located. The return value is the same as that of `load_file/2`. If
275
- the file was already required/loaded, doesn't do anything and returns nil.
288
+ the file was already required/loaded, doesn't do anything and returns ` nil` .
276
289
277
290
Notice that if `require_file` is invoked by different processes concurrently,
278
291
the first process to invoke `require_file` acquires a lock and the remaining
@@ -298,16 +311,18 @@ defmodule Code do
298
311
end
299
312
300
313
@ doc """
301
- Loads the compilation options from the code server.
314
+ Load the compilation options from the code server.
315
+
302
316
Check `compiler_options/1` for more information.
303
317
"""
304
318
def compiler_options do
305
319
:elixir_code_server . call :compiler_options
306
320
end
307
321
308
322
@ doc """
309
- Sets compilation options. These options are global
310
- since they are stored by Elixir's Code Server.
323
+ Set compilation options.
324
+
325
+ These options are global since they are stored by Elixir's Code Server.
311
326
312
327
Available options are:
313
328
@@ -316,7 +331,7 @@ defmodule Code do
316
331
317
332
* `:debug_info` - when `true`, retain debug information in the compiled module.
318
333
This allows a developer to reconstruct the original source
319
- code, for such reasons, `false` by default;
334
+ code, `false` by default;
320
335
321
336
* `:ignore_module_conflict` - when `true`, override modules that were already defined
322
337
without raising errors, `false` by default;
@@ -329,7 +344,9 @@ defmodule Code do
329
344
end
330
345
331
346
@ doc """
332
- Compiles the given string and returns a list of tuples where
347
+ Compile the given string.
348
+
349
+ Returns a list of tuples where
333
350
the first element is the module name and the second one is its
334
351
binary.
335
352
@@ -340,7 +357,9 @@ defmodule Code do
340
357
end
341
358
342
359
@ doc """
343
- Compiles the quoted expression and returns a list of tuples where
360
+ Compile the quoted expression.
361
+
362
+ Returns a list of tuples where
344
363
the first element is the module name and the second one is its
345
364
binary.
346
365
"""
@@ -349,8 +368,10 @@ defmodule Code do
349
368
end
350
369
351
370
@ doc """
352
- Ensures the given module is loaded. If the module is already
353
- loaded, it works as no-op. If the module was not yet loaded,
371
+ Ensure the given module is loaded.
372
+
373
+ If the module is already
374
+ loaded, this works as no-op. If the module was not yet loaded,
354
375
it tries to load it.
355
376
356
377
If it succeeds loading the module, it returns
@@ -391,15 +412,19 @@ defmodule Code do
391
412
end
392
413
393
414
@ doc """
394
- Similar to `ensure_loaded/1`, but returns a boolean in case
395
- it could be ensured or not.
415
+ Ensure the given module is loaded.
416
+
417
+ Similar to `ensure_loaded/1`, but returns `true` if the module
418
+ is already loaded or was successfully loaded. Returns `false` otherwise.
396
419
"""
397
420
def ensure_loaded? ( module ) do
398
421
match? ( { :module , ^ module } , ensure_loaded ( module ) )
399
422
end
400
423
401
424
@ doc """
402
- Ensures the given module is compiled and loaded. If the module
425
+ Ensure the given module is compiled and loaded.
426
+
427
+ If the module
403
428
is already loaded, it works as no-op. If the module was not
404
429
loaded yet, it checks if it needs to be compiled first and
405
430
then tries to load it.
@@ -429,8 +454,11 @@ defmodule Code do
429
454
end
430
455
431
456
@ doc """
432
- Similar to `ensure_compiled/1`, but returns a boolean in case
433
- it could be ensured or not.
457
+ Ensure the given module is compiled and loaded.
458
+
459
+ Similar to `ensure_compiled/1`, but returns `true` if the module
460
+ is already loaded or was successfully loaded and compiled.
461
+ Returns `false` otherwise.
434
462
"""
435
463
def ensure_compiled? ( module ) do
436
464
match? ( { :module , ^ module } , ensure_compiled ( module ) )
0 commit comments