private void FillCallArgs(IntPtr ctx) { foreach (Arg a in _args) { Type t = a.arg.GetType(); if (a.type == ARG_TYPE.OBJECT) { Native.duv_push_ref(ctx, a.iref); Bind.PushDynCsObject(ctx, a.arg, -1); Native.duk_remove(ctx, -2); } else if (a.type == ARG_TYPE.CSOBJECT) { Bind.PushCsObject(ctx, a.arg, a.typeName); } else if (t.IsEnum || t.Name == "Int32") { Uts.Native.duk_push_int(ctx, Convert.ToInt32(a.arg)); } else if (t.Name == "UInt32") { Uts.Native.duk_push_uint(ctx, Convert.ToUInt32(a.arg)); } else if (t.Name == "Float" || t.Name == "Double") { Uts.Native.duk_push_number(ctx, Convert.ToDouble(a.arg)); } else if (t.Name == "Boolean") { Uts.Native.duk_push_boolean(ctx, Convert.ToBoolean(a.arg) ? 1 : 0); } else if (t.Name == "String") { Uts.Native.duk_push_string(ctx, Convert.ToString(a.arg)); } else { Uts.Native.duk_push_undefined(ctx); } } }
static public int __as(IntPtr ctx) { try { // read params IntPtr optr = Native.duk_get_heapptr(ctx, 0); object o = BindObjectsMgr.GetCsObject(optr); string sretType = Native.duk_require_string_s(ctx, 1); // stack.pos2 is tsToTypeClass Type retType = Type.GetType(sretType); if (retType == null) { throw new Exception(string.Format("Not find class {0} type when as op.", sretType)); } BindObjectsMgr.RemoveByCsObject(o); Bind.PushDynCsObject(ctx, Convert.ChangeType(o, retType), 2); return(1); } catch (Exception e) { return(Native.duk_throw_error(ctx, Convert.ToString(e))); } }
static private int pushRetVal(IntPtr ctx, object ret, RET_TYPE retType, string member, Type type, int tsTypePos) { Type t = ret.GetType(); if (retType == RET_TYPE.CLASSOBJECT) { Bind.PushDynCsObject(ctx, ret, tsTypePos); } else if (t.IsEnum || t.Name == "Int32") { Native.duk_push_int(ctx, Convert.ToInt32(ret)); } else if (t.Name == "String") { Native.duk_push_string(ctx, Convert.ToString(ret)); } else { throw new Exception(string.Format("Return value is invalid! attr<{0}> in {1} return type is {2}.", member, type.Name, t.Name)); } return(1); }
static public int __set(IntPtr ctx) { try { // read params object csObject = Bind.RequireThis(ctx); string member = Native.duk_require_string_s(ctx, 0); Type type = csObject.GetType(); FieldInfo finfo = null; PropertyInfo prop = type.GetProperty(member); if (prop == null) { finfo = type.GetField(member); } if (finfo == null && prop == null) { throw new Exception(string.Format("Attr<{0}> not in {1} !", member, type.Name)); } Type valType = prop != null?prop.GetType() : finfo.GetType(); object value = null; fillArg(ctx, 1, valType, out value); if (prop != null) { prop.SetValue(csObject, value, null); } else { finfo.SetValue(csObject, value); } return(0); } catch (Exception e) { return(Native.duk_throw_error(ctx, e.ToString())); } }