@lynx-js/tasm 0.0.30 → 0.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ # 0.0.33
4
+ * fix encode JsBytecode issue.
5
+
6
+ # 0.0.32
7
+ * Support encoding and decoding template attribute arrays, slot indices, and object-form template entries.
8
+
9
+ # 0.0.31
10
+ * add lepus IR optimize module to optimize lepus bytecode;
11
+
3
12
  # 0.0.30
4
13
  * enlarge wasm MAXIMUM_MEMORY to 4G;
5
14
 
Binary file
Binary file
package/index.d.ts CHANGED
@@ -4,8 +4,11 @@
4
4
 
5
5
  /* eslint-disable */
6
6
  export function encode(code: any): Promise<EncodeResult>;
7
- export function loadModule(): any;
8
- export function lepusCheck(code: string): any;
7
+ export function lepusCheck(
8
+ code: string,
9
+ targetSdkVersion?: string,
10
+ execute?: number
11
+ ): Promise<any>;
9
12
  export interface EncodeResult {
10
13
  status: number;
11
14
  buffer: Buffer;
@@ -18,7 +21,7 @@ export function supportNapi(binary?: string): boolean;
18
21
  export function getEncodeMode(binary?: string): (options: any) => Promise<EncodeResult>;
19
22
  export function encrypt(token: string): string;
20
23
  export function decrypt(token: string): string;
21
- export function encrypt_wasm(token: string): string;
22
- export function decrypt_wasm(token: string): string;
24
+ export function encrypt_wasm(token: string): Promise<string>;
25
+ export function decrypt_wasm(token: string): Promise<string>;
23
26
  export function decode_napi(template: Uint8Array): any;
24
- export function decode_wasm(template: Uint8Array): any;
27
+ export function decode_wasm(template: Uint8Array): Promise<any>;
package/index.js CHANGED
@@ -5,33 +5,50 @@
5
5
  const path = require('path');
6
6
  const fs = require('fs');
7
7
 
8
- let task = null;
9
8
  function supportNapi(){
10
9
  return process.platform === 'darwin' || (process.platform === 'linux' && process.arch === "x64")
11
10
  }
12
11
 
13
- function loadModule() {
12
+ function createModule() {
13
+ // Reusing a single WASM module instance makes repeated encode/decode calls
14
+ // on JsBytecode custom sections nondeterministic. Use a fresh instance for
15
+ // codec operations that need stable binary output.
14
16
  // eslint-disable-next-line @typescript-eslint/no-var-requires
15
- const _loadModule = require('./lepus');
16
- if (!task) {
17
- task = _loadModule();
17
+ return require('./lepus')();
18
+ }
19
+
20
+ function disposeClassHandle(handle) {
21
+ if (!handle || typeof handle.delete !== 'function') {
22
+ return;
23
+ }
24
+ if (typeof handle.isDeleted === 'function' && handle.isDeleted()) {
25
+ return;
18
26
  }
19
- return task;
27
+ handle.delete();
20
28
  }
21
29
 
22
30
  async function encode_wasm(options) {
23
- const m = await loadModule();
31
+ const m = await createModule();
24
32
  /** @type {import('./index').EncodeResult} */
25
33
  const res = m._encode(JSON.stringify(options))
26
- if (res.status !== 0) {
27
- throw new Error(`encode error: ${res.error_msg}`);
28
- }
29
- const uint8array = new Uint8Array(res.buffer.size());
30
- for (let i = 0; i < res.buffer.size(); i++) {
31
- uint8array[i] = res.buffer.get(i);
32
- }
33
- res.buffer = Buffer.from(uint8array);
34
- if(options.onTemplateDebugGenerated){
34
+ const bufferHandle = res.buffer;
35
+ try {
36
+ if (res.status !== 0) {
37
+ throw new Error(`encode error: ${res.error_msg}`);
38
+ }
39
+ const out = {
40
+ status: res.status,
41
+ error_msg: res.error_msg,
42
+ lepus_code: res.lepus_code,
43
+ lepus_debug: res.lepus_debug,
44
+ section_size: res.section_size,
45
+ };
46
+ const uint8array = new Uint8Array(bufferHandle.size());
47
+ for (let i = 0; i < bufferHandle.size(); i++) {
48
+ uint8array[i] = bufferHandle.get(i);
49
+ }
50
+ out.buffer = Buffer.from(uint8array);
51
+ if(options.onTemplateDebugGenerated){
35
52
  const url = await options.onTemplateDebugGenerated(res.template_debug, options.templateDebugUrl);
36
53
  const template_url_ptr = m.allocateUTF8(url);
37
54
  const res_str_ptr = m._reencode_template_debug(bufferPtr, bufferLen, template_url_ptr, buffer_pool);
@@ -39,9 +56,13 @@ async function encode_wasm(options) {
39
56
  const reEncodedRes = JSON.parse(reEncoded_res_str);
40
57
  const reEncodedBufferPtr = m._readBufferPool_data(buffer_pool, reEncodedRes.buffer);
41
58
  const reEncodedBufferLen = m._readBufferPool_len(buffer_pool, reEncodedRes.buffer);
42
- res.buffer = Array.from(new Uint8Array(m.HEAPU8.buffer, reEncodedBufferPtr, reEncodedBufferLen));
59
+ out.buffer = Array.from(new Uint8Array(m.HEAPU8.buffer, reEncodedBufferPtr, reEncodedBufferLen));
60
+ }
61
+ return out;
62
+ } finally {
63
+ disposeClassHandle(bufferHandle);
64
+ disposeClassHandle(res);
43
65
  }
44
- return res;
45
66
  }
46
67
  function encode_napi(options) {
47
68
  const lepus = require(`./build/${process.platform}/Release/lepus.node`);
@@ -52,8 +73,8 @@ function encode_napi(options) {
52
73
  return res
53
74
  }
54
75
 
55
- function lepusCheck(sourceFile, targetSdkVersion, execute = 0) {
56
- const m = loadModule();
76
+ async function lepusCheck(sourceFile, targetSdkVersion, execute = 0) {
77
+ const m = await createModule();
57
78
  const _encode = m.cwrap('lepusCheck', 'string', ['string', 'string', 'number']);
58
79
  const res = _encode(sourceFile, targetSdkVersion, execute);
59
80
  return JSON.parse(res);
@@ -79,14 +100,20 @@ function decrypt(cipher) {
79
100
  return res
80
101
  }
81
102
 
82
- function encrypt_wasm(plain) {
83
- const m = loadModule();
103
+ async function encrypt_wasm(plain) {
104
+ const m = await createModule();
105
+ if (typeof m._encrypt !== 'function') {
106
+ throw new Error('encrypt_wasm is not supported by the current wasm build');
107
+ }
84
108
  const res = m._encrypt(plain)
85
109
  return res;
86
110
  }
87
111
 
88
- function decrypt_wasm(plain) {
89
- const m = loadModule();
112
+ async function decrypt_wasm(plain) {
113
+ const m = await createModule();
114
+ if (typeof m._decrypt !== 'function') {
115
+ throw new Error('decrypt_wasm is not supported by the current wasm build');
116
+ }
90
117
  const res = m._decrypt(plain)
91
118
  return res;
92
119
  }
@@ -102,7 +129,7 @@ function decode_napi(templateJS) {
102
129
  }
103
130
 
104
131
  async function decode_wasm(buffer) {
105
- const Module = await loadModule();
132
+ const Module = await createModule();
106
133
  // console.log(templateJs);
107
134
  // const uint8array = new Uint8Array(templateJs.size());
108
135
  // const res = m._decode(uint8array);
@@ -116,10 +143,14 @@ async function decode_wasm(buffer) {
116
143
  const res = Module._decode(byteArrayPtr, byteArrayLength);
117
144
  // Free the allocated memory
118
145
  Module._free(byteArrayPtr);
119
- if (res.status !== 0) {
120
- throw new Error(`decode error: ${res.result}`);
146
+ try {
147
+ if (res.status !== 0) {
148
+ throw new Error(`decode error: ${res.result}`);
149
+ }
150
+ return JSON.parse(res.result);
151
+ } finally {
152
+ disposeClassHandle(res);
121
153
  }
122
- return JSON.parse(res.result);
123
154
  }
124
155
 
125
156
  let encode = encode_napi;
@@ -129,7 +160,6 @@ module.exports = {
129
160
  encode_napi,
130
161
  encode_wasm,
131
162
  lepusCheck,
132
- loadModule,
133
163
  getEncodeMode,
134
164
  encrypt,
135
165
  decrypt,
package/lepus.d.ts CHANGED
@@ -31,6 +31,7 @@ declare namespace RuntimeExports {
31
31
  const HEAPU8: any;
32
32
  }
33
33
  interface WasmModule {
34
+ __ZNK4lynx5lepus2ir5Block4DumpERNSt3__213basic_ostreamIcNS3_11char_traitsIcEEEE(_0: number, _1: number): void;
34
35
  _quickjsCheck(_0: number): void;
35
36
  _createBufferPool(): number;
36
37
  _dropBufferPool(_0: number): void;
@@ -66,12 +67,14 @@ export type EncodeResult = {
66
67
  buffer: VectorUInt8,
67
68
  lepus_code: EmbindString,
68
69
  lepus_debug: EmbindString,
69
- section_size: EmbindString
70
+ section_size: EmbindString,
71
+ css_diagnostics: EmbindString
70
72
  };
71
73
 
72
74
  export type DecodeResult = {
73
75
  status: number,
74
- result: EmbindString
76
+ result: EmbindString,
77
+ error_msg: EmbindString
75
78
  };
76
79
 
77
80
  interface EmbindModule {