public static JsFunction CreateConstructor(JsTypeFunction enclosingType, JsFunction implementation) { implementation.memberset(SpecialNames.TypeField, enclosingType); implementation.memberset(SpecialNames.New, Jsni.function(() => { /* * if (!enclosingType.IsPrototypeInitialized) * { * enclosingType.IsPrototypeInitialized = true; * enclosingType.prototype = Jsni.@new(enclosingType.PrototypeFactory.invoke()); * } */ return(Jsni.@new(enclosingType, implementation, Jsni.arguments())); })); return(implementation); }
public static JsFunction CreateDelegate(JsObject thisExpression, JsFunction lambda, JsTypeFunction delegateType = null, string delegateKey = null) { delegateType = delegateType ?? Jsni.reference("System.Delegate").As <JsTypeFunction>(); JsFunction delegateFunc = null; delegateFunc = Jsni.function(() => { return(lambda.apply(delegateFunc.As <Delegate>().Target.As <JsObject>(), Jsni.arguments().As <JsArray>())); }); delegateFunc.prototype = Jsni.@new(delegateType); delegateFunc.memberset("get_Target", Jsni.function(() => thisExpression)); delegateFunc.memberset("GetType", Jsni.function(() => delegateType.GetTypeFromType.invoke())); Jsni.memberset(delegateFunc, SpecialNames.TypeField, delegateType); Jsni.memberset(delegateFunc, "Invoke", delegateFunc); Jsni.memberset(delegateFunc, "DynamicInvoke", Jsni.function(args => delegateFunc.apply(delegateFunc, args.As <JsArray>()))); Jsni.memberset(delegateFunc, "GetHashCode", Jsni.function(() => lambda.toString().GetHashCode().As <JsObject>())); Jsni.memberset(delegateFunc, "lambda", lambda); Jsni.memberset(delegateFunc, "Equals", Jsni.function(x => x != null && lambda == x.member("lambda"))); return(delegateFunc); }
public static JsFunction CreateDelegate(JsObject thisExpression, JsTypeFunction delegateType, JsFunction lambda, string delegateKey = null) { if (delegateKey != null) { if (thisExpression[delegateKey]) { return(thisExpression[delegateKey].As <JsFunction>()); } } else { if (lambda.member("$delegate") != null) { return(lambda.member("$delegate").As <JsFunction>()); } } JsFunction delegateFunc = null; delegateFunc = Jsni.function(() => { return(lambda.apply(delegateFunc.As <Delegate>().Target.As <JsObject>(), Jsni.arguments().As <JsArray>())); }); delegateFunc.prototype = Jsni.@new(delegateType); Jsni.type <object>().TypeInitializer.invoke(delegateFunc, delegateFunc); Jsni.type <Delegate>().TypeInitializer.invoke(delegateFunc, delegateFunc); Jsni.type <MulticastDelegate>().TypeInitializer.invoke(delegateFunc, delegateFunc); delegateType.TypeInitializer.invoke(delegateFunc, delegateFunc); Jsni.invoke(Jsni.member(Jsni.member(Jsni.type <MulticastDelegate>().prototype, "$ctor"), "call"), delegateFunc, thisExpression, new[] { delegateFunc }.As <JsArray>()); Jsni.memberset(delegateFunc, SpecialNames.TypeField, delegateType); if (delegateKey != null) { thisExpression[delegateKey] = delegateFunc; } else { lambda.memberset("$delegate", delegateFunc); } return(delegateFunc); }