Module.hx 6.1 KB

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