コード例 #1
0
 public JsMethod(IMember csharpMember, string name, IEnumerable<string> typeParameterNames, JsFunctionDefinitionExpression definition)
 {
     CSharpMember       = csharpMember;
     Name               = name;
     TypeParameterNames = typeParameterNames.AsReadOnly();
     Definition         = definition;
 }
コード例 #2
0
ファイル: JsMethod.cs プロジェクト: tml/SaltarelleCompiler
 public JsMethod(IMember csharpMember, string name, IEnumerable<string> typeParameterNames, JsFunctionDefinitionExpression definition)
 {
     Require.ValidJavaScriptIdentifier(name, "name");
     CSharpMember       = csharpMember;
     Name               = name;
     TypeParameterNames = typeParameterNames.AsReadOnly();
     Definition         = definition;
 }
コード例 #3
0
		protected void CompileMethod(string source, IMetadataImporter metadataImporter = null, INamer namer = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, string methodName = "M", bool addSkeleton = true, IEnumerable<IAssemblyReference> references = null, bool allowUserDefinedStructs = false) {
			Compile(new[] { addSkeleton ? "using System; class C { " + source + "}" : source }, metadataImporter: metadataImporter, namer: namer, runtimeLibrary: runtimeLibrary, errorReporter: errorReporter, methodCompiled: (m, res, mc) => {
				if (m.Name == methodName) {
					Method = m;
					MethodCompiler = mc;
					CompiledMethod = res;
				}
			}, references: references, allowUserDefinedStructs: allowUserDefinedStructs);

			Assert.That(Method, Is.Not.Null, "Method " + methodName + " was not compiled");
		}
コード例 #4
0
		protected void Compile(string source, IMetadataImporter metadataImporter = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, bool useFirstConstructor = false) {
			Compile(new[] { source }, metadataImporter: metadataImporter, runtimeLibrary: runtimeLibrary, errorReporter: errorReporter, methodCompiled: (m, res, mc) => {
				if (m.IsConstructor && (m.Attributes.Any() || useFirstConstructor)) {
					Constructor = m;
					MethodCompiler = mc;
					CompiledConstructor = res;
				}
			});

			Assert.That(Constructor, Is.Not.Null, "No constructors with attributes were compiled.");
		}
コード例 #5
0
 public JsNamedConstructor(string name, JsFunctionDefinitionExpression definition)
 {
     Require.ValidJavaScriptIdentifier(name, "name");
     Name       = name;
     Definition = definition;
 }
コード例 #6
0
		private JsFunctionDefinitionExpression StateMachineRewriteAsyncMethod(JsFunctionDefinitionExpression function, bool returnsTask, IType taskGenericArgument) {
			if (DisableStateMachineRewriteTestingUseOnly)
				return function;

			var usedLoopLabels = new HashSet<string>();
			string stateMachineVariable = _namer.GetVariableName(_namer.AsyncStateMachineVariableDesiredName, _usedVariableNames);
			_usedVariableNames.Add(stateMachineVariable);
			string doFinallyBlocksVariable = _namer.GetVariableName(_namer.AsyncDoFinallyBlocksVariableDesiredName, _usedVariableNames);
			_usedVariableNames.Add(doFinallyBlocksVariable);
			string taskCompletionSourceVariable;
			if (returnsTask) {
				taskCompletionSourceVariable = _namer.GetVariableName(_namer.AsyncTaskCompletionSourceVariableDesiredName, _usedVariableNames);
				_usedVariableNames.Add(taskCompletionSourceVariable);
			}
			else {
				taskCompletionSourceVariable = null;
			}

			var body = StateMachineRewriter.RewriteAsyncMethod(function.Body,
			                                                   IsJsExpressionComplexEnoughToGetATemporaryVariable.Analyze,
			                                                   () => { var result = _namer.GetVariableName(null, _usedVariableNames); _usedVariableNames.Add(result); return result; },
			                                                   () => { var result = _namer.GetVariableName(_namer.StateVariableDesiredName, _usedVariableNames); _usedVariableNames.Add(result); return result; },
			                                                   () => { var result = _namer.GetStateMachineLoopLabel(usedLoopLabels); usedLoopLabels.Add(result); return result; },
			                                                   stateMachineVariable,
			                                                   doFinallyBlocksVariable,
			                                                   taskCompletionSourceVariable != null ? JsStatement.Declaration(taskCompletionSourceVariable, _runtimeLibrary.CreateTaskCompletionSource(taskGenericArgument, this)) : null,
			                                                   taskCompletionSourceVariable != null ? expr => _runtimeLibrary.SetAsyncResult(JsExpression.Identifier(taskCompletionSourceVariable), expr, this) : (Func<JsExpression, JsExpression>)null,
			                                                   taskCompletionSourceVariable != null ? expr => _runtimeLibrary.SetAsyncException(JsExpression.Identifier(taskCompletionSourceVariable), expr, this) : (Func<JsExpression, JsExpression>)null,
			                                                   taskCompletionSourceVariable != null ? () => _runtimeLibrary.GetTaskFromTaskCompletionSource(JsExpression.Identifier(taskCompletionSourceVariable), this) : (Func<JsExpression>)null,
			                                                   (f, t) => _runtimeLibrary.Bind(f, t, this));

			return ReferenceEquals(body, function.Body) ? function : JsExpression.FunctionDefinition(function.ParameterNames, body, function.Name);
		}
コード例 #7
0
		private JsFunctionDefinitionExpression StateMachineRewriteIteratorBlock(JsFunctionDefinitionExpression function, bool returnsIEnumerable, IType yieldType) {
			if (DisableStateMachineRewriteTestingUseOnly)
				return function;

			var usedLoopLabels = new HashSet<string>();
			string yieldResultVariable = _namer.GetVariableName(_namer.YieldResultVariableDesiredName, _usedVariableNames);
			_usedVariableNames.Add(yieldResultVariable);
			var body = StateMachineRewriter.RewriteIteratorBlock(function.Body,
			                                                     IsJsExpressionComplexEnoughToGetATemporaryVariable.Analyze,
			                                                     () => { var result = _namer.GetVariableName(null, _usedVariableNames); _usedVariableNames.Add(result); return result; },
			                                                     () => { var result = _namer.GetVariableName(_namer.StateVariableDesiredName, _usedVariableNames); _usedVariableNames.Add(result); return result; },
			                                                     () => { var result = _namer.GetStateMachineLoopLabel(usedLoopLabels); usedLoopLabels.Add(result); return result; },
			                                                     () => { var result = _namer.GetVariableName(_namer.FinallyHandlerDesiredName, _usedVariableNames); _usedVariableNames.Add(result); return result; },
			                                                     x => JsExpression.Assign(JsExpression.Identifier(yieldResultVariable), x),
			                                                     sm => MakeIteratorBody(sm, returnsIEnumerable, yieldType, yieldResultVariable, function.ParameterNames));

			return JsExpression.FunctionDefinition(function.ParameterNames, body, function.Name);
		}
コード例 #8
0
		internal JsFunctionDefinitionExpression StateMachineRewriteNormalMethod(JsFunctionDefinitionExpression function) {
			if (DisableStateMachineRewriteTestingUseOnly)
				return function;

			var usedLoopLabels = new HashSet<string>();
			var body = StateMachineRewriter.RewriteNormalMethod(function.Body,
			                                                    IsJsExpressionComplexEnoughToGetATemporaryVariable.Analyze,
			                                                    () => { var result = _namer.GetVariableName(null, _usedVariableNames); _usedVariableNames.Add(result); return result; },
			                                                    () => { var result = _namer.GetVariableName(_namer.StateVariableDesiredName, _usedVariableNames); _usedVariableNames.Add(result); return result; },
			                                                    () => { var result = _namer.GetStateMachineLoopLabel(usedLoopLabels); usedLoopLabels.Add(result); return result; });

			return ReferenceEquals(body, function.Body) ? function : JsExpression.FunctionDefinition(function.ParameterNames, body, function.Name);
		}
コード例 #9
0
 private void OnMethodCompiled(IMethod method, JsFunctionDefinitionExpression result, MethodCompiler mc)
 {
     if (MethodCompiled != null)
         MethodCompiled(method, result, mc);
 }
コード例 #10
0
        private void AddCompiledConstructorToType(JsClass jsClass, IMethod constructor, ConstructorScriptSemantics options, JsFunctionDefinitionExpression jsConstructor)
        {
            switch (options.Type) {
                case ConstructorScriptSemantics.ImplType.UnnamedConstructor:
                    if (jsClass.UnnamedConstructor != null) {
                        _errorReporter.Region = constructor.Region;
                        _errorReporter.Message(7501, constructor.DeclaringType.FullName);
                    }
                    else {
                        jsClass.UnnamedConstructor = jsConstructor;
                    }
                    break;
                case ConstructorScriptSemantics.ImplType.NamedConstructor:
                    jsClass.NamedConstructors.Add(new JsNamedConstructor(options.Name, jsConstructor));
                    break;

                case ConstructorScriptSemantics.ImplType.StaticMethod:
                    jsClass.StaticMethods.Add(new JsMethod(constructor, options.Name, new string[0], jsConstructor));
                    break;
            }
        }
コード例 #11
0
		public void Setup() {
			Method = null;
			MethodCompiler = null;
			CompiledMethod = null;
		}
コード例 #12
0
 public JsNamedConstructor(string name, JsFunctionDefinitionExpression definition)
 {
     Name       = name;
     Definition = definition;
 }
コード例 #13
0
 public override JsExpression VisitFunctionDefinitionExpression(JsFunctionDefinitionExpression expression, object data)
 {
     return(expression);
 }
コード例 #14
0
 public void Setup()
 {
     Method         = null;
     MethodCompiler = null;
     CompiledMethod = null;
 }
コード例 #15
0
 public override JsExpression VisitFunctionDefinitionExpression(JsFunctionDefinitionExpression expression, IList <string> data)
 {
     return(base.VisitFunctionDefinitionExpression(expression, _result[expression] = new List <string>()));
 }