summaryrefslogtreecommitdiff
path: root/src/func.cc
diff options
context:
space:
mode:
authorŁukasz Stelmach <stlman@poczta.fm>2013-08-18 15:18:43 (GMT)
committerŁukasz Stelmach <stlman@poczta.fm>2013-08-18 15:19:49 (GMT)
commit400d28d753aa8af8de60a7f82851ffdc3cdae672 (patch)
tree3e76d4c4449556c26d48073200956045bc092111 /src/func.cc
parentd67e0129161a98f852ed2685f43e7eb7c7538bf0 (diff)
Enable module stack introspection from within an SCAD script
The _current_module and _parent_module variables have been replaced by a built-in function parent_module(n). It takes one numeric parameter n, and returns n-th element from the module stack. If no argument provided, n defaults to 1 and the function returns the name of the direct parent module. If n is equal 0 current module name is returned.
Diffstat (limited to 'src/func.cc')
-rw-r--r--src/func.cc24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/func.cc b/src/func.cc
index 20f487a..865a2b4 100644
--- a/src/func.cc
+++ b/src/func.cc
@@ -528,6 +528,29 @@ Value builtin_version_num(const Context *ctx, const EvalContext *evalctx)
return Value(y * 10000 + m * 100 + d);
}
+Value builtin_parent_module(const Context *, const EvalContext *evalctx)
+{
+ int n;
+ double d;
+ int s = Module::stack_size();
+ if (evalctx->numArgs() == 0)
+ d=1; // parent module
+ else if (evalctx->numArgs() == 1 && evalctx->getArgValue(0).type() == Value::NUMBER)
+ evalctx->getArgValue(0).getDouble(d);
+ else
+ return Value();
+ n=trunc(d);
+ if (n < 0) {
+ PRINTB("WARNING: Negative parent module index (%d) not allowed", n);
+ return Value();
+ }
+ if (n >= s) {
+ PRINTB("WARNING: Parent module index (%d) greater than the number of modules on the stack", n);
+ return Value();
+ }
+ return Value(Module::stack_element(s - 1 - n));
+}
+
void register_builtin_functions()
{
Builtins::init("abs", new BuiltinFunction(&builtin_abs));
@@ -556,4 +579,5 @@ void register_builtin_functions()
Builtins::init("search", new BuiltinFunction(&builtin_search));
Builtins::init("version", new BuiltinFunction(&builtin_version));
Builtins::init("version_num", new BuiltinFunction(&builtin_version_num));
+ Builtins::init("parent_module", new BuiltinFunction(&builtin_parent_module));
}
contact: Jan Huwald // Impressum