public static Expr Join(ICall call) { var ctx = call.Ctx; var separator = call.Arg(0, "separator"); var values = call.Arg(1); var sepExpr = new ExprJsExplicit(ctx, "separator || \"\"", ctx.String, separator); var expr = new ExprJsResolvedMethod(ctx, ctx.String, values, "join", sepExpr); return(expr); }
public static Stmt get_Value(Ctx ctx) { var excCtor = ctx.Module.Import(typeof(InvalidOperationException).GetConstructor(Type.EmptyTypes)); var invalidOperationException = new ExprNewObj(ctx, excCtor); var throwInvalidOperationException = new StmtThrow(ctx, invalidOperationException); var cond = new ExprJsExplicit(ctx, "(this === null)", ctx.Boolean, ctx.ThisNamed); var throwIf = new StmtIf(ctx, cond, throwInvalidOperationException, null); var ret = new StmtReturn(ctx, ctx.This); var stmt = new StmtBlock(ctx, throwIf, ret); return(stmt); }
public static Expr SortComparer(ICall call) { var ctx = call.Ctx; var t = call.CallMethod.DeclaringType.GetGenericArgument(0); var tComparer = call.CallMethod.Parameters.First().ParameterType.FullResolve(call.CallMethod); var mGenSort = ((Action <T[], IComparer <T> >)Array.Sort <T>).Method.GetGenericMethodDefinition(); var mSort = ctx.Module.Import(mGenSort).MakeGeneric(t); var comparer = call.Arg(0, "comparer"); var mDefaultComparerNet = typeof(Comparer <T>).GetProperty("Default").GetMethod; var mDefaultComparer = ctx.Module.Import(mDefaultComparerNet); var defaultComparerCall = new ExprCall(ctx, mDefaultComparer, null).Named("defaultComparerCall"); var ensureComparer = new ExprJsExplicit(ctx, "(comparer || defaultComparerCall)", tComparer, comparer, defaultComparerCall); return(new ExprCall(ctx, mSort, null, GetNamedArrayField(call).Expr, ensureComparer)); }
public Stmt GetImpl(Ctx ctx) { // Recursively deep-copy a value-type var unmappedType = ((GenericInstanceMethod)ctx.MRef).GenericArguments[0]; if (unmappedType.IsNullable()) { var innerType = unmappedType.GetNullableInnerType(); var dc = ValueTypeDeepCopyIfRequired(innerType, () => ctx.MethodParameter(0)); var a = ctx.MethodParameter(0, "a"); if (dc == null) { return(new StmtJsExplicit(ctx, "return a;", a)); } else { var copy = dc.Named("copy"); var js = "return a ? copy : a;"; return(new StmtJsExplicit(ctx, js, a, copy)); } } else { var type = JsResolver.TypeMap(unmappedType) ?? unmappedType; var fields = type.EnumResolvedFields().Where(x => !x.Resolve().IsStatic).ToArray(); var o = ctx.MethodParameter(0, "o"); var copy = string.Join(",", fields.Select((x, i) => string.Format("f{0}:v{0}", i))); var fieldNames = fields.Select((x, i) => new ExprJsFieldVarName(ctx, x).Named("f" + i)).ToArray(); var fieldValues = fields.Select((x, i) => { var fieldType = x.FieldType.FullResolve(x); var fieldAccess = new ExprJsExplicit(ctx, "o.field", fieldType, o, new ExprJsFieldVarName(ctx, x).Named("field")); var dc = ValueTypeDeepCopyIfRequired(fieldType, () => fieldAccess); return((dc ?? fieldAccess).Named("v" + i)); }).ToArray(); var js = "return o._ ? o : {" + copy + "}"; return(new StmtJsExplicit(ctx, js, fieldNames.Concat(fieldValues).Concat(o))); } }
protected override ICode VisitJsExplicit(ExprJsExplicit e) { this.code.Append(e.JavaScript); return(e); }