Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 67.88 KB | None | 0 0
  1. "use strict";
  2. (function() {
  3.  
  4. Error.stackTraceLimit = Infinity;
  5.  
  6. var $global, $module;
  7. if (typeof window !== "undefined") { /* web page */
  8. $global = window;
  9. } else if (typeof self !== "undefined") { /* web worker */
  10. $global = self;
  11. } else if (typeof global !== "undefined") { /* Node.js */
  12. $global = global;
  13. $global.require = require;
  14. } else { /* others (e.g. Nashorn) */
  15. $global = this;
  16. }
  17.  
  18. if ($global === undefined || $global.Array === undefined) {
  19. throw new Error("no global object found");
  20. }
  21. if (typeof module !== "undefined") {
  22. $module = module;
  23. }
  24.  
  25. var $packages = {}, $idCounter = 0;
  26. var $keys = function(m) { return m ? Object.keys(m) : []; };
  27. var $flushConsole = function() {};
  28. var $throwRuntimeError; /* set by package "runtime" */
  29. var $throwNilPointerError = function() { $throwRuntimeError("invalid memory address or nil pointer dereference"); };
  30. var $call = function(fn, rcvr, args) { return fn.apply(rcvr, args); };
  31. var $makeFunc = function(fn) { return function() { return $externalize(fn(this, new ($sliceType($jsObjectPtr))($global.Array.prototype.slice.call(arguments, []))), $emptyInterface); }; };
  32. var $unused = function(v) {};
  33.  
  34. var $mapArray = function(array, f) {
  35. var newArray = new array.constructor(array.length);
  36. for (var i = 0; i < array.length; i++) {
  37. newArray[i] = f(array[i]);
  38. }
  39. return newArray;
  40. };
  41.  
  42. var $methodVal = function(recv, name) {
  43. var vals = recv.$methodVals || {};
  44. recv.$methodVals = vals; /* noop for primitives */
  45. var f = vals[name];
  46. if (f !== undefined) {
  47. return f;
  48. }
  49. var method = recv[name];
  50. f = function() {
  51. $stackDepthOffset--;
  52. try {
  53. return method.apply(recv, arguments);
  54. } finally {
  55. $stackDepthOffset++;
  56. }
  57. };
  58. vals[name] = f;
  59. return f;
  60. };
  61.  
  62. var $methodExpr = function(typ, name) {
  63. var method = typ.prototype[name];
  64. if (method.$expr === undefined) {
  65. method.$expr = function() {
  66. $stackDepthOffset--;
  67. try {
  68. if (typ.wrapped) {
  69. arguments[0] = new typ(arguments[0]);
  70. }
  71. return Function.call.apply(method, arguments);
  72. } finally {
  73. $stackDepthOffset++;
  74. }
  75. };
  76. }
  77. return method.$expr;
  78. };
  79.  
  80. var $ifaceMethodExprs = {};
  81. var $ifaceMethodExpr = function(name) {
  82. var expr = $ifaceMethodExprs["$" + name];
  83. if (expr === undefined) {
  84. expr = $ifaceMethodExprs["$" + name] = function() {
  85. $stackDepthOffset--;
  86. try {
  87. return Function.call.apply(arguments[0][name], arguments);
  88. } finally {
  89. $stackDepthOffset++;
  90. }
  91. };
  92. }
  93. return expr;
  94. };
  95.  
  96. var $subslice = function(slice, low, high, max) {
  97. if (low < 0 || high < low || max < high || high > slice.$capacity || max > slice.$capacity) {
  98. $throwRuntimeError("slice bounds out of range");
  99. }
  100. var s = new slice.constructor(slice.$array);
  101. s.$offset = slice.$offset + low;
  102. s.$length = slice.$length - low;
  103. s.$capacity = slice.$capacity - low;
  104. if (high !== undefined) {
  105. s.$length = high - low;
  106. }
  107. if (max !== undefined) {
  108. s.$capacity = max - low;
  109. }
  110. return s;
  111. };
  112.  
  113. var $substring = function(str, low, high) {
  114. if (low < 0 || high < low || high > str.length) {
  115. $throwRuntimeError("slice bounds out of range");
  116. }
  117. return str.substring(low, high);
  118. };
  119.  
  120. var $sliceToArray = function(slice) {
  121. if (slice.$length === 0) {
  122. return [];
  123. }
  124. if (slice.$array.constructor !== Array) {
  125. return slice.$array.subarray(slice.$offset, slice.$offset + slice.$length);
  126. }
  127. return slice.$array.slice(slice.$offset, slice.$offset + slice.$length);
  128. };
  129.  
  130. var $decodeRune = function(str, pos) {
  131. var c0 = str.charCodeAt(pos);
  132.  
  133. if (c0 < 0x80) {
  134. return [c0, 1];
  135. }
  136.  
  137. if (c0 !== c0 || c0 < 0xC0) {
  138. return [0xFFFD, 1];
  139. }
  140.  
  141. var c1 = str.charCodeAt(pos + 1);
  142. if (c1 !== c1 || c1 < 0x80 || 0xC0 <= c1) {
  143. return [0xFFFD, 1];
  144. }
  145.  
  146. if (c0 < 0xE0) {
  147. var r = (c0 & 0x1F) << 6 | (c1 & 0x3F);
  148. if (r <= 0x7F) {
  149. return [0xFFFD, 1];
  150. }
  151. return [r, 2];
  152. }
  153.  
  154. var c2 = str.charCodeAt(pos + 2);
  155. if (c2 !== c2 || c2 < 0x80 || 0xC0 <= c2) {
  156. return [0xFFFD, 1];
  157. }
  158.  
  159. if (c0 < 0xF0) {
  160. var r = (c0 & 0x0F) << 12 | (c1 & 0x3F) << 6 | (c2 & 0x3F);
  161. if (r <= 0x7FF) {
  162. return [0xFFFD, 1];
  163. }
  164. if (0xD800 <= r && r <= 0xDFFF) {
  165. return [0xFFFD, 1];
  166. }
  167. return [r, 3];
  168. }
  169.  
  170. var c3 = str.charCodeAt(pos + 3);
  171. if (c3 !== c3 || c3 < 0x80 || 0xC0 <= c3) {
  172. return [0xFFFD, 1];
  173. }
  174.  
  175. if (c0 < 0xF8) {
  176. var r = (c0 & 0x07) << 18 | (c1 & 0x3F) << 12 | (c2 & 0x3F) << 6 | (c3 & 0x3F);
  177. if (r <= 0xFFFF || 0x10FFFF < r) {
  178. return [0xFFFD, 1];
  179. }
  180. return [r, 4];
  181. }
  182.  
  183. return [0xFFFD, 1];
  184. };
  185.  
  186. var $encodeRune = function(r) {
  187. if (r < 0 || r > 0x10FFFF || (0xD800 <= r && r <= 0xDFFF)) {
  188. r = 0xFFFD;
  189. }
  190. if (r <= 0x7F) {
  191. return String.fromCharCode(r);
  192. }
  193. if (r <= 0x7FF) {
  194. return String.fromCharCode(0xC0 | r >> 6, 0x80 | (r & 0x3F));
  195. }
  196. if (r <= 0xFFFF) {
  197. return String.fromCharCode(0xE0 | r >> 12, 0x80 | (r >> 6 & 0x3F), 0x80 | (r & 0x3F));
  198. }
  199. return String.fromCharCode(0xF0 | r >> 18, 0x80 | (r >> 12 & 0x3F), 0x80 | (r >> 6 & 0x3F), 0x80 | (r & 0x3F));
  200. };
  201.  
  202. var $stringToBytes = function(str) {
  203. var array = new Uint8Array(str.length);
  204. for (var i = 0; i < str.length; i++) {
  205. array[i] = str.charCodeAt(i);
  206. }
  207. return array;
  208. };
  209.  
  210. var $bytesToString = function(slice) {
  211. if (slice.$length === 0) {
  212. return "";
  213. }
  214. var str = "";
  215. for (var i = 0; i < slice.$length; i += 10000) {
  216. str += String.fromCharCode.apply(undefined, slice.$array.subarray(slice.$offset + i, slice.$offset + Math.min(slice.$length, i + 10000)));
  217. }
  218. return str;
  219. };
  220.  
  221. var $stringToRunes = function(str) {
  222. var array = new Int32Array(str.length);
  223. var rune, j = 0;
  224. for (var i = 0; i < str.length; i += rune[1], j++) {
  225. rune = $decodeRune(str, i);
  226. array[j] = rune[0];
  227. }
  228. return array.subarray(0, j);
  229. };
  230.  
  231. var $runesToString = function(slice) {
  232. if (slice.$length === 0) {
  233. return "";
  234. }
  235. var str = "";
  236. for (var i = 0; i < slice.$length; i++) {
  237. str += $encodeRune(slice.$array[slice.$offset + i]);
  238. }
  239. return str;
  240. };
  241.  
  242. var $copyString = function(dst, src) {
  243. var n = Math.min(src.length, dst.$length);
  244. for (var i = 0; i < n; i++) {
  245. dst.$array[dst.$offset + i] = src.charCodeAt(i);
  246. }
  247. return n;
  248. };
  249.  
  250. var $copySlice = function(dst, src) {
  251. var n = Math.min(src.$length, dst.$length);
  252. $copyArray(dst.$array, src.$array, dst.$offset, src.$offset, n, dst.constructor.elem);
  253. return n;
  254. };
  255.  
  256. var $copyArray = function(dst, src, dstOffset, srcOffset, n, elem) {
  257. if (n === 0 || (dst === src && dstOffset === srcOffset)) {
  258. return;
  259. }
  260.  
  261. if (src.subarray) {
  262. dst.set(src.subarray(srcOffset, srcOffset + n), dstOffset);
  263. return;
  264. }
  265.  
  266. switch (elem.kind) {
  267. case $kindArray:
  268. case $kindStruct:
  269. if (dst === src && dstOffset > srcOffset) {
  270. for (var i = n - 1; i >= 0; i--) {
  271. elem.copy(dst[dstOffset + i], src[srcOffset + i]);
  272. }
  273. return;
  274. }
  275. for (var i = 0; i < n; i++) {
  276. elem.copy(dst[dstOffset + i], src[srcOffset + i]);
  277. }
  278. return;
  279. }
  280.  
  281. if (dst === src && dstOffset > srcOffset) {
  282. for (var i = n - 1; i >= 0; i--) {
  283. dst[dstOffset + i] = src[srcOffset + i];
  284. }
  285. return;
  286. }
  287. for (var i = 0; i < n; i++) {
  288. dst[dstOffset + i] = src[srcOffset + i];
  289. }
  290. };
  291.  
  292. var $clone = function(src, type) {
  293. var clone = type.zero();
  294. type.copy(clone, src);
  295. return clone;
  296. };
  297.  
  298. var $pointerOfStructConversion = function(obj, type) {
  299. if(obj.$proxies === undefined) {
  300. obj.$proxies = {};
  301. obj.$proxies[obj.constructor.string] = obj;
  302. }
  303. var proxy = obj.$proxies[type.string];
  304. if (proxy === undefined) {
  305. var properties = {};
  306. for (var i = 0; i < type.elem.fields.length; i++) {
  307. (function(fieldProp) {
  308. properties[fieldProp] = {
  309. get: function() { return obj[fieldProp]; },
  310. set: function(value) { obj[fieldProp] = value; }
  311. };
  312. })(type.elem.fields[i].prop);
  313. }
  314. proxy = Object.create(type.prototype, properties);
  315. proxy.$val = proxy;
  316. obj.$proxies[type.string] = proxy;
  317. proxy.$proxies = obj.$proxies;
  318. }
  319. return proxy;
  320. };
  321.  
  322. var $append = function(slice) {
  323. return $internalAppend(slice, arguments, 1, arguments.length - 1);
  324. };
  325.  
  326. var $appendSlice = function(slice, toAppend) {
  327. if (toAppend.constructor === String) {
  328. var bytes = $stringToBytes(toAppend);
  329. return $internalAppend(slice, bytes, 0, bytes.length);
  330. }
  331. return $internalAppend(slice, toAppend.$array, toAppend.$offset, toAppend.$length);
  332. };
  333.  
  334. var $internalAppend = function(slice, array, offset, length) {
  335. if (length === 0) {
  336. return slice;
  337. }
  338.  
  339. var newArray = slice.$array;
  340. var newOffset = slice.$offset;
  341. var newLength = slice.$length + length;
  342. var newCapacity = slice.$capacity;
  343.  
  344. if (newLength > newCapacity) {
  345. newOffset = 0;
  346. newCapacity = Math.max(newLength, slice.$capacity < 1024 ? slice.$capacity * 2 : Math.floor(slice.$capacity * 5 / 4));
  347.  
  348. if (slice.$array.constructor === Array) {
  349. newArray = slice.$array.slice(slice.$offset, slice.$offset + slice.$length);
  350. newArray.length = newCapacity;
  351. var zero = slice.constructor.elem.zero;
  352. for (var i = slice.$length; i < newCapacity; i++) {
  353. newArray[i] = zero();
  354. }
  355. } else {
  356. newArray = new slice.$array.constructor(newCapacity);
  357. newArray.set(slice.$array.subarray(slice.$offset, slice.$offset + slice.$length));
  358. }
  359. }
  360.  
  361. $copyArray(newArray, array, newOffset + slice.$length, offset, length, slice.constructor.elem);
  362.  
  363. var newSlice = new slice.constructor(newArray);
  364. newSlice.$offset = newOffset;
  365. newSlice.$length = newLength;
  366. newSlice.$capacity = newCapacity;
  367. return newSlice;
  368. };
  369.  
  370. var $equal = function(a, b, type) {
  371. if (type === $jsObjectPtr) {
  372. return a === b;
  373. }
  374. switch (type.kind) {
  375. case $kindComplex64:
  376. case $kindComplex128:
  377. return a.$real === b.$real && a.$imag === b.$imag;
  378. case $kindInt64:
  379. case $kindUint64:
  380. return a.$high === b.$high && a.$low === b.$low;
  381. case $kindArray:
  382. if (a.length !== b.length) {
  383. return false;
  384. }
  385. for (var i = 0; i < a.length; i++) {
  386. if (!$equal(a[i], b[i], type.elem)) {
  387. return false;
  388. }
  389. }
  390. return true;
  391. case $kindStruct:
  392. for (var i = 0; i < type.fields.length; i++) {
  393. var f = type.fields[i];
  394. if (!$equal(a[f.prop], b[f.prop], f.typ)) {
  395. return false;
  396. }
  397. }
  398. return true;
  399. case $kindInterface:
  400. return $interfaceIsEqual(a, b);
  401. default:
  402. return a === b;
  403. }
  404. };
  405.  
  406. var $interfaceIsEqual = function(a, b) {
  407. if (a === $ifaceNil || b === $ifaceNil) {
  408. return a === b;
  409. }
  410. if (a.constructor !== b.constructor) {
  411. return false;
  412. }
  413. if (a.constructor === $jsObjectPtr) {
  414. return a.object === b.object;
  415. }
  416. if (!a.constructor.comparable) {
  417. $throwRuntimeError("comparing uncomparable type " + a.constructor.string);
  418. }
  419. return $equal(a.$val, b.$val, a.constructor);
  420. };
  421.  
  422. var $min = Math.min;
  423. var $mod = function(x, y) { return x % y; };
  424. var $parseInt = parseInt;
  425. var $parseFloat = function(f) {
  426. if (f !== undefined && f !== null && f.constructor === Number) {
  427. return f;
  428. }
  429. return parseFloat(f);
  430. };
  431.  
  432. var $froundBuf = new Float32Array(1);
  433. var $fround = Math.fround || function(f) {
  434. $froundBuf[0] = f;
  435. return $froundBuf[0];
  436. };
  437.  
  438. var $imul = Math.imul || function(a, b) {
  439. var ah = (a >>> 16) & 0xffff;
  440. var al = a & 0xffff;
  441. var bh = (b >>> 16) & 0xffff;
  442. var bl = b & 0xffff;
  443. return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0) >> 0);
  444. };
  445.  
  446. var $floatKey = function(f) {
  447. if (f !== f) {
  448. $idCounter++;
  449. return "NaN$" + $idCounter;
  450. }
  451. return String(f);
  452. };
  453.  
  454. var $flatten64 = function(x) {
  455. return x.$high * 4294967296 + x.$low;
  456. };
  457.  
  458. var $shiftLeft64 = function(x, y) {
  459. if (y === 0) {
  460. return x;
  461. }
  462. if (y < 32) {
  463. return new x.constructor(x.$high << y | x.$low >>> (32 - y), (x.$low << y) >>> 0);
  464. }
  465. if (y < 64) {
  466. return new x.constructor(x.$low << (y - 32), 0);
  467. }
  468. return new x.constructor(0, 0);
  469. };
  470.  
  471. var $shiftRightInt64 = function(x, y) {
  472. if (y === 0) {
  473. return x;
  474. }
  475. if (y < 32) {
  476. return new x.constructor(x.$high >> y, (x.$low >>> y | x.$high << (32 - y)) >>> 0);
  477. }
  478. if (y < 64) {
  479. return new x.constructor(x.$high >> 31, (x.$high >> (y - 32)) >>> 0);
  480. }
  481. if (x.$high < 0) {
  482. return new x.constructor(-1, 4294967295);
  483. }
  484. return new x.constructor(0, 0);
  485. };
  486.  
  487. var $shiftRightUint64 = function(x, y) {
  488. if (y === 0) {
  489. return x;
  490. }
  491. if (y < 32) {
  492. return new x.constructor(x.$high >>> y, (x.$low >>> y | x.$high << (32 - y)) >>> 0);
  493. }
  494. if (y < 64) {
  495. return new x.constructor(0, x.$high >>> (y - 32));
  496. }
  497. return new x.constructor(0, 0);
  498. };
  499.  
  500. var $mul64 = function(x, y) {
  501. var high = 0, low = 0;
  502. if ((y.$low & 1) !== 0) {
  503. high = x.$high;
  504. low = x.$low;
  505. }
  506. for (var i = 1; i < 32; i++) {
  507. if ((y.$low & 1<<i) !== 0) {
  508. high += x.$high << i | x.$low >>> (32 - i);
  509. low += (x.$low << i) >>> 0;
  510. }
  511. }
  512. for (var i = 0; i < 32; i++) {
  513. if ((y.$high & 1<<i) !== 0) {
  514. high += x.$low << i;
  515. }
  516. }
  517. return new x.constructor(high, low);
  518. };
  519.  
  520. var $div64 = function(x, y, returnRemainder) {
  521. if (y.$high === 0 && y.$low === 0) {
  522. $throwRuntimeError("integer divide by zero");
  523. }
  524.  
  525. var s = 1;
  526. var rs = 1;
  527.  
  528. var xHigh = x.$high;
  529. var xLow = x.$low;
  530. if (xHigh < 0) {
  531. s = -1;
  532. rs = -1;
  533. xHigh = -xHigh;
  534. if (xLow !== 0) {
  535. xHigh--;
  536. xLow = 4294967296 - xLow;
  537. }
  538. }
  539.  
  540. var yHigh = y.$high;
  541. var yLow = y.$low;
  542. if (y.$high < 0) {
  543. s *= -1;
  544. yHigh = -yHigh;
  545. if (yLow !== 0) {
  546. yHigh--;
  547. yLow = 4294967296 - yLow;
  548. }
  549. }
  550.  
  551. var high = 0, low = 0, n = 0;
  552. while (yHigh < 2147483648 && ((xHigh > yHigh) || (xHigh === yHigh && xLow > yLow))) {
  553. yHigh = (yHigh << 1 | yLow >>> 31) >>> 0;
  554. yLow = (yLow << 1) >>> 0;
  555. n++;
  556. }
  557. for (var i = 0; i <= n; i++) {
  558. high = high << 1 | low >>> 31;
  559. low = (low << 1) >>> 0;
  560. if ((xHigh > yHigh) || (xHigh === yHigh && xLow >= yLow)) {
  561. xHigh = xHigh - yHigh;
  562. xLow = xLow - yLow;
  563. if (xLow < 0) {
  564. xHigh--;
  565. xLow += 4294967296;
  566. }
  567. low++;
  568. if (low === 4294967296) {
  569. high++;
  570. low = 0;
  571. }
  572. }
  573. yLow = (yLow >>> 1 | yHigh << (32 - 1)) >>> 0;
  574. yHigh = yHigh >>> 1;
  575. }
  576.  
  577. if (returnRemainder) {
  578. return new x.constructor(xHigh * rs, xLow * rs);
  579. }
  580. return new x.constructor(high * s, low * s);
  581. };
  582.  
  583. var $divComplex = function(n, d) {
  584. var ninf = n.$real === Infinity || n.$real === -Infinity || n.$imag === Infinity || n.$imag === -Infinity;
  585. var dinf = d.$real === Infinity || d.$real === -Infinity || d.$imag === Infinity || d.$imag === -Infinity;
  586. var nnan = !ninf && (n.$real !== n.$real || n.$imag !== n.$imag);
  587. var dnan = !dinf && (d.$real !== d.$real || d.$imag !== d.$imag);
  588. if(nnan || dnan) {
  589. return new n.constructor(NaN, NaN);
  590. }
  591. if (ninf && !dinf) {
  592. return new n.constructor(Infinity, Infinity);
  593. }
  594. if (!ninf && dinf) {
  595. return new n.constructor(0, 0);
  596. }
  597. if (d.$real === 0 && d.$imag === 0) {
  598. if (n.$real === 0 && n.$imag === 0) {
  599. return new n.constructor(NaN, NaN);
  600. }
  601. return new n.constructor(Infinity, Infinity);
  602. }
  603. var a = Math.abs(d.$real);
  604. var b = Math.abs(d.$imag);
  605. if (a <= b) {
  606. var ratio = d.$real / d.$imag;
  607. var denom = d.$real * ratio + d.$imag;
  608. return new n.constructor((n.$real * ratio + n.$imag) / denom, (n.$imag * ratio - n.$real) / denom);
  609. }
  610. var ratio = d.$imag / d.$real;
  611. var denom = d.$imag * ratio + d.$real;
  612. return new n.constructor((n.$imag * ratio + n.$real) / denom, (n.$imag - n.$real * ratio) / denom);
  613. };
  614.  
  615. var $kindBool = 1;
  616. var $kindInt = 2;
  617. var $kindInt8 = 3;
  618. var $kindInt16 = 4;
  619. var $kindInt32 = 5;
  620. var $kindInt64 = 6;
  621. var $kindUint = 7;
  622. var $kindUint8 = 8;
  623. var $kindUint16 = 9;
  624. var $kindUint32 = 10;
  625. var $kindUint64 = 11;
  626. var $kindUintptr = 12;
  627. var $kindFloat32 = 13;
  628. var $kindFloat64 = 14;
  629. var $kindComplex64 = 15;
  630. var $kindComplex128 = 16;
  631. var $kindArray = 17;
  632. var $kindChan = 18;
  633. var $kindFunc = 19;
  634. var $kindInterface = 20;
  635. var $kindMap = 21;
  636. var $kindPtr = 22;
  637. var $kindSlice = 23;
  638. var $kindString = 24;
  639. var $kindStruct = 25;
  640. var $kindUnsafePointer = 26;
  641.  
  642. var $methodSynthesizers = [];
  643. var $addMethodSynthesizer = function(f) {
  644. if ($methodSynthesizers === null) {
  645. f();
  646. return;
  647. }
  648. $methodSynthesizers.push(f);
  649. };
  650. var $synthesizeMethods = function() {
  651. $methodSynthesizers.forEach(function(f) { f(); });
  652. $methodSynthesizers = null;
  653. };
  654.  
  655. var $ifaceKeyFor = function(x) {
  656. if (x === $ifaceNil) {
  657. return 'nil';
  658. }
  659. var c = x.constructor;
  660. return c.string + '$' + c.keyFor(x.$val);
  661. };
  662.  
  663. var $identity = function(x) { return x; };
  664.  
  665. var $typeIDCounter = 0;
  666.  
  667. var $idKey = function(x) {
  668. if (x.$id === undefined) {
  669. $idCounter++;
  670. x.$id = $idCounter;
  671. }
  672. return String(x.$id);
  673. };
  674.  
  675. var $newType = function(size, kind, string, named, pkg, exported, constructor) {
  676. var typ;
  677. switch(kind) {
  678. case $kindBool:
  679. case $kindInt:
  680. case $kindInt8:
  681. case $kindInt16:
  682. case $kindInt32:
  683. case $kindUint:
  684. case $kindUint8:
  685. case $kindUint16:
  686. case $kindUint32:
  687. case $kindUintptr:
  688. case $kindUnsafePointer:
  689. typ = function(v) { this.$val = v; };
  690. typ.wrapped = true;
  691. typ.keyFor = $identity;
  692. break;
  693.  
  694. case $kindString:
  695. typ = function(v) { this.$val = v; };
  696. typ.wrapped = true;
  697. typ.keyFor = function(x) { return "$" + x; };
  698. break;
  699.  
  700. case $kindFloat32:
  701. case $kindFloat64:
  702. typ = function(v) { this.$val = v; };
  703. typ.wrapped = true;
  704. typ.keyFor = function(x) { return $floatKey(x); };
  705. break;
  706.  
  707. case $kindInt64:
  708. typ = function(high, low) {
  709. this.$high = (high + Math.floor(Math.ceil(low) / 4294967296)) >> 0;
  710. this.$low = low >>> 0;
  711. this.$val = this;
  712. };
  713. typ.keyFor = function(x) { return x.$high + "$" + x.$low; };
  714. break;
  715.  
  716. case $kindUint64:
  717. typ = function(high, low) {
  718. this.$high = (high + Math.floor(Math.ceil(low) / 4294967296)) >>> 0;
  719. this.$low = low >>> 0;
  720. this.$val = this;
  721. };
  722. typ.keyFor = function(x) { return x.$high + "$" + x.$low; };
  723. break;
  724.  
  725. case $kindComplex64:
  726. typ = function(real, imag) {
  727. this.$real = $fround(real);
  728. this.$imag = $fround(imag);
  729. this.$val = this;
  730. };
  731. typ.keyFor = function(x) { return x.$real + "$" + x.$imag; };
  732. break;
  733.  
  734. case $kindComplex128:
  735. typ = function(real, imag) {
  736. this.$real = real;
  737. this.$imag = imag;
  738. this.$val = this;
  739. };
  740. typ.keyFor = function(x) { return x.$real + "$" + x.$imag; };
  741. break;
  742.  
  743. case $kindArray:
  744. typ = function(v) { this.$val = v; };
  745. typ.wrapped = true;
  746. typ.ptr = $newType(4, $kindPtr, "*" + string, false, "", false, function(array) {
  747. this.$get = function() { return array; };
  748. this.$set = function(v) { typ.copy(this, v); };
  749. this.$val = array;
  750. });
  751. typ.init = function(elem, len) {
  752. typ.elem = elem;
  753. typ.len = len;
  754. typ.comparable = elem.comparable;
  755. typ.keyFor = function(x) {
  756. return Array.prototype.join.call($mapArray(x, function(e) {
  757. return String(elem.keyFor(e)).replace(/\\/g, "\\\\").replace(/\$/g, "\\$");
  758. }), "$");
  759. };
  760. typ.copy = function(dst, src) {
  761. $copyArray(dst, src, 0, 0, src.length, elem);
  762. };
  763. typ.ptr.init(typ);
  764. Object.defineProperty(typ.ptr.nil, "nilCheck", { get: $throwNilPointerError });
  765. };
  766. break;
  767.  
  768. case $kindChan:
  769. typ = function(v) { this.$val = v; };
  770. typ.wrapped = true;
  771. typ.keyFor = $idKey;
  772. typ.init = function(elem, sendOnly, recvOnly) {
  773. typ.elem = elem;
  774. typ.sendOnly = sendOnly;
  775. typ.recvOnly = recvOnly;
  776. };
  777. break;
  778.  
  779. case $kindFunc:
  780. typ = function(v) { this.$val = v; };
  781. typ.wrapped = true;
  782. typ.init = function(params, results, variadic) {
  783. typ.params = params;
  784. typ.results = results;
  785. typ.variadic = variadic;
  786. typ.comparable = false;
  787. };
  788. break;
  789.  
  790. case $kindInterface:
  791. typ = { implementedBy: {}, missingMethodFor: {} };
  792. typ.keyFor = $ifaceKeyFor;
  793. typ.init = function(methods) {
  794. typ.methods = methods;
  795. methods.forEach(function(m) {
  796. $ifaceNil[m.prop] = $throwNilPointerError;
  797. });
  798. };
  799. break;
  800.  
  801. case $kindMap:
  802. typ = function(v) { this.$val = v; };
  803. typ.wrapped = true;
  804. typ.init = function(key, elem) {
  805. typ.key = key;
  806. typ.elem = elem;
  807. typ.comparable = false;
  808. };
  809. break;
  810.  
  811. case $kindPtr:
  812. typ = constructor || function(getter, setter, target) {
  813. this.$get = getter;
  814. this.$set = setter;
  815. this.$target = target;
  816. this.$val = this;
  817. };
  818. typ.keyFor = $idKey;
  819. typ.init = function(elem) {
  820. typ.elem = elem;
  821. typ.wrapped = (elem.kind === $kindArray);
  822. typ.nil = new typ($throwNilPointerError, $throwNilPointerError);
  823. };
  824. break;
  825.  
  826. case $kindSlice:
  827. typ = function(array) {
  828. if (array.constructor !== typ.nativeArray) {
  829. array = new typ.nativeArray(array);
  830. }
  831. this.$array = array;
  832. this.$offset = 0;
  833. this.$length = array.length;
  834. this.$capacity = array.length;
  835. this.$val = this;
  836. };
  837. typ.init = function(elem) {
  838. typ.elem = elem;
  839. typ.comparable = false;
  840. typ.nativeArray = $nativeArray(elem.kind);
  841. typ.nil = new typ([]);
  842. };
  843. break;
  844.  
  845. case $kindStruct:
  846. typ = function(v) { this.$val = v; };
  847. typ.wrapped = true;
  848. typ.ptr = $newType(4, $kindPtr, "*" + string, false, "", exported, constructor);
  849. typ.ptr.elem = typ;
  850. typ.ptr.prototype.$get = function() { return this; };
  851. typ.ptr.prototype.$set = function(v) { typ.copy(this, v); };
  852. typ.init = function(pkgPath, fields) {
  853. typ.pkgPath = pkgPath;
  854. typ.fields = fields;
  855. fields.forEach(function(f) {
  856. if (!f.typ.comparable) {
  857. typ.comparable = false;
  858. }
  859. });
  860. typ.keyFor = function(x) {
  861. var val = x.$val;
  862. return $mapArray(fields, function(f) {
  863. return String(f.typ.keyFor(val[f.prop])).replace(/\\/g, "\\\\").replace(/\$/g, "\\$");
  864. }).join("$");
  865. };
  866. typ.copy = function(dst, src) {
  867. for (var i = 0; i < fields.length; i++) {
  868. var f = fields[i];
  869. switch (f.typ.kind) {
  870. case $kindArray:
  871. case $kindStruct:
  872. f.typ.copy(dst[f.prop], src[f.prop]);
  873. continue;
  874. default:
  875. dst[f.prop] = src[f.prop];
  876. continue;
  877. }
  878. }
  879. };
  880. /* nil value */
  881. var properties = {};
  882. fields.forEach(function(f) {
  883. properties[f.prop] = { get: $throwNilPointerError, set: $throwNilPointerError };
  884. });
  885. typ.ptr.nil = Object.create(constructor.prototype, properties);
  886. typ.ptr.nil.$val = typ.ptr.nil;
  887. /* methods for embedded fields */
  888. $addMethodSynthesizer(function() {
  889. var synthesizeMethod = function(target, m, f) {
  890. if (target.prototype[m.prop] !== undefined) { return; }
  891. target.prototype[m.prop] = function() {
  892. var v = this.$val[f.prop];
  893. if (f.typ === $jsObjectPtr) {
  894. v = new $jsObjectPtr(v);
  895. }
  896. if (v.$val === undefined) {
  897. v = new f.typ(v);
  898. }
  899. return v[m.prop].apply(v, arguments);
  900. };
  901. };
  902. fields.forEach(function(f) {
  903. if (f.name === "") {
  904. $methodSet(f.typ).forEach(function(m) {
  905. synthesizeMethod(typ, m, f);
  906. synthesizeMethod(typ.ptr, m, f);
  907. });
  908. $methodSet($ptrType(f.typ)).forEach(function(m) {
  909. synthesizeMethod(typ.ptr, m, f);
  910. });
  911. }
  912. });
  913. });
  914. };
  915. break;
  916.  
  917. default:
  918. $panic(new $String("invalid kind: " + kind));
  919. }
  920.  
  921. switch (kind) {
  922. case $kindBool:
  923. case $kindMap:
  924. typ.zero = function() { return false; };
  925. break;
  926.  
  927. case $kindInt:
  928. case $kindInt8:
  929. case $kindInt16:
  930. case $kindInt32:
  931. case $kindUint:
  932. case $kindUint8 :
  933. case $kindUint16:
  934. case $kindUint32:
  935. case $kindUintptr:
  936. case $kindUnsafePointer:
  937. case $kindFloat32:
  938. case $kindFloat64:
  939. typ.zero = function() { return 0; };
  940. break;
  941.  
  942. case $kindString:
  943. typ.zero = function() { return ""; };
  944. break;
  945.  
  946. case $kindInt64:
  947. case $kindUint64:
  948. case $kindComplex64:
  949. case $kindComplex128:
  950. var zero = new typ(0, 0);
  951. typ.zero = function() { return zero; };
  952. break;
  953.  
  954. case $kindPtr:
  955. case $kindSlice:
  956. typ.zero = function() { return typ.nil; };
  957. break;
  958.  
  959. case $kindChan:
  960. typ.zero = function() { return $chanNil; };
  961. break;
  962.  
  963. case $kindFunc:
  964. typ.zero = function() { return $throwNilPointerError; };
  965. break;
  966.  
  967. case $kindInterface:
  968. typ.zero = function() { return $ifaceNil; };
  969. break;
  970.  
  971. case $kindArray:
  972. typ.zero = function() {
  973. var arrayClass = $nativeArray(typ.elem.kind);
  974. if (arrayClass !== Array) {
  975. return new arrayClass(typ.len);
  976. }
  977. var array = new Array(typ.len);
  978. for (var i = 0; i < typ.len; i++) {
  979. array[i] = typ.elem.zero();
  980. }
  981. return array;
  982. };
  983. break;
  984.  
  985. case $kindStruct:
  986. typ.zero = function() { return new typ.ptr(); };
  987. break;
  988.  
  989. default:
  990. $panic(new $String("invalid kind: " + kind));
  991. }
  992.  
  993. typ.id = $typeIDCounter;
  994. $typeIDCounter++;
  995. typ.size = size;
  996. typ.kind = kind;
  997. typ.string = string;
  998. typ.named = named;
  999. typ.pkg = pkg;
  1000. typ.exported = exported;
  1001. typ.methods = [];
  1002. typ.methodSetCache = null;
  1003. typ.comparable = true;
  1004. return typ;
  1005. };
  1006.  
  1007. var $methodSet = function(typ) {
  1008. if (typ.methodSetCache !== null) {
  1009. return typ.methodSetCache;
  1010. }
  1011. var base = {};
  1012.  
  1013. var isPtr = (typ.kind === $kindPtr);
  1014. if (isPtr && typ.elem.kind === $kindInterface) {
  1015. typ.methodSetCache = [];
  1016. return [];
  1017. }
  1018.  
  1019. var current = [{typ: isPtr ? typ.elem : typ, indirect: isPtr}];
  1020.  
  1021. var seen = {};
  1022.  
  1023. while (current.length > 0) {
  1024. var next = [];
  1025. var mset = [];
  1026.  
  1027. current.forEach(function(e) {
  1028. if (seen[e.typ.string]) {
  1029. return;
  1030. }
  1031. seen[e.typ.string] = true;
  1032.  
  1033. if (e.typ.named) {
  1034. mset = mset.concat(e.typ.methods);
  1035. if (e.indirect) {
  1036. mset = mset.concat($ptrType(e.typ).methods);
  1037. }
  1038. }
  1039.  
  1040. switch (e.typ.kind) {
  1041. case $kindStruct:
  1042. e.typ.fields.forEach(function(f) {
  1043. if (f.name === "") {
  1044. var fTyp = f.typ;
  1045. var fIsPtr = (fTyp.kind === $kindPtr);
  1046. next.push({typ: fIsPtr ? fTyp.elem : fTyp, indirect: e.indirect || fIsPtr});
  1047. }
  1048. });
  1049. break;
  1050.  
  1051. case $kindInterface:
  1052. mset = mset.concat(e.typ.methods);
  1053. break;
  1054. }
  1055. });
  1056.  
  1057. mset.forEach(function(m) {
  1058. if (base[m.name] === undefined) {
  1059. base[m.name] = m;
  1060. }
  1061. });
  1062.  
  1063. current = next;
  1064. }
  1065.  
  1066. typ.methodSetCache = [];
  1067. Object.keys(base).sort().forEach(function(name) {
  1068. typ.methodSetCache.push(base[name]);
  1069. });
  1070. return typ.methodSetCache;
  1071. };
  1072.  
  1073. var $Bool = $newType( 1, $kindBool, "bool", true, "", false, null);
  1074. var $Int = $newType( 4, $kindInt, "int", true, "", false, null);
  1075. var $Int8 = $newType( 1, $kindInt8, "int8", true, "", false, null);
  1076. var $Int16 = $newType( 2, $kindInt16, "int16", true, "", false, null);
  1077. var $Int32 = $newType( 4, $kindInt32, "int32", true, "", false, null);
  1078. var $Int64 = $newType( 8, $kindInt64, "int64", true, "", false, null);
  1079. var $Uint = $newType( 4, $kindUint, "uint", true, "", false, null);
  1080. var $Uint8 = $newType( 1, $kindUint8, "uint8", true, "", false, null);
  1081. var $Uint16 = $newType( 2, $kindUint16, "uint16", true, "", false, null);
  1082. var $Uint32 = $newType( 4, $kindUint32, "uint32", true, "", false, null);
  1083. var $Uint64 = $newType( 8, $kindUint64, "uint64", true, "", false, null);
  1084. var $Uintptr = $newType( 4, $kindUintptr, "uintptr", true, "", false, null);
  1085. var $Float32 = $newType( 4, $kindFloat32, "float32", true, "", false, null);
  1086. var $Float64 = $newType( 8, $kindFloat64, "float64", true, "", false, null);
  1087. var $Complex64 = $newType( 8, $kindComplex64, "complex64", true, "", false, null);
  1088. var $Complex128 = $newType(16, $kindComplex128, "complex128", true, "", false, null);
  1089. var $String = $newType( 8, $kindString, "string", true, "", false, null);
  1090. var $UnsafePointer = $newType( 4, $kindUnsafePointer, "unsafe.Pointer", true, "", false, null);
  1091.  
  1092. var $nativeArray = function(elemKind) {
  1093. switch (elemKind) {
  1094. case $kindInt:
  1095. return Int32Array;
  1096. case $kindInt8:
  1097. return Int8Array;
  1098. case $kindInt16:
  1099. return Int16Array;
  1100. case $kindInt32:
  1101. return Int32Array;
  1102. case $kindUint:
  1103. return Uint32Array;
  1104. case $kindUint8:
  1105. return Uint8Array;
  1106. case $kindUint16:
  1107. return Uint16Array;
  1108. case $kindUint32:
  1109. return Uint32Array;
  1110. case $kindUintptr:
  1111. return Uint32Array;
  1112. case $kindFloat32:
  1113. return Float32Array;
  1114. case $kindFloat64:
  1115. return Float64Array;
  1116. default:
  1117. return Array;
  1118. }
  1119. };
  1120. var $toNativeArray = function(elemKind, array) {
  1121. var nativeArray = $nativeArray(elemKind);
  1122. if (nativeArray === Array) {
  1123. return array;
  1124. }
  1125. return new nativeArray(array);
  1126. };
  1127. var $arrayTypes = {};
  1128. var $arrayType = function(elem, len) {
  1129. var typeKey = elem.id + "$" + len;
  1130. var typ = $arrayTypes[typeKey];
  1131. if (typ === undefined) {
  1132. typ = $newType(12, $kindArray, "[" + len + "]" + elem.string, false, "", false, null);
  1133. $arrayTypes[typeKey] = typ;
  1134. typ.init(elem, len);
  1135. }
  1136. return typ;
  1137. };
  1138.  
  1139. var $chanType = function(elem, sendOnly, recvOnly) {
  1140. var string = (recvOnly ? "<-" : "") + "chan" + (sendOnly ? "<- " : " ") + elem.string;
  1141. var field = sendOnly ? "SendChan" : (recvOnly ? "RecvChan" : "Chan");
  1142. var typ = elem[field];
  1143. if (typ === undefined) {
  1144. typ = $newType(4, $kindChan, string, false, "", false, null);
  1145. elem[field] = typ;
  1146. typ.init(elem, sendOnly, recvOnly);
  1147. }
  1148. return typ;
  1149. };
  1150. var $Chan = function(elem, capacity) {
  1151. if (capacity < 0 || capacity > 2147483647) {
  1152. $throwRuntimeError("makechan: size out of range");
  1153. }
  1154. this.$elem = elem;
  1155. this.$capacity = capacity;
  1156. this.$buffer = [];
  1157. this.$sendQueue = [];
  1158. this.$recvQueue = [];
  1159. this.$closed = false;
  1160. };
  1161. var $chanNil = new $Chan(null, 0);
  1162. $chanNil.$sendQueue = $chanNil.$recvQueue = { length: 0, push: function() {}, shift: function() { return undefined; }, indexOf: function() { return -1; } };
  1163.  
  1164. var $funcTypes = {};
  1165. var $funcType = function(params, results, variadic) {
  1166. var typeKey = $mapArray(params, function(p) { return p.id; }).join(",") + "$" + $mapArray(results, function(r) { return r.id; }).join(",") + "$" + variadic;
  1167. var typ = $funcTypes[typeKey];
  1168. if (typ === undefined) {
  1169. var paramTypes = $mapArray(params, function(p) { return p.string; });
  1170. if (variadic) {
  1171. paramTypes[paramTypes.length - 1] = "..." + paramTypes[paramTypes.length - 1].substr(2);
  1172. }
  1173. var string = "func(" + paramTypes.join(", ") + ")";
  1174. if (results.length === 1) {
  1175. string += " " + results[0].string;
  1176. } else if (results.length > 1) {
  1177. string += " (" + $mapArray(results, function(r) { return r.string; }).join(", ") + ")";
  1178. }
  1179. typ = $newType(4, $kindFunc, string, false, "", false, null);
  1180. $funcTypes[typeKey] = typ;
  1181. typ.init(params, results, variadic);
  1182. }
  1183. return typ;
  1184. };
  1185.  
  1186. var $interfaceTypes = {};
  1187. var $interfaceType = function(methods) {
  1188. var typeKey = $mapArray(methods, function(m) { return m.pkg + "," + m.name + "," + m.typ.id; }).join("$");
  1189. var typ = $interfaceTypes[typeKey];
  1190. if (typ === undefined) {
  1191. var string = "interface {}";
  1192. if (methods.length !== 0) {
  1193. string = "interface { " + $mapArray(methods, function(m) {
  1194. return (m.pkg !== "" ? m.pkg + "." : "") + m.name + m.typ.string.substr(4);
  1195. }).join("; ") + " }";
  1196. }
  1197. typ = $newType(8, $kindInterface, string, false, "", false, null);
  1198. $interfaceTypes[typeKey] = typ;
  1199. typ.init(methods);
  1200. }
  1201. return typ;
  1202. };
  1203. var $emptyInterface = $interfaceType([]);
  1204. var $ifaceNil = {};
  1205. var $error = $newType(8, $kindInterface, "error", true, "", false, null);
  1206. $error.init([{prop: "Error", name: "Error", pkg: "", typ: $funcType([], [$String], false)}]);
  1207.  
  1208. var $mapTypes = {};
  1209. var $mapType = function(key, elem) {
  1210. var typeKey = key.id + "$" + elem.id;
  1211. var typ = $mapTypes[typeKey];
  1212. if (typ === undefined) {
  1213. typ = $newType(4, $kindMap, "map[" + key.string + "]" + elem.string, false, "", false, null);
  1214. $mapTypes[typeKey] = typ;
  1215. typ.init(key, elem);
  1216. }
  1217. return typ;
  1218. };
  1219. var $makeMap = function(keyForFunc, entries) {
  1220. var m = {};
  1221. for (var i = 0; i < entries.length; i++) {
  1222. var e = entries[i];
  1223. m[keyForFunc(e.k)] = e;
  1224. }
  1225. return m;
  1226. };
  1227.  
  1228. var $ptrType = function(elem) {
  1229. var typ = elem.ptr;
  1230. if (typ === undefined) {
  1231. typ = $newType(4, $kindPtr, "*" + elem.string, false, "", elem.exported, null);
  1232. elem.ptr = typ;
  1233. typ.init(elem);
  1234. }
  1235. return typ;
  1236. };
  1237.  
  1238. var $newDataPointer = function(data, constructor) {
  1239. if (constructor.elem.kind === $kindStruct) {
  1240. return data;
  1241. }
  1242. return new constructor(function() { return data; }, function(v) { data = v; });
  1243. };
  1244.  
  1245. var $indexPtr = function(array, index, constructor) {
  1246. array.$ptr = array.$ptr || {};
  1247. return array.$ptr[index] || (array.$ptr[index] = new constructor(function() { return array[index]; }, function(v) { array[index] = v; }));
  1248. };
  1249.  
  1250. var $sliceType = function(elem) {
  1251. var typ = elem.slice;
  1252. if (typ === undefined) {
  1253. typ = $newType(12, $kindSlice, "[]" + elem.string, false, "", false, null);
  1254. elem.slice = typ;
  1255. typ.init(elem);
  1256. }
  1257. return typ;
  1258. };
  1259. var $makeSlice = function(typ, length, capacity) {
  1260. capacity = capacity || length;
  1261. if (length < 0 || length > 2147483647) {
  1262. $throwRuntimeError("makeslice: len out of range");
  1263. }
  1264. if (capacity < 0 || capacity < length || capacity > 2147483647) {
  1265. $throwRuntimeError("makeslice: cap out of range");
  1266. }
  1267. var array = new typ.nativeArray(capacity);
  1268. if (typ.nativeArray === Array) {
  1269. for (var i = 0; i < capacity; i++) {
  1270. array[i] = typ.elem.zero();
  1271. }
  1272. }
  1273. var slice = new typ(array);
  1274. slice.$length = length;
  1275. return slice;
  1276. };
  1277.  
  1278. var $structTypes = {};
  1279. var $structType = function(pkgPath, fields) {
  1280. var typeKey = $mapArray(fields, function(f) { return f.name + "," + f.typ.id + "," + f.tag; }).join("$");
  1281. var typ = $structTypes[typeKey];
  1282. if (typ === undefined) {
  1283. var string = "struct { " + $mapArray(fields, function(f) {
  1284. return f.name + " " + f.typ.string + (f.tag !== "" ? (" \"" + f.tag.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"") : "");
  1285. }).join("; ") + " }";
  1286. if (fields.length === 0) {
  1287. string = "struct {}";
  1288. }
  1289. typ = $newType(0, $kindStruct, string, false, "", false, function() {
  1290. this.$val = this;
  1291. for (var i = 0; i < fields.length; i++) {
  1292. var f = fields[i];
  1293. var arg = arguments[i];
  1294. this[f.prop] = arg !== undefined ? arg : f.typ.zero();
  1295. }
  1296. });
  1297. $structTypes[typeKey] = typ;
  1298. typ.init(pkgPath, fields);
  1299. }
  1300. return typ;
  1301. };
  1302.  
  1303. var $assertType = function(value, type, returnTuple) {
  1304. var isInterface = (type.kind === $kindInterface), ok, missingMethod = "";
  1305. if (value === $ifaceNil) {
  1306. ok = false;
  1307. } else if (!isInterface) {
  1308. ok = value.constructor === type;
  1309. } else {
  1310. var valueTypeString = value.constructor.string;
  1311. ok = type.implementedBy[valueTypeString];
  1312. if (ok === undefined) {
  1313. ok = true;
  1314. var valueMethodSet = $methodSet(value.constructor);
  1315. var interfaceMethods = type.methods;
  1316. for (var i = 0; i < interfaceMethods.length; i++) {
  1317. var tm = interfaceMethods[i];
  1318. var found = false;
  1319. for (var j = 0; j < valueMethodSet.length; j++) {
  1320. var vm = valueMethodSet[j];
  1321. if (vm.name === tm.name && vm.pkg === tm.pkg && vm.typ === tm.typ) {
  1322. found = true;
  1323. break;
  1324. }
  1325. }
  1326. if (!found) {
  1327. ok = false;
  1328. type.missingMethodFor[valueTypeString] = tm.name;
  1329. break;
  1330. }
  1331. }
  1332. type.implementedBy[valueTypeString] = ok;
  1333. }
  1334. if (!ok) {
  1335. missingMethod = type.missingMethodFor[valueTypeString];
  1336. }
  1337. }
  1338.  
  1339. if (!ok) {
  1340. if (returnTuple) {
  1341. return [type.zero(), false];
  1342. }
  1343. $panic(new $packages["runtime"].TypeAssertionError.ptr("", (value === $ifaceNil ? "" : value.constructor.string), type.string, missingMethod));
  1344. }
  1345.  
  1346. if (!isInterface) {
  1347. value = value.$val;
  1348. }
  1349. if (type === $jsObjectPtr) {
  1350. value = value.object;
  1351. }
  1352. return returnTuple ? [value, true] : value;
  1353. };
  1354.  
  1355. var $stackDepthOffset = 0;
  1356. var $getStackDepth = function() {
  1357. var err = new Error();
  1358. if (err.stack === undefined) {
  1359. return undefined;
  1360. }
  1361. return $stackDepthOffset + err.stack.split("\n").length;
  1362. };
  1363.  
  1364. var $panicStackDepth = null, $panicValue;
  1365. var $callDeferred = function(deferred, jsErr, fromPanic) {
  1366. if (!fromPanic && deferred !== null && deferred.index >= $curGoroutine.deferStack.length) {
  1367. throw jsErr;
  1368. }
  1369. if (jsErr !== null) {
  1370. var newErr = null;
  1371. try {
  1372. $curGoroutine.deferStack.push(deferred);
  1373. $panic(new $jsErrorPtr(jsErr));
  1374. } catch (err) {
  1375. newErr = err;
  1376. }
  1377. $curGoroutine.deferStack.pop();
  1378. $callDeferred(deferred, newErr);
  1379. return;
  1380. }
  1381. if ($curGoroutine.asleep) {
  1382. return;
  1383. }
  1384.  
  1385. $stackDepthOffset--;
  1386. var outerPanicStackDepth = $panicStackDepth;
  1387. var outerPanicValue = $panicValue;
  1388.  
  1389. var localPanicValue = $curGoroutine.panicStack.pop();
  1390. if (localPanicValue !== undefined) {
  1391. $panicStackDepth = $getStackDepth();
  1392. $panicValue = localPanicValue;
  1393. }
  1394.  
  1395. try {
  1396. while (true) {
  1397. if (deferred === null) {
  1398. deferred = $curGoroutine.deferStack[$curGoroutine.deferStack.length - 1];
  1399. if (deferred === undefined) {
  1400. /* The panic reached the top of the stack. Clear it and throw it as a JavaScript error. */
  1401. $panicStackDepth = null;
  1402. if (localPanicValue.Object instanceof Error) {
  1403. throw localPanicValue.Object;
  1404. }
  1405. var msg;
  1406. if (localPanicValue.constructor === $String) {
  1407. msg = localPanicValue.$val;
  1408. } else if (localPanicValue.Error !== undefined) {
  1409. msg = localPanicValue.Error();
  1410. } else if (localPanicValue.String !== undefined) {
  1411. msg = localPanicValue.String();
  1412. } else {
  1413. msg = localPanicValue;
  1414. }
  1415. throw new Error(msg);
  1416. }
  1417. }
  1418. var call = deferred.pop();
  1419. if (call === undefined) {
  1420. $curGoroutine.deferStack.pop();
  1421. if (localPanicValue !== undefined) {
  1422. deferred = null;
  1423. continue;
  1424. }
  1425. return;
  1426. }
  1427. var r = call[0].apply(call[2], call[1]);
  1428. if (r && r.$blk !== undefined) {
  1429. deferred.push([r.$blk, [], r]);
  1430. if (fromPanic) {
  1431. throw null;
  1432. }
  1433. return;
  1434. }
  1435.  
  1436. if (localPanicValue !== undefined && $panicStackDepth === null) {
  1437. throw null; /* error was recovered */
  1438. }
  1439. }
  1440. } finally {
  1441. if (localPanicValue !== undefined) {
  1442. if ($panicStackDepth !== null) {
  1443. $curGoroutine.panicStack.push(localPanicValue);
  1444. }
  1445. $panicStackDepth = outerPanicStackDepth;
  1446. $panicValue = outerPanicValue;
  1447. }
  1448. $stackDepthOffset++;
  1449. }
  1450. };
  1451.  
  1452. var $panic = function(value) {
  1453. $curGoroutine.panicStack.push(value);
  1454. $callDeferred(null, null, true);
  1455. };
  1456. var $recover = function() {
  1457. if ($panicStackDepth === null || ($panicStackDepth !== undefined && $panicStackDepth !== $getStackDepth() - 2)) {
  1458. return $ifaceNil;
  1459. }
  1460. $panicStackDepth = null;
  1461. return $panicValue;
  1462. };
  1463. var $throw = function(err) { throw err; };
  1464.  
  1465. var $noGoroutine = { asleep: false, exit: false, deferStack: [], panicStack: [] };
  1466. var $curGoroutine = $noGoroutine, $totalGoroutines = 0, $awakeGoroutines = 0, $checkForDeadlock = true;
  1467. var $mainFinished = false;
  1468. var $go = function(fun, args, direct) {
  1469. $totalGoroutines++;
  1470. $awakeGoroutines++;
  1471. var $goroutine = function() {
  1472. try {
  1473. $curGoroutine = $goroutine;
  1474. var r = fun.apply(undefined, args);
  1475. if (r && r.$blk !== undefined) {
  1476. fun = function() { return r.$blk(); };
  1477. args = [];
  1478. return;
  1479. }
  1480. $goroutine.exit = true;
  1481. } catch (err) {
  1482. if (!$goroutine.exit) {
  1483. throw err;
  1484. }
  1485. } finally {
  1486. $curGoroutine = $noGoroutine;
  1487. if ($goroutine.exit) { /* also set by runtime.Goexit() */
  1488. $totalGoroutines--;
  1489. $goroutine.asleep = true;
  1490. }
  1491. if ($goroutine.asleep) {
  1492. $awakeGoroutines--;
  1493. if (!$mainFinished && $awakeGoroutines === 0 && $checkForDeadlock) {
  1494. console.error("fatal error: all goroutines are asleep - deadlock!");
  1495. if ($global.process !== undefined) {
  1496. $global.process.exit(2);
  1497. }
  1498. }
  1499. }
  1500. }
  1501. };
  1502. $goroutine.asleep = false;
  1503. $goroutine.exit = false;
  1504. $goroutine.deferStack = [];
  1505. $goroutine.panicStack = [];
  1506. $schedule($goroutine);
  1507. };
  1508.  
  1509. var $scheduled = [];
  1510. var $runScheduled = function() {
  1511. try {
  1512. var r;
  1513. while ((r = $scheduled.shift()) !== undefined) {
  1514. r();
  1515. }
  1516. } finally {
  1517. if ($scheduled.length > 0) {
  1518. setTimeout($runScheduled, 0);
  1519. }
  1520. }
  1521. };
  1522.  
  1523. var $schedule = function(goroutine) {
  1524. if (goroutine.asleep) {
  1525. goroutine.asleep = false;
  1526. $awakeGoroutines++;
  1527. }
  1528. $scheduled.push(goroutine);
  1529. if ($curGoroutine === $noGoroutine) {
  1530. $runScheduled();
  1531. }
  1532. };
  1533.  
  1534. var $setTimeout = function(f, t) {
  1535. $awakeGoroutines++;
  1536. return setTimeout(function() {
  1537. $awakeGoroutines--;
  1538. f();
  1539. }, t);
  1540. };
  1541.  
  1542. var $block = function() {
  1543. if ($curGoroutine === $noGoroutine) {
  1544. $throwRuntimeError("cannot block in JavaScript callback, fix by wrapping code in goroutine");
  1545. }
  1546. $curGoroutine.asleep = true;
  1547. };
  1548.  
  1549. var $send = function(chan, value) {
  1550. if (chan.$closed) {
  1551. $throwRuntimeError("send on closed channel");
  1552. }
  1553. var queuedRecv = chan.$recvQueue.shift();
  1554. if (queuedRecv !== undefined) {
  1555. queuedRecv([value, true]);
  1556. return;
  1557. }
  1558. if (chan.$buffer.length < chan.$capacity) {
  1559. chan.$buffer.push(value);
  1560. return;
  1561. }
  1562.  
  1563. var thisGoroutine = $curGoroutine;
  1564. var closedDuringSend;
  1565. chan.$sendQueue.push(function(closed) {
  1566. closedDuringSend = closed;
  1567. $schedule(thisGoroutine);
  1568. return value;
  1569. });
  1570. $block();
  1571. return {
  1572. $blk: function() {
  1573. if (closedDuringSend) {
  1574. $throwRuntimeError("send on closed channel");
  1575. }
  1576. }
  1577. };
  1578. };
  1579. var $recv = function(chan) {
  1580. var queuedSend = chan.$sendQueue.shift();
  1581. if (queuedSend !== undefined) {
  1582. chan.$buffer.push(queuedSend(false));
  1583. }
  1584. var bufferedValue = chan.$buffer.shift();
  1585. if (bufferedValue !== undefined) {
  1586. return [bufferedValue, true];
  1587. }
  1588. if (chan.$closed) {
  1589. return [chan.$elem.zero(), false];
  1590. }
  1591.  
  1592. var thisGoroutine = $curGoroutine;
  1593. var f = { $blk: function() { return this.value; } };
  1594. var queueEntry = function(v) {
  1595. f.value = v;
  1596. $schedule(thisGoroutine);
  1597. };
  1598. chan.$recvQueue.push(queueEntry);
  1599. $block();
  1600. return f;
  1601. };
  1602. var $close = function(chan) {
  1603. if (chan.$closed) {
  1604. $throwRuntimeError("close of closed channel");
  1605. }
  1606. chan.$closed = true;
  1607. while (true) {
  1608. var queuedSend = chan.$sendQueue.shift();
  1609. if (queuedSend === undefined) {
  1610. break;
  1611. }
  1612. queuedSend(true); /* will panic */
  1613. }
  1614. while (true) {
  1615. var queuedRecv = chan.$recvQueue.shift();
  1616. if (queuedRecv === undefined) {
  1617. break;
  1618. }
  1619. queuedRecv([chan.$elem.zero(), false]);
  1620. }
  1621. };
  1622. var $select = function(comms) {
  1623. var ready = [];
  1624. var selection = -1;
  1625. for (var i = 0; i < comms.length; i++) {
  1626. var comm = comms[i];
  1627. var chan = comm[0];
  1628. switch (comm.length) {
  1629. case 0: /* default */
  1630. selection = i;
  1631. break;
  1632. case 1: /* recv */
  1633. if (chan.$sendQueue.length !== 0 || chan.$buffer.length !== 0 || chan.$closed) {
  1634. ready.push(i);
  1635. }
  1636. break;
  1637. case 2: /* send */
  1638. if (chan.$closed) {
  1639. $throwRuntimeError("send on closed channel");
  1640. }
  1641. if (chan.$recvQueue.length !== 0 || chan.$buffer.length < chan.$capacity) {
  1642. ready.push(i);
  1643. }
  1644. break;
  1645. }
  1646. }
  1647.  
  1648. if (ready.length !== 0) {
  1649. selection = ready[Math.floor(Math.random() * ready.length)];
  1650. }
  1651. if (selection !== -1) {
  1652. var comm = comms[selection];
  1653. switch (comm.length) {
  1654. case 0: /* default */
  1655. return [selection];
  1656. case 1: /* recv */
  1657. return [selection, $recv(comm[0])];
  1658. case 2: /* send */
  1659. $send(comm[0], comm[1]);
  1660. return [selection];
  1661. }
  1662. }
  1663.  
  1664. var entries = [];
  1665. var thisGoroutine = $curGoroutine;
  1666. var f = { $blk: function() { return this.selection; } };
  1667. var removeFromQueues = function() {
  1668. for (var i = 0; i < entries.length; i++) {
  1669. var entry = entries[i];
  1670. var queue = entry[0];
  1671. var index = queue.indexOf(entry[1]);
  1672. if (index !== -1) {
  1673. queue.splice(index, 1);
  1674. }
  1675. }
  1676. };
  1677. for (var i = 0; i < comms.length; i++) {
  1678. (function(i) {
  1679. var comm = comms[i];
  1680. switch (comm.length) {
  1681. case 1: /* recv */
  1682. var queueEntry = function(value) {
  1683. f.selection = [i, value];
  1684. removeFromQueues();
  1685. $schedule(thisGoroutine);
  1686. };
  1687. entries.push([comm[0].$recvQueue, queueEntry]);
  1688. comm[0].$recvQueue.push(queueEntry);
  1689. break;
  1690. case 2: /* send */
  1691. var queueEntry = function() {
  1692. if (comm[0].$closed) {
  1693. $throwRuntimeError("send on closed channel");
  1694. }
  1695. f.selection = [i];
  1696. removeFromQueues();
  1697. $schedule(thisGoroutine);
  1698. return comm[1];
  1699. };
  1700. entries.push([comm[0].$sendQueue, queueEntry]);
  1701. comm[0].$sendQueue.push(queueEntry);
  1702. break;
  1703. }
  1704. })(i);
  1705. }
  1706. $block();
  1707. return f;
  1708. };
  1709.  
  1710. var $jsObjectPtr, $jsErrorPtr;
  1711.  
  1712. var $needsExternalization = function(t) {
  1713. switch (t.kind) {
  1714. case $kindBool:
  1715. case $kindInt:
  1716. case $kindInt8:
  1717. case $kindInt16:
  1718. case $kindInt32:
  1719. case $kindUint:
  1720. case $kindUint8:
  1721. case $kindUint16:
  1722. case $kindUint32:
  1723. case $kindUintptr:
  1724. case $kindFloat32:
  1725. case $kindFloat64:
  1726. return false;
  1727. default:
  1728. return t !== $jsObjectPtr;
  1729. }
  1730. };
  1731.  
  1732. var $externalize = function(v, t) {
  1733. if (t === $jsObjectPtr) {
  1734. return v;
  1735. }
  1736. switch (t.kind) {
  1737. case $kindBool:
  1738. case $kindInt:
  1739. case $kindInt8:
  1740. case $kindInt16:
  1741. case $kindInt32:
  1742. case $kindUint:
  1743. case $kindUint8:
  1744. case $kindUint16:
  1745. case $kindUint32:
  1746. case $kindUintptr:
  1747. case $kindFloat32:
  1748. case $kindFloat64:
  1749. return v;
  1750. case $kindInt64:
  1751. case $kindUint64:
  1752. return $flatten64(v);
  1753. case $kindArray:
  1754. if ($needsExternalization(t.elem)) {
  1755. return $mapArray(v, function(e) { return $externalize(e, t.elem); });
  1756. }
  1757. return v;
  1758. case $kindFunc:
  1759. return $externalizeFunction(v, t, false);
  1760. case $kindInterface:
  1761. if (v === $ifaceNil) {
  1762. return null;
  1763. }
  1764. if (v.constructor === $jsObjectPtr) {
  1765. return v.$val.object;
  1766. }
  1767. return $externalize(v.$val, v.constructor);
  1768. case $kindMap:
  1769. var m = {};
  1770. var keys = $keys(v);
  1771. for (var i = 0; i < keys.length; i++) {
  1772. var entry = v[keys[i]];
  1773. m[$externalize(entry.k, t.key)] = $externalize(entry.v, t.elem);
  1774. }
  1775. return m;
  1776. case $kindPtr:
  1777. if (v === t.nil) {
  1778. return null;
  1779. }
  1780. return $externalize(v.$get(), t.elem);
  1781. case $kindSlice:
  1782. if ($needsExternalization(t.elem)) {
  1783. return $mapArray($sliceToArray(v), function(e) { return $externalize(e, t.elem); });
  1784. }
  1785. return $sliceToArray(v);
  1786. case $kindString:
  1787. if (v.search(/^[\x00-\x7F]*$/) !== -1) {
  1788. return v;
  1789. }
  1790. var s = "", r;
  1791. for (var i = 0; i < v.length; i += r[1]) {
  1792. r = $decodeRune(v, i);
  1793. var c = r[0];
  1794. if (c > 0xFFFF) {
  1795. var h = Math.floor((c - 0x10000) / 0x400) + 0xD800;
  1796. var l = (c - 0x10000) % 0x400 + 0xDC00;
  1797. s += String.fromCharCode(h, l);
  1798. continue;
  1799. }
  1800. s += String.fromCharCode(c);
  1801. }
  1802. return s;
  1803. case $kindStruct:
  1804. var timePkg = $packages["time"];
  1805. if (timePkg !== undefined && v.constructor === timePkg.Time.ptr) {
  1806. var milli = $div64(v.UnixNano(), new $Int64(0, 1000000));
  1807. return new Date($flatten64(milli));
  1808. }
  1809.  
  1810. var noJsObject = {};
  1811. var searchJsObject = function(v, t) {
  1812. if (t === $jsObjectPtr) {
  1813. return v;
  1814. }
  1815. switch (t.kind) {
  1816. case $kindPtr:
  1817. if (v === t.nil) {
  1818. return noJsObject;
  1819. }
  1820. return searchJsObject(v.$get(), t.elem);
  1821. case $kindStruct:
  1822. var f = t.fields[0];
  1823. return searchJsObject(v[f.prop], f.typ);
  1824. case $kindInterface:
  1825. return searchJsObject(v.$val, v.constructor);
  1826. default:
  1827. return noJsObject;
  1828. }
  1829. };
  1830. var o = searchJsObject(v, t);
  1831. if (o !== noJsObject) {
  1832. return o;
  1833. }
  1834.  
  1835. o = {};
  1836. for (var i = 0; i < t.fields.length; i++) {
  1837. var f = t.fields[i];
  1838. if (!f.exported) {
  1839. continue;
  1840. }
  1841. o[f.name] = $externalize(v[f.prop], f.typ);
  1842. }
  1843. return o;
  1844. }
  1845. $throwRuntimeError("cannot externalize " + t.string);
  1846. };
  1847.  
  1848. var $externalizeFunction = function(v, t, passThis) {
  1849. if (v === $throwNilPointerError) {
  1850. return null;
  1851. }
  1852. if (v.$externalizeWrapper === undefined) {
  1853. $checkForDeadlock = false;
  1854. v.$externalizeWrapper = function() {
  1855. var args = [];
  1856. for (var i = 0; i < t.params.length; i++) {
  1857. if (t.variadic && i === t.params.length - 1) {
  1858. var vt = t.params[i].elem, varargs = [];
  1859. for (var j = i; j < arguments.length; j++) {
  1860. varargs.push($internalize(arguments[j], vt));
  1861. }
  1862. args.push(new (t.params[i])(varargs));
  1863. break;
  1864. }
  1865. args.push($internalize(arguments[i], t.params[i]));
  1866. }
  1867. var canBlock = $curGoroutine.canBlock;
  1868. $curGoroutine.canBlock = false;
  1869. try {
  1870. var result = v.apply(passThis ? this : undefined, args);
  1871. } finally {
  1872. $curGoroutine.canBlock = canBlock;
  1873. }
  1874. switch (t.results.length) {
  1875. case 0:
  1876. return;
  1877. case 1:
  1878. return $externalize(result, t.results[0]);
  1879. default:
  1880. for (var i = 0; i < t.results.length; i++) {
  1881. result[i] = $externalize(result[i], t.results[i]);
  1882. }
  1883. return result;
  1884. }
  1885. };
  1886. }
  1887. return v.$externalizeWrapper;
  1888. };
  1889.  
  1890. var $internalize = function(v, t, recv) {
  1891. if (t === $jsObjectPtr) {
  1892. return v;
  1893. }
  1894. if (t === $jsObjectPtr.elem) {
  1895. $throwRuntimeError("cannot internalize js.Object, use *js.Object instead");
  1896. }
  1897. if (v && v.__internal_object__ !== undefined) {
  1898. return $assertType(v.__internal_object__, t, false);
  1899. }
  1900. var timePkg = $packages["time"];
  1901. if (timePkg !== undefined && t === timePkg.Time) {
  1902. if (!(v !== null && v !== undefined && v.constructor === Date)) {
  1903. $throwRuntimeError("cannot internalize time.Time from " + typeof v + ", must be Date");
  1904. }
  1905. return timePkg.Unix(new $Int64(0, 0), new $Int64(0, v.getTime() * 1000000));
  1906. }
  1907. switch (t.kind) {
  1908. case $kindBool:
  1909. return !!v;
  1910. case $kindInt:
  1911. return parseInt(v);
  1912. case $kindInt8:
  1913. return parseInt(v) << 24 >> 24;
  1914. case $kindInt16:
  1915. return parseInt(v) << 16 >> 16;
  1916. case $kindInt32:
  1917. return parseInt(v) >> 0;
  1918. case $kindUint:
  1919. return parseInt(v);
  1920. case $kindUint8:
  1921. return parseInt(v) << 24 >>> 24;
  1922. case $kindUint16:
  1923. return parseInt(v) << 16 >>> 16;
  1924. case $kindUint32:
  1925. case $kindUintptr:
  1926. return parseInt(v) >>> 0;
  1927. case $kindInt64:
  1928. case $kindUint64:
  1929. return new t(0, v);
  1930. case $kindFloat32:
  1931. case $kindFloat64:
  1932. return parseFloat(v);
  1933. case $kindArray:
  1934. if (v.length !== t.len) {
  1935. $throwRuntimeError("got array with wrong size from JavaScript native");
  1936. }
  1937. return $mapArray(v, function(e) { return $internalize(e, t.elem); });
  1938. case $kindFunc:
  1939. return function() {
  1940. var args = [];
  1941. for (var i = 0; i < t.params.length; i++) {
  1942. if (t.variadic && i === t.params.length - 1) {
  1943. var vt = t.params[i].elem, varargs = arguments[i];
  1944. for (var j = 0; j < varargs.$length; j++) {
  1945. args.push($externalize(varargs.$array[varargs.$offset + j], vt));
  1946. }
  1947. break;
  1948. }
  1949. args.push($externalize(arguments[i], t.params[i]));
  1950. }
  1951. var result = v.apply(recv, args);
  1952. switch (t.results.length) {
  1953. case 0:
  1954. return;
  1955. case 1:
  1956. return $internalize(result, t.results[0]);
  1957. default:
  1958. for (var i = 0; i < t.results.length; i++) {
  1959. result[i] = $internalize(result[i], t.results[i]);
  1960. }
  1961. return result;
  1962. }
  1963. };
  1964. case $kindInterface:
  1965. if (t.methods.length !== 0) {
  1966. $throwRuntimeError("cannot internalize " + t.string);
  1967. }
  1968. if (v === null) {
  1969. return $ifaceNil;
  1970. }
  1971. if (v === undefined) {
  1972. return new $jsObjectPtr(undefined);
  1973. }
  1974. switch (v.constructor) {
  1975. case Int8Array:
  1976. return new ($sliceType($Int8))(v);
  1977. case Int16Array:
  1978. return new ($sliceType($Int16))(v);
  1979. case Int32Array:
  1980. return new ($sliceType($Int))(v);
  1981. case Uint8Array:
  1982. return new ($sliceType($Uint8))(v);
  1983. case Uint16Array:
  1984. return new ($sliceType($Uint16))(v);
  1985. case Uint32Array:
  1986. return new ($sliceType($Uint))(v);
  1987. case Float32Array:
  1988. return new ($sliceType($Float32))(v);
  1989. case Float64Array:
  1990. return new ($sliceType($Float64))(v);
  1991. case Array:
  1992. return $internalize(v, $sliceType($emptyInterface));
  1993. case Boolean:
  1994. return new $Bool(!!v);
  1995. case Date:
  1996. if (timePkg === undefined) {
  1997. /* time package is not present, internalize as &js.Object{Date} so it can be externalized into original Date. */
  1998. return new $jsObjectPtr(v);
  1999. }
  2000. return new timePkg.Time($internalize(v, timePkg.Time));
  2001. case Function:
  2002. var funcType = $funcType([$sliceType($emptyInterface)], [$jsObjectPtr], true);
  2003. return new funcType($internalize(v, funcType));
  2004. case Number:
  2005. return new $Float64(parseFloat(v));
  2006. case String:
  2007. return new $String($internalize(v, $String));
  2008. default:
  2009. if ($global.Node && v instanceof $global.Node) {
  2010. return new $jsObjectPtr(v);
  2011. }
  2012. var mapType = $mapType($String, $emptyInterface);
  2013. return new mapType($internalize(v, mapType));
  2014. }
  2015. case $kindMap:
  2016. var m = {};
  2017. var keys = $keys(v);
  2018. for (var i = 0; i < keys.length; i++) {
  2019. var k = $internalize(keys[i], t.key);
  2020. m[t.key.keyFor(k)] = { k: k, v: $internalize(v[keys[i]], t.elem) };
  2021. }
  2022. return m;
  2023. case $kindPtr:
  2024. if (t.elem.kind === $kindStruct) {
  2025. return $internalize(v, t.elem);
  2026. }
  2027. case $kindSlice:
  2028. return new t($mapArray(v, function(e) { return $internalize(e, t.elem); }));
  2029. case $kindString:
  2030. v = String(v);
  2031. if (v.search(/^[\x00-\x7F]*$/) !== -1) {
  2032. return v;
  2033. }
  2034. var s = "";
  2035. var i = 0;
  2036. while (i < v.length) {
  2037. var h = v.charCodeAt(i);
  2038. if (0xD800 <= h && h <= 0xDBFF) {
  2039. var l = v.charCodeAt(i + 1);
  2040. var c = (h - 0xD800) * 0x400 + l - 0xDC00 + 0x10000;
  2041. s += $encodeRune(c);
  2042. i += 2;
  2043. continue;
  2044. }
  2045. s += $encodeRune(h);
  2046. i++;
  2047. }
  2048. return s;
  2049. case $kindStruct:
  2050. var noJsObject = {};
  2051. var searchJsObject = function(t) {
  2052. if (t === $jsObjectPtr) {
  2053. return v;
  2054. }
  2055. if (t === $jsObjectPtr.elem) {
  2056. $throwRuntimeError("cannot internalize js.Object, use *js.Object instead");
  2057. }
  2058. switch (t.kind) {
  2059. case $kindPtr:
  2060. return searchJsObject(t.elem);
  2061. case $kindStruct:
  2062. var f = t.fields[0];
  2063. var o = searchJsObject(f.typ);
  2064. if (o !== noJsObject) {
  2065. var n = new t.ptr();
  2066. n[f.prop] = o;
  2067. return n;
  2068. }
  2069. return noJsObject;
  2070. default:
  2071. return noJsObject;
  2072. }
  2073. };
  2074. var o = searchJsObject(t);
  2075. if (o !== noJsObject) {
  2076. return o;
  2077. }
  2078. }
  2079. $throwRuntimeError("cannot internalize " + t.string);
  2080. };
  2081.  
  2082. $packages["github.com/gopherjs/gopherjs/js"] = (function() {
  2083. var $pkg = {}, $init, Object, Error, sliceType, ptrType, ptrType$1, init;
  2084. Object = $pkg.Object = $newType(0, $kindStruct, "js.Object", true, "github.com/gopherjs/gopherjs/js", true, function(object_) {
  2085. this.$val = this;
  2086. if (arguments.length === 0) {
  2087. this.object = null;
  2088. return;
  2089. }
  2090. this.object = object_;
  2091. });
  2092. Error = $pkg.Error = $newType(0, $kindStruct, "js.Error", true, "github.com/gopherjs/gopherjs/js", true, function(Object_) {
  2093. this.$val = this;
  2094. if (arguments.length === 0) {
  2095. this.Object = null;
  2096. return;
  2097. }
  2098. this.Object = Object_;
  2099. });
  2100. sliceType = $sliceType($emptyInterface);
  2101. ptrType = $ptrType(Object);
  2102. ptrType$1 = $ptrType(Error);
  2103. Object.ptr.prototype.Get = function(key) {
  2104. var $ptr, key, o;
  2105. o = this;
  2106. return o.object[$externalize(key, $String)];
  2107. };
  2108. Object.prototype.Get = function(key) { return this.$val.Get(key); };
  2109. Object.ptr.prototype.Set = function(key, value) {
  2110. var $ptr, key, o, value;
  2111. o = this;
  2112. o.object[$externalize(key, $String)] = $externalize(value, $emptyInterface);
  2113. };
  2114. Object.prototype.Set = function(key, value) { return this.$val.Set(key, value); };
  2115. Object.ptr.prototype.Delete = function(key) {
  2116. var $ptr, key, o;
  2117. o = this;
  2118. delete o.object[$externalize(key, $String)];
  2119. };
  2120. Object.prototype.Delete = function(key) { return this.$val.Delete(key); };
  2121. Object.ptr.prototype.Length = function() {
  2122. var $ptr, o;
  2123. o = this;
  2124. return $parseInt(o.object.length);
  2125. };
  2126. Object.prototype.Length = function() { return this.$val.Length(); };
  2127. Object.ptr.prototype.Index = function(i) {
  2128. var $ptr, i, o;
  2129. o = this;
  2130. return o.object[i];
  2131. };
  2132. Object.prototype.Index = function(i) { return this.$val.Index(i); };
  2133. Object.ptr.prototype.SetIndex = function(i, value) {
  2134. var $ptr, i, o, value;
  2135. o = this;
  2136. o.object[i] = $externalize(value, $emptyInterface);
  2137. };
  2138. Object.prototype.SetIndex = function(i, value) { return this.$val.SetIndex(i, value); };
  2139. Object.ptr.prototype.Call = function(name, args) {
  2140. var $ptr, args, name, o, obj;
  2141. o = this;
  2142. return (obj = o.object, obj[$externalize(name, $String)].apply(obj, $externalize(args, sliceType)));
  2143. };
  2144. Object.prototype.Call = function(name, args) { return this.$val.Call(name, args); };
  2145. Object.ptr.prototype.Invoke = function(args) {
  2146. var $ptr, args, o;
  2147. o = this;
  2148. return o.object.apply(undefined, $externalize(args, sliceType));
  2149. };
  2150. Object.prototype.Invoke = function(args) { return this.$val.Invoke(args); };
  2151. Object.ptr.prototype.New = function(args) {
  2152. var $ptr, args, o;
  2153. o = this;
  2154. return new ($global.Function.prototype.bind.apply(o.object, [undefined].concat($externalize(args, sliceType))));
  2155. };
  2156. Object.prototype.New = function(args) { return this.$val.New(args); };
  2157. Object.ptr.prototype.Bool = function() {
  2158. var $ptr, o;
  2159. o = this;
  2160. return !!(o.object);
  2161. };
  2162. Object.prototype.Bool = function() { return this.$val.Bool(); };
  2163. Object.ptr.prototype.String = function() {
  2164. var $ptr, o;
  2165. o = this;
  2166. return $internalize(o.object, $String);
  2167. };
  2168. Object.prototype.String = function() { return this.$val.String(); };
  2169. Object.ptr.prototype.Int = function() {
  2170. var $ptr, o;
  2171. o = this;
  2172. return $parseInt(o.object) >> 0;
  2173. };
  2174. Object.prototype.Int = function() { return this.$val.Int(); };
  2175. Object.ptr.prototype.Int64 = function() {
  2176. var $ptr, o;
  2177. o = this;
  2178. return $internalize(o.object, $Int64);
  2179. };
  2180. Object.prototype.Int64 = function() { return this.$val.Int64(); };
  2181. Object.ptr.prototype.Uint64 = function() {
  2182. var $ptr, o;
  2183. o = this;
  2184. return $internalize(o.object, $Uint64);
  2185. };
  2186. Object.prototype.Uint64 = function() { return this.$val.Uint64(); };
  2187. Object.ptr.prototype.Float = function() {
  2188. var $ptr, o;
  2189. o = this;
  2190. return $parseFloat(o.object);
  2191. };
  2192. Object.prototype.Float = function() { return this.$val.Float(); };
  2193. Object.ptr.prototype.Interface = function() {
  2194. var $ptr, o;
  2195. o = this;
  2196. return $internalize(o.object, $emptyInterface);
  2197. };
  2198. Object.prototype.Interface = function() { return this.$val.Interface(); };
  2199. Object.ptr.prototype.Unsafe = function() {
  2200. var $ptr, o;
  2201. o = this;
  2202. return o.object;
  2203. };
  2204. Object.prototype.Unsafe = function() { return this.$val.Unsafe(); };
  2205. Error.ptr.prototype.Error = function() {
  2206. var $ptr, err;
  2207. err = this;
  2208. return "JavaScript error: " + $internalize(err.Object.message, $String);
  2209. };
  2210. Error.prototype.Error = function() { return this.$val.Error(); };
  2211. Error.ptr.prototype.Stack = function() {
  2212. var $ptr, err;
  2213. err = this;
  2214. return $internalize(err.Object.stack, $String);
  2215. };
  2216. Error.prototype.Stack = function() { return this.$val.Stack(); };
  2217. init = function() {
  2218. var $ptr, e;
  2219. e = new Error.ptr(null);
  2220. $unused(e);
  2221. };
  2222. ptrType.methods = [{prop: "Get", name: "Get", pkg: "", typ: $funcType([$String], [ptrType], false)}, {prop: "Set", name: "Set", pkg: "", typ: $funcType([$String, $emptyInterface], [], false)}, {prop: "Delete", name: "Delete", pkg: "", typ: $funcType([$String], [], false)}, {prop: "Length", name: "Length", pkg: "", typ: $funcType([], [$Int], false)}, {prop: "Index", name: "Index", pkg: "", typ: $funcType([$Int], [ptrType], false)}, {prop: "SetIndex", name: "SetIndex", pkg: "", typ: $funcType([$Int, $emptyInterface], [], false)}, {prop: "Call", name: "Call", pkg: "", typ: $funcType([$String, sliceType], [ptrType], true)}, {prop: "Invoke", name: "Invoke", pkg: "", typ: $funcType([sliceType], [ptrType], true)}, {prop: "New", name: "New", pkg: "", typ: $funcType([sliceType], [ptrType], true)}, {prop: "Bool", name: "Bool", pkg: "", typ: $funcType([], [$Bool], false)}, {prop: "String", name: "String", pkg: "", typ: $funcType([], [$String], false)}, {prop: "Int", name: "Int", pkg: "", typ: $funcType([], [$Int], false)}, {prop: "Int64", name: "Int64", pkg: "", typ: $funcType([], [$Int64], false)}, {prop: "Uint64", name: "Uint64", pkg: "", typ: $funcType([], [$Uint64], false)}, {prop: "Float", name: "Float", pkg: "", typ: $funcType([], [$Float64], false)}, {prop: "Interface", name: "Interface", pkg: "", typ: $funcType([], [$emptyInterface], false)}, {prop: "Unsafe", name: "Unsafe", pkg: "", typ: $funcType([], [$Uintptr], false)}];
  2223. ptrType$1.methods = [{prop: "Error", name: "Error", pkg: "", typ: $funcType([], [$String], false)}, {prop: "Stack", name: "Stack", pkg: "", typ: $funcType([], [$String], false)}];
  2224. Object.init("github.com/gopherjs/gopherjs/js", [{prop: "object", name: "object", exported: false, typ: ptrType, tag: ""}]);
  2225. Error.init("", [{prop: "Object", name: "", exported: true, typ: ptrType, tag: ""}]);
  2226. $init = function() {
  2227. $pkg.$init = function() {};
  2228. /* */ var $f, $c = false, $s = 0, $r; if (this !== undefined && this.$blk !== undefined) { $f = this; $c = true; $s = $f.$s; $r = $f.$r; } s: while (true) { switch ($s) { case 0:
  2229. init();
  2230. /* */ } return; } if ($f === undefined) { $f = { $blk: $init }; } $f.$s = $s; $f.$r = $r; return $f;
  2231. };
  2232. $pkg.$init = $init;
  2233. return $pkg;
  2234. })();
  2235. $packages["runtime/internal/sys"] = (function() {
  2236. var $pkg = {}, $init;
  2237. $init = function() {
  2238. $pkg.$init = function() {};
  2239. /* */ var $f, $c = false, $s = 0, $r; if (this !== undefined && this.$blk !== undefined) { $f = this; $c = true; $s = $f.$s; $r = $f.$r; } s: while (true) { switch ($s) { case 0:
  2240. /* */ } return; } if ($f === undefined) { $f = { $blk: $init }; } $f.$s = $s; $f.$r = $r; return $f;
  2241. };
  2242. $pkg.$init = $init;
  2243. return $pkg;
  2244. })();
  2245. $packages["runtime"] = (function() {
  2246. var $pkg = {}, $init, js, sys, TypeAssertionError, errorString, ptrType$3, init;
  2247. js = $packages["github.com/gopherjs/gopherjs/js"];
  2248. sys = $packages["runtime/internal/sys"];
  2249. TypeAssertionError = $pkg.TypeAssertionError = $newType(0, $kindStruct, "runtime.TypeAssertionError", true, "runtime", true, function(interfaceString_, concreteString_, assertedString_, missingMethod_) {
  2250. this.$val = this;
  2251. if (arguments.length === 0) {
  2252. this.interfaceString = "";
  2253. this.concreteString = "";
  2254. this.assertedString = "";
  2255. this.missingMethod = "";
  2256. return;
  2257. }
  2258. this.interfaceString = interfaceString_;
  2259. this.concreteString = concreteString_;
  2260. this.assertedString = assertedString_;
  2261. this.missingMethod = missingMethod_;
  2262. });
  2263. errorString = $pkg.errorString = $newType(8, $kindString, "runtime.errorString", true, "runtime", false, null);
  2264. ptrType$3 = $ptrType(TypeAssertionError);
  2265. init = function() {
  2266. var $ptr, e, jsPkg;
  2267. jsPkg = $packages[$externalize("github.com/gopherjs/gopherjs/js", $String)];
  2268. $jsObjectPtr = jsPkg.Object.ptr;
  2269. $jsErrorPtr = jsPkg.Error.ptr;
  2270. $throwRuntimeError = (function(msg) {
  2271. var $ptr, msg;
  2272. $panic(new errorString(msg));
  2273. });
  2274. e = $ifaceNil;
  2275. e = new TypeAssertionError.ptr("", "", "", "");
  2276. $unused(e);
  2277. };
  2278. TypeAssertionError.ptr.prototype.RuntimeError = function() {
  2279. var $ptr;
  2280. };
  2281. TypeAssertionError.prototype.RuntimeError = function() { return this.$val.RuntimeError(); };
  2282. TypeAssertionError.ptr.prototype.Error = function() {
  2283. var $ptr, e, inter;
  2284. e = this;
  2285. inter = e.interfaceString;
  2286. if (inter === "") {
  2287. inter = "interface";
  2288. }
  2289. if (e.concreteString === "") {
  2290. return "interface conversion: " + inter + " is nil, not " + e.assertedString;
  2291. }
  2292. if (e.missingMethod === "") {
  2293. return "interface conversion: " + inter + " is " + e.concreteString + ", not " + e.assertedString;
  2294. }
  2295. return "interface conversion: " + e.concreteString + " is not " + e.assertedString + ": missing method " + e.missingMethod;
  2296. };
  2297. TypeAssertionError.prototype.Error = function() { return this.$val.Error(); };
  2298. errorString.prototype.RuntimeError = function() {
  2299. var $ptr, e;
  2300. e = this.$val;
  2301. };
  2302. $ptrType(errorString).prototype.RuntimeError = function() { return new errorString(this.$get()).RuntimeError(); };
  2303. errorString.prototype.Error = function() {
  2304. var $ptr, e;
  2305. e = this.$val;
  2306. return "runtime error: " + e;
  2307. };
  2308. $ptrType(errorString).prototype.Error = function() { return new errorString(this.$get()).Error(); };
  2309. ptrType$3.methods = [{prop: "RuntimeError", name: "RuntimeError", pkg: "", typ: $funcType([], [], false)}, {prop: "Error", name: "Error", pkg: "", typ: $funcType([], [$String], false)}];
  2310. errorString.methods = [{prop: "RuntimeError", name: "RuntimeError", pkg: "", typ: $funcType([], [], false)}, {prop: "Error", name: "Error", pkg: "", typ: $funcType([], [$String], false)}];
  2311. TypeAssertionError.init("runtime", [{prop: "interfaceString", name: "interfaceString", exported: false, typ: $String, tag: ""}, {prop: "concreteString", name: "concreteString", exported: false, typ: $String, tag: ""}, {prop: "assertedString", name: "assertedString", exported: false, typ: $String, tag: ""}, {prop: "missingMethod", name: "missingMethod", exported: false, typ: $String, tag: ""}]);
  2312. $init = function() {
  2313. $pkg.$init = function() {};
  2314. /* */ var $f, $c = false, $s = 0, $r; if (this !== undefined && this.$blk !== undefined) { $f = this; $c = true; $s = $f.$s; $r = $f.$r; } s: while (true) { switch ($s) { case 0:
  2315. $r = js.$init(); /* */ $s = 1; case 1: if($c) { $c = false; $r = $r.$blk(); } if ($r && $r.$blk !== undefined) { break s; }
  2316. $r = sys.$init(); /* */ $s = 2; case 2: if($c) { $c = false; $r = $r.$blk(); } if ($r && $r.$blk !== undefined) { break s; }
  2317. init();
  2318. /* */ } return; } if ($f === undefined) { $f = { $blk: $init }; } $f.$s = $s; $f.$r = $r; return $f;
  2319. };
  2320. $pkg.$init = $init;
  2321. return $pkg;
  2322. })();
  2323. $packages["main"] = (function() {
  2324. var $pkg = {}, $init, MagikCraft, main, lightning;
  2325. MagikCraft = $pkg.MagikCraft = $newType(0, $kindStruct, "main.MagikCraft", true, "main", true, function(x_) {
  2326. this.$val = this;
  2327. if (arguments.length === 0) {
  2328. this.x = 0;
  2329. return;
  2330. }
  2331. this.x = x_;
  2332. });
  2333. MagikCraft.ptr.prototype.shakti = function() {
  2334. var $ptr, m;
  2335. m = this;
  2336. return "You cast lightning";
  2337. };
  2338. MagikCraft.prototype.shakti = function() { return this.$val.shakti(); };
  2339. main = function(arg) {
  2340. var $ptr, arg;
  2341. lightning(arg);
  2342. };
  2343. lightning = function(num) {
  2344. var $ptr, i, magik, num, spell;
  2345. magik = new MagikCraft.ptr(0);
  2346. i = 0;
  2347. while (true) {
  2348. if (!(i < num)) { break; }
  2349. spell = $clone(magik, MagikCraft).shakti();
  2350. console.log(spell);
  2351. i = i + (1) >> 0;
  2352. }
  2353. };
  2354. MagikCraft.methods = [{prop: "shakti", name: "shakti", pkg: "main", typ: $funcType([], [$String], false)}];
  2355. MagikCraft.init("main", [{prop: "x", name: "x", exported: false, typ: $Int, tag: ""}]);
  2356. $init = function() {
  2357. $pkg.$init = function() {};
  2358. /* */ var $f, $c = false, $s = 0, $r; if (this !== undefined && this.$blk !== undefined) { $f = this; $c = true; $s = $f.$s; $r = $f.$r; } s: while (true) { switch ($s) { case 0:
  2359. if ($pkg === $mainPkg) {
  2360. main();
  2361. $mainFinished = true;
  2362. }
  2363. /* */ } return; } if ($f === undefined) { $f = { $blk: $init }; } $f.$s = $s; $f.$r = $r; return $f;
  2364. };
  2365. $pkg.$init = $init;
  2366. return $pkg;
  2367. })();
  2368. $synthesizeMethods();
  2369. var $mainPkg = $packages["main"];
  2370. $packages["runtime"].$init();
  2371. $go($mainPkg.$init, []);
  2372. $flushConsole();
  2373.  
  2374. }).call(this);
  2375. //# sourceMappingURL=mc.js.map
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement