Socket
Socket
Sign inDemoInstall

@albertli90/coreclr-host

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @albertli90/coreclr-host

A node addon hosts core clr


Version published
Weekly downloads
3
Maintainers
1
Install size
798 kB
Created
Weekly downloads
 

Readme

Source

coreclr node

npm version nuget version

A simple bridge between coreclr and nodejs

This is a very early release of this package, do not suggest using it in production till latter stable version

Add the CoreclrNodeHost reference to the csproj config

<PackageReference Include="CoreclrNodeHost" Version="0.0.1" />

create an example c# entry class

namespace BasicExample
{
    using System;
    using FluentAssertions;
    using System.Threading.Tasks;
    using CoreclrNodeHost;
    using CoreclrNodeHost.InProcess;

    public class Program
    {
        
        private static readonly TaskCompletionSource<int> MainTaskCompletionSource = new TaskCompletionSource<int>();

        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;
        }

        public static Task<int> Main(string[] args)
        {
            Console.WriteLine($"Basic example entry invoked {args[0]}");

            return MainTaskCompletionSource.Task;
        }
    }
}

then invoke the static method from javascript

const assert = require('assert');
const path = require('path');
const coreclrhost = require('@albertli90/coreclr-host');

const result = coreclrhost.runCoreApp(
    path.join(__dirname, 'bin', 'Debug', 'netcoreapp3.1', 'BasicExample.dll'),
    "AdditionalArgument"
);

const setMainTcsResult = coreclrhost.callManagedFunction(
    "BasicExample.Program",
    "setMainTcs",
    true,
    1e3,
    "oneString",
    {
        message: "oneMessageStringInObj"
    }
)

assert.strictEqual(typeof setMainTcsResult, "object");
assert.strictEqual(setMainTcsResult.argCount, 4);
assert.strictEqual(setMainTcsResult.message, "oneMessageStringInResultObj");


(async ()=>{
    assert.strictEqual(await result, 0);    
})()

Keywords

FAQs

Last updated on 13 May 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc