quickjs-emscripten-sync 1.2.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 (59) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +13 -9
  3. package/dist/index.d.ts +106 -20
  4. package/dist/quickjs-emscripten-sync.es.js +951 -0
  5. package/dist/quickjs-emscripten-sync.umd.js +64 -2
  6. package/package.json +24 -46
  7. package/src/index.test.ts +70 -0
  8. package/src/index.ts +9 -4
  9. package/src/marshal/function.test.ts +8 -7
  10. package/src/marshal/index.test.ts +46 -5
  11. package/src/marshal/index.ts +9 -3
  12. package/src/marshal/json.test.ts +5 -3
  13. package/src/marshal/object.test.ts +9 -7
  14. package/src/marshal/primitive.test.ts +2 -0
  15. package/src/marshal/promise.test.ts +86 -0
  16. package/src/marshal/promise.ts +26 -0
  17. package/src/marshal/properties.test.ts +4 -2
  18. package/src/marshal/symbol.test.ts +3 -1
  19. package/src/unmarshal/function.test.ts +15 -16
  20. package/src/unmarshal/index.test.ts +81 -84
  21. package/src/unmarshal/index.ts +4 -2
  22. package/src/unmarshal/object.test.ts +11 -9
  23. package/src/unmarshal/primitive.test.ts +2 -0
  24. package/src/unmarshal/promise.test.ts +44 -0
  25. package/src/unmarshal/promise.ts +40 -0
  26. package/src/unmarshal/properties.test.ts +3 -1
  27. package/src/unmarshal/symbol.test.ts +3 -1
  28. package/src/util.test.ts +19 -2
  29. package/src/util.ts +15 -0
  30. package/src/vmmap.test.ts +22 -0
  31. package/src/vmmap.ts +4 -0
  32. package/src/vmutil.test.ts +44 -3
  33. package/src/vmutil.ts +47 -7
  34. package/src/wrapper.test.ts +24 -22
  35. package/dist/default.d.ts +0 -4
  36. package/dist/marshal/function.d.ts +0 -2
  37. package/dist/marshal/index.d.ts +0 -11
  38. package/dist/marshal/json.d.ts +0 -2
  39. package/dist/marshal/object.d.ts +0 -2
  40. package/dist/marshal/primitive.d.ts +0 -2
  41. package/dist/marshal/properties.d.ts +0 -2
  42. package/dist/marshal/symbol.d.ts +0 -2
  43. package/dist/quickjs-emscripten-sync.cjs +0 -2
  44. package/dist/quickjs-emscripten-sync.cjs.map +0 -1
  45. package/dist/quickjs-emscripten-sync.modern.js +0 -2
  46. package/dist/quickjs-emscripten-sync.modern.js.map +0 -1
  47. package/dist/quickjs-emscripten-sync.module.js +0 -2
  48. package/dist/quickjs-emscripten-sync.module.js.map +0 -1
  49. package/dist/quickjs-emscripten-sync.umd.js.map +0 -1
  50. package/dist/unmarshal/function.d.ts +0 -2
  51. package/dist/unmarshal/index.d.ts +0 -9
  52. package/dist/unmarshal/object.d.ts +0 -2
  53. package/dist/unmarshal/primitive.d.ts +0 -2
  54. package/dist/unmarshal/properties.d.ts +0 -2
  55. package/dist/unmarshal/symbol.d.ts +0 -2
  56. package/dist/util.d.ts +0 -7
  57. package/dist/vmmap.d.ts +0 -35
  58. package/dist/vmutil.d.ts +0 -11
  59. package/dist/wrapper.d.ts +0 -11
@@ -1,2 +1,64 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).quickjsEmscriptenSync={})}(this,function(e){function t(e,n){return t=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},t(e,n)}function n(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(e){return!1}}function r(e,o,i){return r=n()?Reflect.construct:function(e,n,r){var o=[null];o.push.apply(o,n);var i=new(Function.bind.apply(e,o));return r&&t(i,r.prototype),i},r.apply(null,arguments)}function o(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function i(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return o(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a=function(e){function t(e){var t=this;this.vm=void 0,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._mapGet=void 0,this._mapSet=void 0,this._mapDelete=void 0,this._mapClear=void 0,this._counter=0,this.vm=e;var n=e.unwrapResult(e.evalCode('() => {\n const mapSym = new Map();\n let map = new WeakMap();\n let map2 = new WeakMap();\n const isObj = o => typeof o === "object" && o !== null || typeof o === "function";\n return {\n get: key => mapSym.get(key) ?? map.get(key) ?? map2.get(key) ?? -1,\n set: (key, value, key2) => {\n if (typeof key === "symbol") mapSym.set(key, value);\n if (isObj(key)) map.set(key, value);\n if (isObj(key2)) map2.set(key2, value);\n },\n delete: (key, key2) => {\n mapSym.delete(key);\n map.delete(key);\n map2.delete(key2);\n },\n clear: () => {\n mapSym.clear();\n map = new WeakMap();\n map2 = new WeakMap();\n }\n };\n }')).consume(function(e){return t._call(e,void 0)});this._mapGet=e.getProp(n,"get"),this._mapSet=e.getProp(n,"set"),this._mapDelete=e.getProp(n,"delete"),this._mapClear=e.getProp(n,"clear"),n.dispose(),this._disposables.add(this._mapGet),this._disposables.add(this._mapSet),this._disposables.add(this._mapDelete),this._disposables.add(this._mapClear)}var n,r=t.prototype;return r.set=function(e,t,n,r){var o,i=this;if(!t.alive||r&&!r.alive)return!1;var a=null!=(o=this.get(e))?o:this.get(n);if(a)return a===t||a===r;var s=this._counter++;return this._map1.set(e,s),this._map3.set(s,t),this._counterMap.set(s,e),n&&(this._map2.set(n,s),r&&this._map4.set(s,r)),this.vm.newNumber(s).consume(function(e){i._call(i._mapSet,void 0,t,e,null!=r?r:i.vm.undefined)}),!0},r.merge=function(e){if(e)for(var t,n=i(e);!(t=n()).done;){var r=t.value;r&&r[1]&&this.set(r[0],r[1],r[2],r[3])}},r.get=function(e){var t,n=null!=(t=this._map1.get(e))?t:this._map2.get(e),r="number"==typeof n?this._map3.get(n):void 0;if(r){if(r.alive)return r;this.delete(e)}},r.getByHandle=function(e){if(e.alive)return this._counterMap.get(this.vm.getNumber(this._call(this._mapGet,void 0,e)))},r.has=function(e){return!!this.get(e)},r.hasHandle=function(e){return void 0!==this.getByHandle(e)},r.delete=function(e,t){var n,r=null!=(n=this._map1.get(e))?n:this._map2.get(e);if(void 0!==r){var o=this._map3.get(r),a=this._map4.get(r);this._call.apply(this,[this._mapDelete,void 0].concat([o,a].filter(function(e){return!(null==e||!e.alive)}))),this._map1.delete(e),this._map2.delete(e),this._map3.delete(r),this._map4.delete(r);for(var s,u=i(this._map1);!(s=u()).done;){var l=s.value;if(l[1]===r){this._map1.delete(l[0]);break}}for(var p,c=i(this._map2);!(p=c()).done;){var f=p.value;if(f[1]===r){this._map2.delete(f[0]);break}}for(var d,v=i(this._counterMap);!(d=v()).done;){var y=d.value;if(y[1]===e){this._counterMap.delete(y[0]);break}}t&&(null!=o&&o.alive&&o.dispose(),null!=a&&a.alive&&a.dispose())}},r.deleteByHandle=function(e,t){var n=this.getByHandle(e);void 0!==n&&this.delete(n,t)},r.clear=function(){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)},r.dispose=function(){for(var e,t=i(this._disposables.values());!(e=t()).done;){var n=e.value;n.alive&&n.dispose()}for(var r,o=i(this._map3.values());!(r=o()).done;){var a=r.value;a.alive&&a.dispose()}for(var s,u=i(this._map4.values());!(s=u()).done;){var l=s.value;l.alive&&l.dispose()}this._disposables.clear(),this.clear()},r[e]=function(){var e=this,t=this._map1.keys();return{next:function(){for(;;){var n=t.next();if(n.done)return{value:void 0,done:!0};var r=e._map1.get(n.value);if(void 0!==r){var o=e._map3.get(r),i=e._map4.get(r);if(o){var a=e._get2(r);return{value:[n.value,o,a,i],done:!1}}}}}}},r._get2=function(e){for(var t,n=i(this._map2);!(t=n()).done;){var r=t.value;if(r[1]===e)return r[0]}},r._call=function(e,t){var n;return this.vm.unwrapResult((n=this.vm).callFunction.apply(n,[e,void 0===t?this.vm.undefined:t].concat([].slice.call(arguments,2))))},(n=[{key:"size",get:function(){return this._map1.size}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(t.prototype,n),t}(Symbol.iterator);function s(e){return"function"==typeof e&&/^class\s/.test(Function.prototype.toString.call(e))}function u(e){return"function"==typeof e||"object"==typeof e&&null!==e}function l(e,t){var n=new Set;return function e(r){if(u(r)&&!n.has(r)&&!1!==(null==t?void 0:t(r,n)))if(n.add(r),Array.isArray(r))for(var o,a=i(r);!(o=a()).done;)e(o.value);else{if("object"==typeof r){var s=Object.getPrototypeOf(r);s&&s!==Object.prototype&&e(s)}for(var l=0,p=Object.values(Object.getOwnPropertyDescriptors(r));l<p.length;l++){var c=p[l];"value"in c&&e(c.value),"get"in c&&e(c.get),"set"in c&&e(c.set)}}}(e),n}function p(e,t,n){var r=[].slice.call(arguments,3);return e.unwrapResult(e.evalCode(t)).consume(function(t){return void 0===n&&0===r.length?t:e.unwrapResult(e.callFunction.apply(e,[t,null!=n?n:e.undefined].concat(r)))})}function c(e,t,n){return e.dump(p(e,"Object.is",void 0,t,n))}function f(e,t){return e.dump(p(e,'a => typeof a === "object" && a !== null || typeof a === "function"',void 0,t))}function d(e,t){var n=JSON.stringify(t);return n?p(e,"JSON.parse",void 0,e.newString(n)):e.undefined}function v(e,t){try{return t(e)}finally{for(var n,r=i(e);!(n=r()).done;){var o=n.value;o.alive&&o.dispose()}}}function y(e,t){try{return t.apply(void 0,e.map(function(e){return e[0]}))}finally{for(var n,r=i(e);!(n=r()).done;){var o=n.value;o[1]&&o[0].dispose()}}}function h(e,t,n,r){var o=e.newObject(),i=function(t,n){var i=r(t),a=void 0===n.value?void 0:r(n.value),s=void 0===n.get?void 0:r(n.get),u=void 0===n.set?void 0:r(n.set);e.newObject().consume(function(t){Object.entries(n).forEach(function(n){var r=n[0],o="value"===r?a:"get"===r?s:"set"===r?u:n[1]?e.true:e.false;o&&e.setProp(t,r,o)}),e.setProp(o,i,t)})},a=Object.getOwnPropertyDescriptors(t);Object.entries(a).forEach(function(e){return i(e[0],e[1])}),Object.getOwnPropertySymbols(a).forEach(function(e){return i(e,a[e])}),p(e,"Object.defineProperties",void 0,n,o).dispose(),o.dispose()}function m(e,t){var n,o,i,a=t.vm,l=t.unmarshal,c=t.isMarshalable,f=t.find,v=t.pre,y=function(e,t){switch(typeof t){case"undefined":return e.undefined;case"number":return e.newNumber(t);case"string":return e.newString(t);case"boolean":return t?e.true:e.false;case"object":return null===t?e.null:void 0}}(a,e);if(y)return y;var _=f(e);if(_)return _;var b=null==c?void 0:c(e);if(!1===b)return a.undefined;var g=function(e,t){return v(e,t,b)};if("json"===b)return function(e,t,n){var r,o=d(e,t);return null!=(r=n(t,o))?r:o}(a,e,g);var w=function(e){return m(e,t)};return null!=(n=null!=(o=null!=(i=function(e,t,n){var r;if("symbol"==typeof t){var o=p(e,"d => Symbol(d)",void 0,t.description?e.newString(t.description):e.undefined);return null!=(r=n(t,o))?r:o}}(a,e,g))?i:function(e,t,n,o,i,a){var l;if("function"==typeof t){var c=e.newFunction(t.name,function(){var i=this,l=o(this),p=[].slice.call(arguments).map(function(e){return o(e)});if(s(t)&&u(l)){var c=r(t,p);return Object.entries(c).forEach(function(t){e.setProp(i,t[0],n(t[1]))}),this}return n(a?a(t,l,p):t.apply(l,p))}).consume(function(t){return p(e,"Cls => {\n const fn = function(...args) { return Cls.apply(this, args); };\n fn.name = Cls.name;\n fn.length = Cls.length;\n return fn;\n }",void 0,t)}),f=null!=(l=i(t,c))?l:c;return h(e,t,c,n),f}}(a,e,w,l,g,t.preApply))?o:function(e,t,n,r){var o;if("object"==typeof t&&null!==t){var i=Array.isArray(t)?e.newArray():e.newObject(),a=null!=(o=r(t,i))?o:i,s=Object.getPrototypeOf(t),u=s&&s!==Object.prototype&&s!==Array.prototype?n(s):void 0;return u&&p(e,"Object.setPrototypeOf",void 0,a,u).dispose(),h(e,t,i,n),a}}(a,e,w,g))?n:a.undefined}function _(e,t,n,r){e.newFunction("",function(t,o){var i=r(t)[0];if("string"==typeof i||"number"==typeof i||"symbol"==typeof i){var a=[["value",!0],["get",!0],["set",!0],["configurable",!1],["enumerable",!1],["writable",!1]].reduce(function(t,n){var i=n[0],a=n[1],s=e.getProp(o,i),u=e.typeof(s);if("undefined"===u)return t;if(!a&&"boolean"===u)return t[i]=e.dump(e.getProp(o,i)),t;var l=r(s),p=l[0];return l[1]&&s.dispose(),t[i]=p,t},{});Object.defineProperty(n,i,a)}}).consume(function(n){p(e,"(o, fn) => {\n const descs = Object.getOwnPropertyDescriptors(o);\n Object.entries(descs).forEach(([k, v]) => fn(k, v));\n Object.getOwnPropertySymbols(descs).forEach(k => fn(k, descs[k]));\n }",void 0,t,n).dispose()})}function b(e,t){return g(e,t)[0]}function g(e,t){var n,r,o=t.vm,i=t.marshal,a=t.find,s=t.pre,u=function(e,t){var n=e.typeof(t);return"undefined"===n||"number"===n||"string"===n||"boolean"===n?[e.dump(t),!0]:"object"===n&&e.unwrapResult(e.evalCode("a => a === null")).consume(function(n){return e.dump(e.unwrapResult(e.callFunction(n,e.undefined,t)))})?[null,!0]:[void 0,!1]}(o,e);if(u[1])return[u[0],!1];var l=a(e);if(l)return[l,!0];var c=function(e){return g(e,t)},f=null!=(n=null!=(r=function(e,t,n){var r;if("symbol"===e.typeof(t)){var o=e.getString(e.getProp(t,"description")),i=Symbol(o);return null!=(r=n(i,t))?r:i}}(o,e,s))?r:function(e,t,n,r,o){var i;if("function"===e.typeof(t)){var a=function o(){var i=this instanceof o?this.constructor:void 0,a=this;return y([n(this)].concat([].slice.call(arguments).map(function(e){return n(e)})),function(n){var o=[].slice.call(arguments,1);if(i){var s=r(p.apply(void 0,[e,"(Cls, ...args) => new Cls(...args)",n,t].concat(o)));return Object.defineProperties(a,Object.getOwnPropertyDescriptors(s[0])),a}var u=e.unwrapResult(e.callFunction.apply(e,[t,n].concat(o))),l=r(u),c=l[0];return l[1]&&u.dispose(),c})},s=null!=(i=o(a,t))?i:a;return _(e,t,a,r),s}}(o,e,i,c,s))?n:function(e,t,n,r){var o;if("object"===e.typeof(t)&&!e.unwrapResult(e.evalCode("o => o === null")).consume(function(n){return e.dump(e.unwrapResult(e.callFunction(n,e.undefined,t)))})){var i=p(e,"Array.isArray",void 0,t).consume(function(t){return e.dump(t)})?[]:{},a=null!=(o=r(i,t))?o:i,s=p(e,"o => {\n const p = Object.getPrototypeOf(o);\n return !p || p === Object.prototype || p === Array.prototype ? undefined : p;\n }",void 0,t).consume(function(t){if("undefined"!==e.typeof(t))return n(t)[0]});return"object"==typeof s&&Object.setPrototypeOf(a,s),_(e,t,i,n),a}}(o,e,c,s);return[f,!1]}function w(e,t){var n;return u(e)&&null!=(n=e[t])?n:e}function j(e,t,n){return S(e,t,n)?[e.getProp(t,n),!0]:[t,!1]}function S(e,t,n){return!!e.dump(p(e,'(a, s) => (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]',void 0,t,n))}var O=[[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"]].concat(Object.getOwnPropertyNames(Symbol).filter(function(e){return"symbol"==typeof Symbol[e]}).map(function(e){return[Symbol[e],"Symbol."+e]}));e.Arena=function(){function e(e,t){var n,r=this;this.vm=void 0,this._map=void 0,this._registeredMap=void 0,this._registeredMapDispose=new Set,this._sync=new Set,this._temporalSync=new Set,this._symbol=Symbol(),this._symbolHandle=void 0,this._options=void 0,this._isMarshalable=function(e){var t,n,o=null==(t=r._options)?void 0:t.isMarshalable;return null!=(n="function"==typeof o?o(r._unwrap(e)):o)?n:"json"},this._marshalFind=function(e){var t,n,o,i=r._unwrap(e);return null!=(t=null!=(n=null!=(o=r._registeredMap.get(e))?o:i!==e?r._registeredMap.get(i):void 0)?n:r._map.get(e))?t:i!==e?r._map.get(i):void 0},this._marshalPre=function(e,t,n){var o;if("json"!==n)return null==(o=r._register(e,t,r._map))?void 0:o[1]},this._marshalPreApply=function(e,t,n){var o=u(t)?r._unwrap(t):void 0;o&&r._temporalSync.add(o);try{return e.apply(t,n)}finally{o&&r._temporalSync.delete(o)}},this._marshal=function(e){var t,n=r._registeredMap.get(e);if(n)return[n,!1];var o=m(null!=(t=r._wrap(e))?t:e,{vm:r.vm,unmarshal:r._unmarshal,isMarshalable:r._isMarshalable,find:r._marshalFind,pre:r._marshalPre,preApply:r._marshalPreApply});return[o,!r._map.hasHandle(o)]},this._preUnmarshal=function(e,t){var n;return null==(n=r._register(e,t,void 0,!0))?void 0:n[0]},this._unmarshalFind=function(e){var t;return null!=(t=r._registeredMap.getByHandle(e))?t:r._map.getByHandle(e)},this._unmarshal=function(e){var t=r._registeredMap.getByHandle(e);if(void 0!==t)return t;var n=r._wrapHandle(e)[0];return b(null!=n?n:e,{vm:r.vm,marshal:r._marshal,find:r._unmarshalFind,pre:r._preUnmarshal})},this._syncMode=function(e){var t=r._unwrap(e);return r._sync.has(t)||r._temporalSync.has(t)?"both":void 0},this._unwrapIfNotSynced=function(e){var t=r._unwrap(e);return r._sync.has(t)?e:t},this.vm=e,this._options=t,this._symbolHandle=e.unwrapResult(e.evalCode("Symbol()")),this._map=new a(e),this._registeredMap=new a(e),this.registerAll(null!=(n=null==t?void 0:t.registeredObjects)?n:O)}var t=e.prototype;return t.dispose=function(){this._map.dispose(),this._registeredMap.dispose(),this._symbolHandle.dispose()},t.evalCode=function(e){var t=this.vm.evalCode(e);return this._unwrapResultAndUnmarshal(t)},t.executePendingJobs=function(e){var t=this.vm.executePendingJobs(e);if("value"in t)return t.value;throw this._unwrapIfNotSynced(t.error.consume(this._unmarshal))},t.expose=function(e){for(var t=this,n=0,r=Object.entries(e);n<r.length;n++)i=(o=r[n])[0],function(e,n){var r=e[0],o=e[1];try{!function(e){t.vm.setProp(t.vm.global,i,e)}(r)}finally{o&&r.dispose()}}(t._marshal(o[1]));var o,i},t.sync=function(e){var t=this,n=this._wrap(e);return void 0===n?e:(l(n,function(e){t._sync.add(t._unwrap(e))}),n)},t.register=function(e,t){if(!this._registeredMap.has(e)){var n="string"==typeof t?this._unwrapResult(this.vm.evalCode(t)):t;c(this.vm,n,this.vm.undefined)||("string"==typeof t&&this._registeredMapDispose.add(e),this._registeredMap.set(e,n))}},t.registerAll=function(e){for(var t,n=i(e);!(t=n()).done;){var r=t.value;this.register(r[0],r[1])}},t.unregister=function(e,t){this._registeredMap.delete(e,this._registeredMapDispose.has(e)||t),this._registeredMapDispose.delete(e)},t.unregisterAll=function(e,t){for(var n,r=i(e);!(n=r()).done;)this.unregister(n.value,t)},t.startSync=function(e){u(e)&&this._sync.add(this._unwrap(e))},t.endSync=function(e){this._sync.delete(this._unwrap(e))},t._unwrapResult=function(e){if("value"in e)return e.value;throw this._unwrapIfNotSynced(e.error.consume(this._unmarshal))},t._unwrapResultAndUnmarshal=function(e){if(e)return this._unwrapIfNotSynced(this._unwrapResult(e).consume(this._unmarshal))},t._register=function(e,t,n,r){if(void 0===n&&(n=this._map),!this._registeredMap.has(e)&&!this._registeredMap.hasHandle(t)){var o=this._wrap(e),i=this._wrapHandle(t)[0];if(o&&i){var a=this._unwrap(e),s=this._unwrapHandle(t),u=s[0],l=s[1];if(!n.set(o,i,a,u))throw l&&u.dispose(),new Error("already registered");return r&&this._sync.add(a),[o,i]}}},t._wrap=function(e){return function(e,t,n,r,o,i){if(u(t)){if(s=n,u(a=t)&&a[s])return t;var a,s,l=new Proxy(t,{get:function(e,t){return t===n?e:Reflect.get(e,t)},set:function(t,a,s,u){var l,p=w(s,n),c=null!=(l=null==i?void 0:i(u))?l:"host";return"vm"!==c&&!Reflect.set(t,a,p,u)||"host"===c||!e.alive||y([o(u),o(a),o(p)],function(t,n,o){var i=j(e,t,r),a=i[0];i[1]?a.consume(function(t){return e.setProp(t,n,o)}):e.setProp(a,n,o)}),!0},deleteProperty:function(t,n){var a,s=null!=(a=null==i?void 0:i(l))?a:"host";return y([o(l),o(n)],function(o,i){var a=j(e,o,r),u=a[0],l=a[1];if("vm"===s||Reflect.deleteProperty(t,n)){if("host"===s||!e.alive)return!0;l?u.consume(function(t){return p(e,"(a, b) => delete a[b]",void 0,t,i)}):p(e,"(a, b) => delete a[b]",void 0,u,i)}return!0})}});return l}}(this.vm,e,this._symbol,this._symbolHandle,this._marshal,this._syncMode)},t._unwrap=function(e){return w(e,this._symbol)},t._wrapHandle=function(e){return function(e,t,n,r,o,i){return f(e,t)?S(e,t,r)?[t,!1]:v([e.newFunction("getSyncMode",function(t){var n=null==i?void 0:i(o(t));return"string"==typeof n?e.newString(n):e.undefined}),e.newFunction("setter",function(e,t,r){var i=o(e);if(i){var a=o(t);if("__proto__"!==a){var s=o(r);w(i,n)[a]=s}}}),e.newFunction("deleter",function(e,t){var r=o(e);if(r){var i=o(t);delete w(r,n)[i]}})],function(n){return[p.apply(void 0,[e,'(target, sym, getSyncMode, setter, deleter) => {\n const rec = new Proxy(target, {\n get(obj, key, receiver) {\n return key === sym ? obj : Reflect.get(obj, key, receiver)\n },\n set(obj, key, value, receiver) {\n const v = typeof value === "object" && value !== null || typeof value === "function"\n ? value[sym] ?? value\n : value;\n const sync = getSyncMode(receiver) ?? "vm";\n if (sync === "host" || Reflect.set(obj, key, v, receiver)) {\n if (sync !== "vm") {\n setter(receiver, key, v);\n }\n }\n return true;\n },\n deleteProperty(obj, key) {\n const sync = getSyncMode(rec) ?? "vm";\n if (sync === "host" || Reflect.deleteProperty(obj, key)) {\n if (sync !== "vm") {\n deleter(rec, key);\n }\n }\n return true;\n },\n });\n return rec;\n }',void 0,t,r].concat(n)),!0]}):[void 0,!1]}(this.vm,e,this._symbol,this._symbolHandle,this._unmarshal,this._syncMode)},t._unwrapHandle=function(e){return j(this.vm,e,this._symbolHandle)},e}(),e.VMMap=a,e.call=p,e.complexity=function(e,t){return l(e,t?function(e,n){return n.size<t}:void 0).size},e.consumeAll=v,e.defaultRegisteredObjects=O,e.eq=c,e.isES2015Class=s,e.isHandleObject=f,e.isObject=u,e.json=d,e.marshal=m,e.unmarshal=b,e.walkObject=l});
2
- //# sourceMappingURL=quickjs-emscripten-sync.umd.js.map
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,16 +1,20 @@
1
1
  {
2
2
  "name": "quickjs-emscripten-sync",
3
3
  "author": "rot1024",
4
- "version": "1.2.0",
4
+ "version": "1.3.0",
5
5
  "license": "MIT",
6
- "type": "module",
7
6
  "source": "./src/index.ts",
8
- "main": "./dist/quickjs-emscripten-sync.cjs",
9
- "module": "./dist/quickjs-emscripten-sync.module.js",
10
- "exports": "./dist/quickjs-emscripten-sync.modern.js",
7
+ "main": "./dist/quickjs-emscripten-sync.umd.js",
8
+ "module": "./dist/quickjs-emscripten-sync.es.js",
11
9
  "unpkg": "./dist/quickjs-emscripten-sync.umd.js",
12
10
  "types": "./dist/index.d.ts",
13
11
  "sideEffects": false,
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/quickjs-emscripten-sync.es.js",
15
+ "require": "./dist/quickjs-emscripten-sync.umd.js"
16
+ }
17
+ },
14
18
  "files": [
15
19
  "dist",
16
20
  "src"
@@ -19,57 +23,31 @@
19
23
  "node": ">=10"
20
24
  },
21
25
  "scripts": {
22
- "start": "microbundle watch --tsconfig tsconfig.build.json",
23
- "build": "microbundle --tsconfig tsconfig.build.json",
24
- "test": "jest",
25
- "lint": "tsc && eslint --ext '.ts,.tsx,.js' .",
26
- "size": "size-limit",
27
- "analyze": "size-limit --why"
26
+ "build": "tsc && vite build",
27
+ "test": "vitest",
28
+ "lint": "eslint ."
28
29
  },
29
30
  "peerDependencies": {
30
31
  "quickjs-emscripten": "*"
31
32
  },
32
- "jest": {
33
- "preset": "ts-jest"
34
- },
35
33
  "prettier": {
36
34
  "printWidth": 80,
37
35
  "semi": true,
38
36
  "singleQuote": false,
39
37
  "trailingComma": "es5"
40
38
  },
41
- "size-limit": [
42
- {
43
- "path": "dist/quickjs-emscripten-sync.cjs",
44
- "limit": "10 KB"
45
- },
46
- {
47
- "path": "dist/quickjs-emscripten-sync.umd.js",
48
- "limit": "10 KB"
49
- },
50
- {
51
- "path": "dist/quickjs-emscripten-sync.module.js",
52
- "limit": "10 KB"
53
- },
54
- {
55
- "path": "dist/quickjs-emscripten-sync.modern.js",
56
- "limit": "10 KB"
57
- }
58
- ],
59
39
  "devDependencies": {
60
- "@size-limit/preset-small-lib": "^5.0.3",
61
- "@types/jest": "^27.0.1",
62
- "@typescript-eslint/eslint-plugin": "^4.31.1",
63
- "@typescript-eslint/parser": "^4.31.1",
64
- "eslint": "^7.32.0",
65
- "eslint-config-prettier": "^8.3.0",
66
- "eslint-plugin-prettier": "^4.0.0",
67
- "jest": "^27.2.0",
68
- "microbundle": "^0.13.3",
69
- "prettier": "^2.4.0",
70
- "quickjs-emscripten": "^0.13.0",
71
- "size-limit": "^5.0.3",
72
- "ts-jest": "^27.0.5",
73
- "typescript": "^4.4.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"
74
52
  }
75
53
  }
package/src/index.test.ts CHANGED
@@ -1,6 +1,76 @@
1
1
  import { getQuickJS } from "quickjs-emscripten";
2
+ import { describe, expect, test, vi } from "vitest";
3
+
2
4
  import { Arena } from ".";
3
5
 
6
+ describe("readme", () => {
7
+ test("first", async () => {
8
+ class Cls {
9
+ field = 0;
10
+
11
+ method() {
12
+ return ++this.field;
13
+ }
14
+ }
15
+
16
+ const vm = (await getQuickJS()).createVm();
17
+ const arena = new Arena(vm, { isMarshalable: true });
18
+
19
+ // We can pass objects to the VM and run code safely
20
+ const exposed = {
21
+ Cls,
22
+ cls: new Cls(),
23
+ syncedCls: arena.sync(new Cls()),
24
+ };
25
+ arena.expose(exposed);
26
+
27
+ expect(arena.evalCode(`cls instanceof Cls`)).toBe(true);
28
+ expect(arena.evalCode(`cls.field`)).toBe(0);
29
+ expect(arena.evalCode(`cls.method()`)).toBe(1);
30
+ expect(arena.evalCode(`cls.field`)).toBe(1);
31
+
32
+ expect(arena.evalCode(`syncedCls.field`)).toBe(0);
33
+ expect(exposed.syncedCls.method()).toBe(1);
34
+ expect(arena.evalCode(`syncedCls.field`)).toBe(1);
35
+
36
+ arena.dispose();
37
+ vm.dispose();
38
+ });
39
+
40
+ test("usage", async () => {
41
+ const quickjs = await getQuickJS();
42
+ const vm = quickjs.createVm();
43
+
44
+ // init Arena
45
+ // ⚠️ Marshaling is opt-in for security reasons.
46
+ // ⚠️ Be careful when activating marshalling.
47
+ const arena = new Arena(vm, { isMarshalable: true });
48
+
49
+ // expose objects as global objects in QuickJS VM
50
+ const log = vi.fn();
51
+ arena.expose({
52
+ console: { log },
53
+ });
54
+ arena.evalCode(`console.log("hello, world");`); // run console.log
55
+ expect(log).toBeCalledWith("hello, world");
56
+ arena.evalCode(`1 + 1`); // 2
57
+
58
+ // expose objects but also enable sync
59
+ const data = arena.sync({ hoge: "foo" });
60
+ arena.expose({ data });
61
+
62
+ arena.evalCode(`data.hoge = "bar"`);
63
+ // eval code and operations to exposed objects are automatically synced
64
+ expect(data.hoge).toBe("bar");
65
+ data.hoge = "changed!";
66
+ expect(arena.evalCode(`data.hoge`)).toBe("changed!");
67
+
68
+ // Don't forget calling arena.dispose() before disposing QuickJS VM!
69
+ arena.dispose();
70
+ vm.dispose();
71
+ });
72
+ });
73
+
4
74
  describe("evalCode", () => {
5
75
  test("simple object and function", async () => {
6
76
  const vm = (await getQuickJS()).createVm();
package/src/index.ts CHANGED
@@ -1,5 +1,9 @@
1
- import { QuickJSHandle, QuickJSVm } from "quickjs-emscripten";
2
- import {
1
+ import type {
2
+ QuickJSDeferredPromise,
3
+ QuickJSHandle,
4
+ QuickJSVm,
5
+ } from "quickjs-emscripten";
6
+ import type {
3
7
  SuccessOrFail,
4
8
  VmCallResult,
5
9
  } from "quickjs-emscripten/dist/vm-interface";
@@ -16,6 +20,7 @@ import {
16
20
  json,
17
21
  consumeAll,
18
22
  mayConsume,
23
+ handleFrom,
19
24
  } from "./vmutil";
20
25
  import { defaultRegisteredObjects } from "./default";
21
26
 
@@ -214,11 +219,11 @@ export class Arena {
214
219
 
215
220
  _marshalPre = (
216
221
  t: unknown,
217
- h: QuickJSHandle,
222
+ h: QuickJSHandle | QuickJSDeferredPromise,
218
223
  mode: true | "json" | undefined
219
224
  ): Wrapped<QuickJSHandle> | undefined => {
220
225
  if (mode === "json") return;
221
- return this._register(t, h, this._map)?.[1];
226
+ return this._register(t, handleFrom(h), this._map)?.[1];
222
227
  };
223
228
 
224
229
  _marshalPreApply = (
@@ -1,16 +1,17 @@
1
1
  import { getQuickJS, QuickJSHandle } from "quickjs-emscripten";
2
2
  import { json, eq, call } from "../vmutil";
3
3
  import marshalFunction from "./function";
4
+ import { expect, test, vi } from "vitest";
4
5
 
5
6
  test("normal func", async () => {
6
7
  const vm = (await getQuickJS()).createVm();
7
8
 
8
- const marshal = jest.fn((v) => json(vm, v));
9
- const unmarshal = jest.fn((v) =>
9
+ const marshal = vi.fn((v) => json(vm, v));
10
+ const unmarshal = vi.fn((v) =>
10
11
  eq(vm, v, vm.global) ? undefined : vm.dump(v)
11
12
  );
12
- const preMarshal = jest.fn((_, a) => a);
13
- const innerfn = jest.fn((..._args: any[]) => "hoge");
13
+ const preMarshal = vi.fn((_, a) => a);
14
+ const innerfn = vi.fn((..._args: any[]) => "hoge");
14
15
  const fn = (...args: any[]) => innerfn(...args);
15
16
 
16
17
  const handle = marshalFunction(vm, fn, marshal, unmarshal, preMarshal);
@@ -40,7 +41,7 @@ test("normal func", async () => {
40
41
 
41
42
  test("func which has properties", async () => {
42
43
  const vm = (await getQuickJS()).createVm();
43
- const marshal = jest.fn((v) => json(vm, v));
44
+ const marshal = vi.fn((v) => json(vm, v));
44
45
 
45
46
  const fn = () => {};
46
47
  fn.hoge = "foo";
@@ -118,7 +119,7 @@ test("preApply", async () => {
118
119
  };
119
120
  const unmarshal = (v: QuickJSHandle) =>
120
121
  vm.typeof(v) === "object" ? that : vm.dump(v);
121
- const preApply = jest.fn(
122
+ const preApply = vi.fn(
122
123
  (a: Function, b: any, c: any[]) => a.apply(b, c) + "!"
123
124
  );
124
125
  const that = {};
@@ -152,7 +153,7 @@ test("preApply", async () => {
152
153
 
153
154
  test("undefined", async () => {
154
155
  const vm = (await getQuickJS()).createVm();
155
- const f = jest.fn();
156
+ const f = vi.fn();
156
157
 
157
158
  expect(marshalFunction(vm, undefined, f, f, f)).toBe(undefined);
158
159
  expect(marshalFunction(vm, null, f, f, f)).toBe(undefined);
@@ -1,7 +1,10 @@
1
1
  import { getQuickJS } from "quickjs-emscripten";
2
+ import { expect, test, vi } from "vitest";
3
+
2
4
  import VMMap from "../vmmap";
3
- import { instanceOf, call } from "../vmutil";
5
+ import { instanceOf, call, handleFrom, fn } from "../vmutil";
4
6
  import marshal from ".";
7
+ import { newDeferred } from "../util";
5
8
 
6
9
  test("primitive, array, object", async () => {
7
10
  const { vm, map, marshal, dispose } = await setup();
@@ -90,6 +93,42 @@ test("function", async () => {
90
93
  dispose();
91
94
  });
92
95
 
96
+ test("promise", async () => {
97
+ const { vm, marshal, dispose } = await setup();
98
+ const register = fn(
99
+ vm,
100
+ `promise => { promise.then(d => notify("resolve", d), d => notify("reject", d)); }`
101
+ );
102
+
103
+ let notified: any;
104
+ vm.newFunction("notify", (...handles) => {
105
+ notified = handles.map((h) => vm.dump(h));
106
+ }).consume((h) => {
107
+ vm.setProp(vm.global, "notify", h);
108
+ });
109
+
110
+ const deferred = newDeferred();
111
+ const handle = marshal(deferred.promise);
112
+ register(undefined, handle);
113
+
114
+ deferred.resolve("foo");
115
+ await deferred.promise;
116
+ expect(vm.unwrapResult(vm.executePendingJobs())).toBe(1);
117
+ expect(notified).toEqual(["resolve", "foo"]);
118
+
119
+ const deferred2 = newDeferred();
120
+ const handle2 = marshal(deferred2.promise);
121
+ register(undefined, handle2);
122
+
123
+ deferred2.reject("bar");
124
+ await expect(deferred2.promise).rejects.toBe("bar");
125
+ expect(vm.unwrapResult(vm.executePendingJobs())).toBe(1);
126
+ expect(notified).toEqual(["reject", "bar"]);
127
+
128
+ register.dispose();
129
+ dispose();
130
+ });
131
+
93
132
  test("class", async () => {
94
133
  const { vm, map, marshal, dispose } = await setup();
95
134
 
@@ -117,6 +156,7 @@ test("class", async () => {
117
156
  }
118
157
  }
119
158
 
159
+ expect(A.name).toBe("_A"); // class A's name is _A in vitest
120
160
  const handle = marshal(A);
121
161
  if (!map) throw new Error("map is undefined");
122
162
 
@@ -134,7 +174,7 @@ test("class", async () => {
134
174
 
135
175
  expect(vm.typeof(handle)).toBe("function");
136
176
  expect(vm.dump(vm.getProp(handle, "length"))).toBe(1);
137
- expect(vm.dump(vm.getProp(handle, "name"))).toBe("A");
177
+ expect(vm.dump(vm.getProp(handle, "name"))).toBe("_A");
138
178
  const staticA = vm.getProp(handle, "a");
139
179
  expect(instanceOf(vm, staticA, handle)).toBe(true);
140
180
  expect(vm.dump(vm.getProp(staticA, "a"))).toBe(100);
@@ -172,7 +212,7 @@ test("class", async () => {
172
212
  });
173
213
 
174
214
  test("marshalable", async () => {
175
- const isMarshalable = jest.fn((a: any) => a !== globalThis);
215
+ const isMarshalable = vi.fn((a: any) => a !== globalThis);
176
216
  const { vm, marshal, dispose } = await setup({
177
217
  isMarshalable,
178
218
  });
@@ -187,7 +227,7 @@ test("marshalable", async () => {
187
227
  });
188
228
 
189
229
  test("marshalable json", async () => {
190
- const isMarshalable = jest.fn<"json", [any]>(() => "json");
230
+ const isMarshalable = vi.fn(() => "json" as const);
191
231
  const { vm, marshal, dispose } = await setup({
192
232
  isMarshalable,
193
233
  });
@@ -225,7 +265,8 @@ const setup = async ({
225
265
  vm,
226
266
  unmarshal: (h) => map.getByHandle(h) ?? vm.dump(h),
227
267
  isMarshalable,
228
- pre: (t, h) => {
268
+ pre: (t, d) => {
269
+ const h = handleFrom(d);
229
270
  map.set(t, h);
230
271
  return h;
231
272
  },
@@ -1,9 +1,14 @@
1
- import { QuickJSHandle, QuickJSVm } from "quickjs-emscripten";
1
+ import type {
2
+ QuickJSDeferredPromise,
3
+ QuickJSHandle,
4
+ QuickJSVm,
5
+ } from "quickjs-emscripten";
2
6
  import marshalFunction from "./function";
3
7
  import marshalObject from "./object";
4
8
  import marshalPrimitive from "./primitive";
5
9
  import marshalSymbol from "./symbol";
6
10
  import marshalJSON from "./json";
11
+ import marshalPromise from "./promise";
7
12
 
8
13
  export type Options = {
9
14
  vm: QuickJSVm;
@@ -12,7 +17,7 @@ export type Options = {
12
17
  find: (target: unknown) => QuickJSHandle | undefined;
13
18
  pre: (
14
19
  target: unknown,
15
- handle: QuickJSHandle,
20
+ handle: QuickJSHandle | QuickJSDeferredPromise,
16
21
  mode: true | "json" | undefined
17
22
  ) => QuickJSHandle | undefined;
18
23
  preApply?: (target: Function, thisArg: unknown, args: unknown[]) => any;
@@ -38,7 +43,7 @@ export function marshal(target: unknown, options: Options): QuickJSHandle {
38
43
  return vm.undefined;
39
44
  }
40
45
 
41
- const pre2 = (target: any, handle: QuickJSHandle) =>
46
+ const pre2 = (target: any, handle: QuickJSHandle | QuickJSDeferredPromise) =>
42
47
  pre(target, handle, marshalable);
43
48
  if (marshalable === "json") {
44
49
  return marshalJSON(vm, target, pre2);
@@ -47,6 +52,7 @@ export function marshal(target: unknown, options: Options): QuickJSHandle {
47
52
  const marshal2 = (t: unknown) => marshal(t, options);
48
53
  return (
49
54
  marshalSymbol(vm, target, pre2) ??
55
+ marshalPromise(vm, target, marshal2, pre2) ??
50
56
  marshalFunction(vm, target, marshal2, unmarshal, pre2, options.preApply) ??
51
57
  marshalObject(vm, target, marshal2, pre2) ??
52
58
  vm.undefined
@@ -1,4 +1,6 @@
1
1
  import { getQuickJS } from "quickjs-emscripten";
2
+ import { expect, test, vi } from "vitest";
3
+
2
4
  import marshalJSON from "./json";
3
5
 
4
6
  test("empty object", async () => {
@@ -8,7 +10,7 @@ test("empty object", async () => {
8
10
  );
9
11
 
10
12
  const obj = {};
11
- const preMarshal = jest.fn((_, a) => a);
13
+ const preMarshal = vi.fn((_, a) => a);
12
14
 
13
15
  const handle = marshalJSON(vm, obj, preMarshal);
14
16
  if (!handle) throw new Error("handle is undefined");
@@ -36,7 +38,7 @@ test("normal object", async () => {
36
38
  const entries = vm.unwrapResult(vm.evalCode(`Object.entries`));
37
39
 
38
40
  const obj = { a: 100, b: "hoge" };
39
- const preMarshal = jest.fn((_, a) => a);
41
+ const preMarshal = vi.fn((_, a) => a);
40
42
 
41
43
  const handle = marshalJSON(vm, obj, preMarshal);
42
44
  if (!handle) throw new Error("handle is undefined");
@@ -70,7 +72,7 @@ test("array", async () => {
70
72
  const isArray = vm.unwrapResult(vm.evalCode(`Array.isArray`));
71
73
 
72
74
  const array = [1, "aa"];
73
- const preMarshal = jest.fn((_, a) => a);
75
+ const preMarshal = vi.fn((_, a) => a);
74
76
 
75
77
  const handle = marshalJSON(vm, array, preMarshal);
76
78
  if (!handle) throw new Error("handle is undefined");
@@ -1,4 +1,6 @@
1
1
  import { getQuickJS } from "quickjs-emscripten";
2
+ import { expect, test, vi } from "vitest";
3
+
2
4
  import { call } from "../vmutil";
3
5
  import marshalObject from "./object";
4
6
 
@@ -9,8 +11,8 @@ test("empty object", async () => {
9
11
  );
10
12
 
11
13
  const obj = {};
12
- const marshal = jest.fn();
13
- const preMarshal = jest.fn((_, a) => a);
14
+ const marshal = vi.fn();
15
+ const preMarshal = vi.fn((_, a) => a);
14
16
 
15
17
  const handle = marshalObject(vm, obj, marshal, preMarshal);
16
18
  if (!handle) throw new Error("handle is undefined");
@@ -39,14 +41,14 @@ test("normal object", async () => {
39
41
  const entries = vm.unwrapResult(vm.evalCode(`Object.entries`));
40
42
 
41
43
  const obj = { a: 100, b: "hoge" };
42
- const marshal = jest.fn((v) =>
44
+ const marshal = vi.fn((v) =>
43
45
  typeof v === "number"
44
46
  ? vm.newNumber(v)
45
47
  : typeof v === "string"
46
48
  ? vm.newString(v)
47
49
  : vm.null
48
50
  );
49
- const preMarshal = jest.fn((_, a) => a);
51
+ const preMarshal = vi.fn((_, a) => a);
50
52
 
51
53
  const handle = marshalObject(vm, obj, marshal, preMarshal);
52
54
  if (!handle) throw new Error("handle is undefined");
@@ -81,14 +83,14 @@ test("array", async () => {
81
83
  const isArray = vm.unwrapResult(vm.evalCode(`Array.isArray`));
82
84
 
83
85
  const array = [1, "aa"];
84
- const marshal = jest.fn((v) =>
86
+ const marshal = vi.fn((v) =>
85
87
  typeof v === "number"
86
88
  ? vm.newNumber(v)
87
89
  : typeof v === "string"
88
90
  ? vm.newString(v)
89
91
  : vm.null
90
92
  );
91
- const preMarshal = jest.fn((_, a) => a);
93
+ const preMarshal = vi.fn((_, a) => a);
92
94
 
93
95
  const handle = marshalObject(vm, array, marshal, preMarshal);
94
96
  if (!handle) throw new Error("handle is undefined");
@@ -123,7 +125,7 @@ test("prototype", async () => {
123
125
  const proto = { a: 100 };
124
126
  const protoHandle = vm.newObject();
125
127
  vm.setProp(protoHandle, "a", vm.newNumber(100));
126
- const preMarshal = jest.fn((_, a) => a);
128
+ const preMarshal = vi.fn((_, a) => a);
127
129
 
128
130
  const obj = Object.create(proto);
129
131
  obj.b = "hoge";
@@ -1,4 +1,6 @@
1
1
  import { getQuickJS } from "quickjs-emscripten";
2
+ import { expect, test } from "vitest";
3
+
2
4
  import { eq } from "../vmutil";
3
5
  import marshalPrimitive from "./primitive";
4
6