public Stmt GetImpl(Ctx ctx) { var obj = ctx.MethodParameter(0, "obj"); var toType = ctx.MethodParameter(1, "toType"); var canCastTo = new ExprVarLocal(ctx, ctx.Type.MakeArray()).Named("canCastTo"); var mGetType = ctx.Module.Import(typeof(object).GetMethod("GetType")); var getTypeCall = new ExprCall(ctx, mGetType, obj.Expr).Named("getTypeCall"); var assignableTo = new ExprJsTypeData(ctx, TypeData.AssignableTo).Named("assignableTo"); var i = new ExprVarLocal(ctx, ctx.Int32).Named("i"); var t = new ExprVarLocal(ctx, ctx.Type).Named("temp"); var js = @" if (obj == null) return true; temp = getTypeCall; if (!temp) return false; // Required because C# objects received via JSON may have null type when type is never refered to in JS if (temp === toType) return true; canCastTo = temp.assignableTo; for (i = canCastTo.length - 1; i >= 0; i--) if (canCastTo[i] === toType) return true; return false; "; var stmt = new StmtJsExplicit(ctx, js, obj, toType, canCastTo, getTypeCall, assignableTo, i, t); return(stmt); }
public static Stmt get_FullName(Ctx ctx) { var eNamespace = new ExprJsTypeData(ctx, TypeData.Namespace).Named("namespace"); var eName = new ExprJsTypeData(ctx, TypeData.Name).Named("name"); var stmt = new StmtJsExplicit(ctx, "return this.namespace+\".\"+this.name;", ctx.ThisNamed, eNamespace, eName); return(stmt); }
public static Stmt GetValue(Ctx ctx) { var value = ctx.Local(ctx.Object, "value"); var index = ctx.MethodParameter(0, "index"); var thisType = ctx.Local(ctx.Type, "thisType"); var thisGetTypeCall = new ExprCall(ctx, (Func <Type>)(new object().GetType), ctx.This).Named("thisGetTypeCall"); var eElementType = new ExprJsTypeData(ctx, TypeData.ElementType).Named("eElementType"); var eIsValueType = new ExprJsTypeData(ctx, TypeData.IsValueType).Named("eIsValueType"); var js = @" value = this[index]; thisType = thisGetTypeCall; return thisType.eElementType.eIsValueType ? {v:value, _:thisType.eElementType} : value; "; var stmt = new StmtJsExplicit(ctx, js, ctx.ThisNamed, value, index, thisType, thisGetTypeCall, eElementType, eIsValueType); return(stmt); }
protected override ICode VisitJsTypeData(ExprJsTypeData e) { this.code.Append(e.TypeData); return(e); }
public static Stmt Encode(Ctx ctx) { var arg = ctx.MethodParameter(0, "arg"); var id = ctx.Local(ctx.Int32, "id"); var todo = ctx.Local(ctx.Object, "todo"); var todoOfs = ctx.Local(ctx.Int32, "todoOfs"); var enc = ctx.Local(ctx.Object, "enc"); var obj = ctx.Local(ctx.Object, "obj"); var json = ctx.Local(ctx.Object, "json"); var jsonPart = ctx.Local(ctx.Object, "jsonPart"); var o = ctx.Local(ctx.Object, "o"); var type = ctx.Local(ctx.Type, "type"); var oKey = ctx.Local(ctx.String, "oKey"); var isRoot = ctx.Local(ctx.Boolean, "isRoot"); var i = ctx.Local(ctx.Int32, "i"); var isArray = new ExprJsTypeData(ctx, TypeData.IsArray).Named("isArray"); var jsName = new ExprJsTypeData(ctx, TypeData.JsName).Named("jsName"); var jsIsDict = new ExprJsTypeData(ctx, TypeData.IsDictionary).Named("jsIsDict"); var slots = ctx.Local(ctx.Object, "slots"); var ret = ctx.Local(ctx.Object, "ret"); // Value-types will be boxed on entry var js = @" try { id = 0; if (arg && arg._) { arg.$$ = '0'; } todo = [arg]; todoOfs = 0; enc = function(o, isRoot) { if (o === null || o === undefined) { return null; } var type = o._; if (!type) { // Unboxed value-type/primitive if (typeof(o) !== 'object' || o instanceof Array) { // primitive number, boolean, string, 64-bit number (array) if (typeof(o) === 'number') { // Number if (isNaN(o)) { return [0]; } if (o === Number.NEGATIVE_INFINITY) { return [-1]; } if (o === Number.POSITIVE_INFINITY) { return [1]; } } return o; } else { // Non-primitive value-type var ret = {}; for (var oKey in o) { ret[oKey] = enc(o[oKey]); } return ret; } } if (isRoot) { // Direct object encoding required if (type.jsIsDict) { var ret = { '_': type.jsName, 'v': [] }; var slots = o[type.jsIsDict[0]]; for (var i=0; i<slots.length; i++) { if (slots[i] && slots[i][type.jsIsDict[1]] >= 0) { ret.v.push(enc(slots[i][type.jsIsDict[2]]), enc(slots[i][type.jsIsDict[3]])); } } return ret; } if (type && type.isArray) { var ret = { '_': type.jsName, 'v': [] }; for (var i=0; i<o.length; i++) { ret.v.push(enc(o[i])); } return ret; } else { var ret = { '_': type.jsName }; for (var oKey in o) { if (oKey.charAt(0) !== '_' && oKey.charAt(0) !== '$') { ret[oKey] = enc(o[oKey]); } } return ret; } } else { if (!o.$$) { o.$$ = (++id).toString(); todo.push(o); } return [o.$$]; } }; json = []; while (todo.length > todoOfs) { obj = todo[todoOfs++]; jsonPart = enc(obj, true); json.push([(obj && obj.$$) || '0', jsonPart]); } for (i = 0; i<todo.length; i++) { if (todo[i] && todo[i].$$) { delete todo[i].$$; } } return json; } catch (eeee) { console.log('Ex: ' + eeee); throw eeee; } "; var stmt = new StmtJsExplicit(ctx, js, arg, id, todo, todoOfs, enc, obj, json, jsonPart, o, type, oKey, isRoot, i, isArray, ret, jsName, jsIsDict, slots); return(stmt); }