示例#1
0
        protected override ICode VisitJsTypeData(ExprJsTypeData e)
        {
            var name = this.resolver.TypeDataNames[e.TypeData];

            this.js.Append(name);
            return(e);
        }
 protected override ICode VisitJsResolvedProperty(ExprJsResolvedProperty e) {
     if (e.Call.CallMethod != null) {
         var ctx = e.Ctx;
         var mDef = e.Call.CallMethod.Resolve();
         if (mDef.IsSetter) {
             var tRef = e.Call.CallMethod.Parameters.Last().ParameterType;
             var tDef = tRef.Resolve();
             var attr = tDef.GetCustomAttribute<JsStringEnumAttribute>();
             if (attr != null) {
                 var prop = new ExprJsResolvedProperty(ctx, new FakeCall(e.Call), e.PropertyName).Named("prop");
                 var type = new ExprJsTypeVarName(ctx, tRef).Named("type");
                 var enumStringMap = new ExprJsTypeData(ctx, TypeData.EnumStringMap).Named("enumStringMap");
                 var value = e.Call.Args.Last().Named("value");
                 var js =  "prop = type.enumStringMap[value]";
                 var expr = new ExprJsExplicit(ctx, js, e.Type, type, enumStringMap, value, prop);
                 return expr;
             }
         } else {
             var tDef = e.Type.Resolve();
             var attr = tDef.GetCustomAttribute<JsStringEnumAttribute>();
             if (attr != null) {
                 var prop = new ExprJsResolvedProperty(ctx, new FakeCall(e.Call), e.PropertyName).Named("prop");
                 var type = new ExprJsTypeVarName(ctx, e.Type).Named("type");
                 var enumStringMap = new ExprJsTypeData(ctx, TypeData.EnumStringMap).Named("enumStringMap");
                 var js = "type.enumStringMap[prop]";
                 var expr = new ExprJsExplicit(ctx, js, e.Type, prop, type, enumStringMap);
                 return expr;
             }
         }
     }
     return base.VisitJsResolvedProperty(e);
 }
示例#3
0
 protected override ICode VisitJsResolvedProperty(ExprJsResolvedProperty e)
 {
     if (e.Call.CallMethod != null)
     {
         var ctx  = e.Ctx;
         var mDef = e.Call.CallMethod.Resolve();
         if (mDef.IsSetter)
         {
             var tRef = e.Call.CallMethod.Parameters.Last().ParameterType;
             var tDef = tRef.Resolve();
             var attr = tDef.GetCustomAttribute <JsStringEnumAttribute>();
             if (attr != null)
             {
                 var prop          = new ExprJsResolvedProperty(ctx, new FakeCall(e.Call), e.PropertyName).Named("prop");
                 var type          = new ExprJsTypeVarName(ctx, tRef).Named("type");
                 var enumStringMap = new ExprJsTypeData(ctx, TypeData.EnumStringMap).Named("enumStringMap");
                 var value         = e.Call.Args.Last().Named("value");
                 var js            = "prop = type.enumStringMap[value]";
                 var expr          = new ExprJsExplicit(ctx, js, e.Type, type, enumStringMap, value, prop);
                 return(expr);
             }
         }
         else
         {
             var tDef = e.Type.Resolve();
             var attr = tDef.GetCustomAttribute <JsStringEnumAttribute>();
             if (attr != null)
             {
                 var prop          = new ExprJsResolvedProperty(ctx, new FakeCall(e.Call), e.PropertyName).Named("prop");
                 var type          = new ExprJsTypeVarName(ctx, e.Type).Named("type");
                 var enumStringMap = new ExprJsTypeData(ctx, TypeData.EnumStringMap).Named("enumStringMap");
                 var js            = "type.enumStringMap[prop]";
                 var expr          = new ExprJsExplicit(ctx, js, e.Type, prop, type, enumStringMap);
                 return(expr);
             }
         }
     }
     return(base.VisitJsResolvedProperty(e));
 }
 protected virtual ICode VisitJsTypeData(ExprJsTypeData e) {
     this.ThrowOnNoOverride();
     return e;
 }
 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;
 }
示例#6
0
 protected override ICode VisitJsTypeData(ExprJsTypeData e) {
     var name = this.resolver.TypeDataNames[e.TypeData];
     this.js.Append(name);
     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;
        }
 protected override ICode VisitJsTypeData(ExprJsTypeData e) {
     this.code.Append(e.TypeData);
     return e;
 }
 protected virtual ICode VisitJsTypeData(ExprJsTypeData e)
 {
     this.ThrowOnNoOverride();
     return(e);
 }