Skip to content

Commit

Permalink
refactoring C# code, additional CLR types testing
Browse files Browse the repository at this point in the history
  • Loading branch information
agracio committed Feb 27, 2025
1 parent db876ea commit b40eaf1
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 121 deletions.
110 changes: 23 additions & 87 deletions src/double/Edge.js/dotnetcore/coreclrembedding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public static IntPtr CompileFunc(IntPtr v8Options, int payloadType, IntPtr excep

if (!Compilers.ContainsKey(compiler))
{
if (DependencyContext.Default ==null || !DependencyContext.Default.RuntimeLibraries.Any(l => l.Name == compiler))
if (DependencyContext.Default == null || !DependencyContext.Default.RuntimeLibraries.Any(l => l.Name == compiler))
{
if (!File.Exists(options["bootstrapDependencyManifest"].ToString()))
{
Expand Down Expand Up @@ -1078,13 +1078,14 @@ public static IntPtr MarshalCLRToV8(object clrObject, out V8Type v8Type)
return IntPtr.Zero;
}

else if (clrObject is string)
{
v8Type = V8Type.String;
return Marshal.StringToCoTaskMemUTF8((string) clrObject);
}

else if (clrObject is char)
else if (
clrObject is string
|| clrObject is char
|| clrObject is Guid
|| clrObject is Uri
|| clrObject is DateTimeOffset
|| clrObject is decimal
|| clrObject is Enum)
{
v8Type = V8Type.String;
return Marshal.StringToCoTaskMemUTF8(clrObject.ToString());
Expand All @@ -1101,12 +1102,6 @@ public static IntPtr MarshalCLRToV8(object clrObject, out V8Type v8Type)
return memoryLocation;
}

else if (clrObject is Guid)
{
v8Type = V8Type.String;
return Marshal.StringToCoTaskMemUTF8(clrObject.ToString());
}

else if (clrObject is DateTime)
{
v8Type = V8Type.Date;
Expand All @@ -1129,19 +1124,7 @@ public static IntPtr MarshalCLRToV8(object clrObject, out V8Type v8Type)
return memoryLocation;
}

else if (clrObject is DateTimeOffset)
{
v8Type = V8Type.String;
return Marshal.StringToCoTaskMemUTF8(clrObject.ToString());
}

else if (clrObject is Uri)
{
v8Type = V8Type.String;
return Marshal.StringToCoTaskMemUTF8(clrObject.ToString());
}

else if (clrObject is short || clrObject is ushort)
else if (clrObject is short || clrObject is ushort || clrObject is int)
{
v8Type = V8Type.Int32;
IntPtr memoryLocation = Marshal.AllocCoTaskMem(sizeof (int));
Expand All @@ -1150,72 +1133,16 @@ public static IntPtr MarshalCLRToV8(object clrObject, out V8Type v8Type)
return memoryLocation;
}

else if (clrObject is int)
else if (clrObject is long || clrObject is ulong || clrObject is double || clrObject is uint)
{
v8Type = V8Type.Int32;
IntPtr memoryLocation = Marshal.AllocCoTaskMem(sizeof (int));

Marshal.WriteInt32(memoryLocation, (int) clrObject);
return memoryLocation;
return WriteDouble(Convert.ToDouble(clrObject), out v8Type);
}

else if (clrObject is uint)
{
v8Type = V8Type.Number;
IntPtr memoryLocation = Marshal.AllocCoTaskMem(sizeof (uint));

WriteDouble(memoryLocation, Convert.ToDouble(BigInteger.Parse(clrObject.ToString()).ToString()));
return memoryLocation;
}

else if (clrObject is long)
{
v8Type = V8Type.Number;
IntPtr memoryLocation = Marshal.AllocCoTaskMem(sizeof (double));

WriteDouble(memoryLocation, Convert.ToDouble((long) clrObject));
return memoryLocation;
}

else if (clrObject is ulong)
{
v8Type = V8Type.Number;
IntPtr memoryLocation = Marshal.AllocCoTaskMem(sizeof (double));

WriteDouble(memoryLocation, Convert.ToDouble((ulong) clrObject));
return memoryLocation;
}

else if (clrObject is double)
{
v8Type = V8Type.Number;
IntPtr memoryLocation = Marshal.AllocCoTaskMem(sizeof (double));

WriteDouble(memoryLocation, (double) clrObject);
return memoryLocation;
}

else if (clrObject is float)
{
v8Type = V8Type.Number;
IntPtr memoryLocation = Marshal.AllocCoTaskMem(sizeof (double));

WriteDouble(memoryLocation, Convert.ToDouble((Single) clrObject));
return memoryLocation;
}

else if (clrObject is decimal)
{
v8Type = V8Type.String;
return Marshal.StringToCoTaskMemUTF8(clrObject.ToString());
}

else if (clrObject is Enum)
{
v8Type = V8Type.String;
return Marshal.StringToCoTaskMemUTF8(clrObject.ToString());
return WriteDouble(Convert.ToDouble((Single)clrObject), out v8Type);
}

else if (clrObject is byte[] || clrObject is IEnumerable<byte>)
{
v8Type = V8Type.Buffer;
Expand Down Expand Up @@ -1391,6 +1318,15 @@ public static IntPtr MarshalCLRToV8(object clrObject, out V8Type v8Type)
return destinationPointer;
}
}

private static IntPtr WriteDouble(double number, out V8Type v8Type)
{
v8Type = V8Type.Number;
IntPtr memoryLocation = Marshal.AllocCoTaskMem(sizeof (double));

WriteDouble(memoryLocation,number);
return memoryLocation;
}

public static object MarshalV8ToCLR(IntPtr v8Object, V8Type objectType)
{
Expand Down
24 changes: 23 additions & 1 deletion test/102_node2net.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
var edge = require('../lib/edge.js'), assert = require('assert')
, path = require('path');

const crypto = require('crypto');

var edgeTestDll = process.env.EDGE_USE_CORECLR ? 'test' : path.join(__dirname, 'Edge.Tests.dll');
var prefix = process.env.EDGE_USE_CORECLR ? '[CoreCLR]' : process.platform === 'win32' ? '[.NET]' : '[Mono]';

function generateUuidBySeed() {

const hash = crypto.createHash('sha256').update('seed').digest('hex');

// UUID version 4 consists of 32 hexadecimal digits in the form:
// 8-4-4-4-12 (total 36 characters including hyphens)
const uuid = [
hash.substring(0, 8),
hash.substring(8, 12),
'4' + hash.substring(12, 15), // Set the version to 4
'8' + hash.substring(15, 18), // Set the variant to 8 (RFC 4122)
hash.substring(18, 30),
].join('-');

return uuid;
}
describe('async call from node.js to .net', function () {

it(prefix + ' succeeds for hello world', function (done) {
Expand Down Expand Up @@ -39,7 +57,8 @@ describe('async call from node.js to .net', function () {
k: 65535,
l: 4294967295,
m: 18446744073709551615,

n: 'c',
o: generateUuidBySeed(),
};
func(payload, function (error, result) {
assert.ifError(error);
Expand Down Expand Up @@ -78,6 +97,9 @@ describe('async call from node.js to .net', function () {
assert.equal(result.k, 65535);
assert.equal(result.l, 4294967295);
assert.equal(result.m, 18446744073709551615);
assert.equal(result.n, 'c');
assert.equal(result.o, generateUuidBySeed());

done();
});
});
Expand Down
24 changes: 22 additions & 2 deletions test/103_net2node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,27 @@
var edge = require('../lib/edge.js'), assert = require('assert')
, path = require('path');

const crypto = require('crypto');

var edgeTestDll = process.env.EDGE_USE_CORECLR ? 'test' : path.join(__dirname, 'Edge.Tests.dll');
var prefix = process.env.EDGE_USE_CORECLR ? '[CoreCLR]' : process.platform === 'win32' ? '[.NET]' : '[Mono]';

function generateUuidBySeed() {

const hash = crypto.createHash('sha256').update('seed').digest('hex');

// UUID version 4 consists of 32 hexadecimal digits in the form:
// 8-4-4-4-12 (total 36 characters including hyphens)
const uuid = [
hash.substring(0, 8),
hash.substring(8, 12),
'4' + hash.substring(12, 15), // Set the version to 4
'8' + hash.substring(15, 18), // Set the variant to 8 (RFC 4122)
hash.substring(18, 30),
].join('-');

return uuid;
}
describe('async call from .net to node.js', function () {

it(prefix + ' succeeds for hello world', function (done) {
Expand Down Expand Up @@ -70,7 +88,8 @@ describe('async call from .net to node.js', function () {
assert.equal(result.k, 65535);
assert.equal(result.l, 4294967295);
assert.equal(result.m, 18446744073709551615);

assert.equal(result.n, 'c');
assert.equal(result.o, generateUuidBySeed());

callback(null, 'yes');
}
Expand Down Expand Up @@ -119,7 +138,8 @@ describe('async call from .net to node.js', function () {
k: 65535,
l: 4294967295,
m: 18446744073709551615,

n: 'c',
o: generateUuidBySeed(),
};
callback(null, payload);
}
Expand Down
20 changes: 20 additions & 0 deletions test/105_node2net_sync.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
var edge = require('../lib/edge.js'), assert = require('assert')
, path = require('path');

const crypto = require('crypto');

var edgeTestDll = process.env.EDGE_USE_CORECLR ? 'test' : path.join(__dirname, 'Edge.Tests.dll');
var prefix = process.env.EDGE_USE_CORECLR ? '[CoreCLR]' : process.platform === 'win32' ? '[.NET]' : '[Mono]';

function generateUuidBySeed() {

const hash = crypto.createHash('sha256').update('seed').digest('hex');

// UUID version 4 consists of 32 hexadecimal digits in the form:
// 8-4-4-4-12 (total 36 characters including hyphens)
const uuid = [
hash.substring(0, 8),
hash.substring(8, 12),
'4' + hash.substring(12, 15), // Set the version to 4
'8' + hash.substring(15, 18), // Set the variant to 8 (RFC 4122)
hash.substring(18, 30),
].join('-');

return uuid;
}
describe('sync call from node.js to .net', function () {

it(prefix + ' succeeds for hello world', function () {
Expand Down Expand Up @@ -56,6 +74,8 @@ describe('sync call from node.js to .net', function () {
k: 65535,
l: 4294967295,
m: 18446744073709551615,
n: 'c',
o: generateUuidBySeed(),
};
var result = func(payload, true);
assert.equal(result, 'yes');
Expand Down
23 changes: 23 additions & 0 deletions test/106_node2net_symbols.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
var edge = require('../lib/edge.js'), assert = require('assert')
, path = require('path');

const crypto = require('crypto');

var edgeTestDll = path.join(__dirname, '测试', 'Edge.Tests.CoreClr.dll');
var prefix = process.env.EDGE_USE_CORECLR ? '[CoreCLR]' : process.platform === 'win32' ? '[.NET]' : '[Mono]';

function generateUuidBySeed() {

const hash = crypto.createHash('sha256').update('seed').digest('hex');

// UUID version 4 consists of 32 hexadecimal digits in the form:
// 8-4-4-4-12 (total 36 characters including hyphens)
const uuid = [
hash.substring(0, 8),
hash.substring(8, 12),
'4' + hash.substring(12, 15), // Set the version to 4
'8' + hash.substring(15, 18), // Set the variant to 8 (RFC 4122)
hash.substring(18, 30),
].join('-');

return uuid;
}

describe('node.js to .net dll from path with asian chracters', function () {

it(prefix + ' succeeds for hello world', function (done) {
Expand Down Expand Up @@ -45,6 +64,8 @@ describe('node.js to .net dll from path with asian chracters', function () {
k: 65535,
l: 4294967295,
m: 18446744073709551615,
n: 'c',
o: generateUuidBySeed(),
};
func(payload, function (error, result) {
assert.ifError(error);
Expand Down Expand Up @@ -86,6 +107,8 @@ describe('node.js to .net dll from path with asian chracters', function () {
assert.equal(result.k, 65535);
assert.equal(result.l, 4294967295);
assert.equal(result.m, 18446744073709551615);
assert.equal(result.n, 'c');
assert.equal(result.o, generateUuidBySeed());
done();
});
});
Expand Down
Loading

0 comments on commit b40eaf1

Please sign in to comment.