@@ -76,6 +76,10 @@ function fb_utils.redirect_table(t)
76
76
return setmetatable ({}, { __index = t })
77
77
end
78
78
79
+ function fb_utils .set_prototype (t , proto )
80
+ return setmetatable (t , { __index = proto })
81
+ end
82
+
79
83
-- prints an error if a coroutine returns an error
80
84
-- unlike the next function this one still returns the results of coroutine.resume()
81
85
function fb_utils .coroutine .resume_catch (...)
@@ -372,21 +376,27 @@ end
372
376
-- evaluates and runs the given string in both Lua 5.1 and 5.2
373
377
-- the name argument is used for error reporting
374
378
-- provides the mpv modules and the fb module to the string
375
- function fb_utils .evaluate_string (str , name )
376
- local env = fb_utils .redirect_table (_G )
377
- env .mp = fb_utils .redirect_table (mp )
378
- env .msg = fb_utils .redirect_table (msg )
379
- env .utils = fb_utils .redirect_table (utils )
380
- env .fb = fb_utils .redirect_table (fb_utils )
381
- env .input = input_loaded and fb_utils .redirect_table (input )
382
- env .user_input = user_input_loaded and fb_utils .redirect_table (user_input )
379
+ function fb_utils .evaluate_string (str , chunkname , custom_env , env_defaults )
380
+ local env
381
+ if env_defaults ~= false then
382
+ env = fb_utils .redirect_table (_G )
383
+ env .mp = fb_utils .redirect_table (mp )
384
+ env .msg = fb_utils .redirect_table (msg )
385
+ env .utils = fb_utils .redirect_table (utils )
386
+ env .fb = fb_utils .redirect_table (fb_utils )
387
+ env .input = input_loaded and fb_utils .redirect_table (input )
388
+ env .user_input = user_input_loaded and fb_utils .redirect_table (user_input )
389
+ env = fb_utils .set_prototype (custom_env or {}, env )
390
+ else
391
+ env = custom_env or {}
392
+ end
383
393
384
394
local chunk , err
385
395
if setfenv then
386
- chunk , err = loadstring (str , name )
396
+ chunk , err = loadstring (str , chunkname )
387
397
if chunk then setfenv (chunk , env ) end
388
398
else
389
- chunk , err = load (str , name , ' t' , env )
399
+ chunk , err = load (str , chunkname , ' t' , env )
390
400
end
391
401
if not chunk then
392
402
msg .warn (' failed to load string:' , str )
0 commit comments