Skip to content

Commit

Permalink
updating Nuget package
Browse files Browse the repository at this point in the history
  • Loading branch information
agracio committed Dec 27, 2024
1 parent e195a1d commit 185af6c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 35 deletions.
70 changes: 56 additions & 14 deletions src/double/Edge.js/dotnet/EdgeJs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
Expand All @@ -18,6 +17,7 @@ public class Edge
static ManualResetEvent waitHandle = new ManualResetEvent(false);
private static string edgeDirectory;
private static List<Func<object, Task<object>>> compiledFuncs = new List<Func<object, Task<object>>>();
private static readonly bool DebugMode = Environment.GetEnvironmentVariable("EDGE_DEBUG") == "1";

static Edge()
{
Expand All @@ -34,6 +34,14 @@ static Edge()
? Path.GetDirectoryName(asm.Location)
: Path.GetDirectoryName(codeBase.LocalPath);
}

internal static void DebugMessage(string message, params object[] parameters)
{
if (DebugMode)
{
Console.WriteLine(message, parameters);
}
}

static string assemblyDirectory;
static string AssemblyDirectory
Expand All @@ -56,7 +64,7 @@ static string AssemblyDirectory
}
}

// in case we want to set this path and not use an enviroment var
// in case we want to set this path and not use an environment var
public static void SetAssemblyDirectory(string folder)
{
assemblyDirectory = folder;
Expand Down Expand Up @@ -96,6 +104,28 @@ public static void Uninitialize()
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

private static string GetLibNodeDll(string arch)
{
string path = ResolveUnicodeCharactersInPath(string.Format(@"{0}\edge\{1}\libnode.dll", AssemblyDirectory, arch));

if (!File.Exists(path))
{
path = string.Format(@"edge\{0}\libnode.dll", arch);
}

DebugMessage("libnode path: {0}", path);
return path;
}

private static string ResolveUnicodeCharactersInPath(string path)
{
// Workaround for Unicode characters in path
StringBuilder shortPath = new StringBuilder(255);
GetShortPathName(path, shortPath, shortPath.Capacity);
return shortPath.ToString();
// End workaround for Unicode characters in path
}

public static Func<object,Task<object>> Func(string code)
{
if (!initialized)
Expand All @@ -107,14 +137,15 @@ public static Func<object,Task<object>> Func(string code)
Func<int, string[], int> nodeStart;
if (IntPtr.Size == 4)
{
LoadLibrary(AssemblyDirectory + @"\edge\x86\libnode.dll");
LoadLibrary(GetLibNodeDll("x86"));
nodeStart = NodeStartx86;
}
else if (IntPtr.Size == 8)
{
LoadLibrary(AssemblyDirectory + @"\edge\x64\libnode.dll");
LoadLibrary(GetLibNodeDll("x64"));
nodeStart = NodeStartx64;
}

else
{
throw new InvalidOperationException(
Expand All @@ -125,23 +156,34 @@ public static Func<object,Task<object>> Func(string code)
{
List<string> argv = new List<string>();
argv.Add("node");
string node_params = Environment.GetEnvironmentVariable("EDGE_NODE_PARAMS");
if (!string.IsNullOrEmpty(node_params))
var nodeParams = Environment.GetEnvironmentVariable("EDGE_NODE_PARAMS");
if (!string.IsNullOrEmpty(nodeParams))
{
foreach (string p in node_params.Split(' '))
foreach (string p in nodeParams.Split(' '))
{
argv.Add(p);
}
}

// Workaround for unicode characters in path
string path = AssemblyDirectory + "\\edge\\double_edge.js";
StringBuilder shortPath = new StringBuilder(255);
int result = GetShortPathName(path, shortPath, shortPath.Capacity);
argv.Add(shortPath.ToString());
// End workaround for unicode characters in path
var path = ResolveUnicodeCharactersInPath(AssemblyDirectory + @"\edge\double_edge.js");
var edgeDll = ResolveUnicodeCharactersInPath(Path.Combine(edgeDirectory, "EdgeJs.dll"));

if (File.Exists(path) && File.Exists(edgeDll))
{
DebugMessage("double_edge.js path: {0}", path);
DebugMessage("-EdgeJs:{0}", edgeDll);
argv.Add(path);
argv.Add(string.Format("-EdgeJs:{0}", edgeDll));
}
else
{
DebugMessage("double_edge.js path: {0}", @"edge\double_edge.js");
DebugMessage("-EdgeJs:EdgeJs.dll");

argv.Add(@"edge\double_edge.js");
argv.Add("-EdgeJs:EdgeJs.dll");
}

argv.Add(string.Format("-EdgeJs:{0}", Path.Combine(edgeDirectory, "EdgeJs.dll")));
nodeStart(argv.Count, argv.ToArray());
}, 1048576); // Force typical Windows stack size because less is liable to break

Expand Down
2 changes: 1 addition & 1 deletion test/double/double_test/DoubleEdge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void SucceedsCheckingNodeVersion()
")(".NET").Result;

System.Console.WriteLine(result);
Assert.AreEqual(result, "v20.12.2");
Assert.AreEqual(result, "v22.12.0");
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion test/double/double_test/double_test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EdgeJs" Version="20.12.3" />
<PackageReference Include="EdgeJs" Version="22.12.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions test/double/double_test/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyLocalSharedSource" value="packages" />
</packageSources>
</configuration>
42 changes: 27 additions & 15 deletions tools/build_double.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,37 @@ if "%1" equ "" (
exit /b -1
)

FOR /F "tokens=* USEBACKQ" %%F IN (`node -p process.arch`) DO (SET ARCH=%%F)
for /F "delims=." %%a in ("%1") do set MAJORVERSION=%%a
set MAJORVERSION=%MAJORVERSION: =%


call :build_lib
if %ERRORLEVEL% neq 0 exit /b -1

call :download_node %1
if %ERRORLEVEL% neq 0 exit /b -1

call :build_node %1 x86
if %ERRORLEVEL% neq 0 exit /b -1
if %MAJORVERSION% LSS 23 (
call :build_node %1 x86
if %ERRORLEVEL% neq 0 exit /b -1
)

call :build_node %1 x64
if %ERRORLEVEL% neq 0 exit /b -1

call :download_node_exe %1
if %ERRORLEVEL% neq 0 exit /b -1

call :build_edge %1 x86 ia32
if %ERRORLEVEL% neq 0 exit /b -1
@REM call :download_node_exe %1
@REM if %ERRORLEVEL% neq 0 exit /b -1

call :build_edge %1 x64 x64
if %ERRORLEVEL% neq 0 exit /b -1

if %MAJORVERSION% LSS 23 (
call :build_edge %1 x86 ia32
if %ERRORLEVEL% neq 0 exit /b -1
)

call :clean_nuget_package
if %ERRORLEVEL% neq 0 exit /b -1

Expand All @@ -39,14 +50,14 @@ REM ===========================================================
:build_lib
echo :build_lib

if exist "%SELF%\build\nuget\lib\net45" (
echo "%SELF%\build\nuget\lib\net45" already exists.
exit /b 0
)
@REM if exist "%SELF%\build\nuget\lib\net462" (
@REM echo "%SELF%\build\nuget\lib\net462" already exists.
@REM exit /b 0
@REM )

mkdir "%SELF%\..\src\double\Edge.js\bin\Release\net45" > nul 2>&1
mkdir "%SELF%\..\src\double\Edge.js\bin\Release\net462" > nul 2>&1

csc /out:"%SELF%\..\src\double\Edge.js\bin\Release\net45\EdgeJs.dll" /target:library "%SELF%\..\src\double\Edge.js\dotnet\EdgeJs.cs"
csc /out:"%SELF%\..\src\double\Edge.js\bin\Release\net462\EdgeJs.dll" /target:library "%SELF%\..\src\double\Edge.js\dotnet\EdgeJs.cs"
if %ERRORLEVEL% neq 0 exit /b -1

cd "%SELF%\..\src\double\Edge.js"
Expand All @@ -67,8 +78,8 @@ REM ===========================================================
echo :build_node %1 %2

if exist "%SELF%\build\node-%1-%2\node.lib" (
echo "%SELF%\build\node-%1-%2\node.lib" already built
exit /b 0
echo "%SELF%\build\node-%1-%2\node.lib" already built
exit /b 0
)

pushd "%SELF%\build\node-%1"
Expand Down Expand Up @@ -154,7 +165,7 @@ set GYP=%NODEBASE%\node_modules\node-gyp\bin\node-gyp.js

pushd "%SELF%\.."

"%NODEEXE%" "%GYP%" configure --msvs_version=2019
node "%GYP%" configure --msvs_version=2022 --target=%1 --runtime=node --release --arch=%2
if %ERRORLEVEL% neq 0 (
echo Error configuring edge.node %FLAVOR% for node.js %2 v%3
exit /b -1
Expand All @@ -164,6 +175,7 @@ FOR %%F IN (build\*.vcxproj) DO (
echo Patch node.lib in %%F
powershell -Command "(Get-Content -Raw %%F) -replace '\\\\node.lib', '\\\\libnode.lib' | Out-File -Encoding Utf8 %%F"
)

"%NODEEXE%" "%GYP%" build
mkdir "%SELF%\build\nuget\content\edge\%2" > nul 2>&1
copy /y build\release\edge_nativeclr.node "%SELF%\build\nuget\content\edge\%2"
Expand Down
6 changes: 3 additions & 3 deletions tools/nuget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
Edge.js allows you to run Node.js and .NET code in one process. You can call Node.js functions from .NET and .NET functions from Node.js.
Edge.js takes care of marshaling data between CLR and V8. Edge.js also reconciles threading models of single-threaded V8 and multi-threaded CLR. Edge.js ensures correct lifetime of objects on V8 and CLR heaps.

Script CLR from Node.Js on Windows/Linux/macOS with .NET Framework, .Net Core and .NET Standard.
Script CLR from Node.Js on Windows with .NET Framework.

**NOTE** Scripting Node.Js from CLR is only supported on Windows .NET Framework 4.5
**NOTE** Scripting Node.Js from CLR is only supported on Windows .NET Framework 4.5 - 4.8

More documentation is available at [Edge.Js GitHub page](https://github.com/agracio/edge-js).

#### NuGet package compiled using Node.js v20.12.2.
#### NuGet package compiled using Node.js v22.12.0.

### How to use

Expand Down
2 changes: 1 addition & 1 deletion tools/nuget/edge.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>EdgeJs</id>
<version>20.12.3</version>
<version>22.12.0</version>
<authors>Tomasz Janczuk</authors>
<license type="expression">MIT</license>
<licenseUrl>https://licenses.nuget.org/MIT</licenseUrl>
Expand Down

0 comments on commit 185af6c

Please sign in to comment.