This commit is contained in:
Jerome Laban 2024-09-20 22:00:43 -04:00
Родитель daad8808b7
Коммит 53495e7dbe
6 изменённых файлов: 127 добавлений и 57 удалений

Просмотреть файл

@ -9,7 +9,7 @@
# source ./emsdk_env.sh
# Navigate back to this file's folder in the same terminal
mkdir -p bin/wasm/CalcManager.bc/3.0.0/st,simd
mkdir -p bin/wasm/CalcManager.a/3.0.0/st,simd
echo Generating LLVM Bitcode files
emcc \
@ -20,6 +20,6 @@ emcc \
-s ALLOW_MEMORY_GROWTH=1 \
-s BINARYEN=1 \
-fwasm-exceptions \
-o bin/wasm/CalcManager.bc/3.0.0/st,simd/CalcManager.bc \
-o bin/wasm/CalcManager.a/3.0.0/st,simd/CalcManager.a \
-r \
CEngine/*.cpp Ratpack/*.cpp *.cpp -I.

Просмотреть файл

@ -54,6 +54,10 @@ namespace CalculatorApp
Telemetry.AnalyticsService.Initialize();
#endif
#if __WASM__
CalculationManager.NativeDispatch.InitializeExports();
#endif
}
#if __MACOS__

Просмотреть файл

@ -6,11 +6,13 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using System.Threading.Tasks;
namespace CalculationManager
{
public static class NativeDispatch
public static partial class NativeDispatch
{
#if __IOS__ || __MACOS__
private const string DllPath = "__Internal"; // https://docs.microsoft.com/en-us/xamarin/ios/platform/native-interop
@ -161,6 +163,9 @@ namespace CalculationManager
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(MaxDigitsReachedCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void MaxDigitsReachedCallback(IntPtr state)
{
@ -170,8 +175,11 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.MaxDigitsReachedCallback");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(MemoryItemChangedCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void MemoryItemChangedCallback(IntPtr state, int indexOfMemory)
{
@ -181,8 +189,11 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.MemoryItemChangedCallback({indexOfMemory})");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(OnHistoryItemAddedCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void OnHistoryItemAddedCallback(IntPtr state, int addedItemIndex)
{
@ -192,8 +203,11 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.OnHistoryItemAddedCallback({addedItemIndex})");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(OnNoRightParenAddedCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void OnNoRightParenAddedCallback(IntPtr state)
{
@ -203,8 +217,11 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.OnNoRightParenAddedCallback");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(SetExpressionDisplayCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void SetExpressionDisplayCallback(IntPtr state, IntPtr historyItem)
{
@ -223,8 +240,11 @@ namespace CalculationManager
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(SetMemorizedNumbersCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void SetMemorizedNumbersCallback(IntPtr state, int count, IntPtr newMemorizedNumbers)
{
@ -242,8 +262,11 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.SetMemorizedNumbersCallback({string.Join(";", numbers)})");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(SetParenthesisNumberCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void SetParenthesisNumberCallback(IntPtr state, int parenthesisCount)
{
@ -253,10 +276,13 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.SetParenthesisNumberCallback({parenthesisCount})");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(BinaryOperatorReceivedFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void BinaryOperatorReceivedCallback(IntPtr state)
{
var manager = GCHandle.FromIntPtr((IntPtr)state).Target as CalculatorDisplay;
@ -265,8 +291,11 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.BinaryOperatorReceivedCallback");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(SetPrimaryDisplayCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void SetPrimaryDisplayCallback(IntPtr state, IntPtr pDisplayStringValue, bool isError)
{
@ -278,8 +307,11 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.SetPrimaryDisplayCallback({displayStringValue}, {isError})");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(SetIsInErrorCallbackFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static void SetIsInErrorCallback(IntPtr state, bool isError)
{
@ -289,8 +321,11 @@ namespace CalculationManager
DebugTrace($"CalculatorManager.SetIsInErrorCallback({isError})");
}
#if __IOS__ || __MACOS__
#if __IOS__ || __MACOS__
[ObjCRuntime.MonoPInvokeCallback(typeof(GetCEngineStringFunc))]
#endif
#if __WASM__
[JSExport]
#endif
public static IntPtr GetCEngineStringCallback(IntPtr state, IntPtr pResourceId)
{
@ -306,6 +341,15 @@ namespace CalculationManager
return pEngineString;
}
#if __WASM__
[JSImport("globalThis.CalcManager.initializeExports")]
public static partial Task InitializeExports();
[JSImport("globalThis.CalcManager.registerCallbacks")]
public static partial int[] RegisterCallbacks();
#endif
private static bool IsStringUTF32 =>
GetWChar_t_Size() == 4;

Просмотреть файл

@ -108,26 +108,25 @@ namespace CalculationManager
_resourceProviderHandle = GCHandle.Alloc(resourceProvider);
#if __WASM__
var rawPtrs = Uno.Foundation.WebAssemblyRuntime.InvokeJS("CalcManager.registerCallbacks()");
var ptrs = rawPtrs.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var ptrs = NativeDispatch.RegisterCallbacks();
var p = new CalculatorManager_CreateParams
{
CalculatorState = GCHandle.ToIntPtr(_displayCallbackHandle),
ResourceState = GCHandle.ToIntPtr(_resourceProviderHandle),
GetCEngineString = (IntPtr)int.Parse(ptrs[0]),
BinaryOperatorReceived = (IntPtr)int.Parse(ptrs[1]),
SetPrimaryDisplay = (IntPtr)int.Parse(ptrs[2]),
SetIsInError = (IntPtr)int.Parse(ptrs[3]),
SetParenthesisNumber = (IntPtr)int.Parse(ptrs[4]),
MaxDigitsReached = (IntPtr)int.Parse(ptrs[5]),
MemoryItemChanged = (IntPtr)int.Parse(ptrs[6]),
OnHistoryItemAdded = (IntPtr)int.Parse(ptrs[7]),
OnNoRightParenAdded = (IntPtr)int.Parse(ptrs[8]),
SetExpressionDisplay = (IntPtr)int.Parse(ptrs[9]),
SetMemorizedNumbers = (IntPtr)int.Parse(ptrs[10]),
};
GetCEngineString = ptrs[0],
BinaryOperatorReceived = ptrs[1],
SetPrimaryDisplay = ptrs[2],
SetIsInError = ptrs[3],
SetParenthesisNumber = ptrs[4],
MaxDigitsReached = ptrs[5],
MemoryItemChanged = ptrs[6],
OnHistoryItemAdded = ptrs[7],
OnNoRightParenAdded = ptrs[8],
SetExpressionDisplay = ptrs[9],
SetMemorizedNumbers = ptrs[10],
};
#else
var p = new CalculatorManager_CreateParams
@ -153,7 +152,7 @@ namespace CalculationManager
_nativeManager = NativeDispatch.CalculatorManager_Create(ref p);
}
public void Reset(bool clearMemory = true)
public void Reset(bool clearMemory = true)
=> NativeDispatch.CalculatorManager_Reset(_nativeManager, clearMemory);
public void SetStandardMode()
@ -178,7 +177,7 @@ namespace CalculationManager
public void DeSerializePrimaryDisplay(List<long> serializedPrimaryDisplay) => throw new NotImplementedException();
public Command SerializeSavedDegreeMode() => throw new NotImplementedException();
public void MemorizeNumber()
public void MemorizeNumber()
=> NativeDispatch.CalculatorManager_MemorizeNumber(_nativeManager);
public void MemorizedNumberLoad(int value)
@ -202,7 +201,7 @@ namespace CalculationManager
public List<char> GetSavedCommands()
=> throw new NotImplementedException();
public void SetRadix(RADIX_TYPE iRadixType)
public void SetRadix(RADIX_TYPE iRadixType)
=> NativeDispatch.CalculatorManager_SetRadix(_nativeManager, iRadixType);
public void SetMemorizedNumbersString()
@ -322,7 +321,7 @@ namespace CalculationManager
public void SetHistory(CALCULATOR_MODE eMode, List<HISTORYITEM> history) => throw new NotImplementedException();
public void SetInHistoryItemLoadMode(bool isHistoryItemLoadMode)
public void SetInHistoryItemLoadMode(bool isHistoryItemLoadMode)
=> NativeDispatch.CalculatorManager_SetInHistoryItemLoadMode(_nativeManager, isHistoryItemLoadMode);
}
}

Просмотреть файл

@ -7,6 +7,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<DefaultLanguage>en-US</DefaultLanguage>
<IsUnoHead>true</IsUnoHead>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<WasmShellGenerateCompressedFiles Condition="'$(Configuration)'=='Debug'">false</WasmShellGenerateCompressedFiles>
<MonoRuntimeDebuggerEnabled Condition="'$(Configuration)'=='Debug'">true</MonoRuntimeDebuggerEnabled>
@ -87,12 +88,16 @@
<PackageReference Include="Uno.Fonts.OpenSans" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<WasmShellAdditionalPInvokeLibrary Include="CalcManager" />
</ItemGroup>
<ItemGroup>
<MonoRuntimeMixedModeExcludedAssembly Include="Newtonsoft.Json" />
<MonoRuntimeMixedModeExcludedAssembly Include="System.Data" />
</ItemGroup>
<ItemGroup Condition="$([MSBuild]::IsOsPlatform('Linux')) or ( $([MSBuild]::IsOsPlatform('Windows')) and '$(SkipWasmBuild)'=='' )">
<Content Include="..\CalcManager\bin\wasm\**\*.bc" />
<Content Include="..\CalcManager\bin\wasm\**\*.a" />
</ItemGroup>
<ItemGroup>
<TypeScriptCompile Include="ts\GoogleAnalytics.ts" />

Просмотреть файл

@ -1,23 +1,38 @@
class CalcManager {
static async initializeExports() {
// Call as early as possible to that the exports are available after the next
// main thread tick.
if (Module.getAssemblyExports !== undefined) {
try {
CalcManager._calculatorExports = await Module.getAssemblyExports("Calculator.Wasm");
}
catch (e) {
console.error(e);
}
}
}
static registerCallbacks() {
var _getCEngineStringCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:GetCEngineStringCallback");
var _binaryOperatorReceivedCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:BinaryOperatorReceivedCallback");
var _setPrimaryDisplayCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:SetPrimaryDisplayCallback");
var _setIsInErrorCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:SetIsInErrorCallback");
var _setParenthesisNumberCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:SetParenthesisNumberCallback");
var _maxDigitsReachedCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:MaxDigitsReachedCallback");
var _memoryItemChangedCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:MemoryItemChangedCallback");
var _onHistoryItemAddedCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:OnHistoryItemAddedCallback");
var _onNoRightParenAddedCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:OnNoRightParenAddedCallback");
var _setExpressionDisplayCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:SetExpressionDisplayCallback");
var _setMemorizedNumbersCallback = Module.mono_bind_static_method("[Calculator.Wasm] CalculationManager.NativeDispatch:SetMemorizedNumbersCallback");
var _getCEngineStringCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.GetCEngineStringCallback;
var _binaryOperatorReceivedCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.BinaryOperatorReceivedCallback;
var _setPrimaryDisplayCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.SetPrimaryDisplayCallback;
var _setIsInErrorCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.SetIsInErrorCallback;
var _setParenthesisNumberCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.SetParenthesisNumberCallback;
var _maxDigitsReachedCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.MaxDigitsReachedCallback;
var _memoryItemChangedCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.MemoryItemChangedCallback;
var _onHistoryItemAddedCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.OnHistoryItemAddedCallback;
var _onNoRightParenAddedCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.OnNoRightParenAddedCallback;
var _setExpressionDisplayCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.SetExpressionDisplayCallback;
var _setMemorizedNumbersCallback = CalcManager._calculatorExports.CalculationManager.NativeDispatch.SetMemorizedNumbersCallback;
var fGetCEngineStringCallback = Module.addFunction((state, id) => _getCEngineStringCallback(state, id), 'iii');
var fBinaryOperatorReceivedCallback = Module.addFunction((state) => _binaryOperatorReceivedCallback(state), 'vi');
var fSetPrimaryDisplayCallback = Module.addFunction((state, displayStringValue, isError) => _setPrimaryDisplayCallback(state, displayStringValue, isError), 'viii');
var fSetIsInErrorCallback = Module.addFunction((state, isError) => _setIsInErrorCallback(state, isError), 'vii');
var fSetPrimaryDisplayCallback = Module.addFunction((state, displayStringValue, isError) => _setPrimaryDisplayCallback(state, displayStringValue, Boolean(isError)), 'viii');
var fSetIsInErrorCallback = Module.addFunction((state, isError) => _setIsInErrorCallback(state, Boolean(isError)), 'vii');
var fSetParenthesisNumberCallback = Module.addFunction((state, parenthesisCount) => _setParenthesisNumberCallback(state, parenthesisCount), 'vii');
var fMaxDigitsReachedCallback = Module.addFunction((state) => _maxDigitsReachedCallback(state), 'vii');
var fMemoryItemChangedCallback = Module.addFunction((state, indexOfMemory) => _memoryItemChangedCallback(state, indexOfMemory), 'vii');
@ -26,19 +41,22 @@
var fSetExpressionDisplayCallback = Module.addFunction((state, historyItem) => _setExpressionDisplayCallback(state, historyItem), 'vii');
var fSetMemorizedNumbersCallback = Module.addFunction((state, size, numbers) => _setMemorizedNumbersCallback(state, size, numbers), 'viii');
var ret = `${fGetCEngineStringCallback};`
+ `${fBinaryOperatorReceivedCallback};`
+ `${fSetPrimaryDisplayCallback};`
+ `${fSetIsInErrorCallback};`
+ `${fSetParenthesisNumberCallback};`
+ `${fMaxDigitsReachedCallback};`
+ `${fMemoryItemChangedCallback};`
+ `${fOnHistoryItemAddedCallback};`
+ `${fOnNoRightParenAddedCallback};`
+ `${fSetExpressionDisplayCallback};`
+ `${fSetMemorizedNumbersCallback};`
;
var ret = [
fGetCEngineStringCallback
, fBinaryOperatorReceivedCallback
, fSetPrimaryDisplayCallback
, fSetIsInErrorCallback
, fSetParenthesisNumberCallback
, fMaxDigitsReachedCallback
, fMemoryItemChangedCallback
, fOnHistoryItemAddedCallback
, fOnNoRightParenAddedCallback
, fSetExpressionDisplayCallback
, fSetMemorizedNumbersCallback
];
return ret;
}
}
globalThis.CalcManager = CalcManager;