From a8edba6f02acd4b5953018f96dc89b5848ae7a13 Mon Sep 17 00:00:00 2001 From: Knox Date: Sat, 23 Nov 2019 14:07:18 +0800 Subject: [PATCH] Improved type array to get the name of the code (#5780) * Improved type array to get the name of the code * refine code * add error tips * add type array * refine code --- cocos2d/core/platform/instantiate-jit.js | 38 ++++++++++++++++++++---- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/platform/instantiate-jit.js b/cocos2d/core/platform/instantiate-jit.js index add96bbc17a..ec8572a50cc 100644 --- a/cocos2d/core/platform/instantiate-jit.js +++ b/cocos2d/core/platform/instantiate-jit.js @@ -55,11 +55,37 @@ const DEFAULT_MODULE_CACHE = { 'cc.PrefabInfo': false }; -!Float64Array.name && (Float64Array.name = 'Float64Array'); -!Float32Array.name && (Float32Array.name = 'Float32Array'); -!Uint32Array.name && (Uint32Array.name = 'Uint32Array'); -!Int32Array.name && (Int32Array.name = 'Int32Array'); -!Uint8Array.name && (Uint8Array.name = 'Uint8Array'); +try { + // compatible for IE + !Float32Array.name && (Float32Array.name = 'Float32Array'); + !Float64Array.name && (Float64Array.name = 'Float64Array'); + + !Int8Array.name && (Int8Array.name = 'Int8Array'); + !Int16Array.name && (Int16Array.name = 'Int16Array'); + !Int32Array.name && (Int32Array.name = 'Int32Array'); + + !Uint8Array.name && (Uint8Array.name = 'Uint8Array'); + !Uint16Array.name && (Uint16Array.name = 'Uint16Array'); + !Uint32Array.name && (Uint32Array.name = 'Uint32Array'); +} +catch (e) {} + +// compatible for iOS 9 +function getTypedArrayName (constructor) { + if (constructor === Float32Array) { return 'Float32Array'; } + else if (constructor === Float64Array) { return 'Float64Array'; } + + else if (constructor === Int8Array) { return 'Int8Array'; } + else if (constructor === Int16Array) { return 'Int16Array'; } + else if (constructor === Int32Array) { return 'Int32Array'; } + + else if (constructor === Uint8Array) { return 'Uint8Array'; } + else if (constructor === Uint16Array) { return 'Uint16Array'; } + else if (constructor === Uint32Array) { return 'Uint32Array'; } + else { + throw new Error(`Unknown TypedArray could not be instantiated: ${constructor}`); + } +} // HELPER CLASSES @@ -369,7 +395,7 @@ proto.instantiateArray = function (value) { }; proto.instantiateTypedArray = function (value) { - let type = value.constructor.name; + let type = value.constructor.name || getTypedArrayName(value.constructor); if (value.length === 0) { return 'new ' + type; }