Re: JExpr.tc patch
Carl-Adam Brengesjö <[email protected]>
| Newsgroups | gmane.comp.gnu.dotgnu.developer |
|---|---|
| Message-ID | <[email protected]> |
Here's two patches with an sample .js file. These patches fixes three problems with JScript. Firstly, Eval(JAdd) tried to typecast the objects when added two values if one was a string. This is incorrect and an typecast exception was thrown. The correct solution is the ToString() method. The second problem was using arrays. var a = [1, "foo", 0.32]; print(a.length); would output "0". This problem is fixed with the ArrayObject.cs patch. But I discovered another problem afterwards: a.length now returned 1, instead of the expected 3. Reason to this was that Eval(JArrayLiteral) didn't increase the value of index after each loop. On Wednesday 05 May 2004 15.00, Carl-Adam Brengesjö wrote: > Some more cases should/will be added... Sorry for this patch - I was preparing for this mail and apparently my mail client sends the mail on ctrl+enter. :) just ignore it.
JExpr.tc.patch
(text/x-diff, 534 B)
--- pnetlib.orig/JScript/Nodes/JExpr.tc 2004-05-05 16:32:51.000000000 +0200
+++ pnetlib/JScript/Nodes/JExpr.tc 2004-05-05 22:32:15.000000000 +0200
@@ -49,7 +49,7 @@
int index = 0;
while(elem != null)
{
- value[index] = elem.expr.Eval(engine);
+ value[index++] = elem.expr.Eval(engine);
elem = elem.next;
}
@@ -468,7 +468,7 @@
DefaultValueHint.None);
if(value1 is String || value2 is String)
{
- return ((String)value1) + ((String)value2);
+ return value1.ToString() + value2.ToString();
}
else
{
ArrayObject.cs.patch
(text/x-diff, 855 B)
--- pnetlib.orig/JScript/Builtins/ArrayObject.cs 2004-05-05 16:32:51.000000000 +0200
+++ pnetlib/JScript/Builtins/ArrayObject.cs 2004-05-05 22:26:25.000000000 +0200
@@ -151,7 +151,30 @@
// Put a property to this object by numeric index.
internal override void PutIndex(int index, Object value)
{
- // TODO
+ if (index < 0)
+ {
+ throw new ArgumentException();
+ }
+ uint newlen = (uint)(index + 1);
+ if (array == null)
+ {
+ array = new Object[newlen];
+ }
+ if (newlen > arrayLen)
+ {
+ arrayLen = newlen;
+ }
+ if (array.Length <= index)
+ {
+ Object[] a2 = new Object[newlen];
+ array.CopyTo(a2, 0);
+ array = a2;
+ array.SetValue(value, index);
+ }
+ else
+ {
+ array.SetValue(value, index);
+ }
}
// Determine if this object has a specific property.
hello.js
(text/plain, 128 B)
var a = [1,"foo",0.32];
print("a.length = " + a.length);
print("a[0] = ", a[0]);
print("a[1] = ", a[1]);
print("a[2] = ", a[2]);