From 0d3c7cec6f455c91f81024f1ef6227d94263a63b Mon Sep 17 00:00:00 2001 From: mfranz Date: Mon, 15 Oct 2007 16:30:37 +0000 Subject: [PATCH] - setlistener(): add wrapper function to turn ghost arguments into props.Nodes - "modernization": use more named function arguments and "var" keywords --- Nasal/globals.nas | 34 +++++++++++++++++++--------------- Nasal/gui.nas | 4 ++-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Nasal/globals.nas b/Nasal/globals.nas index b973fac25..2057561c2 100644 --- a/Nasal/globals.nas +++ b/Nasal/globals.nas @@ -2,8 +2,7 @@ # Returns true if the first object is an instance of the second # (class) object. Example: isa(someObject, props.Node) # -isa = func { - obj = arg[0]; class = arg[1]; +var isa = func(obj, class) { if(obj == nil or !contains(obj, "parents")) { return 0; } foreach(c; obj.parents) { if(c == class) { return 1; } @@ -19,7 +18,7 @@ isa = func { # string, in which case it specifies a path in the global property # tree. # -fgcommand = func(cmd, node=nil) { +var fgcommand = func(cmd, node=nil) { if(isa(node, props.Node)) node = node._g; _fgcommand(cmd, node); } @@ -30,12 +29,12 @@ fgcommand = func(cmd, node=nil) { # the ghost handle to the argument and wraps it in a # props.Node object. # -cmdarg = func { props.wrapNode(_cmdarg()) } +var cmdarg = func { props.wrapNode(_cmdarg()) } ## # Utility. Does what it you think it does. # -abs = func { if(arg[0] < 0) { -arg[0] } else { arg[0] } } +var abs = func(v) { return v < 0 ? -v : v } ## # Convenience wrapper for the _interpolate function. Takes a @@ -56,10 +55,10 @@ abs = func { if(arg[0] < 0) { -arg[0] } else { arg[0] } } # 16 seconds. Note the use of zero-time interpolation between 360 and # 0 to wrap the interpolated value properly. # -interpolate = func { - if(isa(arg[0], props.Node)) { arg[0] = arg[0]._g; } - elsif(typeof(arg[0]) != "scalar") { return; } - _interpolate(arg[0], size(arg) == 1 ? [] : subvec(arg, 1)); +var interpolate = func(node, val...) { + if(isa(node, props.Node)) node = node._g; + elsif(typeof(node) != "scalar") return; + _interpolate(node, val); } @@ -69,10 +68,15 @@ interpolate = func { # listened to property, a function in arg[1], and an optional # bool in arg[2], which triggers the function initially if true. # -setlistener = func { - if(isa(arg[0], props.Node)) { arg[0] = arg[0]._g; } - var id = _setlistener(arg[0], arg[1], size(arg) > 2 ? arg[2] : 0, - size(arg) > 3 ? arg[3] : 1); +var setlistener = func(node, fun, init=0, runtime=1) { + if(isa(node, props.Node)) node = node._g; + var propghost = ghosttype(node); + var id = _setlistener(node, func { + forindex (var i; arg) + if(ghosttype(arg[i]) == propghost) + arg[i] = props.wrapNode(arg[i], nil); + call(fun, arg); + }, init, runtime); if(__.log_level <= 2) { var c = caller(1); print(sprintf("setting listener #%d in %s, line %s", id, c[2], c[3])) @@ -87,7 +91,7 @@ setlistener = func { # you can use varname in an expression without a undefined symbol # error. # -defined = func(sym) { +var defined = func(sym) { var fn = 1; while((var frame = caller(fn)) != nil) { if(contains(frame[0], sym)) { return 1; } @@ -106,7 +110,7 @@ defined = func(sym) { __ = {}; __.dbg_types = { none:0, bulk:1, debug:2, info:3, warn:4, alert:5 }; __.log_level = __.dbg_types[getprop("/sim/logging/priority")]; -printlog = func(level, args...) { +var printlog = func(level, args...) { if(__.dbg_types[level] >= __.log_level) { call(print, args); } } diff --git a/Nasal/gui.nas b/Nasal/gui.nas index 765f0fbfc..0e1b3efbd 100644 --- a/Nasal/gui.nas +++ b/Nasal/gui.nas @@ -85,8 +85,8 @@ settimer(INIT, 1); ## # Show/hide the fps display dialog. # -fpsDisplay = func { - var w = (caller(0)[0]["arg"] == nil) ? cmdarg().getBoolValue() : arg[0]; +var fpsDisplay = func(n) { + var w = isa(n, props.Node) ? n.getValue() : n; fgcommand(w ? "dialog-show" : "dialog-close", props.Node.new({"dialog-name": "fps"})); }