Module.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. class Module {
  34. /**
  35. The abstract handle.
  36. **/
  37. public var m:ModuleHandle;
  38. public var name(get, set):String;
  39. public function new(m) {
  40. this.m = m;
  41. }
  42. /**
  43. Execute a module and returns its result (the latest evaluated expression).
  44. A module can be executed several times but its globals are only initialized once
  45. the first time the Module is loaded.
  46. **/
  47. public function execute():Dynamic {
  48. return _module_exec(m);
  49. }
  50. function get_name() {
  51. return new String(_module_name(m));
  52. }
  53. function set_name(n:String) {
  54. _module_set_name(m, untyped n.__s);
  55. return n;
  56. }
  57. /**
  58. Returns the Loader that this Module was loaded with.
  59. **/
  60. public function loader() {
  61. return new Loader(_module_loader(m));
  62. }
  63. /**
  64. Returns the codeSize of the Module.
  65. **/
  66. public function codeSize():Int {
  67. return _module_code_size(m);
  68. }
  69. /**
  70. Returns the number of globals in this Module global table.
  71. **/
  72. public function globalsCount():Int {
  73. return _module_nglobals(m);
  74. }
  75. /**
  76. Get a Module global value.
  77. **/
  78. public function getGlobal(n:Int):Dynamic {
  79. return _module_global_get(m, n);
  80. }
  81. /**
  82. Set a Module global value.
  83. **/
  84. public function setGlobal(n:Int, v:Dynamic) {
  85. _module_global_set(m, n, v);
  86. }
  87. public function toString() {
  88. return "[Module:" + name + "]";
  89. }
  90. /**
  91. Each Module has an export table which can be useful to transfert
  92. values between modules.
  93. **/
  94. public function getExports():Map<String, Dynamic> {
  95. var h = new haxe.ds.StringMap();
  96. var exp = _module_exports(m);
  97. for (f in Reflect.fields(exp))
  98. h.set(f, Reflect.field(exp, f));
  99. return h;
  100. }
  101. /**
  102. The raw export table.
  103. **/
  104. public function exportsTable():Dynamic {
  105. return _module_exports(m);
  106. }
  107. /**
  108. Set a value in the Module export table.
  109. **/
  110. public function setExport(name:String, value:Dynamic) {
  111. var exp = _module_exports(m);
  112. Reflect.setField(exp, name, value);
  113. }
  114. /**
  115. Returns the local Module, which is the one in which this
  116. method is included.
  117. **/
  118. public static function local() {
  119. return new Module(untyped __dollar__exports.__module);
  120. }
  121. /**
  122. Reads a module from an Input by using the given Loader.
  123. The module is initialized but has not yet been executed.
  124. **/
  125. public static function read(i:haxe.io.Input, l:Loader):Module {
  126. var m = _module_read(function(buf, pos, len) {
  127. return i.readBytes(untyped new haxe.io.Bytes(len, buf), pos, len);
  128. }, l.l);
  129. return new Module(m);
  130. }
  131. /**
  132. Reads a module from Bytes using the given Loader.
  133. The module is initialized but has not yet been executed.
  134. **/
  135. public static function readBytes(b:haxe.io.Bytes, loader:Loader):Module {
  136. return new Module(_module_read_string(b.getData(), loader.l));
  137. }
  138. /**
  139. Reads a module from a name and using the specified seach path and loader.
  140. The module is initialized but has not yet been executed.
  141. **/
  142. public static function readPath(name:String, path:Array<String>, loader:Loader) {
  143. var p = null;
  144. var i = path.length;
  145. while (--i >= 0)
  146. p = untyped __dollar__array(path[i].__s, p);
  147. var m = _module_read_path(p, untyped name.__s, loader.l);
  148. return new Module(m);
  149. }
  150. /**
  151. Extract the globals names from the given module
  152. **/
  153. public static function readGlobalsNames(i:haxe.io.Input) {
  154. if (i.readByte() != 0x4E || i.readByte() != 0x45 || i.readByte() != 0x4B || i.readByte() != 0x4F)
  155. throw "Not a neko file";
  156. function readInt() {
  157. return i.readInt32();
  158. }
  159. var nglobals = readInt();
  160. /*var nfields =*/ readInt();
  161. /*var codesize =*/ readInt();
  162. var a = new Array();
  163. for (k in 0...nglobals) {
  164. switch (i.readByte()) {
  165. case 1:
  166. a.push(i.readUntil(0));
  167. case 2:
  168. a.push("<fun:" + (readInt() & 0xFFFFFF) + ">");
  169. case 3:
  170. a.push("STRING:" + i.readString(i.readUInt16()));
  171. case 4:
  172. a.push("FLOAT:" + i.readUntil(0));
  173. case 5:
  174. a.push("DEBUG");
  175. case 6:
  176. a.push("VERSION " + i.readByte());
  177. default:
  178. throw "assert";
  179. }
  180. }
  181. return a;
  182. }
  183. function __compare(other:Module) {
  184. return untyped __dollar__compare(this.m, other.m);
  185. }
  186. static var _module_read = neko.Lib.load("std", "module_read", 2);
  187. static var _module_read_path = neko.Lib.load("std", "module_read_path", 3);
  188. static var _module_exec = neko.Lib.load("std", "module_exec", 1);
  189. static var _module_name = neko.Lib.load("std", "module_name", 1);
  190. static var _module_exports = neko.Lib.load("std", "module_exports", 1);
  191. static var _module_loader = neko.Lib.load("std", "module_loader", 1);
  192. static var _module_code_size = neko.Lib.load("std", "module_code_size", 1);
  193. static var _module_nglobals = neko.Lib.load("std", "module_nglobals", 1);
  194. static var _module_global_get = neko.Lib.load("std", "module_global_get", 2);
  195. static var _module_global_set = neko.Lib.load("std", "module_global_set", 3);
  196. static var _module_read_string = neko.Lib.loadLazy("std", "module_read_string", 2);
  197. static var _module_set_name = neko.Lib.loadLazy("std", "module_set_name", 2);
  198. }