Skip to content

Commit

Permalink
Solved issues #92 and #93 (varargs bugs and added checks for illegal …
Browse files Browse the repository at this point in the history
…cross-scripts accesses)
  • Loading branch information
xanathar committed Jun 15, 2015
1 parent 4bf7446 commit aecf50c
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/DevTools/MoonSharp.VmDebugger/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void Console_WriteLine(string fmt, params object[] args)

private void DebugScript(string filename)
{
m_Script = new Script(CoreModules.Preset_Complete);
m_Script = new Script(CoreModules.Preset_HardSandbox);
m_Script.Options.UseLuaErrorLocations = true;
m_Script.Options.DebugPrint = s => { Console_WriteLine("{0}", s); };

Expand Down
12 changes: 12 additions & 0 deletions src/MoonSharp.Interpreter.Tests/EndToEnd/SimpleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,18 @@ public void Simple_Delegate_Interop_2()
}
}

[Test]
public void MissingArgsDefaultToNil()
{
Script S = new Script(CoreModules.None);
DynValue res = S.DoString(@"
function test(a)
return a;
end
test();
");
}

}
}
2 changes: 1 addition & 1 deletion src/MoonSharp.Interpreter.Tests/EndToEnd/TailCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void CheckToString()
return tostring(9)";


Script S = new Script();
Script S = new Script(CoreModules.Basic);
var res = S.DoString(script);

Assert.AreEqual(DataType.String, res.Type);
Expand Down
94 changes: 94 additions & 0 deletions src/MoonSharp.Interpreter.Tests/EndToEnd/VarargsTupleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace MoonSharp.Interpreter.Tests.EndToEnd
{
[TestFixture]
public class VarargsTupleTests
{


private void DoTest(string code, string expectedResult)
{
Script S = new Script();

S.DoString(@"
function f(a,b)
local debug = 'a: ' .. tostring(a) .. ' b: ' .. tostring(b)
return debug
end
function g(a, b, ...)
local debug = 'a: ' .. tostring(a) .. ' b: ' .. tostring(b)
local arg = {...}
debug = debug .. ' arg: {'
for k, v in pairs(arg) do
debug = debug .. tostring(v) .. ', '
end
debug = debug .. '}'
return debug
end
function r()
return 1, 2, 3
end
function h(...)
return g(...)
end
function i(...)
return g('extra', ...)
end
");
DynValue res = S.DoString("return " + code);

Assert.AreEqual(res.Type, DataType.String);
Assert.AreEqual(expectedResult, res.String);
}

[Test]
public void VarArgsTuple_Basic()
{
DoTest("f(3)", "a: 3 b: nil");
DoTest("f(3,4)", "a: 3 b: 4");
DoTest("f(3,4,5)", "a: 3 b: 4");
DoTest("f(r(),10)", "a: 1 b: 10");
DoTest("f(r())", "a: 1 b: 2");
}

[Test]
public void VarArgsTuple_Intermediate()
{
DoTest("g(3) ", "a: 3 b: nil arg: {}");
DoTest("g(3,4) ", "a: 3 b: 4 arg: {}");
DoTest("g(3,4,5,8) ", "a: 3 b: 4 arg: {5, 8, }");
DoTest("g(5,r()) ", "a: 5 b: 1 arg: {2, 3, }");
}

[Test]
public void VarArgsTuple_Advanced()
{
//DoTest("h(3) ", "a: 3 b: nil arg: {}");
//DoTest("h(3,4) ", "a: 3 b: 4 arg: {}");
//DoTest("h(3,4,5,8) ", "a: 3 b: 4 arg: {5, }");
DoTest("h(5,r()) ", "a: 5 b: 1 arg: {2, 3, }");
}

[Test]
public void VarArgsTuple_Advanced2()
{
DoTest("i(3) ", "a: extra b: 3 arg: {}");
DoTest("i(3,4) ", "a: extra b: 3 arg: {4, }");
DoTest("i(3,4,5,8) ", "a: extra b: 3 arg: {4, 5, 8, }");
DoTest("i(5,r()) ", "a: extra b: 5 arg: {1, 2, 3, }");
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<Compile Include="EndToEnd\UserDataEventsTests.cs" />
<Compile Include="EndToEnd\UserDataEnumsTest.cs" />
<Compile Include="EndToEnd\UserDataNestedTypesTests.cs" />
<Compile Include="EndToEnd\VarargsTupleTests.cs" />
<Compile Include="EndToEnd\VtUserDataPropertiesTests.cs" />
<Compile Include="EndToEnd\VtUserDataOverloadsTests.cs" />
<Compile Include="EndToEnd\VtUserDataMethodsTests.cs" />
Expand Down
4 changes: 4 additions & 0 deletions src/MoonSharp.Interpreter.Tests/TestMore/309-os.t
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ is(r, nil, "function execute")
is(s, 'exit')
type_ok(n, 'number')

--[===[ -- Tests commented as currently they are more likely to fail because of OS configuration than implementation details
cmd = lua .. [[ -e "print '# hello from external Lua'; os.exit(2)"]]
r, s, n = os.execute(cmd)
is(r, nil)
Expand Down Expand Up @@ -118,6 +120,8 @@ else
skip("io.popen not supported", 5)
end
--]===]

is(os.getenv('__IMPROBABLE__'), nil, "function getenv")

user = os.getenv('LOGNAME') or os.getenv('USERNAME')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
<Compile Include="..\..\EndToEnd\StringLibTests.cs">
<Link>StringLibTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\StructAssignmentTechnique.cs">
<Link>StructAssignmentTechnique.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\TailCallTests.cs">
<Link>TailCallTests.cs</Link>
</Compile>
Expand All @@ -110,6 +113,9 @@
<Compile Include="..\..\EndToEnd\UserDataNestedTypesTests.cs">
<Link>UserDataNestedTypesTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\VarargsTupleTests.cs">
<Link>VarargsTupleTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\VtUserDataPropertiesTests.cs">
<Link>VtUserDataPropertiesTests.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
<Compile Include="..\..\EndToEnd\StringLibTests.cs">
<Link>StringLibTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\StructAssignmentTechnique.cs">
<Link>StructAssignmentTechnique.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\TailCallTests.cs">
<Link>TailCallTests.cs</Link>
</Compile>
Expand All @@ -126,6 +129,9 @@
<Compile Include="..\..\EndToEnd\UserDataNestedTypesTests.cs">
<Link>UserDataNestedTypesTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\VarargsTupleTests.cs">
<Link>VarargsTupleTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\VtUserDataPropertiesTests.cs">
<Link>VtUserDataPropertiesTests.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require 'Test.More'

plan(54)

local lua = "lua.exe"
local lua = "lua"

clk = os.clock()
type_ok(clk, 'number', "function clock")
Expand Down Expand Up @@ -79,17 +79,19 @@ is(r, nil, "function execute")
is(s, 'exit')
type_ok(n, 'number')

--[===[ -- Tests commented as currently they are more likely to fail because of OS configuration than implementation details
cmd = lua .. [[ -e "print '# hello from external Lua'; os.exit(2)"]]
r, s, n = os.execute(cmd)
is(r, nil)
is(s, 'exit', "function execute & exit")
is(n, 2, "exit value")
is(n, 2, "exit value 1")
cmd = lua .. [[ -e "print '# hello from external Lua'; os.exit(false)"]]
r, s, n = os.execute(cmd)
is(r, nil)
is(s, 'exit', "function execute & exit")
is(n, 1, "exit value")
is(n, 1, "exit value 2")
-- cmd = lua .. [[ -e "print '# hello from external Lua'; os.exit(true, true)"]]
-- is(os.execute(cmd), true, "function execute & exit")
Expand All @@ -113,11 +115,13 @@ if r then
r, s, n = f:close()
is(r, nil)
is(s, 'exit', "exit code")
is(n, 3, "exit value")
is(n, 3, "exit value 3")
else
skip("io.popen not supported", 5)
end
--]===]

is(os.getenv('__IMPROBABLE__'), nil, "function getenv")

user = os.getenv('LOGNAME') or os.getenv('USERNAME')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
<Compile Include="..\..\EndToEnd\StringLibTests.cs">
<Link>StringLibTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\StructAssignmentTechnique.cs">
<Link>StructAssignmentTechnique.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\TailCallTests.cs">
<Link>TailCallTests.cs</Link>
</Compile>
Expand All @@ -126,6 +129,9 @@
<Compile Include="..\..\EndToEnd\UserDataNestedTypesTests.cs">
<Link>UserDataNestedTypesTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\VarargsTupleTests.cs">
<Link>VarargsTupleTests.cs</Link>
</Compile>
<Compile Include="..\..\EndToEnd\VtUserDataPropertiesTests.cs">
<Link>VtUserDataPropertiesTests.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require 'Test.More'

plan(54)

local lua = "lua.exe"
local lua = "lua"

clk = os.clock()
type_ok(clk, 'number', "function clock")
Expand Down Expand Up @@ -79,17 +79,19 @@ is(r, nil, "function execute")
is(s, 'exit')
type_ok(n, 'number')

--[===[ -- Tests commented as currently they are more likely to fail because of OS configuration than implementation details
cmd = lua .. [[ -e "print '# hello from external Lua'; os.exit(2)"]]
r, s, n = os.execute(cmd)
is(r, nil)
is(s, 'exit', "function execute & exit")
is(n, 2, "exit value")
is(n, 2, "exit value 1")
cmd = lua .. [[ -e "print '# hello from external Lua'; os.exit(false)"]]
r, s, n = os.execute(cmd)
is(r, nil)
is(s, 'exit', "function execute & exit")
is(n, 1, "exit value")
is(n, 1, "exit value 2")
-- cmd = lua .. [[ -e "print '# hello from external Lua'; os.exit(true, true)"]]
-- is(os.execute(cmd), true, "function execute & exit")
Expand All @@ -113,11 +115,13 @@ if r then
r, s, n = f:close()
is(r, nil)
is(s, 'exit', "exit code")
is(n, 3, "exit value")
is(n, 3, "exit value 3")
else
skip("io.popen not supported", 5)
end
--]===]

is(os.getenv('__IMPROBABLE__'), nil, "function getenv")

user = os.getenv('LOGNAME') or os.getenv('USERNAME')
Expand Down
5 changes: 5 additions & 0 deletions src/MoonSharp.Interpreter/DataTypes/Coroutine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public System.Collections.IEnumerator AsUnityCoroutine()
/// <exception cref="System.InvalidOperationException">Only non-CLR coroutines can be resumed with this overload of the Resume method. Use the overload accepting a ScriptExecutionContext instead</exception>
public DynValue Resume(params DynValue[] args)
{
this.CheckScriptOwnership(args);

if (Type == CoroutineType.Coroutine)
return m_Processor.Coroutine_Resume(args);
else
Expand All @@ -151,6 +153,9 @@ public DynValue Resume(params DynValue[] args)
/// <returns></returns>
public DynValue Resume(ScriptExecutionContext context, params DynValue[] args)
{
this.CheckScriptOwnership(context);
this.CheckScriptOwnership(args);

if (Type == CoroutineType.Coroutine)
return m_Processor.Coroutine_Resume(args);
else if (Type == CoroutineType.ClrCallback)
Expand Down
12 changes: 11 additions & 1 deletion src/MoonSharp.Interpreter/DataTypes/DynValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public sealed class DynValue




/// <summary>
/// Creates a new writable value initialized to Nil.
/// </summary>
Expand Down Expand Up @@ -666,6 +665,17 @@ public bool CastToBool()
else return (rv.Type != DataType.Nil && rv.Type != DataType.Void);
}

/// <summary>
/// Returns this DynValue as an instance of <see cref="IScriptPrivateResource"/>, if possible,
/// null otherwise
/// </summary>
/// <returns>False if value is false or nil, true otherwise.</returns>
public IScriptPrivateResource GetAsPrivateResource()
{
return m_Object as IScriptPrivateResource;
}


/// <summary>
/// Converts a tuple to a scalar value. If it's already a scalar value, this function returns "this".
/// </summary>
Expand Down
Loading

0 comments on commit aecf50c

Please sign in to comment.