Bug?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug?

Giles Bathgate-2
Whilst looking at the OpenSCAD source I noticed a potential
coding/design bug that results in the following behaviour

module test(a,b)
{
        a=1;
        b=2;
        echo(a,b,c);
}

test(c=3);

output:

ECHO 1,2,3

In the code it seems that the names of the arguments passed to the
module instantiation (line 8) are used to populate variables inside the
module regardless of the parameters defined in the module declaration
(line 1). Which means that you can pass an argument for parameter c that
isn't even declared.

I am not sure if its a serious bug, maybe its just another weirdness of
OpenSCAD like variables that wont vary.

Regards

Giles




Reply | Threaded
Open this post in threaded view
|

Re: Bug?

Whosawhatsis
Maybe a little odd, but given how the language works, I would actually expect this behavior. For one thing, it allows special variables (those beginning with '$') to be passed to a module without needing to appear in its definition.

On Wednesday, December 22, 2010 at 1:11 PM, Giles Bathgate wrote:

Whilst looking at the OpenSCAD source I noticed a potential
coding/design bug that results in the following behaviour

module test(a,b)
{
a=1;
b=2;
echo(a,b,c);
}

test(c=3);

output:

ECHO 1,2,3

In the code it seems that the names of the arguments passed to the
module instantiation (line 8) are used to populate variables inside the
module regardless of the parameters defined in the module declaration
(line 1). Which means that you can pass an argument for parameter c that
isn't even declared.

I am not sure if its a serious bug, maybe its just another weirdness of
OpenSCAD like variables that wont vary.

Regards

Giles



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: Bug?

Giles Bathgate-2
On Wed, 2010-12-22 at 13:17 -0800, Whosawhatsis wrote:
> Maybe a little odd, but given how the language works, I would actually
> expect this behavior. For one thing, it allows special variables
> (those beginning with '$') to be passed to a module without needing to
> appear in its definition.

Yeah, although the code seems to go to quite great length to filter out
those "special" variables (actually called config_variables in the code)
and keep them in a separate hash table. If its possible to filter them
as such, then it should be possible to check that the non-special
arguments match up to the declared parameters.  

Another thing in general about special variables, that I just
remembered, is that they are not defined at the parser level, they are
defined in the tokenizer as a valid first character in the
TOK_IDENTIFIER token meaning
the following openscad script is valid:

function $_() = 3;

module $test($a=1,$b=2)
{
 echo("IDENTIFIER!?");
}

$test(1,2);

Again its not a real problem, just feels like a bit of a kludge to me.

Regards

Giles



Reply | Threaded
Open this post in threaded view
|

Re: Bug?

kintel
Administrator
In reply to this post by Giles Bathgate-2
On Dec 22, 2010, at 22:11 PM, Giles Bathgate wrote:

> In the code it seems that the names of the arguments passed to the
> module instantiation (line 8) are used to populate variables inside the
> module regardless of the parameters defined in the module declaration
> (line 1).

OpenSCAD will happily eat any variable assignment inside a module call and pass that on as module parameters.
I agree that this makes is easy to write unreadable code and that we should consider to do more strict checking.
The only con I can think of atm. is that this may break existing scripts, so this would be a natural candidate for a V2 version of the scad language, which is not really being planned, but I've got a bunch of things which could be improved.

I'll start a section on this in TODO.txt.

 -Marius


Reply | Threaded
Open this post in threaded view
|

Re: Bug?

kintel
Administrator
In reply to this post by Giles Bathgate-2
On Dec 22, 2010, at 22:11 PM, Giles Bathgate wrote:

> In the code it seems that the names of the arguments passed to the
> module instantiation (line 8) are used to populate variables inside the
> module regardless of the parameters defined in the module declaration
> (line 1).

OpenSCAD will happily eat any variable assignment inside a module call and pass that on as module parameters.
I agree that this makes is easy to write unreadable code and that we should consider to do more strict checking.
The only con I can think of atm. is that this may break existing scripts, so this would be a natural candidate for a V2 version of the scad language, which is not really being planned, but I've got a bunch of things which could be improved.

I'll start a section on this in TODO.txt.

-Marius


Reply | Threaded
Open this post in threaded view
|

Re: Bug?

Giles Bathgate-2
On 26 December 2010 15:48, Marius Kintel <[hidden email]> wrote:
> OpenSCAD will happily eat any variable assignment inside a module call and pass that on as module parameters.
> I agree that this makes is easy to write unreadable code and that we should consider to do more strict checking.
> The only con I can think of atm. is that this may break existing scripts, so this would be a natural candidate for a V2 version of the scad language, which is not really being planned, but I've got a bunch of things which could be improved.
>
> I'll start a section on this in TODO.txt.
>
> -Marius

Well I figured it might be helpful to issue a warning to the user at
this stage for the purpose of diagnosing scripts that have this issue.

commit 91cb3f369520e864723b7454e6abaeea06d5ac1a
in the parametersbug branch of the gitorious clone contains a patch
that implements such a warning.

Its intelligent enough not to issue the warning if the argument passed
is a "special" variable.

http://gitorious.org/openscad/openscad/commit/91cb3f369520e864723b7454e6abaeea06d5ac1a

Regards

Giles