From 3362392a23e99a2ca9ffd381c4956525861de3d0 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Sat, 30 Nov 2024 13:59:08 -0500 Subject: [PATCH] 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 --- src/luerl.erl | 10 ++++++++++ src/luerl.hrl | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/luerl.erl b/src/luerl.erl index a90dfc1..ee04c49 100644 --- a/src/luerl.erl +++ b/src/luerl.erl @@ -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). diff --git a/src/luerl.hrl b/src/luerl.hrl index 21ce276..824dcac 100644 --- a/src/luerl.hrl +++ b/src/luerl.hrl @@ -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.