Discussion:
Test for an initialised object
David Hakim
2003-05-06 22:27:53 UTC
Permalink
re: Could we add an "initialised" field to the MotoVal struct, and set
it in motov_new?

Well, all variables in moto are given an initial value at declaration
time. For objects this default value is just null (for numerics and
other atomic types its 0).

Also, variables cannot be used without being declared. If a programmer
tries to use a variable without declaring it first motov will report an
error.

So moto really does not allow variable use prior to inilialization as
such.

Did you want to add this field so motov could report a warning to
programmers that they 'might' be using a variable that they did not yet
assign a value to ?

-Dave
Dave,
there is no method, as far as I see, to test in motov if an object has
been
initialised. So the only way I found, inside motox, to test for a
method call
/* If this is not an object... */
if (v1->type->kind != REF_TYPE) {
/* Nothing to do */
return FTAB_NONE;
}
/* Test for a null object */
if (mode == MODE_MOTOI || mode == MODE_MOTOC) {
if (v1->refval.value == NULL) {
THROW("NullPointerException",
"A method overloaded operator may not be identified on
null");
}
}
i.e throwing an exception in motoi and motoc.
Could we add an "initialised" field to the MotoVal struct, and set it
in
motov_new?
Stefano
Stefano Corsi
2003-05-07 00:34:02 UTC
Permalink
Post by David Hakim
Did you want to add this field so motov could report a warning to
programmers that they 'might' be using a variable that they did not yet
assign a value to ?
Yes, the problem is that if you try to lookup the correct method operator
(like += -- ++ *= and so on) in motox_try_overload_..., and the underlying
object is not initialized, you get a Null Pointer exception.

ex:

${
use "codex.util";

// SymbolTable i = new SymbolTable();
SymbolTable i;
i["dasd"] = "pippo";

}$

Uncaught NullPointerException
message:A method overloaded operator may not be identified on null

NullPointerException thrown in <main>(motox.c:228)

The same happens if you call a method on a non initialized object...
Wouldn't be possible (and maybe cleaner) to warn the user that he's using a
non-initialized object?

Stefano
David Hakim
2003-05-07 02:19:30 UTC
Permalink
If the NullPointerException is being thrown during interpretation (and
not during verification) than throwing the exception is correct
behavior. It is the same result that would be generated by a method
call on a null object.
Post by Stefano Corsi
Post by David Hakim
Did you want to add this field so motov could report a warning to
programmers that they 'might' be using a variable that they did not yet
assign a value to ?
Yes, the problem is that if you try to lookup the correct method operator
(like += -- ++ *= and so on) in motox_try_overload_..., and the underlying
object is not initialized, you get a Null Pointer exception.
During the lookup or during the function call ? The lookup itself
should not fail since the lookup should depend only on the MotoVal's
type, which should be set.
Post by Stefano Corsi
${
use "codex.util";
// SymbolTable i = new SymbolTable();
SymbolTable i;
i["dasd"] = "pippo";
}$
Uncaught NullPointerException
message:A method overloaded operator may not be identified on null
NullPointerException thrown in <main>(motox.c:228)
The same happens if you call a method on a non initialized object...
Wouldn't be possible (and maybe cleaner) to warn the user that he's using a
non-initialized object?
This is a non trivial task for a couple reasons.

First is requires a measure of data flow analysis. When we see a method
(or unary operator) called on a value we would need to be able to tell
that 'there exists a path of execution where by that value is never
explicitly assigned anything'. That test requires some significant
bookkeeping we don't do right now.

Second, since moto does give all variables default values already, we
would want this to be a warning (like in C), and not a verification
error . But when developing pages on a web-server through the
interpreter, there is really no good mechanism that I've thought of to
present warnings. We wouldn't want to send the warnings to the browser
since the page is capable of executing. But if we wrote the warnings to
the log ... who would read them :)

-Dave
Post by Stefano Corsi
Stefano
Loading...