@@ -291,14 +291,20 @@ defmodule Code do
291291 ]
292292
293293 @ typedoc """
294- Options for environment evaluation functions like eval_string/3 and eval_quoted/3 .
294+ Options for evaluation environment, accepted by `env_for_eval/1` .
295295 """
296- @ type env_eval_opts :: [
297- file: binary ( ) ,
298- line: pos_integer ( ) ,
299- module: module ( ) ,
300- prune_binding: boolean ( )
301- ]
296+ @ type env_eval_opt ::
297+ { :file , binary ( ) }
298+ | { :line , pos_integer ( ) }
299+ | { :module , module ( ) }
300+
301+ @ typedoc """
302+ Options for evaluation functions like `eval_string/3`, `eval_quoted/3`
303+ and `eval_quoted_with_env/4`.
304+ """
305+ @ type eval_opt ::
306+ { :prune_binding , boolean ( ) }
307+ | { :dbg_callback , { module ( ) , atom ( ) , list ( ) } }
302308
303309 @ boolean_compiler_options [
304310 :docs ,
@@ -573,9 +579,11 @@ defmodule Code do
573579
574580 ## Options
575581
576- It accepts the same options as `env_for_eval/1`. Additionally, you may
577- also pass an environment as second argument, so the evaluation happens
578- within that environment.
582+ It accepts the same options as both `env_for_eval/1` and
583+ `eval_quoted_with_env/4`. Additionally, you may also pass an environment
584+ as third argument, so the evaluation happens within that environment.
585+
586+ ## Return
579587
580588 Returns a tuple of the form `{value, binding}`, where `value` is the value
581589 returned from evaluating `string`. If an error occurs while evaluating
@@ -605,7 +613,7 @@ defmodule Code do
605613 iex> Enum.sort(binding)
606614 [a: 3, b: 2]
607615
608- For convenience, you can pass `__ENV__/0` as the `opts ` argument and
616+ For convenience, you can pass `__ENV__/0` as the `opts_or_env ` argument and
609617 all imports, requires and aliases defined in the current environment
610618 will be automatically carried over:
611619
@@ -617,15 +625,16 @@ defmodule Code do
617625 [a: 1, b: 2]
618626
619627 """
620- @ spec eval_string ( List.Chars . t ( ) , binding , Macro.Env . t ( ) | env_eval_opts ) :: { term , binding }
621- def eval_string ( string , binding \\ [ ] , opts \\ [ ] )
628+ @ spec eval_string ( List.Chars . t ( ) , binding , Macro.Env . t ( ) | [ eval_opt | env_eval_opt ] ) ::
629+ { term , binding }
630+ def eval_string ( string , binding \\ [ ] , opts_or_env \\ [ ] )
622631
623632 def eval_string ( string , binding , % Macro.Env { } = env ) do
624- validated_eval_string ( string , validate_binding ( binding ) , env )
633+ validated_eval_string ( string , validate_binding ( binding ) , env_for_eval ( env ) , [ ] )
625634 end
626635
627636 def eval_string ( string , binding , opts ) when is_list ( opts ) do
628- validated_eval_string ( string , validate_binding ( binding ) , opts )
637+ validated_eval_string ( string , validate_binding ( binding ) , env_for_eval ( opts ) , opts )
629638 end
630639
631640 defp validate_binding ( binding ) when is_list ( binding ) , do: binding
@@ -634,10 +643,10 @@ defmodule Code do
634643 raise ArgumentError , "binding must be a list, got: #{ inspect ( binding ) } "
635644 end
636645
637- defp validated_eval_string ( string , binding , opts_or_env ) do
638- % { line: line , file: file } = env = env_for_eval ( opts_or_env )
646+ defp validated_eval_string ( string , binding , env , opts ) do
647+ % { line: line , file: file } = env
639648 forms = :elixir . string_to_quoted! ( to_charlist ( string ) , line , 1 , file , [ ] )
640- { value , binding , _env } = eval_verify ( :eval_forms , [ forms , binding , env ] )
649+ { value , binding , _env } = eval_verify ( :eval_forms , [ forms , binding , env , opts ] )
641650 { value , binding }
642651 end
643652
@@ -1140,7 +1149,8 @@ defmodule Code do
11401149 returned quoted expressions (instead of evaluated).
11411150
11421151 See `eval_string/3` for a description of arguments and return types.
1143- The options are described under `env_for_eval/1`.
1152+ It accepts the same options as both `env_for_eval/1` and
1153+ `eval_quoted_with_env/4`.
11441154
11451155 ## Examples
11461156
@@ -1162,11 +1172,20 @@ defmodule Code do
11621172 [a: 1, b: 2]
11631173
11641174 """
1165- @ spec eval_quoted ( Macro . t ( ) , binding , Macro.Env . t ( ) | env_eval_opts ) :: { term , binding }
1166- def eval_quoted ( quoted , binding \\ [ ] , env_or_opts \\ [ ] ) do
1167- { value , binding , _env } =
1168- eval_verify ( :eval_quoted , [ quoted , binding , env_for_eval ( env_or_opts ) ] )
1175+ @ spec eval_quoted ( Macro . t ( ) , binding , Macro.Env . t ( ) | [ eval_opt | env_eval_opt ] ) ::
1176+ { term , binding }
1177+ def eval_quoted ( quoted , binding \\ [ ] , env_or_opts \\ [ ] )
11691178
1179+ def eval_quoted ( quoted , binding , % Macro.Env { } = env ) do
1180+ eval_quoted ( quoted , validate_binding ( binding ) , env_for_eval ( env ) , [ ] )
1181+ end
1182+
1183+ def eval_quoted ( quoted , binding , opts ) when is_list ( opts ) do
1184+ eval_quoted ( quoted , validate_binding ( binding ) , env_for_eval ( opts ) , opts )
1185+ end
1186+
1187+ defp eval_quoted ( quoted , binding , env , opts ) do
1188+ { value , binding , _env } = eval_verify ( :eval_quoted , [ quoted , binding , env , opts ] )
11701189 { value , binding }
11711190 end
11721191
@@ -1194,14 +1213,9 @@ defmodule Code do
11941213
11951214 * `:module` - the module to run the environment on
11961215
1197- * `:prune_binding` - (since v1.14.2) prune binding to keep only
1198- variables read or written by the evaluated code. Note that
1199- variables used by modules are always pruned, even if later used
1200- by the modules. You can submit to the `:on_module` tracer event
1201- and access the variables used by the module from its environment.
12021216 """
12031217 @ doc since: "1.14.0"
1204- @ spec env_for_eval ( Macro.Env . t ( ) | env_eval_opts ) :: Macro.Env . t ( )
1218+ @ spec env_for_eval ( Macro.Env . t ( ) | [ env_eval_opt ] ) :: Macro.Env . t ( )
12051219 def env_for_eval ( env_or_opts ) , do: :elixir . env_for_eval ( env_or_opts )
12061220
12071221 @ doc """
@@ -1215,11 +1229,19 @@ defmodule Code do
12151229
12161230 ## Options
12171231
1218- It accepts the same options as `env_for_eval/1`.
1232+ * `:prune_binding` - (since v1.14.2) prune binding to keep only
1233+ variables read or written by the evaluated code. Note that
1234+ variables used by modules are always pruned, even if later used
1235+ by the modules. You can submit to the `:on_module` tracer event
1236+ and access the variables used by the module from its environment.
1237+
1238+ * `:dbg_callback` - (since v1.20.0) overrides the behaviour of `dbg/2`
1239+ used in the evaluated code. It must be a `{module, function, args}`
1240+ tuple, see `dbg/2` for more details.
12191241
12201242 """
12211243 @ doc since: "1.14.0"
1222- @ spec eval_quoted_with_env ( Macro . t ( ) , binding , Macro.Env . t ( ) , env_eval_opts ) ::
1244+ @ spec eval_quoted_with_env ( Macro . t ( ) , binding , Macro.Env . t ( ) , [ eval_opt ] ) ::
12231245 { term , binding , Macro.Env . t ( ) }
12241246 def eval_quoted_with_env ( quoted , binding , % Macro.Env { } = env , opts \\ [ ] )
12251247 when is_list ( binding ) do
0 commit comments