public JsMethod(IMember csharpMember, string name, IEnumerable<string> typeParameterNames, JsFunctionDefinitionExpression definition) { CSharpMember = csharpMember; Name = name; TypeParameterNames = typeParameterNames.AsReadOnly(); Definition = definition; }
public JsMethod(IMember csharpMember, string name, IEnumerable<string> typeParameterNames, JsFunctionDefinitionExpression definition) { Require.ValidJavaScriptIdentifier(name, "name"); CSharpMember = csharpMember; Name = name; TypeParameterNames = typeParameterNames.AsReadOnly(); Definition = definition; }
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"); }
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."); }
public JsNamedConstructor(string name, JsFunctionDefinitionExpression definition) { Require.ValidJavaScriptIdentifier(name, "name"); Name = name; Definition = definition; }
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); }
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); }
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); }
private void OnMethodCompiled(IMethod method, JsFunctionDefinitionExpression result, MethodCompiler mc) { if (MethodCompiled != null) MethodCompiled(method, result, mc); }
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; } }
public void Setup() { Method = null; MethodCompiler = null; CompiledMethod = null; }
public JsNamedConstructor(string name, JsFunctionDefinitionExpression definition) { Name = name; Definition = definition; }
public override JsExpression VisitFunctionDefinitionExpression(JsFunctionDefinitionExpression expression, object data) { return(expression); }
public void Setup() { Method = null; MethodCompiler = null; CompiledMethod = null; }
public override JsExpression VisitFunctionDefinitionExpression(JsFunctionDefinitionExpression expression, IList <string> data) { return(base.VisitFunctionDefinitionExpression(expression, _result[expression] = new List <string>())); }