Module.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package neko.vm;
  23. /**
  24. The abstract Neko module handle.
  25. **/
  26. @:callable
  27. @:coreType
  28. abstract ModuleHandle {}
  29. /**
  30. A Neko Module represent a execution unit for the Neko Virtual Machine.
  31. Each compiled `.n` bytecode file is a module once loaded by the NekoVM.
  32. **/
  33. @:haxe.warning("-WUnboundMonomorph")
  34. class Module {
  35. /**
  36. The abstract handle.
  37. **/
  38. public var m:ModuleHandle;
  39. public var name(get, set):String;
  40. public function new(m) {
  41. this.m = m;
  42. }
  43. /**
  44. Execute a module and returns its result (the latest evaluated expression).
  45. A module can be executed several times but its globals are only initialized once
  46. the first time the Module is loaded.
  47. **/
  48. public function execute():Dynamic {
  49. return _module_exec(m);
  50. }
  51. function get_name() {
  52. return new String(_module_name(m));
  53. }
  54. function set_name(n:String) {
  55. _module_set_name(m, untyped n.__s);
  56. return n;
  57. }
  58. /**
  59. Returns the Loader that this Module was loaded with.
  60. **/
  61. public function loader() {
  62. return new Loader(_module_loader(m));
  63. }
  64. /**
  65. Returns the codeSize of the Module.
  66. **/
  67. public function codeSize():Int {
  68. return _module_code_size(m);
  69. }
  70. /**
  71. Returns the number of globals in this Module global table.
  72. **/
  73. public function globalsCount():Int {
  74. return _module_nglobals(m);
  75. }
  76. /**
  77. Get a Module global value.
  78. **/
  79. public function getGlobal(n:Int):Dynamic {
  80. return _module_global_get(m, n);
  81. }
  82. /**
  83. Set a Module global value.
  84. **/
  85. public function setGlobal(n:Int, v:Dynamic) {
  86. _module_global_set(m, n, v);
  87. }
  88. public function toString() {
  89. return "[Module:" + name + "]";
  90. }
  91. /**
  92. Each Module has an export table which can be useful to transfert
  93. values between modules.
  94. **/
  95. public function getExports():Map<String, Dynamic> {
  96. var h = new haxe.ds.StringMap();
  97. var exp = _module_exports(m);
  98. for (f in Reflect.fields(exp))
  99. h.set(f, Reflect.field(exp, f));
  100. return h;
  101. }
  102. /**
  103. The raw export table.
  104. **/
  105. public function exportsTable():Dynamic {
  106. return _module_exports(m);
  107. }
  108. /**
  109. Set a value in the Module export table.
  110. **/
  111. public function setExport(name:String, value:Dynamic) {
  112. var exp = _module_exports(m);
  113. Reflect.setField(exp, name, value);
  114. }
  115. /**
  116. Returns the local Module, which is the one in which this
  117. method is included.
  118. **/
  119. public static function local() {
  120. return new Module(untyped __dollar__exports.__module);
  121. }
  122. /**
  123. Reads a module from an Input by using the given Loader.
  124. The module is initialized but has not yet been executed.
  125. **/
  126. public static function read(i:haxe.io.Input, l:Loader):Module {
  127. var m = _module_read(function(buf, pos, len) {
  128. return i.readBytes(untyped new haxe.io.Bytes(len, buf), pos, len);
  129. }, l.l);
  130. return new Module(m);
  131. }
  132. /**
  133. Reads a module from Bytes using the given Loader.
  134. The module is initialized but has not yet been executed.
  135. **/
  136. public static function readBytes(b:haxe.io.Bytes, loader:Loader):Module {
  137. return new Module(_module_read_string(b.getData(), loader.l));
  138. }
  139. /**
  140. Reads a module from a name and using the specified seach path and loader.
  141. The module is initialized but has not yet been executed.
  142. **/
  143. public static function readPath(name:String, path:Array<String>, loader:Loader) {
  144. var p = null;
  145. var i = path.length;
  146. while (--i >= 0)
  147. p = untyped __dollar__array(path[i].__s, p);
  148. var m = _module_read_path(p, untyped name.__s, loader.l);
  149. return new Module(m);
  150. }
  151. /**
  152. Extract the globals names from the given module
  153. **/
  154. public static function readGlobalsNames(i:haxe.io.Input) {
  155. if (i.readByte() != 0x4E || i.readByte() != 0x45 || i.readByte() != 0x4B || i.readByte() != 0x4F)
  156. throw "Not a neko file";
  157. function readInt() {
  158. return i.readInt32();
  159. }
  160. var nglobals = readInt();
  161. /*var nfields =*/ readInt();
  162. /*var codesize =*/ readInt();
  163. var a = new Array();
  164. for (k in 0...nglobals) {
  165. switch (i.readByte()) {
  166. case 1:
  167. a.push(i.readUntil(0));
  168. case 2:
  169. a.push("<fun:" + (readInt() & 0xFFFFFF) + ">");
  170. case 3:
  171. a.push("STRING:" + i.readString(i.readUInt16()));
  172. case 4:
  173. a.push("FLOAT:" + i.readUntil(0));
  174. case 5:
  175. a.push("DEBUG");
  176. case 6:
  177. a.push("VERSION " + i.readByte());
  178. default:
  179. throw "assert";
  180. }
  181. }
  182. return a;
  183. }
  184. function __compare(other:Module) {
  185. return untyped __dollar__compare(this.m, other.m);
  186. }
  187. static var _module_read = neko.Lib.load("std", "module_read", 2);
  188. static var _module_read_path = neko.Lib.load("std", "module_read_path", 3);
  189. static var _module_exec = neko.Lib.load("std", "module_exec", 1);
  190. static var _module_name = neko.Lib.load("std", "module_name", 1);
  191. static var _module_exports = neko.Lib.load("std", "module_exports", 1);
  192. static var _module_loader = neko.Lib.load("std", "module_loader", 1);
  193. static var _module_code_size = neko.Lib.load("std", "module_code_size", 1);
  194. static var _module_nglobals = neko.Lib.load("std", "module_nglobals", 1);
  195. static var _module_global_get = neko.Lib.load("std", "module_global_get", 2);
  196. static var _module_global_set = neko.Lib.load("std", "module_global_set", 3);
  197. static var _module_read_string = neko.Lib.loadLazy("std", "module_read_string", 2);
  198. static var _module_set_name = neko.Lib.loadLazy("std", "module_set_name", 2);
  199. }