diff options
Diffstat (limited to 'node_modules/montage/core/deserializer.js')
-rwxr-xr-x | node_modules/montage/core/deserializer.js | 115 |
1 files changed, 96 insertions, 19 deletions
diff --git a/node_modules/montage/core/deserializer.js b/node_modules/montage/core/deserializer.js index 0abc924b..7e812235 100755 --- a/node_modules/montage/core/deserializer.js +++ b/node_modules/montage/core/deserializer.js | |||
@@ -18,6 +18,14 @@ var Montage = require("montage").Montage, | |||
18 | // By rebinding eval to a new name, it loses its ability to | 18 | // By rebinding eval to a new name, it loses its ability to |
19 | // capture the calling scope. | 19 | // capture the calling scope. |
20 | var globalEval = eval; | 20 | var globalEval = eval; |
21 | var canEval = true; | ||
22 | |||
23 | // CSP doesn't let you eval | ||
24 | try { | ||
25 | eval(""); | ||
26 | } catch(ex) { | ||
27 | canEval = false; | ||
28 | } | ||
21 | 29 | ||
22 | /** | 30 | /** |
23 | @class module:montage/core/deserializer.Deserializer | 31 | @class module:montage/core/deserializer.Deserializer |
@@ -81,6 +89,7 @@ var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deseri | |||
81 | _reset: {value: function() { | 89 | _reset: {value: function() { |
82 | this._serializationString = null; | 90 | this._serializationString = null; |
83 | this._requiredModuleIds = null; | 91 | this._requiredModuleIds = null; |
92 | this._areModulesLoaded = false; | ||
84 | this._parseFunction = null; | 93 | this._parseFunction = null; |
85 | this._serialization = null; | 94 | this._serialization = null; |
86 | this._compiledDeserializationFunction = null; | 95 | this._compiledDeserializationFunction = null; |
@@ -342,7 +351,12 @@ var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deseri | |||
342 | this._parseForModules(); | 351 | this._parseForModules(); |
343 | } | 352 | } |
344 | 353 | ||
345 | this._loadModules(this._requiredModuleIds, callback); | 354 | if (this._requiredModuleIds.length > 0) { |
355 | this._loadModules(this._requiredModuleIds, callback); | ||
356 | } else { | ||
357 | this._areModulesLoaded = true; | ||
358 | return callback(); | ||
359 | } | ||
346 | }}, | 360 | }}, |
347 | 361 | ||
348 | /** | 362 | /** |
@@ -356,16 +370,40 @@ var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deseri | |||
356 | /** | 370 | /** |
357 | @private | 371 | @private |
358 | */ | 372 | */ |
373 | _findObjectNameRegExp: { | ||
374 | value: /([^\/]+?)(\.reel)?$/ | ||
375 | }, | ||
376 | _toCamelCaseRegExp: { | ||
377 | value: /(?:^|-)([^-])/g | ||
378 | }, | ||
379 | _replaceToCamelCase: { | ||
380 | value: function(_, g1) { return g1.toUpperCase() } | ||
381 | }, | ||
359 | _parseForModules: {value: function() { | 382 | _parseForModules: {value: function() { |
360 | var serialization = this._serialization, | 383 | var serialization = this._serialization, |
361 | moduleIds = this._requiredModuleIds = [], | 384 | moduleIds = this._requiredModuleIds = [], |
362 | modules = this._modules; | 385 | modules = this._modules, |
386 | desc, moduleId; | ||
363 | 387 | ||
364 | for (var label in serialization) { | 388 | for (var label in serialization) { |
365 | var desc = serialization[label]; | 389 | desc = serialization[label]; |
366 | var moduleId = desc.module; | 390 | moduleId = null; |
391 | |||
392 | if ("module" in desc) { | ||
393 | moduleId = desc.module; | ||
394 | } else if (name = /*assignment*/(desc.prototype || desc.object)) { | ||
395 | objectLocation = name.split("["); | ||
396 | moduleId = objectLocation[0]; | ||
397 | desc.module = moduleId; | ||
398 | if (objectLocation.length == 2) { | ||
399 | desc.name = objectLocation[1].slice(0, -1); | ||
400 | } else { | ||
401 | this._findObjectNameRegExp.test(moduleId); | ||
402 | desc.name = RegExp.$1.replace(this._toCamelCaseRegExp, this._replaceToCamelCase); | ||
403 | } | ||
404 | } | ||
367 | 405 | ||
368 | if (moduleId && moduleIds.indexOf(moduleId) == -1 && !modules[moduleId]) { | 406 | if (moduleId && !modules[moduleId] && moduleIds.indexOf(moduleId) == -1) { |
369 | moduleIds.push(moduleId); | 407 | moduleIds.push(moduleId); |
370 | } | 408 | } |
371 | } | 409 | } |
@@ -404,7 +442,7 @@ var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deseri | |||
404 | */ | 442 | */ |
405 | _compileAndDeserialize: {value: function(element, deserialize) { | 443 | _compileAndDeserialize: {value: function(element, deserialize) { |
406 | var self = this, | 444 | var self = this, |
407 | serialization = this._serialization, | 445 | serialization, |
408 | exportsStrings = "", | 446 | exportsStrings = "", |
409 | unitsStrings = "", | 447 | unitsStrings = "", |
410 | objectsStrings = "", | 448 | objectsStrings = "", |
@@ -418,6 +456,12 @@ var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deseri | |||
418 | objectNamesCounter = {}, | 456 | objectNamesCounter = {}, |
419 | label; | 457 | label; |
420 | 458 | ||
459 | if (canEval) { | ||
460 | serialization = this._serialization; | ||
461 | } else { | ||
462 | serialization = JSON.parse(this._serializationString); | ||
463 | } | ||
464 | |||
421 | for (label in serialization) { | 465 | for (label in serialization) { |
422 | var objectDesc = serialization[label]; | 466 | var objectDesc = serialization[label]; |
423 | 467 | ||
@@ -452,28 +496,56 @@ var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deseri | |||
452 | } | 496 | } |
453 | } | 497 | } |
454 | 498 | ||
455 | this._compiledDeserializationFunctionString = "(function() {\n" + requireStrings.join("\n") + "\nreturn function(element) {\nvar exports = {};\n" + exportsStrings + "\n\n" + objectsStrings + "\n\n" + unitsStrings + "\n\n" + cleanupStrings + "\nreturn exports;\n}}).call(this)"; | 499 | if (canEval) { |
500 | this._compiledDeserializationFunctionString = "(function() {\n" + requireStrings.join("\n") + "\nreturn function(element) {\nvar exports = {};\n" + exportsStrings + "\n\n" + objectsStrings + "\n\n" + unitsStrings + "\n\n" + cleanupStrings + "\nreturn exports;\n}}).call(this)"; | ||
501 | this._serializationString = this._serialization = serialization = null; | ||
502 | } | ||
503 | |||
456 | if (logger.isDebug) { | 504 | if (logger.isDebug) { |
457 | logger.debug(this._compiledDeserializationFunctionString); | 505 | logger.debug(this._compiledDeserializationFunctionString); |
458 | } | 506 | } |
459 | 507 | ||
460 | this._serialization = serialization = null; | ||
461 | |||
462 | return exports; | 508 | return exports; |
463 | 509 | ||
464 | function deserializeObject(label, desc) { | 510 | function deserializeObject(label, desc) { |
465 | var moduleId = desc.module, | 511 | var moduleId, |
466 | name = desc.name, | 512 | name, |
467 | objectName = name, | 513 | instance, |
468 | fqn = moduleId + "." + name, | 514 | objectName, |
515 | fqn, | ||
469 | properties = desc.properties, | 516 | properties = desc.properties, |
517 | isType, | ||
470 | object, | 518 | object, |
471 | counter, | 519 | counter, |
472 | propertiesString; | 520 | propertiesString, |
521 | objectLocation; | ||
522 | |||
523 | if ("module" in desc) { | ||
524 | moduleId = desc.module; | ||
525 | objectName = name = desc.name; | ||
526 | } else { | ||
527 | objectLocation = (desc.prototype || desc.object).split("["); | ||
528 | // this code is actually only used when canEval == false, | ||
529 | // module+name are added when the modules are parsed but it's | ||
530 | // slow to redo the _serializationString in order to keep the | ||
531 | // added module+name when we do JSON.parse(_serializationString) | ||
532 | // at canEval == false. | ||
533 | moduleId = objectLocation[0]; | ||
534 | if (objectLocation.length == 2) { | ||
535 | objectName = name = objectLocation[1].slice(0, -1); | ||
536 | } else { | ||
537 | self._findObjectNameRegExp.test(moduleId); | ||
538 | objectName = name = RegExp.$1.replace(self._toCamelCaseRegExp, function(_, g1) { return g1.toUpperCase() }); | ||
539 | } | ||
540 | } | ||
541 | isType = "object" in desc; | ||
542 | fqn = moduleId + "." + name; | ||
473 | 543 | ||
474 | if (deserialize) { | 544 | if (deserialize) { |
475 | if (self._objectLabels[label]) { | 545 | if (self._objectLabels[label]) { |
476 | exports[label] = object = self._objectLabels[label]; | 546 | exports[label] = object = self._objectLabels[label]; |
547 | } else if (isType) { | ||
548 | exports[label] = object = modules[moduleId][name]; | ||
477 | } else { | 549 | } else { |
478 | if (!(name in modules[moduleId])) { | 550 | if (!(name in modules[moduleId])) { |
479 | console.log("Warning: Object \"" + name + "\" not found at \"" + moduleId + "\" referenced from " + self._origin + "."); | 551 | console.log("Warning: Object \"" + name + "\" not found at \"" + moduleId + "\" referenced from " + self._origin + "."); |
@@ -492,7 +564,7 @@ var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deseri | |||
492 | } | 564 | } |
493 | 565 | ||
494 | if (fqn in requireStrings) { | 566 | if (fqn in requireStrings) { |
495 | name = requireStrings[fqn]; | 567 | objectName = requireStrings[fqn]; |
496 | } else { | 568 | } else { |
497 | counter = (objectNamesCounter[name] || 0) + 1; | 569 | counter = (objectNamesCounter[name] || 0) + 1; |
498 | objectNamesCounter[name] = counter; | 570 | objectNamesCounter[name] = counter; |
@@ -504,11 +576,15 @@ var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deseri | |||
504 | } | 576 | } |
505 | 577 | ||
506 | exportsStrings += 'if (this._objectLabels["' + label + '"]) {\n'; | 578 | exportsStrings += 'if (this._objectLabels["' + label + '"]) {\n'; |
507 | exportsStrings += ' var ' + label + ' = exports. ' + label + ' = this._objectLabels["' + label + '"]\n'; | 579 | exportsStrings += ' var ' + label + ' = exports.' + label + ' = this._objectLabels["' + label + '"]\n'; |
508 | exportsStrings += '} else {\n'; | 580 | exportsStrings += '} else {\n'; |
509 | exportsStrings += ' var ' + label + ' = exports. ' + label + ' = ' + objectName + '.create();\n'; | 581 | if (isType) { |
510 | exportsStrings += ' Montage.getInfoForObject(' + label + ').label = "' + label + '";\n'; | 582 | exportsStrings += ' var ' + label + ' = exports.' + label + ' = ' + objectName + ';\n'; |
511 | exportsStrings += ' Object.defineProperty(' + label + ', "_suuid", {enumerable: false, value: "' + self.uuid + '-' + label + '"});\n'; | 583 | } else { |
584 | exportsStrings += ' var ' + label + ' = exports.' + label + ' = ' + objectName + '.create();\n'; | ||
585 | exportsStrings += ' Montage.get |