LoadMethods.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MoonSharp.Interpreter.Execution;
  6. namespace MoonSharp.Interpreter.CoreLib
  7. {
  8. [MoonSharpModule]
  9. public class LoadMethods
  10. {
  11. // load (ld [, source [, mode [, env]]])
  12. // ----------------------------------------------------------------
  13. // Loads a chunk.
  14. //
  15. // If ld is a string, the chunk is this string.
  16. //
  17. // If there are no syntactic errors, returns the compiled chunk as a function;
  18. // otherwise, returns nil plus the error message.
  19. //
  20. // source is used as the source of the chunk for error messages and debug
  21. // information (see §4.9). When absent, it defaults to ld, if ld is a string,
  22. // or to "=(load)" otherwise.
  23. //
  24. // The string mode is ignored, and assumed to be "t";
  25. [MoonSharpMethod]
  26. public static DynValue load(ScriptExecutionContext executionContext, CallbackArguments args)
  27. {
  28. try
  29. {
  30. Script S = executionContext.GetOwnerScript();
  31. DynValue ld = args.AsType(0, "load", DataType.String, false);
  32. DynValue source = args.AsType(1, "load", DataType.String, true);
  33. DynValue env = args.AsType(3, "load", DataType.Table, true);
  34. DynValue fn = S.LoadString(ld.String,
  35. !env.IsNil() ? env.Table : null,
  36. !source.IsNil() ? source.String : "=(load)");
  37. return fn;
  38. }
  39. catch (SyntaxErrorException ex)
  40. {
  41. return DynValue.NewTuple(DynValue.Nil, DynValue.NewString(ex.Message));
  42. }
  43. }
  44. // loadfile ([filename [, mode [, env]]])
  45. // ----------------------------------------------------------------
  46. // Similar to load, but gets the chunk from file filename or from the standard input,
  47. // if no file name is given. INCOMPAT: stdin not supported, mode ignored
  48. [MoonSharpMethod]
  49. public static DynValue loadfile(ScriptExecutionContext executionContext, CallbackArguments args)
  50. {
  51. try
  52. {
  53. Script S = executionContext.GetOwnerScript();
  54. DynValue filename = args.AsType(0, "loadfile", DataType.String, false);
  55. DynValue env = args.AsType(2, "loadfile", DataType.Table, true);
  56. DynValue fn = S.LoadFile(filename.String, env.IsNil() ? null : env.Table);
  57. return fn;
  58. }
  59. catch (SyntaxErrorException ex)
  60. {
  61. return DynValue.NewTuple(DynValue.Nil, DynValue.NewString(ex.Message));
  62. }
  63. }
  64. //dofile ([filename])
  65. //--------------------------------------------------------------------------------------------------------------
  66. //Opens the named file and executes its contents as a Lua chunk. When called without arguments,
  67. //dofile executes the contents of the standard input (stdin). Returns all values returned by the chunk.
  68. //In case of errors, dofile propagates the error to its caller (that is, dofile does not run in protected mode).
  69. [MoonSharpMethod]
  70. public static DynValue dofile(ScriptExecutionContext executionContext, CallbackArguments args)
  71. {
  72. try
  73. {
  74. Script S = executionContext.GetOwnerScript();
  75. DynValue v = args.AsType(0, "dofile", DataType.String, false);
  76. DynValue fn = S.LoadFile(v.String);
  77. return DynValue.NewTailCallReq(fn); // tail call to dofile
  78. }
  79. catch (SyntaxErrorException ex)
  80. {
  81. throw new ScriptRuntimeException(ex);
  82. }
  83. }
  84. //require (modname)
  85. //----------------------------------------------------------------------------------------------------------------
  86. //Loads the given module. The function starts by looking into the package.loaded table to determine whether
  87. //modname is already loaded. If it is, then require returns the value stored at package.loaded[modname].
  88. //Otherwise, it tries to find a loader for the module.
  89. //
  90. //To find a loader, require is guided by the package.loaders array. By changing this array, we can change
  91. //how require looks for a module. The following explanation is based on the default configuration for package.loaders.
  92. //
  93. //First require queries package.preload[modname]. If it has a value, this value (which should be a function)
  94. //is the loader. Otherwise require searches for a Lua loader using the path stored in package.path.
  95. //If that also fails, it searches for a C loader using the path stored in package.cpath. If that also fails,
  96. //it tries an all-in-one loader (see package.loaders).
  97. //
  98. //Once a loader is found, require calls the loader with a single argument, modname. If the loader returns any value,
  99. //require assigns the returned value to package.loaded[modname]. If the loader returns no value and has not assigned
  100. //any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the
  101. //final value of package.loaded[modname].
  102. //
  103. //If there is any error loading or running the module, or if it cannot find any loader for the module, then require
  104. //signals an error.
  105. [MoonSharpMethod]
  106. public static DynValue __require_clr_impl(ScriptExecutionContext executionContext, CallbackArguments args)
  107. {
  108. Script S = executionContext.GetOwnerScript();
  109. DynValue v = args.AsType(0, "__require_clr_impl", DataType.String, false);
  110. DynValue fn = S.RequireModule(v.String);
  111. return fn; // tail call to dofile
  112. }
  113. [MoonSharpMethod]
  114. public const string require = @"
  115. function(modulename)
  116. if (package == nil) then package = { }; end
  117. if (package.loaded == nil) then package.loaded = { }; end
  118. local m = package.loaded[modulename];
  119. if (m ~= nil) then
  120. return m;
  121. end
  122. local func = __require_clr_impl(modulename);
  123. local res = func();
  124. package.loaded[modulename] = res;
  125. return res;
  126. end
  127. ";
  128. }
  129. }