Jscript & mushclient arrays

Posted by Prattler on Wed 11 Jan 2006 07:20 PM — 8 posts, 33,471 views.

Lithuania #0
I'm desperate.. I believe this is a bug and a very strange one.. :(

Code:
---
ArraySet("player_records", wildcards[0], ArrayExport("temp_player_record", ","));

note (ArrayGet("player_records", wildcards[0]));

// this should be the same command but lagged
DoAfterSpecial(0.1, "note (ArrayGet(\"player_records\", \"" + wildcards[0] + "\"))", 12);
---

Here is what I get:
1136752032844,lvl\,968\,qp\,56373\,gq\,14\,cp\,466,1137009997339,lvl\,1003\,qp\,57773\,gq\,14\,cp\,480
1136752032844,lvl\,968\,qp\,56373\,gq\,14\,cp\,466,1137009997339,lvl\,1003\,qp\,57773\,gq\,14\,dp\,480
(these should be identical, but notice the c and d at the end of those lines)

1136757632175,lvl\,707\,qp\,42422\,gq\,4\,cp\,220,1137010191078,lvl\,707\,qp\,42439\,gq\,4\,cp\,220
1136757632175,lvl\,707\,qp\,42422\,gq\,4\,cp\,220,1137010191078,lvl\,707\,qp\,42439\,gq\,4\,cp\-220
(now the "," and "-")

And so on. Seems that at exactly that place the charcode increases by one. How can this be? How can they differ at all? From what I've tested it's not a problem with the note function. Something with memory/the way arrays work?

Confused Pratter
Amended on Wed 11 Jan 2006 07:21 PM by Prattler
Australia Forum Administrator #1
It would help to know what wildcards [0] is, but it seems to be you are doing different things here.

Let's say wildcards [0] is "abc".

Now your first line is:


var key = "abc";
note (ArrayGet("player_records", key));


Your second line is:


note (ArrayGet("player_records", "abc"));


You may say these are the same, but what if there are funny characters in the key? Like quote symbols, or backslashes? Then the two methods will behave differently.

Lithuania #2
After about 5 hours of trying to duplicate it.. I THINK I might have something.. It's a lot of reading, but please spare some time.

-----------------------------------------
function doTesting() {
AddTrigger("testing_trigger", "test", "",
eTriggerRegularExpression + eEnabled + eReplace + eTemporary,
NOCHANGE, 0, "", "triggerFired");

// need something to fire the trigger, the mud supports echo, but it can be anything
world.Send("echo test");
}

function triggerFired() {
var date = new Date();
var time = date.getTime();

ArrayCreate("large_array");

ArrayCreate("small_array");
ArrayClear("small_array");

ArrayImport("small_array", ArrayGet("large_array", "same_key"), ",");

var line = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
ArraySet("small_array", time, line);

ArraySet("large_array", "same_key", ArrayExport("small_array", ","));

note(ArrayGet("large_array", "same_key"));
DoAfterSpecial(0.1,"note (ArrayGet(\"large_array\", \"same_key\"))",12);
}
-----------------------------------------

I fire doTesting() twice and I get exactly what I want:
test
1137099506087,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1137099506087,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

test
1137099506087,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,1137099509121,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1137099506087,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,1137099509121,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

now the fun part.. we change the script by adding
DeleteTrigger("testing_trigger");
in the second function. Everything else is left the same:
-----------------------------------------
function doTesting() {
AddTrigger("testing_trigger", "test", "",
eTriggerRegularExpression + eEnabled + eReplace + eTemporary,
NOCHANGE, 0, "", "triggerFired");

// need something to fire the trigger, the mud supports echo, but it can be anything
world.Send("echo test");
}

function triggerFired() {
var date = new Date();
var time = date.getTime();

DeleteTrigger("testing_trigger");

ArrayCreate("large_array");

ArrayCreate("small_array");
ArrayClear("small_array");

ArrayImport("small_array", ArrayGet("large_array", "same_key"), ",");

var line = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
ArraySet("small_array", time, line);

ArraySet("large_array", "same_key", ArrayExport("small_array", ","));

note(ArrayGet("large_array", "same_key"));
DoAfterSpecial(0.1,"note (ArrayGet(\"large_array\", \"same_key\"))",12);
}
-----------------------------------------

I clear the value by ArraySet("large_array", "same_key", "")
run doTesting() twice again.

Now I get:
test
1137099525825,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1137099525825,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

test
1137099525825,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,1137099526646,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1137099525825,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,1137099526646,aaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaa

Notice the b almost at the end of the second line. Please please tell me something is wrong here.. Deleting the trigger shouldn't do anything!
Amended on Thu 12 Jan 2006 08:14 PM by Prattler
Australia Forum Administrator #3
I'm looking into it. When I did your test I got MUSHclient to crash. You seem to be onto something here, not sure what it is yet.
Australia Forum Administrator #4
My initial reaction here, is that you should not delete a trigger that is in the middle of executing a script. That is, do not delete yourself. This is generally bad programming practice.

I'm not certain whether or not this is causing your problem, but you seem to have isolated it to being related to the trigger deletion.




OK - after a bit more checking I think I can account for the "up by 1" result you are seeing.

MUSHclient executes a script for a trigger, and after the script finishes it adds 1 to the "count of script invocations" for that trigger. You can see this count when you look at a trigger in the trigger window.

You have deleted the trigger while the script was executing, thus invalidating the internal pointer to the trigger structure. Then, when MUSHclient added 1 to the script count, it actually corrupted some piece of memory, as you noticed, or in my case, crashed the client.

I suggest you work around this in a couple of ways:

  • Don't delete the trigger - simply disable it. That is valid, as the trigger still exists.
  • Use "DoAfter" to delete the trigger a moment later, perhaps after disabling it first. That way you are not deleting it while it is executing a script.


I do not really regard this as a bug - self-deleting code will tend to fail in many languages.
Lithuania #5
Yes, the workaround is really simple. But the bug is so hard to spot that people in the future might have serious problems. What I think would be good is:

Either adding a line in the function descriptions of DeleteTrigger, DeleteAlias, etc. about such usage.
or
Mushclient doing some check to see if the trigger/alias still exists.

Anyway, good thing I got over this :) Was driving me crazy!
USA #6
Heh Nick. One solution to this glitch would be to flag the trigger/alias/timer as being in the process of executing somehow, then when a delete function is called, generate an actual runtime error, such as:

"<type>(es/s) cannot be deleted while in use. Try disabling the <type> instead."

Since the client wouldn't actually delete it, you avoid the bugs and crashes, while effectively warning people not to do this. This issue has existed for a very long time and I have run into it myself back then. I believe we determined it is safe to do *only* if the deletion happens as the *last* command executed in the script, but that is far from certain either. An error would be far better imho than adding a note to the help file and letting some twit that doesn't read it try anyway.
Australia Forum Administrator #7
It is probably not even safe as the last thing in a script as the "add 1 to script executions" would still happen, randomly adding 1 to some memory location.

I agree it is best if the client does not crash, and have added a test that now stops triggers/aliases/timers from being deleted while they are in the process of executing a script, either in the script file or by "send to script".

This simply returns another error code from DeleteTrigger (or whatever) which you can test for.

This at least stops the crashes, and if are wondering why the trigger is still there you can examine the error code to find out why, and then restructure your code so it doesn't happen.

Also amended the help files to reflect these changes.