ffi.js.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: ffi.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: ffi.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @namespace Sk.ffi
  21. *
  22. */
  23. Sk.ffi = Sk.ffi || {};
  24. /**
  25. * maps from Javascript Object/Array/string to Python dict/list/str.
  26. *
  27. * only works on basic objects that are being used as storage, doesn't handle
  28. * functions, etc.
  29. */
  30. Sk.ffi.remapToPy = function (obj) {
  31. var k;
  32. var kvs;
  33. var i;
  34. var arr;
  35. if (Object.prototype.toString.call(obj) === "[object Array]") {
  36. arr = [];
  37. for (i = 0; i &lt; obj.length; ++i) {
  38. arr.push(Sk.ffi.remapToPy(obj[i]));
  39. }
  40. return new Sk.builtin.list(arr);
  41. } else if (typeof obj === "object") {
  42. kvs = [];
  43. for (k in obj) {
  44. kvs.push(Sk.ffi.remapToPy(k));
  45. kvs.push(Sk.ffi.remapToPy(obj[k]));
  46. }
  47. return new Sk.builtin.dict(kvs);
  48. } else if (typeof obj === "string") {
  49. return new Sk.builtin.str(obj);
  50. } else if (typeof obj === "number") {
  51. return Sk.builtin.assk$(obj);
  52. } else if (typeof obj === "boolean") {
  53. return obj;
  54. }
  55. goog.asserts.fail("unhandled remap type " + typeof(obj));
  56. };
  57. goog.exportSymbol("Sk.ffi.remapToPy", Sk.ffi.remapToPy);
  58. /**
  59. * Maps from Python dict/list/str/number to Javascript Object/Array/string/number.
  60. *
  61. * If obj is a
  62. *
  63. * @param obj {Object} Any Python object (except a function)
  64. *
  65. */
  66. Sk.ffi.remapToJs = function (obj) {
  67. var i;
  68. var kAsJs;
  69. var v;
  70. var iter, k;
  71. var ret;
  72. if (obj instanceof Sk.builtin.dict) {
  73. ret = {};
  74. for (iter = obj.tp$iter(), k = iter.tp$iternext();
  75. k !== undefined;
  76. k = iter.tp$iternext()) {
  77. v = obj.mp$subscript(k);
  78. if (v === undefined) {
  79. v = null;
  80. }
  81. kAsJs = Sk.ffi.remapToJs(k);
  82. // todo; assert that this is a reasonble lhs?
  83. ret[kAsJs] = Sk.ffi.remapToJs(v);
  84. }
  85. return ret;
  86. } else if (obj instanceof Sk.builtin.list || obj instanceof Sk.builtin.tuple) {
  87. ret = [];
  88. for (i = 0; i &lt; obj.v.length; ++i) {
  89. ret.push(Sk.ffi.remapToJs(obj.v[i]));
  90. }
  91. return ret;
  92. } else if (obj instanceof Sk.builtin.int_) {
  93. return Sk.builtin.asnum$(obj);
  94. } else if (obj instanceof Sk.builtin.float_) {
  95. return Sk.builtin.asnum$(obj);
  96. } else if (obj instanceof Sk.builtin.lng) {
  97. return Sk.builtin.asnum$(obj);
  98. } else if (typeof obj === "number" || typeof obj === "boolean") {
  99. return obj;
  100. } else {
  101. return obj.v;
  102. }
  103. };
  104. goog.exportSymbol("Sk.ffi.remapToJs", Sk.ffi.remapToJs);
  105. Sk.ffi.callback = function (fn) {
  106. if (fn === undefined) {
  107. return fn;
  108. }
  109. return function () {
  110. return Sk.misceval.apply(fn, undefined, undefined, undefined, Array.prototype.slice.call(arguments, 0));
  111. };
  112. };
  113. goog.exportSymbol("Sk.ffi.callback", Sk.ffi.callback);
  114. Sk.ffi.stdwrap = function (type, towrap) {
  115. var inst = new type();
  116. inst["v"] = towrap;
  117. return inst;
  118. };
  119. goog.exportSymbol("Sk.ffi.stdwrap", Sk.ffi.stdwrap);
  120. /**
  121. * for when the return type might be one of a variety of basic types.
  122. * number|string, etc.
  123. */
  124. Sk.ffi.basicwrap = function (obj) {
  125. if (obj instanceof Sk.builtin.int_) {
  126. return Sk.builtin.asnum$(obj);
  127. }
  128. if (obj instanceof Sk.builtin.float_) {
  129. return Sk.builtin.asnum$(obj);
  130. }
  131. if (obj instanceof Sk.builtin.lng) {
  132. return Sk.builtin.asnum$(obj);
  133. }
  134. if (typeof obj === "number" || typeof obj === "boolean") {
  135. return obj;
  136. }
  137. if (typeof obj === "string") {
  138. return new Sk.builtin.str(obj);
  139. }
  140. goog.asserts.fail("unexpected type for basicwrap");
  141. };
  142. goog.exportSymbol("Sk.ffi.basicwrap", Sk.ffi.basicwrap);
  143. Sk.ffi.unwrapo = function (obj) {
  144. if (obj === undefined) {
  145. return undefined;
  146. }
  147. return obj["v"];
  148. };
  149. goog.exportSymbol("Sk.ffi.unwrapo", Sk.ffi.unwrapo);
  150. Sk.ffi.unwrapn = function (obj) {
  151. if (obj === null) {
  152. return null;
  153. }
  154. return obj["v"];
  155. };
  156. goog.exportSymbol("Sk.ffi.unwrapn", Sk.ffi.unwrapn);
  157. </code></pre>
  158. </article>
  159. </section>
  160. </div>
  161. <nav>
  162. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sk.abstr.iter-seqIter.html">seqIter</a></li><li><a href="Sk.builtin.bool.html">bool</a></li><li><a href="Sk.builtin.float_.html">float_</a></li><li><a href="Sk.builtin.func.html">func</a></li><li><a href="Sk.builtin.int_.html">int_</a></li><li><a href="Sk.builtin.none.html">none</a></li><li><a href="Sk.builtin.NotImplemented.html">NotImplemented</a></li><li><a href="Sk.builtin.numtype.html">numtype</a></li><li><a href="Sk.builtin.object.html">object</a></li><li><a href="Sk.builtin.seqtype.html">seqtype</a></li><li><a href="Sk.misceval.Suspension.html">Suspension</a></li></ul><h3>Namespaces</h3><ul><li><a href="Sk.html">Sk</a></li><li><a href="Sk.abstr.html">abstr</a></li><li><a href="Sk.builtin.html">builtin</a></li><li><a href="Sk.ffi.html">ffi</a></li><li><a href="Sk.misceval.html">misceval</a></li></ul>
  163. </nav>
  164. <br class="clear">
  165. <footer>
  166. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0</a> on Thu Aug 13 2015 08:14:27 GMT-0500 (CDT)
  167. </footer>
  168. <script> prettyPrint(); </script>
  169. <script src="scripts/linenumber.js"> </script>
  170. </body>
  171. </html>