Exemplo n.º 1
0
        public static JsDynamicObject HelloDotnetFunction(JsValue[] args)
        {
            var host = NodeHost.Instance;

            args.Length.Should().Be(4);

            host.TryGetObject(args[0], typeof(bool), out var oneBool);
            oneBool.Should().Be(true);

            host.TryGetObject(args[1], typeof(double), out var oneNumber);
            oneNumber.Should().Be(1e3);

            host.TryGetObject(args[2], typeof(string), out var oneString);
            oneString.Should().Be("oneString");

            host.TryGetObject(args[3], typeof(Object), out dynamic oneObj);
            // oneObj.Should().BeOfType(typeof(JsDynamicObject));
            ((string)oneObj.message).Should().Be("oneMessageStringInObj");

            var result = host.New();

            host.SetMember(result, "argCount", DotNetValue.FromInt(args.Length));
            host.SetMember(result, "message", DotNetValue.FromString("oneMessageStringInResultObj"));

            return(result);
        }
Exemplo n.º 2
0
        // Convert handles to primitives can be done in managed code based on JsType
        // ATTENTION: 32bit node exists :(

        // Set a member
        public void SetMember(JsValue ownerHandle, string name, DotNetValue value)
        {
            CheckInContext();
            var result = NativeMethods.SetMember(_context, ownerHandle, name, value);

            result.ThrowError(this);
        }
Exemplo n.º 3
0
        public static JsDynamicObject setMainTcs(JsValue[] args)
        {
            Console.WriteLine($"Basic example setMainTcs invoked w/o {args.Length} arguments");

            var host = NodeHost.Instance;

            args.Length.Should().Be(4);

            host.TryGetObject(args[0], typeof(bool), out var oneBool);
            oneBool.Should().Be(true);

            host.TryGetObject(args[1], typeof(double), out var oneNumber);
            oneNumber.Should().Be(1e3);

            host.TryGetObject(args[2], typeof(string), out var oneString);
            oneString.Should().Be("oneString");

            host.TryGetObject(args[3], typeof(Object), out dynamic oneObj);
            // oneObj.Should().BeOfType(typeof(JsDynamicObject));
            ((string)oneObj.message).Should().Be("oneMessageStringInObj");

            var result = host.New();

            host.SetMember(result, "argCount", DotNetValue.FromInt(args.Length));
            host.SetMember(result, "message", DotNetValue.FromString("oneMessageStringInResultObj"));

            MainTaskCompletionSource.SetResult(0);

            return(result);
        }
Exemplo n.º 4
0
        internal static DotNetValue RunHostedApplication(IntPtr context,
                                                         NativeApi nativeMethods,
                                                         int argc,
                                                         [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 2)]
                                                         string[] argv)
        {
            var host = new NativeNodeHost(context, nativeMethods);

            NodeHost.Instance = new NodeBridge(host);

            try
            {
                var assembly_path = argv[0];
                var assembly      = Assembly.Load(Path.GetFileNameWithoutExtension(assembly_path));
                var entryPoint    = assembly.EntryPoint;
                if (entryPoint.IsSpecialName && entryPoint.Name.StartsWith("<") && entryPoint.Name.EndsWith(">"))
                {
                    entryPoint = entryPoint.DeclaringType.GetMethod(entryPoint.Name.Substring(1, entryPoint.Name.Length - 2),
                                                                    BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                }

                var result = host.Scheduler
                             .RunCallbackSynchronously(s =>
                                                       entryPoint.Invoke(null,
                                                                         new object[]
                {
                    argv.Skip(1).ToArray()
                }),
                                                       null);

                return(DotNetValue.FromObject(result, host));
            }
            catch (TargetInvocationException tie)
            {
                return(DotNetValue.FromObject(tie.InnerException, host));
            }
            catch (Exception e)
            {
                return(DotNetValue.FromObject(e, host));
            }
        }
Exemplo n.º 5
0
            private DotNetValue OnCalled(IntPtr deferred)
            {
                try
                {
                    if (_task.IsCompleted)
                    {
                        var exception = UnwrapAggregateException(_task.Exception);
                        var value     = exception == null
                                        ? DotNetValue.FromObject(GetResult(_task), _parent)
                                        : DotNetValue.FromException(exception);

                        _parent.NativeMethods.CompletePromise(_parent._context, deferred, value);
                    }
                    else
                    {
                        _task.ContinueWith(t =>
                        {
                            var exception = UnwrapAggregateException(t.Exception);
                            var value     = exception == null
                                                               ? DotNetValue.FromObject(GetResult(t), _parent)
                                                               : DotNetValue.FromException(exception);
                            _parent.NativeMethods.CompletePromise(_parent._context, deferred, value);
                        },
                                           _parent._scheduler);
                    }

                    return(new DotNetValue {
                        Type = DotNetType.Null, Value = IntPtr.Zero, ReleaseFunc = null
                    });
                }
                catch (Exception e)
                {
                    return(DotNetValue.FromException(e));
                }
                finally
                {
                    _parent._taskRegistry.Remove(CallbackPtr);
                }
            }
Exemplo n.º 6
0
            private void OnCalled(int argc, JsValue[] argv, out DotNetValue result)
            {
                System.Diagnostics.Debug.Assert(argc == (argv?.Length ?? 0), "Marshalling is broken");

                try
                {
                    result = (DotNetValue)_parent._scheduler.RunCallbackSynchronously(
                        state => Wrapped((JsValue[])state),
                        argv ?? new JsValue[0]);
                }
                catch (Exception exception)
                {
                    if (exception is AggregateException aggregateException)
                    {
                        exception = UnwrapAggregateException(aggregateException);
                    }
                    if (exception is TargetInvocationException targetInvocationexception)
                    {
                        exception = targetInvocationexception.InnerException;
                    }

                    result = DotNetValue.FromException(exception);
                }
            }
Exemplo n.º 7
0
        internal static void RunHostedApplication(IntPtr context,
                                                  IntPtr nativeMethodsPtr,
                                                  int argc, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 2)] string[] argv,
                                                  IntPtr resultValue)
        {
            // Switch to default ALC
            var myAlc = AssemblyLoadContext.GetLoadContext(typeof(NativeEntryPoint).Assembly);

            if (myAlc != AssemblyLoadContext.Default)
            {
                var inCtx  = AssemblyLoadContext.Default.LoadFromAssemblyName(typeof(NativeEntryPoint).Assembly.GetName());
                var tInCtx = inCtx.GetType(typeof(NativeEntryPoint).FullName);
                tInCtx.GetMethod(nameof(RunHostedApplication), BindingFlags.Static | BindingFlags.NonPublic)
                .Invoke(null, new object[]
                {
                    context,
                    nativeMethodsPtr,
                    argc,
                    argv,
                    resultValue
                });
                return;
            }

            var nativeMethods = Marshal.PtrToStructure <NativeApi>(nativeMethodsPtr);

            var host = new NativeNodeHost(context, nativeMethods);

            NodeHost.Instance = new NodeBridge(host);

            try
            {
                var assembly_path = argv[0];
                var assembly      = Assembly.Load(Path.GetFileNameWithoutExtension(assembly_path));

                var entryPoint = assembly.EntryPoint;
                if (entryPoint.IsSpecialName && entryPoint.Name.StartsWith("<") && entryPoint.Name.EndsWith(">"))
                {
                    entryPoint = entryPoint.DeclaringType.GetMethod(entryPoint.Name.Substring(1, entryPoint.Name.Length - 2),
                                                                    BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                }

                var result = host.Scheduler
                             .RunCallbackSynchronously(s =>
                                                       entryPoint.Invoke(null,
                                                                         new object[]
                {
                    argv.Skip(1).ToArray()
                }),
                                                       null);

                Marshal.StructureToPtr(DotNetValue.FromObject(result, host), resultValue, false);
            }
            catch (TargetInvocationException tie)
            {
                Marshal.StructureToPtr(DotNetValue.FromObject(tie.InnerException, host), resultValue, false);
            }
            catch (Exception e)
            {
                Marshal.StructureToPtr(DotNetValue.FromObject(e, host), resultValue, false);
            }
        }
Exemplo n.º 8
0
        internal static void InvokeManagedFunction(
            int argc,
            [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 0)] string[] argv,
            int jsArgC,
            [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.SysInt, SizeParamIndex = 2)] IntPtr[] jsArgV,
            IntPtr resultValue)
        {
            // Console.WriteLine($"InvokeManagedFunction c :: asm path {_assembly_path}");
            // Console.WriteLine("InvokeManagedFunction Waiting for debugger!");
            // while(!System.Diagnostics.Debugger.IsAttached) System.Threading.Thread.Sleep(50);
            // System.Diagnostics.Debugger.Launch();
            // Console.WriteLine("InvokeManagedFunction Debugger awaited!");

            // Switch to default ALC
            // var myAlc = AssemblyLoadContext.GetLoadContext(typeof(NativeEntryPoint).Assembly);
            // if (myAlc != AssemblyLoadContext.Default)
            // {
            //    var inCtx = AssemblyLoadContext.Default.LoadFromAssemblyName(typeof(NativeEntryPoint).Assembly.GetName());
            //    var tInCtx = inCtx.GetType(typeof(NativeEntryPoint).FullName);
            //
            //    tInCtx.GetMethod(nameof(InvokeManagedFunction), BindingFlags.Static | BindingFlags.NonPublic)
            //       .Invoke(null, new object[]
            //       {
            //          argc,
            //          argv,
            //          jsArgC,
            //          jsArgV,
            //          resultValue
            //       });
            //    return;
            // }

            if (_host == null)
            {
                throw new Exception("Dll entrypoint was not activated");
            }

            try
            {
                var asmTyp    = argv[0];
                var asmMethod = argv[1];
                var assembly  = Assembly.Load(Path.GetFileNameWithoutExtension(_assembly_path) !);
                var tInCtx    = assembly.GetType(asmTyp);

                if (tInCtx == null)
                {
                    throw new Exception("Cannot find the corresponding type");
                }

                var tMethod = tInCtx
                              .GetMethod(asmMethod, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

                if (tMethod == null)
                {
                    throw new Exception("Cannot find the corresponding method");
                }

                var result = _host.Scheduler.RunCallbackSynchronously(s =>
                                                                      tMethod.Invoke(null,
                                                                                     jsArgV == null ?
                                                                                     new object[]
                {
                    null
                }:
                                                                                     new object[] {
                    jsArgV.Select(jsPtr => (JsValue)Marshal.PtrToStructure(jsPtr, typeof(JsValue))).ToArray()
                }),
                                                                      null
                                                                      );
                Marshal.StructureToPtr(DotNetValue.FromObject(result, _host), resultValue, false);
            }
            catch (TargetInvocationException tie)
            {
                Marshal.StructureToPtr(DotNetValue.FromObject(tie.InnerException, _host), resultValue, false);
            }
            catch (Exception e)
            {
                Marshal.StructureToPtr(DotNetValue.FromObject(e, _host), resultValue, false);
            }
        }
Exemplo n.º 9
0
        internal static void RunHostedApplication(IntPtr context,
                                                  IntPtr nativeMethodsPtr,
                                                  int argc,
                                                  [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 2)]
                                                  string[] argv,
                                                  IntPtr resultValue)
        {
            // Console.WriteLine($"NativeEntryPoint c :: asm path {_assembly_path}");
            // Console.WriteLine("NativeEntryPoint Waiting for debugger!");
            // while(!System.Diagnostics.Debugger.IsAttached) System.Threading.Thread.Sleep(50);
            // System.Diagnostics.Debugger.Launch();
            // Console.WriteLine("NativeEntryPoint Debugger awaited!");


            // Switch to default ALC
            // var myAlc = AssemblyLoadContext.GetLoadContext(typeof(NativeEntryPoint).Assembly);
            // if (myAlc != AssemblyLoadContext.Default)
            // {
            //     var inCtx = AssemblyLoadContext.Default.LoadFromAssemblyName(typeof(NativeEntryPoint).Assembly.GetName());
            //     var tInCtx = inCtx.GetType(typeof(NativeEntryPoint).FullName);
            //     tInCtx.GetMethod(nameof(RunHostedApplication), BindingFlags.Static | BindingFlags.NonPublic)
            //           .Invoke(null, new object[] { context, nativeMethodsPtr, argc, argv, resultValue });
            //     return;
            // }

            var nativeMethods = Marshal.PtrToStructure <NativeApi>(nativeMethodsPtr);

            _host             = new NativeNodeHost(context, nativeMethods);
            NodeHost.Instance = new NodeBridge(_host);

            try
            {
                _assembly_path = argv[0];
                var assembly = Assembly.Load(Path.GetFileNameWithoutExtension(_assembly_path));

                var entryPoint = assembly.EntryPoint;
                if (entryPoint.IsSpecialName && entryPoint.Name.StartsWith("<") && entryPoint.Name.EndsWith(">"))
                {
                    entryPoint = entryPoint.DeclaringType.GetMethod(entryPoint.Name.Substring(1, entryPoint.Name.Length - 2),
                                                                    BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                }

                // Console.WriteLine($"NativeEntryPoint c :: asm path {_assembly_path} entrypoint name {entryPoint.Name}");
                // Console.WriteLine("NativeEntryPoint Waiting for debugger!");
                // while(!System.Diagnostics.Debugger.IsAttached) System.Threading.Thread.Sleep(50);
                // System.Diagnostics.Debugger.Launch();
                // Console.WriteLine("NativeEntryPoint Debugger awaited!");

                var result = _host.Scheduler
                             .RunCallbackSynchronously(s =>
                                                       entryPoint.Invoke(null,
                                                                         new object[] { argv.Skip(1).ToArray() }),
                                                       null);

                Marshal.StructureToPtr(DotNetValue.FromObject(result, _host), resultValue, false);
            }
            catch (TargetInvocationException tie)
            {
                Marshal.StructureToPtr(DotNetValue.FromObject(tie.InnerException, _host), resultValue, false);
            }
            catch (Exception e)
            {
                Marshal.StructureToPtr(DotNetValue.FromObject(e, _host), resultValue, false);
            }
        }