quickjs-emscripten-sync 1.0.0 → 1.3.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 (75) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +303 -0
  3. package/dist/index.d.ts +129 -29
  4. package/dist/quickjs-emscripten-sync.es.js +951 -0
  5. package/dist/quickjs-emscripten-sync.umd.js +64 -0
  6. package/package.json +28 -40
  7. package/src/default.ts +6 -2
  8. package/src/index.test.ts +266 -51
  9. package/src/index.ts +123 -66
  10. package/src/marshal/function.test.ts +11 -10
  11. package/src/marshal/function.ts +3 -3
  12. package/src/marshal/index.test.ts +75 -10
  13. package/src/marshal/index.ts +28 -14
  14. package/src/marshal/json.test.ts +94 -0
  15. package/src/marshal/json.ts +15 -0
  16. package/src/marshal/object.test.ts +49 -6
  17. package/src/marshal/object.ts +2 -2
  18. package/src/marshal/primitive.test.ts +2 -0
  19. package/src/marshal/primitive.ts +8 -2
  20. package/src/marshal/promise.test.ts +86 -0
  21. package/src/marshal/promise.ts +26 -0
  22. package/src/marshal/properties.test.ts +7 -5
  23. package/src/marshal/properties.ts +2 -2
  24. package/src/marshal/symbol.test.ts +3 -1
  25. package/src/unmarshal/function.test.ts +28 -21
  26. package/src/unmarshal/function.ts +31 -23
  27. package/src/unmarshal/index.test.ts +86 -86
  28. package/src/unmarshal/index.ts +5 -4
  29. package/src/unmarshal/object.test.ts +36 -8
  30. package/src/unmarshal/object.ts +8 -4
  31. package/src/unmarshal/primitive.test.ts +2 -0
  32. package/src/unmarshal/primitive.ts +1 -2
  33. package/src/unmarshal/promise.test.ts +44 -0
  34. package/src/unmarshal/promise.ts +40 -0
  35. package/src/unmarshal/properties.test.ts +4 -2
  36. package/src/unmarshal/properties.ts +11 -9
  37. package/src/unmarshal/symbol.test.ts +3 -1
  38. package/src/util.test.ts +22 -5
  39. package/src/util.ts +15 -0
  40. package/src/vmmap.test.ts +22 -0
  41. package/src/vmmap.ts +6 -2
  42. package/src/vmutil.test.ts +116 -8
  43. package/src/vmutil.ts +76 -8
  44. package/src/wrapper.test.ts +44 -31
  45. package/src/wrapper.ts +47 -30
  46. package/dist/default.d.ts +0 -4
  47. package/dist/index.js +0 -8
  48. package/dist/marshal/array.d.ts +0 -2
  49. package/dist/marshal/function.d.ts +0 -2
  50. package/dist/marshal/index.d.ts +0 -11
  51. package/dist/marshal/object.d.ts +0 -2
  52. package/dist/marshal/primitive.d.ts +0 -2
  53. package/dist/marshal/properties.d.ts +0 -2
  54. package/dist/marshal/symbol.d.ts +0 -2
  55. package/dist/quickjs-emscripten-sync.cjs.development.js +0 -1302
  56. package/dist/quickjs-emscripten-sync.cjs.development.js.map +0 -1
  57. package/dist/quickjs-emscripten-sync.cjs.production.min.js +0 -2
  58. package/dist/quickjs-emscripten-sync.cjs.production.min.js.map +0 -1
  59. package/dist/quickjs-emscripten-sync.esm.js +0 -1285
  60. package/dist/quickjs-emscripten-sync.esm.js.map +0 -1
  61. package/dist/unmarshal/array.d.ts +0 -2
  62. package/dist/unmarshal/function.d.ts +0 -2
  63. package/dist/unmarshal/index.d.ts +0 -9
  64. package/dist/unmarshal/object.d.ts +0 -2
  65. package/dist/unmarshal/primitive.d.ts +0 -2
  66. package/dist/unmarshal/properties.d.ts +0 -2
  67. package/dist/unmarshal/symbol.d.ts +0 -2
  68. package/dist/util.d.ts +0 -7
  69. package/dist/vmmap.d.ts +0 -35
  70. package/dist/vmutil.d.ts +0 -7
  71. package/dist/wrapper.d.ts +0 -11
  72. package/src/marshal/array.test.ts +0 -40
  73. package/src/marshal/array.ts +0 -24
  74. package/src/unmarshal/array.test.ts +0 -45
  75. package/src/unmarshal/array.ts +0 -32
@@ -0,0 +1,64 @@
1
+ (function(h,w){typeof exports=="object"&&typeof module!="undefined"?w(exports):typeof define=="function"&&define.amd?define(["exports"],w):(h=typeof globalThis!="undefined"?globalThis:h||self,w(h.QuickjsEmscriptenSync={}))})(this,function(h){"use strict";class w{constructor(e){this._map1=new Map,this._map2=new Map,this._map3=new Map,this._map4=new Map,this._counterMap=new Map,this._disposables=new Set,this._counter=0,this.vm=e;const r=e.unwrapResult(e.evalCode(`() => {
2
+ const mapSym = new Map();
3
+ let map = new WeakMap();
4
+ let map2 = new WeakMap();
5
+ const isObj = o => typeof o === "object" && o !== null || typeof o === "function";
6
+ return {
7
+ get: key => mapSym.get(key) ?? map.get(key) ?? map2.get(key) ?? -1,
8
+ set: (key, value, key2) => {
9
+ if (typeof key === "symbol") mapSym.set(key, value);
10
+ if (isObj(key)) map.set(key, value);
11
+ if (isObj(key2)) map2.set(key2, value);
12
+ },
13
+ delete: (key, key2) => {
14
+ mapSym.delete(key);
15
+ map.delete(key);
16
+ map2.delete(key2);
17
+ },
18
+ clear: () => {
19
+ mapSym.clear();
20
+ map = new WeakMap();
21
+ map2 = new WeakMap();
22
+ }
23
+ };
24
+ }`)).consume(i=>this._call(i,void 0));this._mapGet=e.getProp(r,"get"),this._mapSet=e.getProp(r,"set"),this._mapDelete=e.getProp(r,"delete"),this._mapClear=e.getProp(r,"clear"),r.dispose(),this._disposables.add(this._mapGet),this._disposables.add(this._mapSet),this._disposables.add(this._mapDelete),this._disposables.add(this._mapClear)}set(e,r,i,n){var u;if(!r.alive||n&&!n.alive)return!1;const s=(u=this.get(e))!=null?u:this.get(i);if(s)return s===r||s===n;const o=this._counter++;return this._map1.set(e,o),this._map3.set(o,r),this._counterMap.set(o,e),i&&(this._map2.set(i,o),n&&this._map4.set(o,n)),this.vm.newNumber(o).consume(a=>{this._call(this._mapSet,void 0,r,a,n!=null?n:this.vm.undefined)}),!0}merge(e){if(!!e)for(const r of e)!r||r[1]&&this.set(r[0],r[1],r[2],r[3])}get(e){var n;const r=(n=this._map1.get(e))!=null?n:this._map2.get(e),i=typeof r=="number"?this._map3.get(r):void 0;if(!!i){if(!i.alive){this.delete(e);return}return i}}getByHandle(e){if(!!e.alive)return this._counterMap.get(this.vm.getNumber(this._call(this._mapGet,void 0,e)))}has(e){return!!this.get(e)}hasHandle(e){return typeof this.getByHandle(e)!="undefined"}keys(){return this._map1.keys()}delete(e,r){var o;const i=(o=this._map1.get(e))!=null?o:this._map2.get(e);if(typeof i=="undefined")return;const n=this._map3.get(i),s=this._map4.get(i);this._call(this._mapDelete,void 0,...[n,s].filter(u=>!!(u!=null&&u.alive))),this._map1.delete(e),this._map2.delete(e),this._map3.delete(i),this._map4.delete(i);for(const[u,a]of this._map1)if(a===i){this._map1.delete(u);break}for(const[u,a]of this._map2)if(a===i){this._map2.delete(u);break}for(const[u,a]of this._counterMap)if(a===e){this._counterMap.delete(u);break}r&&(n!=null&&n.alive&&n.dispose(),s!=null&&s.alive&&s.dispose())}deleteByHandle(e,r){const i=this.getByHandle(e);typeof i!="undefined"&&this.delete(i,r)}clear(){this._counter=0,this._map1.clear(),this._map2.clear(),this._map3.clear(),this._map4.clear(),this._counterMap.clear(),this._mapClear.alive&&this._call(this._mapClear,void 0)}dispose(){for(const e of this._disposables.values())e.alive&&e.dispose();for(const e of this._map3.values())e.alive&&e.dispose();for(const e of this._map4.values())e.alive&&e.dispose();this._disposables.clear(),this.clear()}get size(){return this._map1.size}[Symbol.iterator](){const e=this._map1.keys();return{next:()=>{for(;;){const r=e.next();if(r.done)return{value:void 0,done:!0};const i=this._map1.get(r.value);if(typeof i=="undefined")continue;const n=this._map3.get(i),s=this._map4.get(i);if(!n)continue;const o=this._get2(i);return{value:[r.value,n,o,s],done:!1}}}}}_get2(e){for(const[r,i]of this._map2)if(i===e)return r}_call(e,r,...i){return this.vm.unwrapResult(this.vm.callFunction(e,typeof r=="undefined"?this.vm.undefined:r,...i))}}function E(t){return typeof t=="function"&&/^class\s/.test(Function.prototype.toString.call(t))}function m(t){return typeof t=="function"||typeof t=="object"&&t!==null}function v(t,e){const r=new Set,i=n=>{if(!(!m(n)||r.has(n)||(e==null?void 0:e(n,r))===!1)){if(r.add(n),Array.isArray(n)){for(const s of n)i(s);return}if(typeof n=="object"){const s=Object.getPrototypeOf(n);s&&s!==Object.prototype&&i(s)}for(const s of Object.values(Object.getOwnPropertyDescriptors(n)))"value"in s&&i(s.value),"get"in s&&i(s.get),"set"in s&&i(s.set)}};return i(t),r}function I(t,e){return v(t,e?(r,i)=>i.size<e:void 0).size}function U(){let t=()=>{},e=()=>{};return{promise:new Promise((i,n)=>{t=i,e=n}),resolve:t,reject:e}}function J(t,e){const r=t.unwrapResult(t.evalCode(e)),i=(n,...s)=>t.unwrapResult(t.callFunction(r,n!=null?n:t.undefined,...s));return i.dispose=()=>r.dispose(),i.alive=!0,Object.defineProperty(i,"alive",{get:()=>r.alive}),i}function y(t,e,r,...i){const n=J(t,e);try{return n(r,...i)}finally{n.dispose()}}function R(t,e,r){return t.dump(y(t,"Object.is",void 0,e,r))}function W(t,e,r){return t.dump(y(t,"(a, b) => a instanceof b",void 0,e,r))}function M(t,e){return t.dump(y(t,'a => typeof a === "object" && a !== null || typeof a === "function"',void 0,e))}function k(t,e){const r=JSON.stringify(e);return r?y(t,"JSON.parse",void 0,t.newString(r)):t.undefined}function H(t,e){try{return e(t)}finally{for(const r of t)r.alive&&r.dispose()}}function z([t,e],r){try{return r(t)}finally{e&&t.dispose()}}function O(t,e){try{return e(...t.map(r=>r[0]))}finally{for(const[r,i]of t)i&&r.dispose()}}function G(t){return"handle"in t}function q(t){return G(t)?t.handle:t}function F(t,e,r,i){const n=t.newObject(),s=(u,a)=>{const p=i(u),f=typeof a.value=="undefined"?void 0:i(a.value),c=typeof a.get=="undefined"?void 0:i(a.get),d=typeof a.set=="undefined"?void 0:i(a.set);t.newObject().consume(_=>{Object.entries(a).forEach(([l,b])=>{const g=l==="value"?f:l==="get"?c:l==="set"?d:b?t.true:t.false;g&&t.setProp(_,l,g)}),t.setProp(n,p,_)})},o=Object.getOwnPropertyDescriptors(e);Object.entries(o).forEach(([u,a])=>s(u,a)),Object.getOwnPropertySymbols(o).forEach(u=>s(u,o[u])),y(t,"Object.defineProperties",void 0,r,n).dispose(),n.dispose()}function Q(t,e,r,i,n,s){var a;if(typeof e!="function")return;const o=t.newFunction(e.name,function(...p){const f=i(this),c=p.map(d=>i(d));if(E(e)&&m(f)){const d=new e(...c);return Object.entries(d).forEach(([_,l])=>{t.setProp(this,_,r(l))}),this}return r(s?s(e,f,c):e.apply(f,c))}).consume(p=>y(t,`Cls => {
25
+ const fn = function(...args) { return Cls.apply(this, args); };
26
+ fn.name = Cls.name;
27
+ fn.length = Cls.length;
28
+ return fn;
29
+ }`,void 0,p)),u=(a=n(e,o))!=null?a:o;return F(t,e,o,r),u}function V(t,e,r,i){var a;if(typeof e!="object"||e===null)return;const n=Array.isArray(e)?t.newArray():t.newObject(),s=(a=i(e,n))!=null?a:n,o=Object.getPrototypeOf(e),u=o&&o!==Object.prototype&&o!==Array.prototype?r(o):void 0;return u&&y(t,"Object.setPrototypeOf",void 0,s,u).dispose(),F(t,e,n,r),s}function $(t,e){switch(typeof e){case"undefined":return t.undefined;case"number":return t.newNumber(e);case"string":return t.newString(e);case"boolean":return e?t.true:t.false;case"object":return e===null?t.null:void 0}}function L(t,e,r){var n;if(typeof e!="symbol")return;const i=y(t,"d => Symbol(d)",void 0,e.description?t.newString(e.description):t.undefined);return(n=r(e,i))!=null?n:i}function X(t,e,r){var s;const i=k(t,e);return(s=r(e,i))!=null?s:i}function Y(t,e,r,i){var s;if(!(e instanceof Promise))return;const n=t.newPromise();return e.then(o=>n.resolve(r(o)),o=>n.reject(r(o))),(s=i(e,n))!=null?s:n.handle}function S(t,e){var f,c,d,_;const{vm:r,unmarshal:i,isMarshalable:n,find:s,pre:o}=e;{const l=$(r,t);if(l)return l}{const l=s(t);if(l)return l}const u=n==null?void 0:n(t);if(u===!1)return r.undefined;const a=(l,b)=>o(l,b,u);if(u==="json")return X(r,t,a);const p=l=>S(l,e);return(_=(d=(c=(f=L(r,t,a))!=null?f:Y(r,t,p,a))!=null?c:Q(r,t,p,i,a,e.preApply))!=null?d:V(r,t,p,a))!=null?_:r.undefined}function A(t,e,r,i){t.newFunction("",(n,s)=>{const[o]=i(n);if(typeof o!="string"&&typeof o!="number"&&typeof o!="symbol")return;const u=[["value",!0],["get",!0],["set",!0],["configurable",!1],["enumerable",!1],["writable",!1]].reduce((a,[p,f])=>{const c=t.getProp(s,p),d=t.typeof(c);if(d==="undefined")return a;if(!f&&d==="boolean")return a[p]=t.dump(t.getProp(s,p)),a;const[_,l]=i(c);return l&&c.dispose(),a[p]=_,a},{});Object.defineProperty(r,o,u)}).consume(n=>{y(t,`(o, fn) => {
30
+ const descs = Object.getOwnPropertyDescriptors(o);
31
+ Object.entries(descs).forEach(([k, v]) => fn(k, v));
32
+ Object.getOwnPropertySymbols(descs).forEach(k => fn(k, descs[k]));
33
+ }`,void 0,e,n).dispose()})}function Z(t,e,r,i,n){var u;if(t.typeof(e)!=="function")return;const s=function(...a){return O([r(this),...a.map(p=>r(p))],(p,...f)=>{if(new.target){const[l]=i(y(t,"(Cls, ...args) => new Cls(...args)",p,e,...f));return Object.defineProperties(this,Object.getOwnPropertyDescriptors(l)),this}const c=t.unwrapResult(t.callFunction(e,p,...f)),[d,_]=i(c);return _&&c.dispose(),d})},o=(u=n(s,e))!=null?u:s;return A(t,e,s,i),o}function x(t,e,r,i){var u;if(t.typeof(e)!=="object"||t.unwrapResult(t.evalCode("o => o === null")).consume(a=>t.dump(t.unwrapResult(t.callFunction(a,t.undefined,e)))))return;const n=y(t,"Array.isArray",void 0,e).consume(a=>t.dump(a))?[]:{},s=(u=i(n,e))!=null?u:n,o=y(t,`o => {
34
+ const p = Object.getPrototypeOf(o);
35
+ return !p || p === Object.prototype || p === Array.prototype ? undefined : p;
36
+ }`,void 0,e).consume(a=>{if(t.typeof(a)==="undefined")return;const[p]=r(a);return p});return typeof o=="object"&&Object.setPrototypeOf(s,o),A(t,e,n,r),s}function K(t,e){const r=t.typeof(e);return r==="undefined"||r==="number"||r==="string"||r==="boolean"?[t.dump(e),!0]:r==="object"&&t.unwrapResult(t.evalCode("a => a === null")).consume(n=>t.dump(t.unwrapResult(t.callFunction(n,t.undefined,e))))?[null,!0]:[void 0,!1]}function ee(t,e,r,i){var p;if(!te(e))return;const n=U(),[s,o]=r(n.resolve),[u,a]=r(n.reject);return y(t,"(p, res, rej) => { p.then(res, rej); }",void 0,e,s,u),o&&s.dispose(),a&&u.dispose(),(p=i(n.promise,e))!=null?p:n.promise}function te(t){return t.owner?t.owner.unwrapResult(t.owner.evalCode("Promise")).consume(e=>t.owner?W(t.owner,t,e):!1):!1}function re(t,e,r){var s;if(t.typeof(e)!=="symbol")return;const i=t.getString(t.getProp(e,"description")),n=Symbol(i);return(s=r(n,e))!=null?s:n}function C(t,e){const[r]=D(t,e);return r}function D(t,e){var a,p,f;const{vm:r,marshal:i,find:n,pre:s}=e;{const[c,d]=K(r,t);if(d)return[c,!1]}{const c=n(t);if(c)return[c,!0]}const o=c=>D(c,e);return[(f=(p=(a=re(r,t,s))!=null?a:ee(r,t,i,s))!=null?p:Z(r,t,i,o,s))!=null?f:x(r,t,o,s),!1]}function ne(t,e,r,i,n,s){if(!m(e))return;if(ie(e,r))return e;const o=new Proxy(e,{get(u,a){return a===r?u:Reflect.get(u,a)},set(u,a,p,f){var _;const c=j(p,r),d=(_=s==null?void 0:s(f))!=null?_:"host";return d!=="vm"&&!Reflect.set(u,a,c,f)||d==="host"||!t.alive||O([n(f),n(a),n(c)],(l,b,g)=>{const[T,ue]=P(t,l,i);ue?T.consume(ae=>t.setProp(ae,b,g)):t.setProp(T,b,g)}),!0},deleteProperty(u,a){var f;const p=(f=s==null?void 0:s(o))!=null?f:"host";return O([n(o),n(a)],(c,d)=>{const[_,l]=P(t,c,i);if(p==="vm"||Reflect.deleteProperty(u,a)){if(p==="host"||!t.alive)return!0;l?_.consume(b=>y(t,"(a, b) => delete a[b]",void 0,b,d)):y(t,"(a, b) => delete a[b]",void 0,_,d)}return!0})}});return o}function se(t,e,r,i,n,s){return M(t,e)?B(t,e,i)?[e,!1]:H([t.newFunction("getSyncMode",o=>{const u=s==null?void 0:s(n(o));return typeof u=="string"?t.newString(u):t.undefined}),t.newFunction("setter",(o,u,a)=>{const p=n(o);if(!p)return;const f=n(u);if(f==="__proto__")return;const c=n(a);j(p,r)[f]=c}),t.newFunction("deleter",(o,u)=>{const a=n(o);if(!a)return;const p=n(u);delete j(a,r)[p]})],o=>[y(t,`(target, sym, getSyncMode, setter, deleter) => {
37
+ const rec = new Proxy(target, {
38
+ get(obj, key, receiver) {
39
+ return key === sym ? obj : Reflect.get(obj, key, receiver)
40
+ },
41
+ set(obj, key, value, receiver) {
42
+ const v = typeof value === "object" && value !== null || typeof value === "function"
43
+ ? value[sym] ?? value
44
+ : value;
45
+ const sync = getSyncMode(receiver) ?? "vm";
46
+ if (sync === "host" || Reflect.set(obj, key, v, receiver)) {
47
+ if (sync !== "vm") {
48
+ setter(receiver, key, v);
49
+ }
50
+ }
51
+ return true;
52
+ },
53
+ deleteProperty(obj, key) {
54
+ const sync = getSyncMode(rec) ?? "vm";
55
+ if (sync === "host" || Reflect.deleteProperty(obj, key)) {
56
+ if (sync !== "vm") {
57
+ deleter(rec, key);
58
+ }
59
+ }
60
+ return true;
61
+ },
62
+ });
63
+ return rec;
64
+ }`,void 0,e,i,...o),!0]):[void 0,!1]}function j(t,e){var r;return m(t)&&(r=t[e])!=null?r:t}function P(t,e,r){return B(t,e,r)?[t.getProp(e,r),!0]:[e,!1]}function ie(t,e){return m(t)&&!!t[e]}function B(t,e,r){return!!t.dump(y(t,'(a, s) => (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]',void 0,e,r))}const N=[[Symbol,"Symbol"],[Symbol.prototype,"Symbol.prototype"],[Object,"Object"],[Object.prototype,"Object.prototype"],[Function,"Function"],[Function.prototype,"Function.prototype"],[Boolean,"Boolean"],[Boolean.prototype,"Boolean.prototype"],[Array,"Array"],[Array.prototype,"Array.prototype"],[Error,"Error"],[Error.prototype,"Error.prototype"],[EvalError,"EvalError"],[EvalError.prototype,"EvalError.prototype"],[RangeError,"RangeError"],[RangeError.prototype,"RangeError.prototype"],[ReferenceError,"ReferenceError"],[ReferenceError.prototype,"ReferenceError.prototype"],[SyntaxError,"SyntaxError"],[SyntaxError.prototype,"SyntaxError.prototype"],[TypeError,"TypeError"],[TypeError.prototype,"TypeError.prototype"],[URIError,"URIError"],[URIError.prototype,"URIError.prototype"],...Object.getOwnPropertyNames(Symbol).filter(t=>typeof Symbol[t]=="symbol").map(t=>[Symbol[t],`Symbol.${t}`])];class oe{constructor(e,r){var i;this._registeredMapDispose=new Set,this._sync=new Set,this._temporalSync=new Set,this._symbol=Symbol(),this._isMarshalable=n=>{var o,u;const s=(o=this._options)==null?void 0:o.isMarshalable;return(u=typeof s=="function"?s(this._unwrap(n)):s)!=null?u:"json"},this._marshalFind=n=>{var u,a,p;const s=this._unwrap(n);return(p=(a=(u=this._registeredMap.get(n))!=null?u:s!==n?this._registeredMap.get(s):void 0)!=null?a:this._map.get(n))!=null?p:s!==n?this._map.get(s):void 0},this._marshalPre=(n,s,o)=>{var u;if(o!=="json")return(u=this._register(n,q(s),this._map))==null?void 0:u[1]},this._marshalPreApply=(n,s,o)=>{const u=m(s)?this._unwrap(s):void 0;u&&this._temporalSync.add(u);try{return n.apply(s,o)}finally{u&&this._temporalSync.delete(u)}},this._marshal=n=>{var u;const s=this._registeredMap.get(n);if(s)return[s,!1];const o=S((u=this._wrap(n))!=null?u:n,{vm:this.vm,unmarshal:this._unmarshal,isMarshalable:this._isMarshalable,find:this._marshalFind,pre:this._marshalPre,preApply:this._marshalPreApply});return[o,!this._map.hasHandle(o)]},this._preUnmarshal=(n,s)=>{var o;return(o=this._register(n,s,void 0,!0))==null?void 0:o[0]},this._unmarshalFind=n=>{var s;return(s=this._registeredMap.getByHandle(n))!=null?s:this._map.getByHandle(n)},this._unmarshal=n=>{const s=this._registeredMap.getByHandle(n);if(typeof s!="undefined")return s;const[o]=this._wrapHandle(n);return C(o!=null?o:n,{vm:this.vm,marshal:this._marshal,find:this._unmarshalFind,pre:this._preUnmarshal})},this._syncMode=n=>{const s=this._unwrap(n);return this._sync.has(s)||this._temporalSync.has(s)?"both":void 0},this._unwrapIfNotSynced=n=>{const s=this._unwrap(n);return this._sync.has(s)?n:s},this.vm=e,this._options=r,this._symbolHandle=e.unwrapResult(e.evalCode("Symbol()")),this._map=new w(e),this._registeredMap=new w(e),this.registerAll((i=r==null?void 0:r.registeredObjects)!=null?i:N)}dispose(){this._map.dispose(),this._registeredMap.dispose(),this._symbolHandle.dispose()}evalCode(e){const r=this.vm.evalCode(e);return this._unwrapResultAndUnmarshal(r)}executePendingJobs(e){const r=this.vm.executePendingJobs(e);if("value"in r)return r.value;throw this._unwrapIfNotSynced(r.error.consume(this._unmarshal))}expose(e){for(const[r,i]of Object.entries(e))z(this._marshal(i),n=>{this.vm.setProp(this.vm.global,r,n)})}sync(e){const r=this._wrap(e);return typeof r=="undefined"?e:(v(r,i=>{this._sync.add(this._unwrap(i))}),r)}register(e,r){if(this._registeredMap.has(e))return;const i=typeof r=="string"?this._unwrapResult(this.vm.evalCode(r)):r;R(this.vm,i,this.vm.undefined)||(typeof r=="string"&&this._registeredMapDispose.add(e),this._registeredMap.set(e,i))}registerAll(e){for(const[r,i]of e)this.register(r,i)}unregister(e,r){this._registeredMap.delete(e,this._registeredMapDispose.has(e)||r),this._registeredMapDispose.delete(e)}unregisterAll(e,r){for(const i of e)this.unregister(i,r)}startSync(e){!m(e)||this._sync.add(this._unwrap(e))}endSync(e){this._sync.delete(this._unwrap(e))}_unwrapResult(e){if("value"in e)return e.value;throw this._unwrapIfNotSynced(e.error.consume(this._unmarshal))}_unwrapResultAndUnmarshal(e){if(!!e)return this._unwrapIfNotSynced(this._unwrapResult(e).consume(this._unmarshal))}_register(e,r,i=this._map,n){if(this._registeredMap.has(e)||this._registeredMap.hasHandle(r))return;const s=this._wrap(e),[o]=this._wrapHandle(r);if(!s||!o)return;const u=this._unwrap(e),[a,p]=this._unwrapHandle(r);if(i.set(s,o,u,a))n&&this._sync.add(u);else throw p&&a.dispose(),new Error("already registered");return[s,o]}_wrap(e){return ne(this.vm,e,this._symbol,this._symbolHandle,this._marshal,this._syncMode)}_unwrap(e){return j(e,this._symbol)}_wrapHandle(e){return se(this.vm,e,this._symbol,this._symbolHandle,this._unmarshal,this._syncMode)}_unwrapHandle(e){return P(this.vm,e,this._symbolHandle)}}h.Arena=oe,h.VMMap=w,h.call=y,h.complexity=I,h.consumeAll=H,h.defaultRegisteredObjects=N,h.eq=R,h.isES2015Class=E,h.isHandleObject=M,h.isObject=m,h.json=k,h.marshal=S,h.unmarshal=C,h.walkObject=v,Object.defineProperties(h,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json CHANGED
@@ -1,10 +1,20 @@
1
1
  {
2
2
  "name": "quickjs-emscripten-sync",
3
3
  "author": "rot1024",
4
- "version": "1.0.0",
4
+ "version": "1.3.0",
5
5
  "license": "MIT",
6
- "main": "dist/index.js",
7
- "typings": "dist/index.d.ts",
6
+ "source": "./src/index.ts",
7
+ "main": "./dist/quickjs-emscripten-sync.umd.js",
8
+ "module": "./dist/quickjs-emscripten-sync.es.js",
9
+ "unpkg": "./dist/quickjs-emscripten-sync.umd.js",
10
+ "types": "./dist/index.d.ts",
11
+ "sideEffects": false,
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/quickjs-emscripten-sync.es.js",
15
+ "require": "./dist/quickjs-emscripten-sync.umd.js"
16
+ }
17
+ },
8
18
  "files": [
9
19
  "dist",
10
20
  "src"
@@ -13,53 +23,31 @@
13
23
  "node": ">=10"
14
24
  },
15
25
  "scripts": {
16
- "start": "tsdx watch",
17
- "build": "tsdx build",
18
- "test": "tsdx test",
19
- "lint": "tsdx lint",
20
- "prepare": "tsdx build",
21
- "size": "size-limit",
22
- "analyze": "size-limit --why"
26
+ "build": "tsc && vite build",
27
+ "test": "vitest",
28
+ "lint": "eslint ."
23
29
  },
24
30
  "peerDependencies": {
25
31
  "quickjs-emscripten": "*"
26
32
  },
27
- "husky": {
28
- "hooks": {
29
- "pre-commit": "tsdx lint"
30
- }
31
- },
32
33
  "prettier": {
33
34
  "printWidth": 80,
34
35
  "semi": true,
35
36
  "singleQuote": false,
36
37
  "trailingComma": "es5"
37
38
  },
38
- "module": "dist/quickjs-emscripten-sync.esm.js",
39
- "size-limit": [
40
- {
41
- "path": "dist/quickjs-emscripten-sync.cjs.production.min.js",
42
- "limit": "10 KB"
43
- },
44
- {
45
- "path": "dist/quickjs-emscripten-sync.esm.js",
46
- "limit": "10 KB"
47
- }
48
- ],
49
39
  "devDependencies": {
50
- "@size-limit/preset-small-lib": "^4.10.2",
51
- "@typescript-eslint/eslint-plugin": "^4.22.0",
52
- "@typescript-eslint/parser": "^4.22.0",
53
- "husky": "^6.0.0",
54
- "quickjs-emscripten": "^0.11.0",
55
- "size-limit": "^4.10.2",
56
- "tsdx": "^0.14.1",
57
- "tslib": "^2.2.0",
58
- "typescript": "^4.2.4"
59
- },
60
- "resolutions": {
61
- "**/@typescript-eslint/eslint-plugin": "^4.20.0",
62
- "**/@typescript-eslint/parser": "^4.20.0",
63
- "**/typescript": "^4.2.3"
40
+ "@typescript-eslint/eslint-plugin": "^5.30.6",
41
+ "@typescript-eslint/parser": "^5.30.6",
42
+ "c8": "^7.11.3",
43
+ "eslint": "^8.19.0",
44
+ "eslint-config-prettier": "^8.5.0",
45
+ "eslint-plugin-prettier": "^4.2.1",
46
+ "prettier": "^2.7.1",
47
+ "quickjs-emscripten": "^0.15.0",
48
+ "typescript": "^4.7.4",
49
+ "vite": "^2.9.14",
50
+ "vite-plugin-dts": "^1.2.1",
51
+ "vitest": "^0.18.0"
64
52
  }
65
53
  }
package/src/default.ts CHANGED
@@ -11,6 +11,10 @@ export const defaultRegisteredObjects: [any, string][] = [
11
11
  [Function.prototype, "Function.prototype"],
12
12
  [Boolean, "Boolean"],
13
13
  [Boolean.prototype, "Boolean.prototype"],
14
+ [Array, "Array"],
15
+ [Array.prototype, "Array.prototype"],
16
+ // [BigInt, "BigInt"],
17
+ // [BigInt.prototype, "BigInt.prototype"],
14
18
  // errors
15
19
  [Error, "Error"],
16
20
  [Error.prototype, "Error.prototype"],
@@ -28,6 +32,6 @@ export const defaultRegisteredObjects: [any, string][] = [
28
32
  [URIError.prototype, "URIError.prototype"],
29
33
  // built-in symbols
30
34
  ...Object.getOwnPropertyNames(Symbol)
31
- .filter(k => typeof (Symbol as any)[k] === "symbol")
32
- .map<[any, string]>(k => [(Symbol as any)[k], `Symbol.${k}`]),
35
+ .filter((k) => typeof (Symbol as any)[k] === "symbol")
36
+ .map<[any, string]>((k) => [(Symbol as any)[k], `Symbol.${k}`]),
33
37
  ];