@@ -59,11 +59,11 @@ defmodule Code do
59
59
end
60
60
61
61
@ doc """
62
- Evaluate the contents given by `string`.
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.
63
66
64
- The `binding` argument is a keyword list of variable bindings.
65
- The `opts` argument is a keyword list of environment options.
66
-
67
67
Those options can be:
68
68
69
69
* `:file` - the file to be considered in the evaluation
@@ -118,15 +118,12 @@ defmodule Code do
118
118
def eval_string ( string , binding \\ [ ] , opts \\ [ ] )
119
119
120
120
def eval_string ( string , binding , Macro.Env [ ] = env ) do
121
- do_eval_string ( string , binding , env . to_keywords )
121
+ { value , binding , _env , _scope } = :elixir . eval to_char_list ( string ) , binding , env . to_keywords
122
+ { value , binding }
122
123
end
123
124
124
125
def eval_string ( string , binding , opts ) when is_list ( opts ) do
125
126
validate_eval_opts ( opts )
126
- do_eval_string ( string , binding , opts )
127
- end
128
-
129
- defp do_eval_string ( string , binding , opts ) when is_list ( binding ) do
130
127
{ value , binding , _env , _scope } = :elixir . eval to_char_list ( string ) , binding , opts
131
128
{ value , binding }
132
129
end
@@ -153,15 +150,12 @@ defmodule Code do
153
150
def eval_quoted ( quoted , binding \\ [ ] , opts \\ [ ] )
154
151
155
152
def eval_quoted ( quoted , binding , Macro.Env [ ] = env ) do
156
- do_eval_quoted ( quoted , binding , env . to_keywords )
153
+ { value , binding , _env , _scope } = :elixir . eval_quoted quoted , binding , env . to_keywords
154
+ { value , binding }
157
155
end
158
156
159
157
def eval_quoted ( quoted , binding , opts ) when is_list ( opts ) do
160
158
validate_eval_opts ( opts )
161
- do_eval_quoted ( quoted , binding , opts )
162
- end
163
-
164
- defp do_eval_quoted ( quoted , binding , opts ) when is_list ( binding ) do
165
159
{ value , binding , _env , _scope } = :elixir . eval_quoted quoted , binding , opts
166
160
{ value , binding }
167
161
end
@@ -204,8 +198,8 @@ defmodule Code do
204
198
end
205
199
206
200
@ doc """
207
- Convert the given string to its quoted form.
208
-
201
+ Convert the given string to its quoted form.
202
+
209
203
Returns `{ :ok, quoted_form }`
210
204
if it succeeds, `{ :error, { line, error, token } }` otherwise.
211
205
@@ -228,17 +222,12 @@ defmodule Code do
228
222
def string_to_quoted ( string , opts \\ [ ] ) when is_list ( opts ) do
229
223
file = Keyword . get opts , :file , "nofile"
230
224
line = Keyword . get opts , :line , 1
231
- res = :elixir . string_to_quoted ( to_char_list ( string ) , line , file , opts )
232
-
233
- case res do
234
- { :ok , forms } -> { :ok , forms }
235
- _ -> res
236
- end
225
+ :elixir . string_to_quoted ( to_char_list ( string ) , line , file , opts )
237
226
end
238
227
239
228
@ doc """
240
- Convert the given string to its quoted form.
241
-
229
+ Convert the given string to its quoted form.
230
+
242
231
It returns the ast if it succeeds,
243
232
raises an exception otherwise. The exception is a `TokenMissingError`
244
233
in case a token is missing (usually because the expression is incomplete),
@@ -253,18 +242,31 @@ defmodule Code do
253
242
end
254
243
255
244
@ doc """
256
- Load the given file.
257
-
258
- Accepts `relative_to` as an argument to tell where
259
- the file is located. If the file was already required/loaded, loads it again.
260
- It returns a list of tuples `{ ModuleName, <<byte_code>> }`, one tuple for each
261
- module defined in the file.
262
-
263
- Notice that if `load_file` is invoked by different processes
264
- concurrently, the target file will be invoked concurrently
265
- many times. I.e. if `load_file` is called N times with
266
- a given file, the given file will be loaded N times. Check
267
- `require_file/2` if you don't want a file to be loaded concurrently.
245
+ Evals the given file.
246
+
247
+ Accepts `relative_to` as an argument to tell where the file is located.
248
+
249
+ While `load_file` loads a file and returns the loaded modules and their
250
+ byte code, `eval_file` simply evalutes the file contents and returns the
251
+ evaluation result and its bindings.
252
+ """
253
+ def eval_file ( file , relative_to \\ nil ) do
254
+ file = find_file ( file , relative_to )
255
+ eval_string File . read! ( file ) , [ ] , [ ]
256
+ end
257
+
258
+ @ doc """
259
+ Load the given file.
260
+
261
+ Accepts `relative_to` as an argument to tell where the file is located.
262
+ If the file was already required/loaded, loads it again.
263
+
264
+ It returns a list of tuples `{ ModuleName, <<byte_code>> }`, one tuple for
265
+ each module defined in the file.
266
+
267
+ Notice that if `load_file` is invoked by different processes concurrently,
268
+ the target file will be loaded concurrently many times. Check `require_file/2`
269
+ if you don't want a file to be loaded concurrently.
268
270
"""
269
271
def load_file ( file , relative_to \\ nil ) when is_binary ( file ) do
270
272
file = find_file ( file , relative_to )
@@ -275,19 +277,19 @@ defmodule Code do
275
277
end
276
278
277
279
@ doc """
278
- Require the given `file`.
279
-
280
- Accepts `relative_to` as an argument to tell where
281
- the file is located. The return value is the same as that of `load_file/2`. If
282
- the file was already required/loaded, doesn't do anything and returns `nil`.
280
+ Requires the given `file`.
281
+
282
+ Accepts `relative_to` as an argument to tell where the file is located.
283
+ The return value is the same as that of `load_file/2`. If the file was already
284
+ required/loaded, doesn't do anything and returns `nil`.
283
285
284
286
Notice that if `require_file` is invoked by different processes concurrently,
285
287
the first process to invoke `require_file` acquires a lock and the remaining
286
288
ones will block until the file is available. I.e. if `require_file` is called
287
289
N times with a given file, it will be loaded only once. The first process to
288
290
call `require_file` will get the list of loaded modules, others will get `nil`.
289
291
290
- Check `load_file/2` if you want a file to be loaded concurrently .
292
+ Check `load_file/2` if you want a file to be loaded multiple times .
291
293
"""
292
294
def require_file ( file , relative_to \\ nil ) when is_binary ( file ) do
293
295
file = find_file ( file , relative_to )
@@ -305,7 +307,7 @@ defmodule Code do
305
307
end
306
308
307
309
@ doc """
308
- Load the compilation options from the code server.
310
+ Gets the compilation options from the code server.
309
311
310
312
Check `compiler_options/1` for more information.
311
313
"""
@@ -314,7 +316,7 @@ defmodule Code do
314
316
end
315
317
316
318
@ doc """
317
- Set compilation options.
319
+ Sets compilation options.
318
320
319
321
These options are global since they are stored by Elixir's Code Server.
320
322
@@ -338,11 +340,10 @@ defmodule Code do
338
340
end
339
341
340
342
@ doc """
341
- Compile the given string.
343
+ Compiles the given string.
342
344
343
- Returns a list of tuples where
344
- the first element is the module name and the second one is its
345
- binary.
345
+ Returns a list of tuples where the first element is the module name
346
+ and the second one is its byte code (as a binary).
346
347
347
348
For compiling many files at once, check `Kernel.ParallelCompiler.files/2`.
348
349
"""
@@ -351,26 +352,23 @@ defmodule Code do
351
352
end
352
353
353
354
@ doc """
354
- Compile the quoted expression.
355
-
356
- Returns a list of tuples where
357
- the first element is the module name and the second one is its
358
- binary.
355
+ Compiles the quoted expression.
356
+
357
+ Returns a list of tuples where the first element is the module name and
358
+ the second one is its byte code (as a binary).
359
359
"""
360
360
def compile_quoted ( quoted , file \\ "nofile" ) when is_binary ( file ) do
361
361
:elixir_compiler . quoted quoted , file
362
362
end
363
363
364
364
@ doc """
365
- Ensure the given module is loaded.
366
-
367
- If the module is already
368
- loaded, this works as no-op. If the module was not yet loaded,
369
- it tries to load it.
365
+ Ensures the given module is loaded.
366
+
367
+ If the module is already loaded, this works as no-op. If the module
368
+ was not yet loaded, it tries to load it.
370
369
371
- If it succeeds loading the module, it returns
372
- `{ :module, module }`. If not, returns `{ :error, reason }` with
373
- the error reason.
370
+ If it succeeds loading the module, it returns `{ :module, module }`.
371
+ If not, returns `{ :error, reason }` with the error reason.
374
372
375
373
## Code loading on the Erlang VM
376
374
@@ -385,47 +383,46 @@ defmodule Code do
385
383
module uses this function to check if a specific parser exists for a given
386
384
URI scheme.
387
385
388
- ## Code.ensure_compiled
386
+ ## ` Code.ensure_compiled/1`
389
387
390
388
Elixir also contains an `ensure_compiled/1` function that is a
391
389
superset of `ensure_loaded/1`.
392
390
393
391
Since Elixir's compilation happens in parallel, in some situations
394
- you may need to use a module but that was not yet compiled, therefore
392
+ you may need to use a module that was not yet compiled, therefore
395
393
it can't even be loaded.
396
394
397
395
`ensure_compiled/1` halts the current process until the
398
396
module we are depending on is available.
399
397
400
- In most cases, `ensure_loaded` is enough. `ensure_compiled`
401
- must be used in some rare cases, usually involving macros
402
- that need to invoke a module for callback information.
398
+ In most cases, `ensure_loaded/1 ` is enough. `ensure_compiled/1 `
399
+ must be used in rare cases, usually involving macros that need to
400
+ invoke a module for callback information.
403
401
"""
404
402
def ensure_loaded ( module ) when is_atom ( module ) do
405
403
:code . ensure_loaded ( module )
406
404
end
407
405
408
406
@ doc """
409
- Ensure the given module is loaded.
407
+ Ensures the given module is loaded.
410
408
411
409
Similar to `ensure_loaded/1`, but returns `true` if the module
412
- is already loaded or was successfully loaded. Returns `false` otherwise.
410
+ is already loaded or was successfully loaded. Returns `false`
411
+ otherwise.
413
412
"""
414
413
def ensure_loaded? ( module ) do
415
414
match? ( { :module , ^ module } , ensure_loaded ( module ) )
416
415
end
417
416
418
417
@ doc """
419
- Ensure the given module is compiled and loaded.
420
-
421
- If the module
422
- is already loaded, it works as no-op. If the module was not
423
- loaded yet, it checks if it needs to be compiled first and
424
- then tries to load it.
418
+ Ensures the given module is compiled and loaded.
419
+
420
+ If the module is already loaded, it works as no-op. If the module was
421
+ not loaded yet, it checks if it needs to be compiled first and then
422
+ tries to load it.
425
423
426
- If it succeeds loading the module, it returns
427
- `{ :module, module }`. If not, returns `{ :error, reason }` with
428
- the error reason.
424
+ If it succeeds loading the module, it returns `{ :module, module }`.
425
+ If not, returns `{ :error, reason }` with the error reason.
429
426
430
427
Check `ensure_loaded/1` for more information on module loading
431
428
and when to use `ensure_loaded/1` or `ensure_compiled/1`.
@@ -448,10 +445,10 @@ defmodule Code do
448
445
end
449
446
450
447
@ doc """
451
- Ensure the given module is compiled and loaded.
448
+ Ensures the given module is compiled and loaded.
452
449
453
450
Similar to `ensure_compiled/1`, but returns `true` if the module
454
- is already loaded or was successfully loaded and compiled.
451
+ is already loaded or was successfully loaded and compiled.
455
452
Returns `false` otherwise.
456
453
"""
457
454
def ensure_compiled? ( module ) do
@@ -461,6 +458,7 @@ defmodule Code do
461
458
## Helpers
462
459
463
460
# Finds the file given the relative_to path.
461
+ #
464
462
# If the file is found, returns its path in binary, fails otherwise.
465
463
defp find_file ( file , relative_to ) do
466
464
file = if relative_to do
0 commit comments