opencc-rust-wasm

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

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.greatest.deepsurf.us/scripts/413698/859508/opencc-rust-wasm.js을(를) 사용하여 포함하는 라이브러리입니다.

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