Module cSandbox
cSandbox allows you to execute code in a controlled environment.
#
## How to use
-- create instance sandbox = cSandbox()
-- supply function sandbox.callback_str = "print('hello world')"
-- and call it... sandback.callback()
## Arguments and return values
-- if you need to supply arguments and define return value, -- one approach is to define a 'prefix' and 'suffix' -- (always added to the generated code)
-- define arguments sandbox.str_prefix = "local some_arg = select(1, ...)" -- define return value sandbox.str_suffix = "return some_arg" -- supply function sandbox.callback_str = "some_arg = some_arg + 'foo'".. "print(some_arg)"
-- now call the function like this: local result = sandback.callback('my_arg')
## Custom properties
TODO how to ...
Functions
compile () | call method to evaluate function (ensure that it's safe to run) |
contains_comment_blocks (str) | nested block comments/longstrings are depricated and will fail |
test_syntax (str_fn) | check for syntax errors (assert provides better error messages) |
strip_comments (str_fn) | strip code comments from a string |
contains_code (str_fn) | check if a given string consists of comments only |
insert_return (str_fn) | automatically insert a return statement into a code snippet step 1: detect if return statement is present |
rename_string_token (str_fn, old_name, new_name, prefix) | "safer" renaming of a string token (for example, a variable name) |
Fields
self.callback | function, compiled function (when set, always valid) |
self.str_prefix | string, code to insert into all generated functions |
self.compile_at_once | boolean, set to true for instant compilation of callback string |
self.callback_str | string, text representation of the function |
self.properties | properties can contain custom get/set methods |
self.modified_observable | invoked when callback has changed |
self.env | table, sandbox environment |
Functions
- compile ()
-
call method to evaluate function (ensure that it's safe to run)
Returns:
- boolean, true when method passed
- string, error message when failed
- contains_comment_blocks (str)
-
nested block comments/longstrings are depricated and will fail
Parameters:
- str
- test_syntax (str_fn)
-
check for syntax errors
(assert provides better error messages)
Parameters:
- str_fn (string) function as string
Returns:
- boolean, true when passed
- string, when failed
- strip_comments (str_fn)
-
strip code comments from a string
Parameters:
- str_fn (string)
Returns:
-
string
- contains_code (str_fn)
-
check if a given string consists of comments only
Parameters:
- str_fn (string)
Returns:
-
bool
- insert_return (str_fn)
-
automatically insert a return statement into a code snippet
step 1: detect if return statement is present
Parameters:
- str_fn
- rename_string_token (str_fn, old_name, new_name, prefix)
-
"safer" renaming of a string token (for example, a variable name)
Parameters:
- str_fn (string), the function text
- old_name (string)
- new_name (string)
- prefix (string), prepend to old/new name when defined
Fields
- self.callback
- function, compiled function (when set, always valid)
- self.str_prefix
- string, code to insert into all generated functions
- self.compile_at_once
- boolean, set to true for instant compilation of callback string
- self.callback_str
- string, text representation of the function
- self.properties
- properties can contain custom get/set methods
- self.modified_observable
- invoked when callback has changed
- self.env
- table, sandbox environment