Skip to content

Commit

Permalink
API for storing and retrieving "private" data
Browse files Browse the repository at this point in the history
Adds an API that allows a consumer of Luerl to store private data that
can be retrieved from functions called within Lua. This is useful in
applications that may need to retrieve secrets from within Luerl, but
don't want Lua scripts to be able to access these values.

I've chatted with a number of users of Luerl, including Sam Aaron, who
have had the need to expose secrets into their Luerl programs. Some have
achieved this by using closures to close over private data and expose
those functions to Luerl. I have used the process dictionary to acheive
the same thing.

If Luerl had an API to store and retrieve private data, as proposed by
this PR, then both techniques would be unnecessary.

Provide a `update_private/2` function instead that takes a function that
can modify the private map

- [ ] Get a nod from Robert
- [ ] Implement in other "Elixir" API
- [ ] Decide if we need to implement for "old"
- [ ] Tests
- [ ] Documentation
  • Loading branch information
davydog187 committed Nov 30, 2024
1 parent 7f1c5f4 commit 3362392
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/luerl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
%% Helping with storing VM state
-export([externalize/1,internalize/1]).

%% Storing and retrieving private data
-export([put_private/3,get_private/2]).

%% init() -> State.

init() ->
Expand Down Expand Up @@ -506,3 +509,10 @@ externalize(S) ->

internalize(S) ->
luerl_lib_math:internalize(S).

put_private(Key, Value, S) ->
Private = maps:put(Key, Value, S#luerl.private),
S#luerl{private=Private}.

get_private(Key, S) ->
maps:get(Key, S#luerl.private).
3 changes: 2 additions & 1 deletion src/luerl.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
rand, %Random state
tag, %Unique tag
trace_func=none, %Trace function
trace_data %Trace data
trace_data, %Trace data
private=#{}
}).

%% Table structure.
Expand Down

0 comments on commit 3362392

Please sign in to comment.