quickjs-emscripten-sync 1.4.0 → 1.5.0

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.
Files changed (50) hide show
  1. package/README.md +28 -7
  2. package/dist/index.d.ts +12 -7
  3. package/dist/quickjs-emscripten-sync.mjs +842 -0
  4. package/dist/quickjs-emscripten-sync.umd.js +15 -15
  5. package/package.json +9 -18
  6. package/src/default.ts +2 -2
  7. package/src/index.test.ts +21 -13
  8. package/src/index.ts +30 -43
  9. package/src/marshal/custom.test.ts +50 -0
  10. package/src/marshal/custom.ts +36 -0
  11. package/src/marshal/function.test.ts +17 -37
  12. package/src/marshal/function.ts +8 -12
  13. package/src/marshal/index.test.ts +35 -39
  14. package/src/marshal/index.ts +6 -9
  15. package/src/marshal/json.test.ts +9 -15
  16. package/src/marshal/json.ts +1 -4
  17. package/src/marshal/object.test.ts +19 -51
  18. package/src/marshal/object.ts +3 -11
  19. package/src/marshal/primitive.test.ts +4 -13
  20. package/src/marshal/primitive.ts +1 -1
  21. package/src/marshal/promise.test.ts +9 -10
  22. package/src/marshal/promise.ts +4 -11
  23. package/src/marshal/properties.test.ts +7 -14
  24. package/src/marshal/properties.ts +6 -9
  25. package/src/unmarshal/custom.test.ts +50 -0
  26. package/src/unmarshal/custom.ts +31 -0
  27. package/src/unmarshal/function.test.ts +20 -44
  28. package/src/unmarshal/function.ts +7 -17
  29. package/src/unmarshal/index.test.ts +30 -23
  30. package/src/unmarshal/index.ts +4 -6
  31. package/src/unmarshal/object.test.ts +9 -17
  32. package/src/unmarshal/object.ts +7 -12
  33. package/src/unmarshal/primitive.test.ts +1 -4
  34. package/src/unmarshal/primitive.ts +3 -10
  35. package/src/unmarshal/promise.test.ts +3 -3
  36. package/src/unmarshal/promise.ts +3 -10
  37. package/src/unmarshal/properties.test.ts +2 -2
  38. package/src/unmarshal/properties.ts +4 -8
  39. package/src/util.test.ts +1 -7
  40. package/src/util.ts +3 -8
  41. package/src/vmmap.ts +13 -31
  42. package/src/vmutil.test.ts +15 -29
  43. package/src/vmutil.ts +12 -39
  44. package/src/wrapper.test.ts +27 -90
  45. package/src/wrapper.ts +43 -50
  46. package/dist/quickjs-emscripten-sync.es.js +0 -962
  47. package/src/marshal/symbol.test.ts +0 -24
  48. package/src/marshal/symbol.ts +0 -21
  49. package/src/unmarshal/symbol.test.ts +0 -25
  50. package/src/unmarshal/symbol.ts +0 -12
package/README.md CHANGED
@@ -193,19 +193,40 @@ Options accepted:
193
193
 
194
194
  ```ts
195
195
  type Options = {
196
+ /** A callback that returns a boolean value that determines whether an object is marshalled or not. If false, no marshaling will be done and undefined will be passed to the QuickJS VM, otherwise marshaling will be done. By default, all objects will be marshalled. */
196
197
  isMarshalable?: boolean | "json" | ((target: any) => boolean | "json");
198
+ /** Pre-registered pairs of objects that will be considered the same between the host and the QuickJS VM. This will be used automatically during the conversion. By default, it will be registered automatically with `defaultRegisteredObjects`.
199
+ *
200
+ * Instead of a string, you can also pass a QuickJSHandle directly. In that case, however, you have to dispose of them manually when destroying the VM.
201
+ */
197
202
  registeredObjects?: Iterable<[any, QuickJSHandle | string]>;
203
+ /** Register functions to convert an object to a QuickJS handle. */
204
+ customMarshaller?: Iterable<
205
+ (target: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined
206
+ >;
207
+ /** Register functions to convert a QuickJS handle to an object. */
208
+ customUnmarshaller?: Iterable<
209
+ (target: QuickJSHandle, ctx: QuickJSContext) => any
210
+ >;
211
+ /** A callback that returns a boolean value that determines whether an object is wrappable by proxies. If returns false, note that the object cannot be synchronized between the host and the QuickJS even if arena.sync is used. */
212
+ isWrappable?: (target: any) => boolean;
213
+ /** A callback that returns a boolean value that determines whether an QuickJS handle is wrappable by proxies. If returns false, note that the handle cannot be synchronized between the host and the QuickJS even if arena.sync is used. */
214
+ isHandleWrappable?: (handle: QuickJSHandle, ctx: QuickJSContext) => boolean;
215
+ /** Compatibility with quickjs-emscripten prior to v0.15. Inject code for compatibility into context at Arena class initialization time. */
198
216
  compat?: boolean;
199
217
  }
200
218
  ```
201
219
 
202
- - **`isMarshalable`**: Determines how marshalling will be done when sending objects from the host to the context. **Make sure to set the marshalling to be the minimum necessary as it may reduce the security of your application.** [Please read the section on security above.](#security-warning)
203
- - `"json"` (**default**, safety): Target object will be serialized as JSON in host and then parsed in context. Functions and classes will be lost in the process.
204
- - `false` (safety): Target object will not be always marshalled as `undefined`.
205
- - `(target: any) => boolean | "json"` (recoomended): You can control marshalling mode for each objects. If you want to do marshalling, usually use this method. Allow partial marshalling by returning `true` only for some objects.
206
- - `true` (**risky and not recommended**): Target object will be always marshaled. This setting may reduce security.
207
- - **`registeredObjects`**: You can pre-register a pair of objects that will be considered the same between the host and the QuickJS context. This will be used automatically during the conversion. By default, it will be registered automatically with [`defaultRegisteredObjects`](src/default.ts). If you want to add a new pair to this, please do the following:
208
- - **`compat`**: If you want to use quickjs-emscripten v0.15 or older, enable this option to automatically inject code to the VM for compatibility.
220
+ Notes:
221
+
222
+ **`isMarshalable`**: Determines how marshalling will be done when sending objects from the host to the context. **Make sure to set the marshalling to be the minimum necessary as it may reduce the security of your application.** [Please read the section on security above.](#security-warning)
223
+
224
+ - `"json"` (**default**, safety): Target object will be serialized as JSON in host and then parsed in context. Functions and classes will be lost in the process.
225
+ - `false` (safety): Target object will not be always marshalled as `undefined`.
226
+ - `(target: any) => boolean | "json"` (recoomended): You can control marshalling mode for each objects. If you want to do marshalling, usually use this method. Allow partial marshalling by returning `true` only for some objects.
227
+ - `true` (**risky and not recommended**): Target object will be always marshaled. This setting may reduce security.
228
+
229
+ **`registeredObjects`**: You can pre-register a pair of objects that will be considered the same between the host and the QuickJS context. This will be used automatically during the conversion. By default, it will be registered automatically with [`defaultRegisteredObjects`](src/default.ts). If you want to add a new pair to this, please do the following:
209
230
 
210
231
  ```js
211
232
  import { defaultRegisteredObjects } from "quickjs-emscripten-sync";
package/dist/index.d.ts CHANGED
@@ -114,11 +114,19 @@ export declare function marshal(target: unknown, options: Options_2): QuickJSHan
114
114
  export declare type Options = {
115
115
  /** A callback that returns a boolean value that determines whether an object is marshalled or not. If false, no marshaling will be done and undefined will be passed to the QuickJS VM, otherwise marshaling will be done. By default, all objects will be marshalled. */
116
116
  isMarshalable?: boolean | "json" | ((target: any) => boolean | "json");
117
- /** You can pre-register a pair of objects that will be considered the same between the host and the QuickJS VM. This will be used automatically during the conversion. By default, it will be registered automatically with `defaultRegisteredObjects`.
117
+ /** Pre-registered pairs of objects that will be considered the same between the host and the QuickJS VM. This will be used automatically during the conversion. By default, it will be registered automatically with `defaultRegisteredObjects`.
118
118
  *
119
119
  * Instead of a string, you can also pass a QuickJSHandle directly. In that case, however, you have to dispose of them manually when destroying the VM.
120
120
  */
121
121
  registeredObjects?: Iterable<[any, QuickJSHandle | string]>;
122
+ /** Register functions to convert an object to a QuickJS handle. */
123
+ customMarshaller?: Iterable<(target: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined>;
124
+ /** Register functions to convert a QuickJS handle to an object. */
125
+ customUnmarshaller?: Iterable<(target: QuickJSHandle, ctx: QuickJSContext) => any>;
126
+ /** A callback that returns a boolean value that determines whether an object is wrappable by proxies. If returns false, note that the object cannot be synchronized between the host and the QuickJS even if arena.sync is used. */
127
+ isWrappable?: (target: any) => boolean;
128
+ /** A callback that returns a boolean value that determines whether an QuickJS handle is wrappable by proxies. If returns false, note that the handle cannot be synchronized between the host and the QuickJS even if arena.sync is used. */
129
+ isHandleWrappable?: (handle: QuickJSHandle, ctx: QuickJSContext) => boolean;
122
130
  /** Compatibility with quickjs-emscripten prior to v0.15. Inject code for compatibility into context at Arena class initialization time. */
123
131
  compat?: boolean;
124
132
  };
@@ -130,6 +138,7 @@ declare type Options_2 = {
130
138
  find: (target: unknown) => QuickJSHandle | undefined;
131
139
  pre: (target: unknown, handle: QuickJSHandle | QuickJSDeferredPromise, mode: true | "json" | undefined) => QuickJSHandle | undefined;
132
140
  preApply?: (target: Function, thisArg: unknown, args: unknown[]) => any;
141
+ custom?: Iterable<(obj: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined>;
133
142
  };
134
143
 
135
144
  declare type Options_3 = {
@@ -138,6 +147,7 @@ declare type Options_3 = {
138
147
  marshal: (target: unknown) => [QuickJSHandle, boolean];
139
148
  find: (handle: QuickJSHandle) => unknown | undefined;
140
149
  pre: <T = unknown>(target: T, handle: QuickJSHandle) => T | undefined;
150
+ custom?: Iterable<(obj: QuickJSHandle, ctx: QuickJSContext) => any>;
141
151
  };
142
152
 
143
153
  export declare function unmarshal(handle: QuickJSHandle, options: Options_3): any;
@@ -168,12 +178,7 @@ export declare class VMMap {
168
178
  clear(): void;
169
179
  dispose(): void;
170
180
  get size(): number;
171
- [Symbol.iterator](): Iterator<[
172
- any,
173
- QuickJSHandle,
174
- any,
175
- QuickJSHandle | undefined
176
- ]>;
181
+ [Symbol.iterator](): Iterator<[any, QuickJSHandle, any, QuickJSHandle | undefined]>;
177
182
  _get2(num: number): any;
178
183
  _call(fn: QuickJSHandle, thisArg: QuickJSHandle | undefined, ...args: QuickJSHandle[]): QuickJSHandle;
179
184
  }