agracio / edge-js

Run .NET and Node.js code in-process on Windows, macOS, and Linux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error encountered when operating on dynamic arguments

TwelveNights opened this issue · comments

I've noticed a problem with dynamic arguments when using edge-js to create a C# function in Javascript, specifically when operations are performed on the dynamic argument. Please take a look at the following test which illustrates this behaviour:

docker run --rm -it debian:bullseye-slim bash

# in the container
export DOTNET_CLI_TELEMETRY_OPTOUT=1
export DOTNET_ROOT=/opt/dotnet
export PATH="${PATH}:${DOTNET_ROOT}"
export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1

apt update && apt install -y wget curl git unzip python3 make g++

mkdir /opt/dotnet
cd /opt/dotnet
wget -nv https://download.visualstudio.microsoft.com/download/pr/1d2007d3-da35-48ad-80cc-a39cbc726908/1f3555baa8b14c3327bb4eaa570d7d07/dotnet-sdk-6.0.403-linux-x64.tar.gz
tar -xzf dotnet-sdk-6.0.403-linux-x64.tar.gz -C /opt/dotnet
rm dotnet-sdk-6.0.403-linux-x64.tar.gz

curl -fsSL https://deb.nodesource.com/setup_16.x | bash - &&\
apt-get install -y nodejs

cd /tmp

npm i edge-js
node -p 'require("edge-js").func(`(dynamic ctx) => { return "Hello, " + ctx.ToString(); } `)("World!", console.log);'

The result I receive is:

/tmp/node_modules/edge-js/lib/edge.js:177
    return edge.initializeClrFunc(options);
                ^

Error: Unable to compile C# code.
----> Errors when compiling as a CLR library:
CS1022: Type or namespace definition, or end-of-file expected
CS1022: Type or namespace definition, or end-of-file expected
CS1022: Type or namespace definition, or end-of-file expected
CS8124: Tuple must contain at least two elements.
----> Errors when compiling as a CLR async lambda expression:
CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Dynamic.Runtime' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Dynamic.Runtime' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.BinaryOperation'
    at Object.exports.func (/tmp/node_modules/edge-js/lib/edge.js:177:17)
    at [eval]:1:20
    at Script.runInThisContext (node:vm:129:12)
    at Object.runInThisContext (node:vm:313:38)
    at node:internal/process/execution:79:19
    at [eval]-wrapper:6:22
    at evalScript (node:internal/process/execution:78:60)
    at node:internal/main/eval_string:27:3 {
  TargetSite: {
    Name: '',
    DeclaringType: '',
    ReflectedType: '',
    MemberType: '',
    MetadataToken: '',
    Module: '',
    IsSecurityCritical: '',
    IsSecuritySafeCritical: '',
    IsSecurityTransparent: '',
    MethodHandle: '',
    Attributes: '',
    CallingConvention: '',
    ReturnType: '',
    ReturnTypeCustomAttributes: '',
    ReturnParameter: '',
    IsCollectible: '',
    IsGenericMethod: '',
    IsGenericMethodDefinition: '',
    ContainsGenericParameters: '',
    MethodImplementationFlags: '',
    IsAbstract: '',
    IsConstructor: '',
    IsFinal: '',
    IsHideBySig: '',
    IsSpecialName: '',
    IsStatic: '',
    IsVirtual: '',
    IsAssembly: '',
    IsFamily: '',
    IsFamilyAndAssembly: '',
    IsFamilyOrAssembly: '',
    IsPrivate: '',
    IsPublic: '',
    IsConstructedGenericMethod: '',
    CustomAttributes: ''
  },
  Message: 'Unable to compile C# code.\n' +
    '----> Errors when compiling as a CLR library:\n' +
    'CS1022: Type or namespace definition, or end-of-file expected\n' +
    'CS1022: Type or namespace definition, or end-of-file expected\n' +
    'CS1022: Type or namespace definition, or end-of-file expected\n' +
    'CS8124: Tuple must contain at least two elements.\n' +
    '----> Errors when compiling as a CLR async lambda expression:\n' +
    "CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy\n" +
    "CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy\n" +
    "CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy\n" +
    "CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy\n" +
    "CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Threading.Tasks' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy\n" +
    "CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Dynamic.Runtime' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy\n" +
    "CS1701: Assuming assembly reference 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Dynamic.Runtime' matches identity 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy\n" +
    "CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.BinaryOperation'",
  Data: {},
  InnerException: null,
  HelpLink: null,
  Source: 'Edge.js.CSharp',
  HResult: -2146233079,
  StackTrace: '   at EdgeCompiler.CompileFunc(IDictionary`2 parameters, IDictionary`2 compileAssemblies)',
  Name: 'System.InvalidOperationException',
  name: 'System.InvalidOperationException'
}

The line that stands out to me is CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.BinaryOperation'. I've tested this with the following functions and they all seem to work:

  • async (dynamic ctx) { return ctx; };
  • async (dynamic ctx) { return "Hello, " + "World!"; }

So it seems to be the fact that we're trying to operate between the dynamic type and the ordinary string value. Please let me know if there's a workaround for this behaviour, or if you'd like additional information.

Please take a look at https://github.com/agracio/edge-js-quick-start, dynamic inputs work correctly.