Merge pull request #1317 from jareguo/master

optimize deserialization by using eval
This commit is contained in:
Jare Guo
2016-11-25 15:16:55 +08:00
committed by GitHub
2 changed files with 142 additions and 63 deletions
+2
View File
@@ -408,6 +408,8 @@ function getNewValueTypeCode (value) {
return res + ')';
}
// TODO - move escapeForJS, VAR_REG, getNewValueTypeCode to misc.js or a new source file
// convert a normal string including newlines, quotes and unicode characters into a string literal
// ready to use in JavaScript source
function escapeForJS (s) {
+140 -63
View File
@@ -26,6 +26,8 @@
var JS = require('./js');
var CCObject = require('./CCObject');
var Attr = require('./attribute');
var CCClass = require('./CCClass');
var cleanEval = require('../utils/misc').cleanEval;
// HELPERS
@@ -63,20 +65,6 @@ var Details = function () {
* @property {String} rawProp
*/
this.rawProp = '';
if (CC_DEV) {
/**
* 用户可以指定一个在反序列化过程中会被触发的回调,该回调会在反序列化之前调用,并且传回反序列化时解析到的字段。
* NOTE:
* - only available in editor
* - 会被传回的字段仅限于非 Asset 类型,并且如果字段值为 null 或 undefined,则可能不会被传回。
* @callback visit
* @param {Object} obj
* @param {String} propName
* @private
*/
this.visit = null;
}
};
/**
* @method reset
@@ -191,8 +179,10 @@ var _Deserializer = (function () {
}
}
var prototype = _Deserializer.prototype;
// 和 _deserializeObject 不同的地方在于会判断 id 和 uuid
_Deserializer.prototype._deserializeObjField = function (obj, jsonObj, propName, target) {
prototype._deserializeObjField = function (obj, jsonObj, propName, target) {
var id = jsonObj.__id__;
if (typeof id === 'undefined') {
var uuid = jsonObj.__uuid__;
@@ -217,9 +207,6 @@ var _Deserializer = (function () {
else {
obj[propName] = _deserializeObject(this, jsonObj);
}
if (CC_DEV && this.result.visit) {
this.result.visit(obj, propName);
}
}
}
else {
@@ -232,22 +219,17 @@ var _Deserializer = (function () {
this._idObjList.push(obj);
this._idPropList.push(propName);
}
if (CC_DEV && this.result.visit) {
this.result.visit(obj, propName);
}
}
};
function _deserializePrimitiveObject (self, instance, serialized) {
prototype._deserializePrimitiveObject = function (instance, serialized) {
var self = this;
for (var propName in serialized) {
if (serialized.hasOwnProperty(propName)) {
var prop = serialized[propName];
if (typeof prop !== 'object') {
if (propName !== '__type__'/* && k != '__id__'*/) {
instance[propName] = prop;
if (CC_DEV && self.result.visit) {
self.result.visit(instance, propName);
}
}
}
else {
@@ -266,7 +248,7 @@ var _Deserializer = (function () {
}
}
}
};
function _deserializeTypedObject (self, instance, serialized) {
//++self.stackCounter;
@@ -297,63 +279,151 @@ var _Deserializer = (function () {
//--self.stackCounter;
}
var RAW_TYPE = Attr.DELIMETER + 'rawType';
var EDITOR_ONLY = Attr.DELIMETER + 'editorOnly';
var SERIALIZABLE = Attr.DELIMETER + 'serializable';
function _deserializeFireClass(self, obj, serialized, klass, target) {
var props = klass.__props__;
// function _deserializeFireClass(self, obj, serialized, klass, target) {
// var RAW_TYPE = Attr.DELIMETER + 'rawType';
// var EDITOR_ONLY = Attr.DELIMETER + 'editorOnly';
// var SERIALIZABLE = Attr.DELIMETER + 'serializable';
// var props = klass.__props__;
// var attrs = Attr.getClassAttrs(klass);
// for (var p = 0; p < props.length; p++) {
// var propName = props[p];
// var rawType = attrs[propName + RAW_TYPE];
// if (!rawType) {
// if (((CC_EDITOR && self._ignoreEditorOnly) || (!CC_EDITOR && CC_DEV && !CC_TEST))
// && attrs[propName + EDITOR_ONLY]) {
// var mayUsedInPersistRoot = (obj instanceof cc.Node && propName === '_id');
// if ( !mayUsedInPersistRoot ) {
// continue; // skip editor only if in preview
// }
// }
// if (attrs[propName + SERIALIZABLE] === false) {
// continue; // skip nonSerialized
// }
// var prop = serialized[propName];
// if (typeof prop === 'undefined') {
// continue;
// }
// if (typeof prop !== 'object') {
// obj[propName] = prop;
// }
// else {
// if (prop) {
// if (CC_DEV) {
// self._deserializeObjField(obj, prop, propName, target && obj);
// }
// else {
// self._deserializeObjField(obj, prop, propName);
// }
// }
// else {
// obj[propName] = null;
// }
// }
// }
// else {
// // always load raw objects even if property not serialized
// if (self.result.rawProp) {
// cc.error('not support multi raw object in a file');
// // 这里假定每个asset都有uuid,每个json只能包含一个asset,只能包含一个rawProp
// }
// self.result.rawProp = propName;
// }
// }
// if (props[props.length - 1] === '_$erialized') {
// // deep copy original serialized data
// obj._$erialized = JSON.parse(JSON.stringify(serialized));
// // parse the serialized data as primitive javascript object, so its __id__ will be dereferenced
// self._deserializePrimitiveObject(obj._$erialized, serialized);
// }
// }
function compileDeserialize (self, klass) {
var RAW_TYPE = Attr.DELIMETER + 'rawType';
var EDITOR_ONLY = Attr.DELIMETER + 'editorOnly';
var SERIALIZABLE = Attr.DELIMETER + 'serializable';
var attrs = Attr.getClassAttrs(klass);
var props = klass.__props__;
// self, obj, serializedData, klass, target
var sources = [
'(function(s,o,d,k,t){',
'var prop;'
];
// sources.push('var vb,vn,vs,vo,vu,vf;'); // boolean, number, string, object, undefined, function
for (var p = 0; p < props.length; p++) {
var propName = props[p];
var propNameLiteral;
var rawType = attrs[propName + RAW_TYPE];
if (!rawType) {
if (((CC_EDITOR && self._ignoreEditorOnly) || (!CC_EDITOR && CC_DEV && !CC_TEST))
&& attrs[propName + EDITOR_ONLY]) {
var mayUsedInPersistRoot = (obj instanceof cc.Node && propName === '_id');
if ( !mayUsedInPersistRoot ) {
var mayUsedInPersistRoot = (propName === '_id' && cc.isChildClassOf(klass, cc.Node));
if (!mayUsedInPersistRoot) {
continue; // skip editor only if in preview
}
}
if (attrs[propName + SERIALIZABLE] === false) {
continue; // skip nonSerialized
}
var prop = serialized[propName];
if (typeof prop === 'undefined') {
continue;
}
if (typeof prop !== 'object') {
obj[propName] = prop;
var accessor;
if (CCClass.VAR_REG.test(propName)) {
propNameLiteral = '"' + propName + '"';
accessor = '.' + propName;
}
else {
if (prop) {
if (CC_DEV) {
self._deserializeObjField(obj, prop, propName, target && obj);
}
else {
self._deserializeObjField(obj, prop, propName);
}
}
else {
obj[propName] = null;
}
propNameLiteral = CCClass.escapeForJS(propName);
accessor = '[' + propNameLiteral + ']';
}
sources.push('prop=d' + accessor + ';');
sources.push('if(typeof prop!=="undefined"){');
sources.push( 'if(typeof prop!=="object"){' +
'o' + accessor + '=prop;');
sources.push( '}else{' +
'if(prop)');
if (CC_DEV) {
sources.push( 's._deserializeObjField(o,prop,' + propNameLiteral + ',t&&o);');
}
else {
sources.push( 's._deserializeObjField(o,prop,' + propNameLiteral + ');');
}
sources.push( 'else o' + accessor + '=null;' +
'}' +
'}');
}
else {
// always load raw objects even if property not serialized
if (self.result.rawProp) {
cc.error('not support multi raw object in a file');
// 这里假定每个asset都有uuid,每个json只能包含一个asset,只能包含一个rawProp
if (CCClass.VAR_REG.test(propName)) {
propNameLiteral = '"' + propName + '"';
}
self.result.rawProp = propName;
else {
propNameLiteral = CCClass.escapeForJS(propName);
}
// always load raw objects even if property not serialized
// 这里假定每个asset都有uuid,每个json只能包含一个asset,只能包含一个rawProp
sources.push('if(s.result.rawProp)\n' +
'cc.error("not support multi raw object in a file");');
sources.push('s.result.rawProp=' + propNameLiteral + ';');
}
}
if (props[props.length - 1] === '_$erialized') {
// deep copy original serialized data
obj._$erialized = JSON.parse(JSON.stringify(serialized));
sources.push('o._$erialized=JSON.parse(JSON.stringify(d));');
// parse the serialized data as primitive javascript object, so its __id__ will be dereferenced
_deserializePrimitiveObject(self, obj._$erialized, serialized);
sources.push('s._deserializePrimitiveObject(o._$erialized,d);');
}
sources.push('})');
return cleanEval(sources.join(''));
}
function _deserializeFireClass (self, obj, serialized, klass, target) {
var deserialize = klass.__deserialize__;
if (!deserialize) {
deserialize = compileDeserialize(self, klass);
// if (CC_TEST && !isPhantomJS) {
// cc.log(deserialize);
// }
Object.defineProperty(klass, '__deserialize__', { value: deserialize, writable: true });
}
deserialize(self, obj, serialized, klass, target);
}
///**
@@ -399,9 +469,19 @@ var _Deserializer = (function () {
obj._deserialize(serialized.content, self);
return obj;
}
if ( cc.Class._isCCClass(klass) ) {
if (cc.Class._isCCClass(klass)) {
_deserializeFireClass(self, obj, serialized, klass, target);
}
else if (type === 'cc.Vec2') {
obj.x = serialized.x;
obj.y = serialized.y;
}
else if (type === 'cc.Color') {
obj.r = serialized.r;
obj.g = serialized.g;
obj.b = serialized.b;
obj.a = serialized.a;
}
else {
_deserializeTypedObject(self, obj, serialized);
}
@@ -411,7 +491,7 @@ var _Deserializer = (function () {
// embedded primitive javascript object
obj = (CC_DEV && target) || {};
_deserializePrimitiveObject(self, obj, serialized);
self._deserializePrimitiveObject(obj, serialized);
}
else {
@@ -437,9 +517,6 @@ var _Deserializer = (function () {
}
else {
obj[i] = prop;
if (CC_DEV && self.result.visit) {
self.result.visit(obj, '' + i);
}
}
}
}