Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Jan 1, 2025
1 parent 966cf7b commit 671c2ea
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 75 deletions.
24 changes: 20 additions & 4 deletions ReactWithDotNet/ILCodeGeneration/MonoCecilToJsonModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,13 @@ static MethodBodyModel AsModel(this MethodBody body, MetadataTable metadataTable

if (operand is MethodReference methodReference)
{
if (methodReference.Name =="Set" && methodReference.DeclaringType.IsArray)
if (methodReference.Name =="Set" && methodReference.DeclaringType is ArrayType arrayType)
{
// todo: fixme
// instructions[^1] = 219;
// continue;
instructions[^1] = 229;


operands.Add(i,OpCodeManaged.MultiDimensionalArray_Set);
continue;
}
if (methodReference.FullName == "System.Void System.Object::.ctor()")
{
Expand All @@ -267,6 +269,20 @@ static MethodBodyModel AsModel(this MethodBody body, MetadataTable metadataTable

if (methodReference.DeclaringType?.FullName == typeof(NativeJsHelper).FullName)
{
if (methodReference.Name == nameof(NativeJsHelper.TypeOf))
{
instructions[^1] = 230;
continue;
}
if (methodReference.Name == nameof(NativeJsHelper.TypeOfIsNumber))
{
instructions[^1] = 231;
continue;
}




instructions[^1] = 220;

if (methodReference.Name == nameof(NativeJsHelper.Set))
Expand Down
171 changes: 102 additions & 69 deletions ReactWithDotNet/ILCodeGeneration/NativeJs.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ReactWithDotNet;
using static ReactWithDotNet.NativeJsHelper;


using static ReactWithDotNet.OpCodeManaged;

namespace ReactWithDotNet;

Expand Down Expand Up @@ -42,6 +41,10 @@ static class NativeJsHelper
public static extern object Get(this object instance, string key);
public static extern object Get(this object instance, int key);

public static extern string TypeOf(this object instance);

public static extern bool TypeOfIsNumber(this object instance);

public static extern object CreateNewPlainObject();

public static extern Array CreateNewArray();
Expand Down Expand Up @@ -73,20 +76,25 @@ static class NativeJsHelper




static class OpCodeManaged
{
public const int MultiDimensionalArray_Set = 0;

public const int InterpreterBridge_NewArr = 0; // todo: free
public const int InterpreterBridge_NullReferenceException = 1;
public const int InterpreterBridge_ArgumentNullException = 2;
public const int InterpreterBridge_DivideByZeroException = 3;
public const int InterpreterBridge_IndexOutOfRangeException = 4;
public const int InterpreterBridge_MissingMethodException = 5;
public const int InterpreterBridge_OverflowException = 6;
}

static class InterpreterBridge
{

public static void Jump()
{
const int InterpreterBridge_NewArr = 0; // todo: free
const int InterpreterBridge_NullReferenceException = 1;
const int InterpreterBridge_ArgumentNullException = 2;
const int InterpreterBridge_DivideByZeroException = 3;
const int InterpreterBridge_IndexOutOfRangeException = 4;
const int InterpreterBridge_MissingMethodException = 5;
const int InterpreterBridge_OverflowException = 6;



var previousStackFrame = PreviousStackFrame;
Expand All @@ -96,73 +104,98 @@ public static void Jump()

var instruction = evaluationStack.pop().As<int>();




if (InterpreterBridge_NewArr == instruction)
switch (instruction)
{
//var length = evaluationStack.pop().As<int>();
case MultiDimensionalArray_Set:
{
var rank = 0;

var value = evaluationStack.pop();

object[] array;
var indexList = CreateNewArray().As<int[]>();
{
while (true)
{
var item = evaluationStack.pop();
if (item.TypeOfIsNumber())
{
indexList.push(item);
rank++;
}
else
{
array = item.As<object[]>();
break;
}
}
indexList.Call("reverse");
}



var dimensions = array.Get("$dimensions").As<int[]>();
var flatIndex = 0;
{
for (var i = 0; i < rank; i++)
{
var multiplier = 1;
for (var j = i + 1; j < rank; j++)
{
multiplier *= dimensions[j];
}

flatIndex += indexList[i] * multiplier;
}
}

array[flatIndex] = value;

return;
}

case InterpreterBridge_NullReferenceException:
{
var message = evaluationStack.pop().As<string>();

throw new NullReferenceException(message);
}

//var typeIndex = evaluationStack.pop().As<int>();

//var array = CreateNewArray();

//array.Set("length", length.AsObject());
//array.Set("$type", typeIndex.AsObject());
case InterpreterBridge_ArgumentNullException:
{
var message = evaluationStack.pop().As<string>();

//for (int i = 0; i < length; i++)
//{
// //array.Set(i,);
//}


throw new ArgumentNullException(message);
}

case InterpreterBridge_DivideByZeroException:
{
var message = evaluationStack.pop().As<string>();

//evaluationStack.push(array);

return;
}

if (InterpreterBridge_NullReferenceException == instruction)
{
var message = evaluationStack.pop().As<string>();

throw new NullReferenceException(message);
}

if (InterpreterBridge_ArgumentNullException == instruction)
{
var message = evaluationStack.pop().As<string>();

throw new ArgumentNullException(message);
}

if (InterpreterBridge_DivideByZeroException == instruction)
{
var message = evaluationStack.pop().As<string>();

throw new DivideByZeroException(message);
}

if (InterpreterBridge_IndexOutOfRangeException == instruction)
{
var message = evaluationStack.pop().As<string>();
throw new DivideByZeroException(message);
}

case InterpreterBridge_IndexOutOfRangeException:
{
var message = evaluationStack.pop().As<string>();

throw new IndexOutOfRangeException(message);
}

if (InterpreterBridge_MissingMethodException == instruction)
{
var methodReferenceModel = evaluationStack.pop().As<MethodReferenceModel>();
throw new IndexOutOfRangeException(message);
}

case InterpreterBridge_MissingMethodException:
{

var methodReferenceModel = evaluationStack.pop().As<MethodReferenceModel>();

throw new MissingMethodException(methodReferenceModel.Name);
}

if (InterpreterBridge_OverflowException == instruction)
{
var message = evaluationStack.pop().As<string>();
throw new MissingMethodException(methodReferenceModel.Name);
}
case InterpreterBridge_OverflowException:
{
var message = evaluationStack.pop().As<string>();

throw new OverflowException(message);
throw new OverflowException(message);
}
}

throw new Exception();
Expand Down
24 changes: 22 additions & 2 deletions ReactWithDotNet/JsClientEngine/clr.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ function Interpret(thread)

if (declaringType && declaringType.ValueTypeId === ArrayType && method.Name === 'Set')
{
const value = evaluationStack.pop();
const value = evaluationStack.pop();

const rank = declaringType.Rank;

Expand Down Expand Up @@ -1925,7 +1925,7 @@ function Interpret(thread)

let caseIndex = evaluationStack.pop();

if ( caseIndex > 0 && jumpTable.length > caseIndex)
if ( caseIndex >= 0 && jumpTable.length > caseIndex)
{
currentStackFrame.Line = jumpTable[caseIndex];

Expand Down Expand Up @@ -4279,6 +4279,26 @@ function Interpret(thread)
break;

}

case 229: // swap managed OpCode
{
evaluationStack.push(operands[currentStackFrame.Line]);
nextInstruction = InterpreterBridge_Jump;
break;
}

case 230:
{
evaluationStack.push( typeof evaluationStack.pop() );
nextInstruction = instructions[++currentStackFrame.Line];
break;
}
case 231:
{
evaluationStack.push( typeof evaluationStack.pop() === 'number' ? 1 : 0 );
nextInstruction = instructions[++currentStackFrame.Line];
break;
}
}
}
catch (exception)
Expand Down

0 comments on commit 671c2ea

Please sign in to comment.