2015-11-06 17:34:07 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {isPresent, global, StringWrapper} from 'angular2/src/facade/lang';
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-28 11:45:39 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								var evalCounter = 0;
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-01 10:07:49 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								var MODULE_URL_REGEX = /^package:(.*)\.js/;
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-28 11:45:39 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-14 15:59:09 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								function nextModuleId() {
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-28 11:45:39 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return `evalScript${evalCounter++}`;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-14 15:59:09 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								export function evalModule(moduleSource: string, imports: string[][], args: any[]): Promise<any> {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var moduleId = nextModuleId();
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-28 11:45:39 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var moduleSourceWithImports = [];
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-01 10:07:49 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  var importModuleIds = [];
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-14 15:59:09 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  imports.forEach(sourceImport => {
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-01 10:07:49 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    var modUrl = sourceImport[0];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    var modMatch = MODULE_URL_REGEX.exec(modUrl);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if (!modMatch) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      throw new Error(`Module url ${modUrl} does not match the pattern ${MODULE_URL_REGEX}`);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    var modId = modMatch[1];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-28 11:45:39 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    var modAlias = sourceImport[1];
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-01 10:07:49 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    importModuleIds.push(modId);
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-02 20:58:02 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    // Note: After transpilation to commonJS and loading this file in a browser
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // using SystemJS, the loader might get confused by the presence of require,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // and attempt to load "+ modName +.js" !?!
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // A simple string concat manages to prevent that, but that is one compiler
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // optimaztion away from breaking again. Proceed with caution!
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-01 10:07:49 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    moduleSourceWithImports.push(`var ${modAlias} = require` + `('${modId}');`);
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-28 11:45:39 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  moduleSourceWithImports.push(moduleSource);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var moduleBody = new Function('require', 'exports', 'module', moduleSourceWithImports.join('\n'));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var System = global['System'];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  if (isPresent(System) && isPresent(System.registerDynamic)) {
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-01 10:07:49 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    System.registerDynamic(moduleId, importModuleIds, false, moduleBody);
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-14 15:59:09 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    return <Promise<any>>System.import(moduleId).then((module) => module.run(args));
							 | 
						
					
						
							
								
									
										
										
										
											2015-08-28 11:45:39 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  } else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    var exports = {};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    moduleBody(require, exports, {});
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return PromiseWrapper.resolve(exports['run'](args));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |