Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
NLua
Commits
25f6cbf2
Commit
25f6cbf2
authored
Apr 01, 2013
by
Vinicius Jarina
Browse files
Added LoadCLRPackage method/tests
parent
ec1c354d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Core/NLua/Lua.cs
View file @
25f6cbf2
...
...
@@ -135,6 +135,114 @@ namespace NLua
"-- Preload the mscorlib assembly \n"
+
"luanet.load_assembly(\"mscorlib\") \n"
;
static
string
clr_package
=
@"---
--- This lua module provides auto importing of .net classes into a named package.
--- Makes for super easy use of LuaInterface glue
---
--- example:
--- Threading = CLRPackage(""System"", ""System.Threading"")
--- Threading.Thread.Sleep(100)
---
--- Extensions:
--- import() is a version of CLRPackage() which puts the package into a list which is used by a global __index lookup,
--- and thus works rather like C#'s using statement. It also recognizes the case where one is importing a local
--- assembly, which must end with an explicit .dll extension.
--- Alternatively, luanet.namespace can be used for convenience without polluting the global namespace:
--- local sys,sysi = luanet.namespace {'System','System.IO'}
-- sys.Console.WriteLine(""we are at {0}"",sysi.Directory.GetCurrentDirectory())
-- LuaInterface hosted with stock Lua interpreter will need to explicitly require this...
if not luanet then require 'luanet' end
local import_type, load_assembly = luanet.import_type, luanet.load_assembly
local mt = {
--- Lookup a previously unfound class and add it to our table
__index = function(package, classname)
local class = rawget(package, classname)
if class == nil then
class = import_type(package.packageName .. ""."" .. classname)
package[classname] = class -- keep what we found around, so it will be shared
end
return class
end
}
function luanet.namespace(ns)
if type(ns) == 'table' then
local res = {}
for i = 1,#ns do
res[i] = luanet.namespace(ns[i])
end
return unpack(res)
end
-- FIXME - table.packageName could instead be a private index (see Lua 13.4.4)
local t = { packageName = ns }
setmetatable(t,mt)
return t
end
local globalMT, packages
local function set_global_mt()
packages = {}
globalMT = {
__index = function(T,classname)
for i,package in ipairs(packages) do
local class = package[classname]
if class then
_G[classname] = class
return class
end
end
end
}
setmetatable(_G, globalMT)
end
--- Create a new Package class
function CLRPackage(assemblyName, packageName)
-- a sensible default...
packageName = packageName or assemblyName
local ok = pcall(load_assembly,assemblyName) -- Make sure our assembly is loaded
return luanet.namespace(packageName)
end
function import (assemblyName, packageName)
if not globalMT then
set_global_mt()
end
if not packageName then
local i = assemblyName:find('%.dll$')
if i then packageName = assemblyName:sub(1,i-1)
else packageName = assemblyName end
end
local t = CLRPackage(assemblyName,packageName)
table.insert(packages,t)
return t
end
function luanet.make_array (tp,tbl)
local arr = tp[#tbl]
for i,v in ipairs(tbl) do
arr:SetValue(v,i-1)
end
return arr
end
function luanet.each(o)
local e = o:GetEnumerator()
return function()
if e:MoveNext() then
return e.Current
end
end
end
"
;
#
region
Globals
auto
-
complete
/// <summary>
/// An alphabetically sorted list of all globals (objects, methods, etc.) externally added to this Lua instance
...
...
@@ -586,6 +694,10 @@ namespace NLua
CodeGeneration
.
Instance
.
RegisterLuaClassType
(
klass
,
luaClass
);
}
public
void
LoadCLRPackage
()
{
LuaLib
.
luaL_dostring
(
luaState
,
Lua
.
clr_package
);
}
/*
* Gets a function global variable as a delegate of
* type delegateType
...
...
NLua.IOS.sln
View file @
25f6cbf2
...
...
@@ -25,72 +25,6 @@ Global
Release|iPhoneSimulator = Release|iPhoneSimulator
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|Any CPU.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|Any CPU.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|Any CPU.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|Any CPU.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|iPhone.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|iPhoneSimulator.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|iPhone.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|Any CPU.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|iPhone.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|iPhone.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|Any CPU.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|Any CPU.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|Any CPU.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|Any CPU.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|iPhone.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|iPhoneSimulator.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|iPhone.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|Any CPU.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|iPhone.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|iPhone.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|iPhone.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|iPhone.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|iPhone.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|iPhone.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|iPhone.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|Any CPU.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|iPhone.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|iPhone.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{2DC0E43A-21B8-41FF-8793-60AB50350977}.Ad-Hoc|Any CPU.ActiveCfg = Ad-Hoc|iPhoneSimulator
{2DC0E43A-21B8-41FF-8793-60AB50350977}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone
{2DC0E43A-21B8-41FF-8793-60AB50350977}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone
...
...
@@ -113,11 +47,77 @@ Global
{2DC0E43A-21B8-41FF-8793-60AB50350977}.Release|iPhone.Build.0 = Release|iPhone
{2DC0E43A-21B8-41FF-8793-60AB50350977}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
{2DC0E43A-21B8-41FF-8793-60AB50350977}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|iPhone.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|iPhone.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|iPhone.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|iPhone.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|iPhone.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|Any CPU.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|iPhone.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|iPhone.Build.0 = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{3F57C208-AA13-4B67-B3E7-ED41307F00A0}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|Any CPU.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|Any CPU.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|Any CPU.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|Any CPU.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|iPhone.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.AppStore|iPhoneSimulator.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|iPhone.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|Any CPU.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|iPhone.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|iPhone.Build.0 = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{44DA9778-C0B0-4C6F-8617-79BA2E0F1361}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|Any CPU.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|Any CPU.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|Any CPU.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|Any CPU.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|iPhone.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.AppStore|iPhoneSimulator.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|iPhone.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|Any CPU.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|iPhone.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|iPhone.Build.0 = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{7064B157-DD57-4828-94C5-CA149B25CB40}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = ios\NLuaTestsiOS\NLuaTest.csproj
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Samples/CLRPackage.lua
View file @
25f6cbf2
---
--- This lua module provides auto importing of .net classes into a named package.
--- Makes for super easy use of
N
Lua glue
--- Makes for super easy use of Lua
Interface
glue
---
--- example:
--- example:
--- Threading = CLRPackage("System", "System.Threading")
--- Threading.Thread.Sleep(100)
---
--- Extensions:
--- import() is a version of CLRPackage() which puts the package into a list which is used by a global __index lookup,
--- and thus works rather like C#'s using statement. It also recognizes the case where one is importing a local
--- assembly, which must end with an explicit .dll extension.
--- Alternatively, luanet.namespace can be used for convenience without polluting the global namespace:
--- local sys,sysi = luanet.namespace {'System','System.IO'}
-- sys.Console.WriteLine("we are at {0}",sysi.Directory.GetCurrentDirectory())
-- LuaInterface hosted with stock Lua interpreter will need to explicitly require this...
if
not
luanet
then
require
'luanet'
end
local
import_type
,
load_assembly
=
luanet
.
import_type
,
luanet
.
load_assembly
local
mt
=
{
--- Lookup a previously unfound class and add it to our table
__index
=
function
(
package
,
classname
)
local
class
=
rawget
(
package
,
classname
)
if
class
==
nil
then
class
=
luanet
.
import_type
(
package
.
packageName
..
"."
..
classname
)
class
=
import_type
(
package
.
packageName
..
"."
..
classname
)
package
[
classname
]
=
class
-- keep what we found around, so it will be shared
end
return
class
end
}
}
function
luanet
.
namespace
(
ns
)
if
type
(
ns
)
==
'table'
then
local
res
=
{}
for
i
=
1
,
#
ns
do
res
[
i
]
=
luanet
.
namespace
(
ns
[
i
])
end
return
unpack
(
res
)
end
-- FIXME - table.packageName could instead be a private index (see Lua 13.4.4)
local
t
=
{
packageName
=
ns
}
setmetatable
(
t
,
mt
)
return
t
end
local
globalMT
,
packages
local
function
set_global_mt
()
packages
=
{}
globalMT
=
{
__index
=
function
(
T
,
classname
)
for
i
,
package
in
ipairs
(
packages
)
do
local
class
=
package
[
classname
]
if
class
then
_G
[
classname
]
=
class
return
class
end
end
end
}
setmetatable
(
_G
,
globalMT
)
end
--- Create a new Package class
function
CLRPackage
(
assemblyName
,
packageName
)
local
table
=
{}
luanet
.
load_assembly
(
assemblyName
)
-- Make sure our assembly is loaded
-- FIXME - table.packageName could instead be a private index (see Lua 13.4.4)
table
.
packageName
=
packageName
setmetatable
(
table
,
mt
)
return
table
-- a sensible default...
packageName
=
packageName
or
assemblyName
local
ok
=
pcall
(
load_assembly
,
assemblyName
)
-- Make sure our assembly is loaded
return
luanet
.
namespace
(
packageName
)
end
function
import
(
assemblyName
,
packageName
)
if
not
globalMT
then
set_global_mt
()
end
if
not
packageName
then
local
i
=
assemblyName
:
find
(
'%.dll$'
)
if
i
then
packageName
=
assemblyName
:
sub
(
1
,
i
-
1
)
else
packageName
=
assemblyName
end
end
local
t
=
CLRPackage
(
assemblyName
,
packageName
)
table.insert
(
packages
,
t
)
return
t
end
function
luanet
.
make_array
(
tp
,
tbl
)
local
arr
=
tp
[
#
tbl
]
for
i
,
v
in
ipairs
(
tbl
)
do
arr
:
SetValue
(
v
,
i
-
1
)
end
return
arr
end
function
luanet
.
each
(
o
)
local
e
=
o
:
GetEnumerator
()
return
function
()
if
e
:
MoveNext
()
then
return
e
.
Current
end
end
end
ios/NLuaTestsiOS/NLuaTest.csproj
View file @
25f6cbf2
...
...
@@ -126,6 +126,7 @@
<Compile
Include=
"..\..\tests\Core.cs"
>
<Link>
Core.cs
</Link>
</Compile>
<Compile
Include=
"NLuaTests.cs"
/>
</ItemGroup>
<Import
Project=
"$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets"
/>
<ItemGroup>
...
...
ios/NLuaTestsiOS/NLuaTests.cs
0 → 100644
View file @
25f6cbf2
using
System
;
using
NUnit.Framework
;
using
NLua
;
using
NLuaTest.Mock
;
#if MONOTOUCH
using
MonoTouch.Foundation
;
using
MonoTouch
;
#endif
namespace
NLuaTest
{
[
TestFixture
]
#
if
MONOTOUCH
[
Preserve
(
AllMembers
=
true
)]
#
endif
public
class
ANLuaTests
{
/*
* Tests capturing an exception
*/
[
Test
]
public
void
TestCLRPackage
()
{
using
(
Lua
lua
=
new
Lua
())
{
lua
.
LoadCLRPackage
();
lua
.
DoString
(
"import ('NLuaTest', 'NLuaTest.Mock') "
);
lua
.
DoString
(
"test = TestClass()"
);
lua
.
DoString
(
"test:setVal(3)"
);
object
[]
res
=
lua
.
DoString
(
"return test"
);
TestClass
test
=
(
TestClass
)
res
[
0
];
Assert
.
AreEqual
(
3
,
test
.
testval
);
}
}
#if MONOTOUCH
[
Test
]
public
void
TestUseNSUrl
()
{
using
(
Lua
lua
=
new
Lua
())
{
lua
.
LoadCLRPackage
();
lua
.
DoString
(
"import ('monotouch', 'MonoTouch.Foundation') "
);
lua
.
DoString
(
"testURL = NSUrl('http://nlua.org/?query=param')"
);
lua
.
DoString
(
"host = testURL.Host"
);
object
res
=
lua
[
"host"
];
string
host
=
(
string
)
res
;
Assert
.
AreEqual
(
"nlua.org"
,
host
);
}
}
#endif
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment