开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 2

紫星/LuaVM

forked from abin-liu/LuaVM
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (2)
master
main
master
分支 (2)
master
main
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (2)
master
main
LuaVM
/
OpenSourceProjects
/
LuaInterface
/
ObjectTranslator.cs
LuaVM
/
OpenSourceProjects
/
LuaInterface
/
ObjectTranslator.cs
ObjectTranslator.cs 26.03 KB
一键复制 编辑 原始数据 按行查看 历史
abin-liu 提交于 2021年06月11日 10:57 +08:00 . Initial commit on Gitee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using LuaInterface.Method;
using LuaInterface.Exceptions;
using LuaInterface.Extensions;
namespace LuaInterface
{
using LuaCore = KopiLua.Lua;
/*
* Passes objects from the CLR to Lua and vice-versa
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class ObjectTranslator
{
private LuaCore.lua_CFunction registerTableFunction, unregisterTableFunction, getMethodSigFunction,
getConstructorSigFunction, importTypeFunction, loadAssemblyFunction;
// object to object #
public readonly Dictionary<object, int> objectsBackMap = new Dictionary<object, int>();
// object # to object (FIXME - it should be possible to get object address as an object #)
public readonly Dictionary<int, object> objects = new Dictionary<int, object>();
internal EventHandlerContainer pendingEvents = new EventHandlerContainer();
private MetaFunctions metaFunctions;
private List<Assembly> assemblies;
internal CheckType typeChecker;
internal Lua interpreter;
/// <summary>
/// We want to ensure that objects always have a unique ID
/// </summary>
private int nextObj = 0;
public ObjectTranslator(Lua interpreter, LuaCore.lua_State luaState)
{
this.interpreter = interpreter;
typeChecker = new CheckType(this);
metaFunctions = new MetaFunctions(this);
assemblies = new List<Assembly>();
importTypeFunction = new LuaCore.lua_CFunction(this.importType);
loadAssemblyFunction = new LuaCore.lua_CFunction(this.loadAssembly);
registerTableFunction = new LuaCore.lua_CFunction(this.registerTable);
unregisterTableFunction = new LuaCore.lua_CFunction(this.unregisterTable);
getMethodSigFunction = new LuaCore.lua_CFunction(this.getMethodSignature);
getConstructorSigFunction = new LuaCore.lua_CFunction(this.getConstructorSignature);
createLuaObjectList(luaState);
createIndexingMetaFunction(luaState);
createBaseClassMetatable(luaState);
createClassMetatable(luaState);
createFunctionMetatable(luaState);
setGlobalFunctions(luaState);
}
/*
* Sets up the list of objects in the Lua side
*/
private void createLuaObjectList(LuaCore.lua_State luaState)
{
LuaLib.lua_pushstring(luaState, "luaNet_objects");
LuaLib.lua_newtable(luaState);
LuaLib.lua_newtable(luaState);
LuaLib.lua_pushstring(luaState, "__mode");
LuaLib.lua_pushstring(luaState, "v");
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_setmetatable(luaState, -2);
LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry);
}
/*
* Registers the indexing function of CLR objects
* passed to Lua
*/
private void createIndexingMetaFunction(LuaCore.lua_State luaState)
{
LuaLib.lua_pushstring(luaState, "luaNet_indexfunction");
LuaLib.luaL_dostring(luaState, MetaFunctions.luaIndexFunction); // steffenj: lua_dostring renamed to luaL_dostring
//LuaLib.lua_pushstdcallcfunction(luaState, indexFunction);
LuaLib.lua_rawset(luaState, (int)LuaIndexes.Registry);
}
/*
* Creates the metatable for superclasses (the base
* field of registered tables)
*/
private void createBaseClassMetatable(LuaCore.lua_State luaState)
{
LuaLib.luaL_newmetatable(luaState, "luaNet_searchbase");
LuaLib.lua_pushstring(luaState, "__gc");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__tostring");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__index");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.baseIndexFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__newindex");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_settop(luaState, -2);
}
/*
* Creates the metatable for type references
*/
private void createClassMetatable(LuaCore.lua_State luaState)
{
LuaLib.luaL_newmetatable(luaState, "luaNet_class");
LuaLib.lua_pushstring(luaState, "__gc");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__tostring");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__index");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.classIndexFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__newindex");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.classNewindexFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__call");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.callConstructorFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_settop(luaState, -2);
}
/*
* Registers the global functions used by LuaInterface
*/
private void setGlobalFunctions(LuaCore.lua_State luaState)
{
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.indexFunction);
LuaLib.lua_setglobal(luaState, "get_object_member");
LuaLib.lua_pushstdcallcfunction(luaState, importTypeFunction);
LuaLib.lua_setglobal(luaState, "import_type");
LuaLib.lua_pushstdcallcfunction(luaState, loadAssemblyFunction);
LuaLib.lua_setglobal(luaState, "load_assembly");
LuaLib.lua_pushstdcallcfunction(luaState, registerTableFunction);
LuaLib.lua_setglobal(luaState, "make_object");
LuaLib.lua_pushstdcallcfunction(luaState, unregisterTableFunction);
LuaLib.lua_setglobal(luaState, "free_object");
LuaLib.lua_pushstdcallcfunction(luaState, getMethodSigFunction);
LuaLib.lua_setglobal(luaState, "get_method_bysig");
LuaLib.lua_pushstdcallcfunction(luaState, getConstructorSigFunction);
LuaLib.lua_setglobal(luaState, "get_constructor_bysig");
}
/*
* Creates the metatable for delegates
*/
private void createFunctionMetatable(LuaCore.lua_State luaState)
{
LuaLib.luaL_newmetatable(luaState, "luaNet_function");
LuaLib.lua_pushstring(luaState, "__gc");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__call");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.execDelegateFunction);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_settop(luaState, -2);
}
/*
* Passes errors (argument e) to the Lua interpreter
*/
internal void throwError(LuaCore.lua_State luaState, object e)
{
// We use this to remove anything pushed by luaL_where
int oldTop = LuaLib.lua_gettop(luaState);
// Stack frame #1 is our C# wrapper, so not very interesting to the user
// Stack frame #2 must be the lua code that called us, so that's what we want to use
LuaLib.luaL_where(luaState, 1);
var curlev = popValues(luaState, oldTop);
// Determine the position in the script where the exception was triggered
string errLocation = string.Empty;
if(curlev.Length > 0)
errLocation = curlev[0].ToString();
string message = e as string;
if(!message.IsNull())
{
// Wrap Lua error (just a string) and store the error location
e = new LuaScriptException(message, errLocation);
}
else
{
var ex = e as Exception;
if(!ex.IsNull())
{
// Wrap generic .NET exception as an InnerException and store the error location
e = new LuaScriptException(ex, errLocation);
}
}
push(luaState, e);
LuaLib.lua_error(luaState);
}
/*
* Implementation of load_assembly. Throws an error
* if the assembly is not found.
*/
private int loadAssembly(LuaCore.lua_State luaState)
{
try
{
string assemblyName = LuaLib.lua_tostring(luaState, 1).ToString();
Assembly assembly = null;
try
{
assembly = Assembly.Load(assemblyName);
}
catch(BadImageFormatException)
{
// The assemblyName was invalid. It is most likely a path.
}
if(assembly.IsNull())
assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyName));
if(!assembly.IsNull() && !assemblies.Contains(assembly))
assemblies.Add(assembly);
}
catch(Exception e)
{
throwError(luaState, e);
}
return 0;
}
internal Type FindType(string className)
{
foreach(var assembly in assemblies)
{
var klass = assembly.GetType(className);
if(!klass.IsNull())
return klass;
}
return null;
}
/*
* Implementation of import_type. Returns nil if the
* type is not found.
*/
private int importType(LuaCore.lua_State luaState)
{
string className = LuaLib.lua_tostring(luaState, 1).ToString();
var klass = FindType(className);
if(!klass.IsNull())
pushType(luaState, klass);
else
LuaLib.lua_pushnil(luaState);
return 1;
}
/*
* Implementation of make_object. Registers a table (first
* argument in the stack) as an object subclassing the
* type passed as second argument in the stack.
*/
private int registerTable(LuaCore.lua_State luaState)
{
if(LuaLib.lua_type(luaState, 1) == LuaTypes.Table)
{
var luaTable = getTable(luaState, 1);
string superclassName = LuaLib.lua_tostring(luaState, 2).ToString();
if(!superclassName.IsNull())
{
var klass = FindType(superclassName);
if(!klass.IsNull())
{
// Creates and pushes the object in the stack, setting
// it as the metatable of the first argument
object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable);
pushObject(luaState, obj, "luaNet_metatable");
LuaLib.lua_newtable(luaState);
LuaLib.lua_pushstring(luaState, "__index");
LuaLib.lua_pushvalue(luaState, -3);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_pushstring(luaState, "__newindex");
LuaLib.lua_pushvalue(luaState, -3);
LuaLib.lua_settable(luaState, -3);
LuaLib.lua_setmetatable(luaState, 1);
// Pushes the object again, this time as the base field
// of the table and with the luaNet_searchbase metatable
LuaLib.lua_pushstring(luaState, "base");
int index = addObject(obj);
pushNewObject(luaState, obj, index, "luaNet_searchbase");
LuaLib.lua_rawset(luaState, 1);
}
else
throwError(luaState, "register_table: can not find superclass '" + superclassName + "'");
}
else
throwError(luaState, "register_table: superclass name can not be null");
}
else
throwError(luaState, "register_table: first arg is not a table");
return 0;
}
/*
* Implementation of free_object. Clears the metatable and the
* base field, freeing the created object for garbage-collection
*/
private int unregisterTable(LuaCore.lua_State luaState)
{
try
{
if(LuaLib.lua_getmetatable(luaState, 1) != 0)
{
LuaLib.lua_pushstring(luaState, "__index");
LuaLib.lua_gettable(luaState, -2);
object obj = getRawNetObject(luaState, -1);
if(obj.IsNull())
throwError(luaState, "unregister_table: arg is not valid table");
var luaTableField = obj.GetType().GetField("__luaInterface_luaTable");
if(luaTableField.IsNull())
throwError(luaState, "unregister_table: arg is not valid table");
luaTableField.SetValue(obj, null);
LuaLib.lua_pushnil(luaState);
LuaLib.lua_setmetatable(luaState, 1);
LuaLib.lua_pushstring(luaState, "base");
LuaLib.lua_pushnil(luaState);
LuaLib.lua_settable(luaState, 1);
}
else
throwError(luaState, "unregister_table: arg is not valid table");
}
catch(Exception e)
{
throwError(luaState, e.Message);
}
return 0;
}
/*
* Implementation of get_method_bysig. Returns nil
* if no matching method is not found.
*/
private int getMethodSignature(LuaCore.lua_State luaState)
{
IReflect klass;
object target;
int udata = LuaLib.luanet_checkudata(luaState, 1, "luaNet_class");
if(udata != -1)
{
klass = (IReflect)objects[udata];
target = null;
}
else
{
target = getRawNetObject(luaState, 1);
if(target.IsNull())
{
throwError(luaState, "get_method_bysig: first arg is not type or object reference");
LuaLib.lua_pushnil(luaState);
return 1;
}
klass = target.GetType();
}
string methodName = LuaLib.lua_tostring(luaState, 2).ToString();
var signature = new Type[LuaLib.lua_gettop(luaState)-2];
for(int i = 0; i < signature.Length; i++)
signature[i] = FindType(LuaLib.lua_tostring(luaState, i+3).ToString());
try
{
//CP: Added ignore case
var method = klass.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static |
BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase, null, signature, null);
pushFunction(luaState, new LuaCore.lua_CFunction((new LuaMethodWrapper(this, target, klass, method)).call));
}
catch(Exception e)
{
throwError(luaState, e);
LuaLib.lua_pushnil(luaState);
}
return 1;
}
/*
* Implementation of get_constructor_bysig. Returns nil
* if no matching constructor is found.
*/
private int getConstructorSignature(LuaCore.lua_State luaState)
{
IReflect klass = null;
int udata = LuaLib.luanet_checkudata(luaState, 1, "luaNet_class");
if(udata != -1)
klass = (IReflect)objects[udata];
if(klass.IsNull())
throwError(luaState, "get_constructor_bysig: first arg is invalid type reference");
var signature = new Type[LuaLib.lua_gettop(luaState)-1];
for(int i = 0; i < signature.Length; i++)
signature[i] = FindType(LuaLib.lua_tostring(luaState, i+2).ToString());
try
{
ConstructorInfo constructor = klass.UnderlyingSystemType.GetConstructor(signature);
pushFunction(luaState, new LuaCore.lua_CFunction((new LuaMethodWrapper(this, null, klass, constructor)).call));
}
catch(Exception e)
{
throwError(luaState, e);
LuaLib.lua_pushnil(luaState);
}
return 1;
}
/*
* Pushes a type reference into the stack
*/
internal void pushType(LuaCore.lua_State luaState, Type t)
{
pushObject(luaState, new ProxyType(t), "luaNet_class");
}
/*
* Pushes a delegate into the stack
*/
internal void pushFunction(LuaCore.lua_State luaState, LuaCore.lua_CFunction func)
{
pushObject(luaState, func, "luaNet_function");
}
/*
* Pushes a CLR object into the Lua stack as an userdata
* with the provided metatable
*/
internal void pushObject(LuaCore.lua_State luaState, object o, string metatable)
{
int index = -1;
// Pushes nil
if(o.IsNull())
{
LuaLib.lua_pushnil(luaState);
return;
}
// Object already in the list of Lua objects? Push the stored reference.
bool found = objectsBackMap.TryGetValue(o, out index);
if(found)
{
LuaLib.luaL_getmetatable(luaState, "luaNet_objects");
LuaLib.lua_rawgeti(luaState, -1, index);
// Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweep
// occurs, but the actual call of the __gc finalizer for that object may not happen until a little while later. During that window we might call
// this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called. In that case, we go ahead and call collect
// object here
// did we find a non nil object in our table? if not, we need to call collect object
var type = LuaLib.lua_type(luaState, -1);
if(type != LuaTypes.Nil)
{
LuaLib.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack
return;
}
// MetaFunctions.dumpStack(this, luaState);
LuaLib.lua_remove(luaState, -1); // remove the nil object value
LuaLib.lua_remove(luaState, -1); // remove the metatable
collectObject(o, index); // Remove from both our tables and fall out to get a new ID
}
index = addObject(o);
pushNewObject(luaState, o, index, metatable);
}
/*
* Pushes a new object into the Lua stack with the provided
* metatable
*/
private void pushNewObject(LuaCore.lua_State luaState, object o, int index, string metatable)
{
if(metatable == "luaNet_metatable")
{
// Gets or creates the metatable for the object's type
LuaLib.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName);
if(LuaLib.lua_isnil(luaState, -1))
{
LuaLib.lua_settop(luaState, -2);
LuaLib.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName);
LuaLib.lua_pushstring(luaState, "cache");
LuaLib.lua_newtable(luaState);
LuaLib.lua_rawset(luaState, -3);
LuaLib.lua_pushlightuserdata(luaState, LuaLib.luanet_gettag());
LuaLib.lua_pushnumber(luaState, 1);
LuaLib.lua_rawset(luaState, -3);
LuaLib.lua_pushstring(luaState, "__index");
LuaLib.lua_pushstring(luaState, "luaNet_indexfunction");
LuaLib.lua_rawget(luaState, (int)LuaIndexes.Registry);
LuaLib.lua_rawset(luaState, -3);
LuaLib.lua_pushstring(luaState, "__gc");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaLib.lua_rawset(luaState, -3);
LuaLib.lua_pushstring(luaState, "__tostring");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
LuaLib.lua_rawset(luaState, -3);
LuaLib.lua_pushstring(luaState, "__newindex");
LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
LuaLib.lua_rawset(luaState, -3);
}
}
else
LuaLib.luaL_getmetatable(luaState, metatable);
// Stores the object index in the Lua list and pushes the
// index into the Lua stack
LuaLib.luaL_getmetatable(luaState, "luaNet_objects");
LuaLib.luanet_newudata(luaState, index);
LuaLib.lua_pushvalue(luaState, -3);
LuaLib.lua_remove(luaState, -4);
LuaLib.lua_setmetatable(luaState, -2);
LuaLib.lua_pushvalue(luaState, -1);
LuaLib.lua_rawseti(luaState, -3, index);
LuaLib.lua_remove(luaState, -2);
}
/*
* Gets an object from the Lua stack with the desired type, if it matches, otherwise
* returns null.
*/
internal object getAsType(LuaCore.lua_State luaState, int stackPos, Type paramType)
{
var extractor = typeChecker.checkType(luaState, stackPos, paramType);
return !extractor.IsNull() ? extractor(luaState, stackPos) : null;
}
/// <summary>
/// Given the Lua int ID for an object remove it from our maps
/// </summary>
/// <param name = "udata"></param>
internal void collectObject(int udata)
{
object o;
bool found = objects.TryGetValue(udata, out o);
// The other variant of collectObject might have gotten here first, in that case we will silently ignore the missing entry
if(found)
{
// Debug.WriteLine("Removing " + o.ToString() + " @ " + udata);
objects.Remove(udata);
objectsBackMap.Remove(o);
}
}
/// <summary>
/// Given an object reference, remove it from our maps
/// </summary>
/// <param name = "udata"></param>
private void collectObject(object o, int udata)
{
// Debug.WriteLine("Removing " + o.ToString() + " @ " + udata);
objects.Remove(udata);
objectsBackMap.Remove(o);
}
private int addObject(object obj)
{
// New object: inserts it in the list
int index = nextObj++;
// Debug.WriteLine("Adding " + obj.ToString() + " @ " + index);
objects[index] = obj;
objectsBackMap[obj] = index;
return index;
}
/*
* Gets an object from the Lua stack according to its Lua type.
*/
internal object getObject(LuaCore.lua_State luaState, int index)
{
var type = LuaLib.lua_type(luaState, index);
switch(type)
{
case LuaTypes.Number:
{
return LuaLib.lua_tonumber(luaState, index);
}
case LuaTypes.String:
{
return LuaLib.lua_tostring(luaState, index);
}
case LuaTypes.Boolean:
{
return LuaLib.lua_toboolean(luaState, index);
}
case LuaTypes.Table:
{
return getTable(luaState, index);
}
case LuaTypes.Function:
{
return getFunction(luaState, index);
}
case LuaTypes.UserData:
{
int udata = LuaLib.luanet_tonetobject(luaState, index);
return udata != -1 ? objects[udata] : getUserData(luaState, index);
}
default:
return null;
}
}
/*
* Gets the table in the index positon of the Lua stack.
*/
internal LuaTable getTable(LuaCore.lua_State luaState, int index)
{
LuaLib.lua_pushvalue(luaState, index);
return new LuaTable(LuaLib.lua_ref(luaState, 1), interpreter);
}
/*
* Gets the userdata in the index positon of the Lua stack.
*/
internal LuaUserData getUserData(LuaCore.lua_State luaState, int index)
{
LuaLib.lua_pushvalue(luaState, index);
return new LuaUserData(LuaLib.lua_ref(luaState, 1), interpreter);
}
/*
* Gets the function in the index positon of the Lua stack.
*/
internal LuaFunction getFunction(LuaCore.lua_State luaState, int index)
{
LuaLib.lua_pushvalue(luaState, index);
return new LuaFunction(LuaLib.lua_ref(luaState, 1), interpreter);
}
/*
* Gets the CLR object in the index positon of the Lua stack. Returns
* delegates as Lua functions.
*/
internal object getNetObject(LuaCore.lua_State luaState, int index)
{
int idx = LuaLib.luanet_tonetobject(luaState, index);
return idx != -1 ? objects[idx] : null;
}
/*
* Gets the CLR object in the index positon of the Lua stack. Returns
* delegates as is.
*/
internal object getRawNetObject(LuaCore.lua_State luaState, int index)
{
int udata = LuaLib.luanet_rawnetobj(luaState, index);
return udata != -1 ? objects[udata] : null;
}
/*
* Pushes the entire array into the Lua stack and returns the number
* of elements pushed.
*/
internal int returnValues(LuaCore.lua_State luaState, object[] returnValues)
{
if(LuaLib.lua_checkstack(luaState, returnValues.Length+5))
{
for(int i = 0; i < returnValues.Length; i++)
push(luaState, returnValues[i]);
return returnValues.Length;
}
else
return 0;
}
/*
* Gets the values from the provided index to
* the top of the stack and returns them in an array.
*/
internal object[] popValues(LuaCore.lua_State luaState, int oldTop)
{
int newTop = LuaLib.lua_gettop(luaState);
if(oldTop == newTop)
return null;
else
{
var returnValues = new ArrayList();
for(int i = oldTop+1; i <= newTop; i++)
returnValues.Add(getObject(luaState, i));
LuaLib.lua_settop(luaState, oldTop);
return returnValues.ToArray();
}
}
/*
* Gets the values from the provided index to
* the top of the stack and returns them in an array, casting
* them to the provided types.
*/
internal object[] popValues(LuaCore.lua_State luaState, int oldTop, Type[] popTypes)
{
int newTop = LuaLib.lua_gettop(luaState);
if(oldTop == newTop)
return null;
else
{
int iTypes;
var returnValues = new ArrayList();
if(popTypes[0] == typeof(void))
iTypes = 1;
else
iTypes = 0;
for(int i = oldTop+1; i <= newTop; i++)
{
returnValues.Add(getAsType(luaState, i, popTypes[iTypes]));
iTypes++;
}
LuaLib.lua_settop(luaState, oldTop);
return returnValues.ToArray();
}
}
// kevinh - the following line doesn't work for remoting proxies - they always return a match for 'is'
// else if(o is ILuaGeneratedType)
private static bool IsILua(object o)
{
if(o is ILuaGeneratedType)
{
// Make sure we are _really_ ILuaGenerated
var typ = o.GetType();
return (!typ.GetInterface("ILuaGeneratedType").IsNull ());
}
else
return false;
}
/*
* Pushes the object into the Lua stack according to its type.
*/
internal void push(LuaCore.lua_State luaState, object o)
{
if(o.IsNull())
LuaLib.lua_pushnil(luaState);
else if(o is sbyte || o is byte || o is short || o is ushort ||
o is int || o is uint || o is long || o is float ||
o is ulong || o is decimal || o is double)
{
double d = Convert.ToDouble(o);
LuaLib.lua_pushnumber(luaState, d);
}
else if(o is char)
{
double d = (char)o;
LuaLib.lua_pushnumber(luaState, d);
}
else if(o is string)
{
string str = (string)o;
LuaLib.lua_pushstring(luaState, str);
}
else if(o is bool)
{
bool b = (bool)o;
LuaLib.lua_pushboolean(luaState, b);
}
else if(IsILua(o))
(((ILuaGeneratedType)o).luaInterface_getLuaTable()).push(luaState);
else if(o is LuaTable)
((LuaTable)o).push(luaState);
else if(o is LuaCore.lua_CFunction)
pushFunction(luaState, (LuaCore.lua_CFunction)o);
else if(o is LuaFunction)
((LuaFunction)o).push(luaState);
else
pushObject(luaState, o, "luaNet_metatable");
}
/*
* Checks if the method matches the arguments in the Lua stack, getting
* the arguments if it does.
*/
internal bool matchParameters(LuaCore.lua_State luaState, MethodBase method, ref MethodCache methodCache)
{
return metaFunctions.matchParameters(luaState, method, ref methodCache);
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

基于C#的Lua虚拟机,包含Lua代码编辑器、图形化Lua运行环境,以及若干个内置Lua插件(实现常用的的Win32窗体操作、屏幕像素抓取与识别、键盘鼠标模拟输入等基础功能)。代码中包含插件开发API,可以通过开发插件的方法对Lua环境的功能进行无限扩充。
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/zixing/LuaVM.git
git@gitee.com:zixing/LuaVM.git
zixing
LuaVM
LuaVM
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /