Module.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package neko.vm;
  26. /**
  27. The abstract Neko module handle.
  28. **/
  29. enum ModuleHandle {
  30. }
  31. /**
  32. A Neko Module represent a execution unit for the Neko Virtual Machine. Each compiled [.n] bytecode
  33. file is a module once loaded by the NekoVM.
  34. **/
  35. class Module {
  36. /**
  37. The abstract handle.
  38. **/
  39. public var m : ModuleHandle;
  40. public var name(getName,setName) : String;
  41. public function new( m ) {
  42. this.m = m;
  43. }
  44. /**
  45. Execute a module and returns its result (the latest evaluated expression).
  46. A module can be executed several times but its globals are only initialized once
  47. the first time the Module is loaded.
  48. **/
  49. public function execute() : Dynamic {
  50. return _module_exec(m);
  51. }
  52. function getName() {
  53. return new String(_module_name(m));
  54. }
  55. function setName( n : String ) {
  56. _module_set_name(m,untyped n.__s);
  57. return n;
  58. }
  59. /**
  60. Returns the Loader that this Module was loaded with.s
  61. **/
  62. public function loader() {
  63. return new Loader(_module_loader(m));
  64. }
  65. /**
  66. Returns the codeSize of the Module.
  67. **/
  68. public function codeSize() : Int {
  69. return _module_code_size(m);
  70. }
  71. /**
  72. Returns the number of globals in this Module global table.
  73. **/
  74. public function globalsCount() : Int {
  75. return _module_nglobals(m);
  76. }
  77. /**
  78. Get a Module global value.
  79. **/
  80. public function getGlobal( n : Int ) : Dynamic {
  81. return _module_global_get(m,n);
  82. }
  83. /**
  84. Set a Module global value.
  85. **/
  86. public function setGlobal( n : Int, v : Dynamic ) {
  87. _module_global_set(m,n,v);
  88. }
  89. public function toString() {
  90. return "[Module:"+name+"]";
  91. }
  92. /**
  93. Each Module has an export table which can be useful to transfert
  94. values between modules.
  95. **/
  96. public function getExports() : Hash<Dynamic> {
  97. var h = new Hash();
  98. var exp = _module_exports(m);
  99. for( f in Reflect.fields(exp) )
  100. h.set(f,Reflect.field(exp,f));
  101. return h;
  102. }
  103. /**
  104. The raw export table.
  105. **/
  106. public function exportsTable() : Dynamic {
  107. return _module_exports(m);
  108. }
  109. /**
  110. Set a value in the Module export table.
  111. **/
  112. public function setExport( name : String, value : Dynamic ) {
  113. var exp = _module_exports(m);
  114. Reflect.setField(exp,name,value);
  115. }
  116. /**
  117. Returns the local Module, which is the one in which this
  118. method is included.
  119. **/
  120. public static function local() {
  121. return new Module(untyped __dollar__exports.__module);
  122. }
  123. /**
  124. Reads a module from an Input by using the given Loader.
  125. The module is initialized but has not yet been executed.
  126. **/
  127. public static function read( i : haxe.io.Input, l : Loader ) : Module {
  128. var m = _module_read(function(buf,pos,len) {
  129. return i.readBytes(untyped new haxe.io.Bytes(len,buf),pos,len);
  130. },l.l);
  131. return new Module(m);
  132. }
  133. /**
  134. Reads a module from Bytes using the given Loader.
  135. The module is initialized but has not yet been executed.
  136. **/
  137. public static function readBytes( b : haxe.io.Bytes, loader : Loader ) : Module {
  138. return new Module(_module_read_string(b.getData(),loader.l));
  139. }
  140. /**
  141. Reads a module from a name and using the specified seach path and loader.
  142. The module is initialized but has not yet been executed.
  143. **/
  144. public static function readPath( name : String, path : Array<String>, loader : Loader ) {
  145. var p = null;
  146. var i = path.length;
  147. while( --i >= 0 )
  148. p = untyped __dollar__array(path[i].__s,p);
  149. var m = _module_read_path(p,untyped name.__s,loader.l);
  150. return new Module(m);
  151. }
  152. function __compare( other : Module ) {
  153. return untyped __dollar__compare(this.m,other.m);
  154. }
  155. static var _module_read = neko.Lib.load("std","module_read",2);
  156. static var _module_read_path = neko.Lib.load("std","module_read_path",3);
  157. static var _module_exec = neko.Lib.load("std","module_exec",1);
  158. static var _module_name = neko.Lib.load("std","module_name",1);
  159. static var _module_exports = neko.Lib.load("std","module_exports",1);
  160. static var _module_loader = neko.Lib.load("std","module_loader",1);
  161. static var _module_code_size = neko.Lib.load("std","module_code_size",1);
  162. static var _module_nglobals = neko.Lib.load("std","module_nglobals",1);
  163. static var _module_global_get = neko.Lib.load("std","module_global_get",2);
  164. static var _module_global_set = neko.Lib.load("std","module_global_set",3);
  165. static var _module_read_string = neko.Lib.loadLazy("std","module_read_string",2);
  166. static var _module_set_name = neko.Lib.loadLazy("std","module_set_name",2);
  167. }