Lib.hx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  26. class Lib {
  27. /**
  28. Load and return a Neko primitive from a NDLL library.
  29. **/
  30. public static function load( lib : String, prim : String, nargs : Int ) : Dynamic {
  31. return untyped __dollar__loader.loadprim((lib+"@"+prim).__s,nargs);
  32. }
  33. public static function loadLazy(lib,prim,nargs) : Dynamic {
  34. try {
  35. return load(lib,prim,nargs);
  36. } catch( e : Dynamic ) {
  37. return untyped __dollar__varargs(function(_) { throw e; });
  38. }
  39. }
  40. /**
  41. Print the specified value on the default output.
  42. **/
  43. public static function print( v : Dynamic ) : Void {
  44. untyped __dollar__print(v);
  45. }
  46. /**
  47. Print the specified value on the default output followed by a newline character.
  48. **/
  49. public static function println( v : Dynamic ) : Void {
  50. untyped __dollar__print(v,"\n");
  51. }
  52. /**
  53. Rethrow an exception. This is useful when manually filtering an exception in order
  54. to keep the previous exception stack.
  55. **/
  56. public static function rethrow( e : Dynamic ) : Dynamic {
  57. return untyped __dollar__rethrow(e);
  58. }
  59. /**
  60. Serialize using native Neko serialization. This will return a Binary string that can be
  61. stored for long term usage. The serialized data is optimized for speed and not for size.
  62. **/
  63. public static function serialize( v : Dynamic ) : String {
  64. return new String(__serialize(v));
  65. }
  66. /**
  67. Unserialize a string using native Neko serialization. See [serialize].
  68. **/
  69. public static function unserialize( s : String ) : Dynamic {
  70. return untyped __unserialize(s.__s,__dollar__loader);
  71. }
  72. /**
  73. Unserialize a string using native Neko serialization. See [serialize].
  74. This function assume that all the serialized data was serialized with current
  75. module, even if the module name was different. This can happen if you are unserializing
  76. some data into mod_neko that was serialized on a different server using a different
  77. file path.
  78. **/
  79. public static function localUnserialize( s : String ) : Dynamic {
  80. return untyped __unserialize(s.__s,{
  81. loadmodule : function(m,l) { return __dollar__exports; },
  82. loadprim : function(p,n) { return __dollar__loader.loadprim(p,n); }
  83. });
  84. }
  85. /**
  86. Converts a Neko value to its haXe equivalent. Used for wrapping String and Arrays raw values into haXe Objects.
  87. **/
  88. public static function nekoToHaxe( v : Dynamic ) : Dynamic untyped {
  89. switch( __dollar__typeof(v) ) {
  90. case __dollar__tnull: return v;
  91. case __dollar__tint: return v;
  92. case __dollar__tfloat: return v;
  93. case __dollar__tbool: return v;
  94. case __dollar__tstring: return new String(v);
  95. case __dollar__tarray:
  96. var a = Array.new1(v,__dollar__asize(v));
  97. for( i in 0...a.length )
  98. a[i] = nekoToHaxe(a[i]);
  99. return a;
  100. case __dollar__tobject:
  101. var f = __dollar__objfields(v);
  102. var i = 0;
  103. var l = __dollar__asize(f);
  104. var o = __dollar__new(v);
  105. if( __dollar__objgetproto(v) != null )
  106. throw "Can't convert object prototype";
  107. while( i < l ) {
  108. __dollar__objset(o,f[i],nekoToHaxe(__dollar__objget(v,f[i])));
  109. i += 1;
  110. }
  111. return o;
  112. default:
  113. throw "Can't convert "+string(v);
  114. }
  115. }
  116. /**
  117. Converts a Neko value to its haXe equivalent. Used to unwrap String and Arrays Objects into raw Neko values.
  118. **/
  119. public static function haxeToNeko( v : Dynamic ) : Dynamic untyped {
  120. switch( __dollar__typeof(v) ) {
  121. case __dollar__tnull: return v;
  122. case __dollar__tint: return v;
  123. case __dollar__tfloat: return v;
  124. case __dollar__tbool: return v;
  125. case __dollar__tobject:
  126. var cl = v.__class__;
  127. if( cl == String )
  128. return v.__s;
  129. if( cl == Array ) {
  130. var a = untyped __dollar__amake(v.length);
  131. for( i in 0...v.length )
  132. a[i] = haxeToNeko(v[i]);
  133. return a;
  134. }
  135. if( cl != null || __dollar__objgetproto(v) != null )
  136. throw "Can't convert "+string(v);
  137. var f = __dollar__objfields(v);
  138. var i = 0;
  139. var l = __dollar__asize(f);
  140. var o = __dollar__new(v);
  141. while( i < l ) {
  142. __dollar__objset(o,f[i],haxeToNeko(__dollar__objget(v,f[i])));
  143. i += 1;
  144. }
  145. return o;
  146. default:
  147. throw "Can't convert "+string(v);
  148. }
  149. }
  150. /**
  151. Returns an object containing all compiled packages and classes.
  152. **/
  153. public static function getClasses() : Dynamic {
  154. return untyped neko.Boot.__classes;
  155. }
  156. /**
  157. Returns a string referencing the data contains in bytes.
  158. **/
  159. public inline static function stringReference( b : haxe.io.Bytes ) {
  160. return new String( cast b.getData() );
  161. }
  162. /**
  163. Returns bytes referencing the content of a string.
  164. **/
  165. public inline static function bytesReference( s : String ) : haxe.io.Bytes {
  166. return untyped new haxe.io.Bytes( s.length, s.__s );
  167. }
  168. static var __serialize = load("std","serialize",1);
  169. static var __unserialize = load("std","unserialize",2);
  170. }