public JSValue(IServiceNode parentNode, JavaScriptValue value) : base(parentNode, "JSValue") { ReferenceValue = value; ServiceNode.PushService <ICallContextService>(new CallContextService(value)); //inject service Binding = new JSValueBinding(ServiceNode, value);//binding will create a branch of current service node to persistent hold all delegates created by binding function }
public static void RegisterProxyConverter <T>(this IJSValueConverterService service, Action <JSValueBinding, T, IServiceNode> createBinding) where T : class { toJSValueDelegate <T> tojs = (IServiceNode node, T value) => { return(node.GetService <IContextSwitchService>().With(() => { var result = JavaScriptValue.CreateExternalObject(IntPtr.Zero, null); JSValueBinding binding = new JSValueBinding(node, result); var handle = node.GetService <IGCSyncService>().SyncWithJsValue(value, result); result.ExternalData = GCHandle.ToIntPtr(handle);//save the object reference to javascript values's external data createBinding?.Invoke(binding, value, node); return result; })); }; fromJSValueDelegate <T> fromjs = (IServiceNode node, JavaScriptValue value) => { if (value.HasExternalData) { GCHandle handle = GCHandle.FromIntPtr(value.ExternalData); return(handle.Target as T); } else { throw new ArgumentOutOfRangeException("Convert from jsValue to proxy object failed, jsValue does not have a linked CLR object"); } }; service.RegisterConverter <T>(tojs, fromjs); }
public JSValue(IServiceNode parentNode, JavaScriptValue value) : base(parentNode, "JSValue") { ReferenceValue = value; ServiceNode.PushService <ICallContextService>(new CallContextService(value)); //inject service Binding = new JSValueBinding(ServiceNode, value);//binding will create a branch of current service node to persistent hold all delegates created by binding function //add my own one time delegate handler for method/function call ServiceNode.PushService <INativeFunctionHolderService>(new NativeFunctionHolderService(true)); }
public JavaScriptValue Map <T>(T obj, Action <JSValueBinding, T, IServiceNode> createBinding) where T : class { JavaScriptValue output; var list = getMapList <T>(true); if (list.objList.ContainsKey(obj)) { return(list.objList[obj]);//object already mapped, return cached object } output = runtimeService.InternalContextSwitchService.With <JavaScriptValue>(() => { var result = JavaScriptValue.CreateExternalObject(IntPtr.Zero, null);//do not handle the external GC event as the value will be hold until user dispose current service result.AddRef(); return(result); }); JSValueBinding binding = new JSValueBinding(serviceNode, output); createBinding?.Invoke(binding, obj, serviceNode); list.Add(obj, output, binding); return(output); }
public void Add(T obj, JavaScriptValue value, JSValueBinding binding) { jsList.Add(value, new Tuple <T, JSValueBinding>(obj, binding)); objList.Add(obj, value); }