static public int ctor_DynBind(IntPtr ctx) { try { if (Native.duk_is_string(ctx, 0) != 1) { return(0); // use default ctor: _super.apply(this, arguments); } string className = Native.duk_require_string_s(ctx, 0); int fromStackPos = 1; Type type = Type.GetType(className); if (type == null) { throw new Exception(string.Format("Not find class {0} type in ctor.", className)); } ConstructorInfo ctor = getConstructor(ctx, fromStackPos, type.GetConstructors(BindingFlags.Instance | BindingFlags.Public)); if (ctor == null) { throw new Exception(string.Format("Not find ctor in {0} !", type.Name)); } // handle args object[] args; fillArgs(ctx, fromStackPos, ctor.GetParameters(), out args); object csObject = ctor.Invoke(args); IntPtr ptr = IntPtr.Zero; ptr = Native.duk_require_this(ctx); BindObjectsMgr.Bind(ptr, csObject); return(0); } catch (Exception e) { return(Native.duk_throw_error(ctx, e.ToString())); } }
public static Object RequireThis(IntPtr ctx) { Native.duk_push_this(ctx); IntPtr ptr = Native.duk_get_heapptr(ctx, -1); Native.duk_pop(ctx); return(BindObjectsMgr.GetCsObject(ptr)); }
public static void PushDynCsObject(IntPtr ctx, object csObject, int tsTypeInStackPos) { IntPtr ptr = BindObjectsMgr.GetTsObjectPtr(csObject); if (ptr == IntPtr.Zero) { Uts.Native.duk_dup(ctx, tsTypeInStackPos); // stack: type clor Uts.Native.duk_push_boolean(ctx, 0); // not create cs class object, stack: type clor, isCreate(false) Uts.Native.duk_new(ctx, 1); // stack: object SetFinalizer(ctx); ptr = Native.duk_get_heapptr(ctx, -1); BindObjectsMgr.Bind(ptr, csObject); } else { Native.duk_push_heapptr(ctx, ptr); } }
static private int fillArg(IntPtr ctx, int pos, Type argType, out object arg) { arg = null; DUK_TYPE tsType = (DUK_TYPE)Native.duk_get_type(ctx, pos); if (tsType == DUK_TYPE.NUMBER) { arg = Native.duk_require_int(ctx, pos++); } else if (tsType == DUK_TYPE.STRING) { arg = Native.duk_require_string_s(ctx, pos++); } else if (tsType == DUK_TYPE.OBJECT) { IntPtr ptr = Native.duk_get_heapptr(ctx, pos++); arg = BindObjectsMgr.GetCsObject(ptr); // read object type ARG_TYPE rtType = (ARG_TYPE)Native.duk_require_int(ctx, pos++); if (rtType == ARG_TYPE.OBJECT) { if (arg == null) { throw new Exception("Can not find arg in bindmgr!"); } } else if (rtType == ARG_TYPE.CALLBACK) { int argsCount = Native.duk_require_int(ctx, pos++); Native.duk_push_heapptr(ctx, ptr); int funref = Native.duv_ref(ctx); Context context = Engine.GetContent(ctx); if (context == null) { throw new Exception("Get context failed!"); } TsDelegate td = makeCallBack(context, funref, pos, argsCount); arg = Delegate.CreateDelegate(argType, td, "Deleg", true, true); pos += argsCount; } } return(pos); }
public static void PushCsObject(IntPtr ctx, object csObject, string typeName) { IntPtr ptr = BindObjectsMgr.GetTsObjectPtr(csObject); if (ptr == IntPtr.Zero) { Native.duk_push_object(ctx); // stack : object PushProtoType(ctx, typeName); // stack : object, protoType Native.duk_set_prototype(ctx, -2); // stack : object SetFinalizer(ctx); ptr = Native.duk_get_heapptr(ctx, -1); BindObjectsMgr.Bind(ptr, csObject); } else { Native.duk_push_heapptr(ctx, ptr); } }
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))); } }