Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
HyperSphereStudio committed Jan 12, 2022
2 parents dbbf483 + 4ca6161 commit 1434c10
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ int v = (int) Julia.Eval("2 * 2");
Julia.Exit(0);
```

Struct Handling:
```csharp

#You have two choices, allocate a struct or create a struct.
#Allocating directly sets the memory, creating will call a constructor of the struct

var myAllocatedStruct = Julia.AllocStruct(JLType.JLRef, 3); //Will throw error
var myCreatedStuct = JLType.JLRef.Create(3); //Will call constructor
```

Function Handling:
```csharp
Julia.Init();
Expand All @@ -18,8 +28,38 @@ Function Handling:
JLType willbeInt64 = fun.ParameterTypes[1];
JLType willBeInt32 = fun.ReturnType;

int resultWillBe4 = (int) fun.Invoke(2);
object willReturnNetBoxed4 = fun.Invoke(2).Value;
JLVal resultWillBe4 = fun.Invoke(2);
Julia.Exit(0);
```

Value Handling:
```csharp
//Auto alloc to Julia
var val = new JLVal(3);

//Manual Type Unboxing
long netVal = val.UnboxInt64();

//Auto Unboxing
object newVal2 = val.Value;
```

Array Handling:
```csharp
Julia.Init();
JLArray arr = Julia.Eval("[2, 3, 4]")

//Unpack to .net
object[] o = arr.LinearNetUnPack();

//Make own array
var newArray = long[arr.Length];
for(int i = 1; i < arr.Length; ++i)
newArray[i - 1] = (long) arr[i];

JLType elementType = arr.ElType;

Julia.Exit(0);
```

Exception Handling:
Expand All @@ -32,6 +72,37 @@ Exception Handling:
```


Garbage Collection:
You are (at the current moment of this project) responsible for ensuring object safety on both .NET and Julia. When you make calls to either language, the GC could activate and invalidate the reference you hold in the other language unless you pin it!

CSharp Garbage Collector Pinning:
```csharp
Julia.Init();
JLArray myArr = new JLArray(JLType.Int64, 5); //Allocate Int64 array of length 5
var handle = myArr.Pin(); //Pin the Object
//Stuff calling Julia Functions
handle.Free(); //Optional, handle destructor will auto call it. This is in case you want it freed earlier
Julia.Exit(0);
```
Keep In mind that there is another way to pin Julia objects using Julia.PUSHGC() and Julia.POPGC(). (The Julian way)


Julia Garbage Collector Pinning:
```julia
myBasicType = SharpType("MyBasicType")
myBasicCon = SharpConstructor(myBasicType)
myBasicObj = myBasicCon()
handle = pin(myBasicObj)

#Stuff calling Sharp Functions

free(handle) #Will also auto free. You can also treat it like stream and put it in do end block
```


.NET Interface

Expand Down

0 comments on commit 1434c10

Please sign in to comment.