Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for storing and retrieving "private" data #195

Merged
merged 8 commits into from
Dec 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
API for storing and retrieving "private" data
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
davydog187 committed Nov 30, 2024
commit 989d67b22df287987d1471398630d1e72693dcd1
10 changes: 10 additions & 0 deletions src/luerl.erl
Original file line number Diff line number Diff line change
@@ -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() ->
@@ -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
@@ -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.