6.7.0 - Alpha6 - 新增 structuredClone 全局方法

This commit is contained in:
SuperMonster003
2025-09-24 16:05:04 +08:00
parent c1ee107bc3
commit 29718b6740
3 changed files with 6 additions and 0 deletions

View File

@@ -0,0 +1 @@
module.exports=function(e){function h(e){var t;return"function"==typeof e.slice?e.slice(0):(t=new ArrayBuffer(e.byteLength),e=new Uint8Array(e),new Uint8Array(t).set(e),t)}function m(t,n){if(null===(s=t)||"object"!=typeof s&&"function"!=typeof s)return t;if(n.has(t))return n.get(t);if(Array.isArray(t)){var r=new Array(t.length);n.set(t,r);for(let e=0;e<t.length;e++)e in t&&(r[e]=m(t[e],n));return r}var e,a,c,s=Object.prototype.toString.call(t).slice(8,-1);switch(s){case"Date":return new Date(t.getTime());case"RegExp":var o=function(e){var t=(e.global?"g":"")+(e.ignoreCase?"i":"")+(e.multiline?"m":"")+(e.dotAll?"s":"")+(e.unicode?"u":"")+(e.sticky?"y":""),t=new RegExp(e.source,t);try{t.lastIndex=e.lastIndex}catch(e){}return t}(t);return n.set(t,o),o;case"Map":{o=t;var u=n;const p=new Map;return u.set(o,p),o.forEach(function(e,t){p.set(m(t,u),m(e,u))}),p;return}case"Set":{var i=t;var f=n;const A=new Set;return f.set(i,A),i.forEach(function(e){A.add(m(e,f))}),A;return}case"ArrayBuffer":i=h(t);return n.set(t,i),i;case"DataView":return a=n,c=h((e=t).buffer),c=new DataView(c,e.byteOffset,e.byteLength),a.set(e,c),c;case"Uint8Array":case"Uint8ClampedArray":case"Uint16Array":case"Uint32Array":case"Int8Array":case"Int16Array":case"Int32Array":case"Float32Array":case"Float64Array":case"BigInt64Array":case"BigUint64Array":return a=n,c=new(e=t).constructor(h(e.buffer),e.byteOffset,e.length),a.set(e,c),c;case"Error":{var y=t;var l=n;var g=y.constructor;let r;try{r=new g(y.message)}catch(e){r=new Error(String(y&&y.message))}if(l.set(y,r),"name"in y)try{r.name=y.name}catch(e){}if("stack"in y)try{r.stack=y.stack}catch(e){}if("cause"in y)try{r.cause=m(y.cause,l)}catch(e){}return Reflect.ownKeys(y).forEach(function(e){if("message"!==e&&"name"!==e&&"stack"!==e&&"cause"!==e){var t=Object.getOwnPropertyDescriptor(y,e);if(t){"value"in t&&(t.value=m(t.value,l));try{Object.defineProperty(r,e,t)}catch(e){}}}}),r;return}case"Boolean":case"Number":case"String":case"BigInt":case"Symbol":return Object(t.valueOf());default:if("function"==typeof t)throw new Error("DataCloneError: Cannot clone function");{var v=t;var w=n;g=Object.getPrototypeOf(v);const b=Object.create(g);return w.set(v,b),Reflect.ownKeys(v).forEach(function(e){var t=Object.getOwnPropertyDescriptor(v,e);t&&("value"in t&&(t.value=m(t.value,w)),Object.defineProperty(b,e,t))}),b;return}}}"function"!=typeof e.structuredClone&&(e.structuredClone=function(e){return m(e,new WeakMap)})};

View File

@@ -433,6 +433,8 @@ class ScriptRuntime private constructor(builder: Builder) {
private val js_array_observe_min by lazy { rhinoRequire("array-observe.min") as BaseFunction }
private val js_structured_clone by lazy { rhinoRequire("structured-clone.min") as BaseFunction }
private val js_mod_continuation by lazy { rhinoRequire("continuation") as ScriptableObject }
private val js_mod_internal by lazy { rhinoRequire("internal") as ScriptableObject }
@@ -491,6 +493,8 @@ class ScriptRuntime private constructor(builder: Builder) {
callFunction(this, js_object_observe_lite_min, topLevelScope, topLevelScope, arrayOf(topLevelScope))
callFunction(this, js_array_observe_min, topLevelScope, topLevelScope, arrayOf(topLevelScope))
callFunction(this, js_structured_clone, topLevelScope, topLevelScope, arrayOf(topLevelScope))
if (Pref.isExtendingJsBuildInObjectsEnabled) Jsox.extendAllRhinoWithRuntime(this)
// @Hint by Stardust (https://github.com/hyb1996) on Feb 27, 2018