Adds tostring and type functions

This commit is contained in:
Deukhoofd 2018-11-24 14:49:20 +01:00
parent 194e7236c4
commit 2dc59c5f8b
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 41 additions and 2 deletions

View File

@ -46,7 +46,7 @@ namespace Upsilon.BaseTypes
while (baseEnumerator.MoveNext())
{
var key = baseEnumerator.Current;
if (key.Type == Type.Nil)
if (key == null || key.Type == Type.Nil)
break;
var value = BaseIterator.GetValueFromIndex(key);
if (value.Type == Type.Nil)

View File

@ -37,7 +37,6 @@ namespace Upsilon.StandardLibraries
return new PairsScriptIterator(table);
}
[StandardLibraryScriptFunction("tonumber")]
public ScriptNumber ToNumber(ScriptString obj)
{
@ -51,5 +50,18 @@ namespace Upsilon.StandardLibraries
return new ScriptNumberLong(long.Parse(str));
}
}
[StandardLibraryScriptFunction("tostring")]
public ScriptString ToString(ScriptType obj)
{
return new ScriptString(obj.ToString());
}
[StandardLibraryScriptFunction("type")]
public ScriptString Type(ScriptType obj)
{
return new ScriptString(obj.Type.ToString());
}
}
}

View File

@ -73,5 +73,32 @@ return value
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(284, result);
}
[Fact]
public void ToStringTest()
{
const string input = @"
return tostring(100)
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal("100", result);
}
[Fact]
public void TypeTest()
{
const string input = @"
return type(100)
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal("Number", result);
}
}
}