opencc-rust-wasm

opencc-rust的wasm生成文件,异步完成翻译

目前为 2020-10-18 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/413698/859052/opencc-rust-wasm.js

  1. let wasm;
  2.  
  3. let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
  4.  
  5. cachedTextDecoder.decode();
  6.  
  7. let cachegetUint8Memory0 = null;
  8. function getUint8Memory0() {
  9. if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
  10. cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
  11. }
  12. return cachegetUint8Memory0;
  13. }
  14.  
  15. function getStringFromWasm0(ptr, len) {
  16. return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
  17. }
  18.  
  19. let WASM_VECTOR_LEN = 0;
  20.  
  21. let cachedTextEncoder = new TextEncoder('utf-8');
  22.  
  23. const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
  24. ? function (arg, view) {
  25. return cachedTextEncoder.encodeInto(arg, view);
  26. }
  27. : function (arg, view) {
  28. const buf = cachedTextEncoder.encode(arg);
  29. view.set(buf);
  30. return {
  31. read: arg.length,
  32. written: buf.length
  33. };
  34. });
  35.  
  36. function passStringToWasm0(arg, malloc, realloc) {
  37.  
  38. if (realloc === undefined) {
  39. const buf = cachedTextEncoder.encode(arg);
  40. const ptr = malloc(buf.length);
  41. getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
  42. WASM_VECTOR_LEN = buf.length;
  43. return ptr;
  44. }
  45.  
  46. let len = arg.length;
  47. let ptr = malloc(len);
  48.  
  49. const mem = getUint8Memory0();
  50.  
  51. let offset = 0;
  52.  
  53. for (; offset < len; offset++) {
  54. const code = arg.charCodeAt(offset);
  55. if (code > 0x7F) break;
  56. mem[ptr + offset] = code;
  57. }
  58.  
  59. if (offset !== len) {
  60. if (offset !== 0) {
  61. arg = arg.slice(offset);
  62. }
  63. ptr = realloc(ptr, len, len = offset + arg.length * 3);
  64. const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
  65. const ret = encodeString(arg, view);
  66.  
  67. offset += ret.written;
  68. }
  69.  
  70. WASM_VECTOR_LEN = offset;
  71. return ptr;
  72. }
  73.  
  74. let cachegetInt32Memory0 = null;
  75. function getInt32Memory0() {
  76. if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
  77. cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
  78. }
  79. return cachegetInt32Memory0;
  80. }
  81. /**
  82. */
  83. class Converter {
  84.  
  85. static __wrap(ptr) {
  86. const obj = Object.create(Converter.prototype);
  87. obj.ptr = ptr;
  88.  
  89. return obj;
  90. }
  91.  
  92. free() {
  93. const ptr = this.ptr;
  94. this.ptr = 0;
  95.  
  96. wasm.__wbg_converter_free(ptr);
  97. }
  98. /**
  99. * @param {string} data
  100. * @returns {string}
  101. */
  102. convert(data) {
  103. try {
  104. const retptr = wasm.__wbindgen_export_2.value - 16;
  105. wasm.__wbindgen_export_2.value = retptr;
  106. var ptr0 = passStringToWasm0(data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  107. var len0 = WASM_VECTOR_LEN;
  108. wasm.converter_convert(retptr, this.ptr, ptr0, len0);
  109. var r0 = getInt32Memory0()[retptr / 4 + 0];
  110. var r1 = getInt32Memory0()[retptr / 4 + 1];
  111. return getStringFromWasm0(r0, r1);
  112. } finally {
  113. wasm.__wbindgen_export_2.value += 16;
  114. wasm.__wbindgen_free(r0, r1);
  115. }
  116. }
  117. /**
  118. */
  119. delete() {
  120. wasm.converter_delete(this.ptr);
  121. }
  122. }
  123. /**
  124. */
  125. class ConverterBuild {
  126.  
  127. static __wrap(ptr) {
  128. const obj = Object.create(ConverterBuild.prototype);
  129. obj.ptr = ptr;
  130.  
  131. return obj;
  132. }
  133.  
  134. free() {
  135. const ptr = this.ptr;
  136. this.ptr = 0;
  137.  
  138. wasm.__wbg_converterbuild_free(ptr);
  139. }
  140. /**
  141. * @returns {ConverterBuild}
  142. */
  143. static new() {
  144. var ret = wasm.converterbuild_new();
  145. return ConverterBuild.__wrap(ret);
  146. }
  147. /**
  148. * @param {string} data
  149. */
  150. adddict(data) {
  151. var ptr0 = passStringToWasm0(data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  152. var len0 = WASM_VECTOR_LEN;
  153. wasm.converterbuild_adddict(this.ptr, ptr0, len0);
  154. }
  155. /**
  156. */
  157. group() {
  158. wasm.converterbuild_group(this.ptr);
  159. }
  160. /**
  161. * @returns {Converter}
  162. */
  163. build() {
  164. var ret = wasm.converterbuild_build(this.ptr);
  165. return Converter.__wrap(ret);
  166. }
  167. }
  168.  
  169. async function load(module, imports) {
  170. if (typeof Response === 'function' && module instanceof Response) {
  171.  
  172. if (typeof WebAssembly.instantiateStreaming === 'function') {
  173. try {
  174. return await WebAssembly.instantiateStreaming(module, imports);
  175.  
  176. } catch (e) {
  177. if (module.headers.get('Content-Type') != 'application/wasm') {
  178. console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
  179.  
  180. } else {
  181. throw e;
  182. }
  183. }
  184. }
  185.  
  186. const bytes = await module.arrayBuffer();
  187. return await WebAssembly.instantiate(bytes, imports);
  188.  
  189. } else {
  190.  
  191. const instance = await WebAssembly.instantiate(module, imports);
  192.  
  193. if (instance instanceof WebAssembly.Instance) {
  194. return { instance, module };
  195.  
  196. } else {
  197. return instance;
  198. }
  199. }
  200. }
  201.  
  202. async function init(input) {
  203. const imports = {};
  204. imports.wbg = {};
  205. imports.wbg.__wbindgen_throw = function(arg0, arg1) {
  206. throw new Error(getStringFromWasm0(arg0, arg1));
  207. };
  208. const { instance, module } = await load(await input, imports);
  209.  
  210. wasm = instance.exports;
  211. init.__wbindgen_wasm_module = module;
  212.  
  213. return wasm;
  214. }